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.67 by root, Tue Apr 11 14:36:59 2006 UTC vs.
Revision 1.75 by root, Tue Apr 11 20:44:49 2006 UTC

1package CFClient::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6 6use List::Util ();
7use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9 7
10use CFClient; 8use CFClient;
11 9
12our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
13 11
70 } 68 }
71 69
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73} 71}
74 72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
78
79use SDL::OpenGL;
80
75sub new { 81sub new {
76 my $class = shift; 82 my $class = shift;
77 83
78 bless { 84 bless {
79 x => 0, 85 x => 0,
80 y => 0, 86 y => 0,
81 z => 0, 87 z => 0,
88 w => -1,
89 h => -1,
82 @_ 90 @_
83 }, $class 91 }, $class
84} 92}
85 93
86sub move { 94sub move {
97sub size_request { 105sub size_request {
98 require Carp; 106 require Carp;
99 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
100} 108}
101 109
102sub size_allocate { 110sub _size_allocate {
103 my ($self, $w, $h) = @_; 111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
104 117
105 $self->{w} = $w; 118 $self->{w} = $w;
106 $self->{h} = $h; 119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
107} 128}
108 129
109# translate global koordinates to local coordinate system 130# translate global koordinates to local coordinate system
110sub translate { 131sub translate {
111 my ($self, $x, $y) = @_; 132 my ($self, $x, $y) = @_;
114} 135}
115 136
116sub focus_in { 137sub focus_in {
117 my ($self) = @_; 138 my ($self) = @_;
118 139
140 return if $FOCUS == $self;
141
119 my $focus = $FOCUS; $FOCUS = $self; 142 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus; 143 $focus->update if $focus;
121 $FOCUS->update; 144 $FOCUS->update;
122} 145}
123 146
130 $focus->update if $focus; #? 153 $focus->update if $focus; #?
131} 154}
132 155
133sub mouse_motion { } 156sub mouse_motion { }
134sub button_up { } 157sub button_up { }
135sub button_down { }
136sub key_down { } 158sub key_down { }
137sub key_up { } 159sub key_up { }
160
161sub button_down {
162 my ($self, $ev, $x, $y) = @_;
163
164 $self->focus_in;
165}
138 166
139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
144 172
145sub draw { 173sub draw {
146 my ($self) = @_; 174 my ($self) = @_;
147 175
176 return unless $self->{h} && $self->{w};
177
148 glPushMatrix; 178 glPushMatrix;
149 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
150 $self->_draw; 180 $self->_draw;
181 glPopMatrix;
182
151 if ($self == $HOVER) { 183 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)};
185
152 glColor 1, 1, 1, 0.4; 186 glColor 1, 1, 1, 0.1;
153 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
154 glBegin GL_QUADS; 189 glBegin GL_QUADS;
155 glVertex 0 , 0; 190 glVertex $x , $y;
156 glVertex $self->{w}, 0; 191 glVertex $x + $self->{w}, $y;
157 glVertex $self->{w}, $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
158 glVertex 0 , $self->{h}; 193 glVertex $x , $y + $self->{h};
159 glEnd; 194 glEnd;
160 glDisable GL_BLEND; 195 glDisable GL_BLEND;
161 } 196 }
162 glPopMatrix;
163} 197}
164 198
165sub _draw { 199sub _draw {
166 my ($self) = @_; 200 my ($self) = @_;
167 201
207 241
208 $self->{parent}->update 242 $self->{parent}->update
209 if $self->{parent}; 243 if $self->{parent};
210} 244}
211 245
246sub connect {
247 my ($self, $signal, $cb) = @_;
248
249 push @{ $self->{cb}{$signal} }, $cb;
250}
251
252sub emit {
253 my ($self, $signal, @args) = @_;
254
255 $_->($self, @args)
256 for @{$self->{cb}{$signal} || []};
257}
258
212sub DESTROY { 259sub DESTROY {
213 my ($self) = @_; 260 my ($self) = @_;
214 261
215 #$self->deactivate; 262 #$self->deactivate;
216} 263}
217 264
218############################################################################# 265#############################################################################
219 266
267package CFClient::UI::DrawBG;
268
269our @ISA = CFClient::UI::Base::;
270
271use strict;
272use SDL::OpenGL;
273
274sub new {
275 my $class = shift;
276
277 # range [value, low, high, page]
278
279 $class->SUPER::new (
280 bg => [0, 0, 0, 0.4],
281 active_bg => [1, 1, 1],
282 @_
283 )
284}
285
286sub _draw {
287 my ($self) = @_;
288
289 my ($w, $h) = @$self{qw(w h)};
290
291 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
292 glBegin GL_QUADS;
293 glVertex 0 , 0;
294 glVertex 0 , $h;
295 glVertex $w, $h;
296 glVertex $w, 0;
297 glEnd;
298}
299
300#############################################################################
301
220package CFClient::Widget::Empty; 302package CFClient::UI::Empty;
221 303
222our @ISA = CFClient::Widget::; 304our @ISA = CFClient::UI::Base::;
223 305
224sub size_request { 306sub size_request {
225 (0, 0) 307 (0, 0)
226} 308}
227 309
228sub draw { } 310sub draw { }
229 311
230############################################################################# 312#############################################################################
231 313
232package CFClient::Widget::Container; 314package CFClient::UI::Container;
233 315
234our @ISA = CFClient::Widget::; 316our @ISA = CFClient::UI::Base::;
235 317
236sub new { 318sub new {
237 my ($class, %arg) = @_; 319 my ($class, %arg) = @_;
238 320
239 my $children = delete $arg{children} || []; 321 my $children = delete $arg{children} || [];
253 $self->{children} = [ 335 $self->{children} = [
254 sort { $a->{z} <=> $b->{z} } 336 sort { $a->{z} <=> $b->{z} }
255 @{$self->{children}}, $chld 337 @{$self->{children}}, $chld
256 ]; 338 ];
257 339
258 $self->size_allocate ($self->{w}, $self->{h}) 340 $self->{w} = $self->{h} = -1;
259 if $self->{w}; #TODO: check for "realised state" 341 $self->update;
260} 342}
261 343
262sub remove { 344sub remove {
263 my ($self, $widget) = @_; 345 my ($self, $widget) = @_;
264 346
265 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
266 348
267 $self->size_allocate ($self->{w}, $self->{h}); 349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
268} 350}
269 351
270sub find_widget { 352sub find_widget {
271 my ($self, $x, $y) = @_; 353 my ($self, $x, $y) = @_;
272 354
289 $_->draw for @{$self->{children}}; 371 $_->draw for @{$self->{children}};
290} 372}
291 373
292############################################################################# 374#############################################################################
293 375
294package CFClient::Widget::Bin; 376package CFClient::UI::Bin;
295 377
296our @ISA = CFClient::Widget::Container::; 378our @ISA = CFClient::UI::Container::;
297 379
298sub new { 380sub new {
299 my ($class, %arg) = @_; 381 my ($class, %arg) = @_;
300 382
301 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; 383 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
302 384
303 $class->SUPER::new (children => [$child], %arg) 385 $class->SUPER::new (children => [$child], %arg)
304} 386}
305 387
306sub add { 388sub add {
314sub remove { 396sub remove {
315 my ($self, $widget) = @_; 397 my ($self, $widget) = @_;
316 398
317 $self->SUPER::remove ($widget); 399 $self->SUPER::remove ($widget);
318 400
319 $self->{children} = [new CFClient::Widget::Empty] 401 $self->{children} = [new CFClient::UI::Empty]
320 unless @{$self->{children}}; 402 unless @{$self->{children}};
321} 403}
322 404
323sub child { $_[0]->{children}[0] } 405sub child { $_[0]->{children}[0] }
324 406
325sub size_request { 407sub size_request {
326 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 408 $_[0]{children}[0]->size_request
327} 409}
328 410
329sub size_allocate { 411sub size_allocate {
330 my ($self, $w, $h) = @_; 412 my ($self, $x, $y, $w, $h) = @_;
331 413
332 $self->SUPER::size_allocate ($w, $h); 414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
333 $self->{children}[0]->size_allocate ($w, $h) 416 $self->{children}[0]->size_allocate (0, 0, $w, $h);
334 if $self->{children}[0]
335} 417}
336 418
337############################################################################# 419#############################################################################
338 420
339package CFClient::Widget::Window; 421package CFClient::UI::Window;
340 422
341our @ISA = CFClient::Widget::Bin::; 423our @ISA = CFClient::UI::Bin::;
342 424
343use SDL::OpenGL; 425use SDL::OpenGL;
344 426
345sub new { 427sub new {
346 my ($class, %arg) = @_; 428 my ($class, %arg) = @_;
364 $self->{w}, $self->{h}, sub { $self->child->draw } 446 $self->{w}, $self->{h}, sub { $self->child->draw }
365 ); 447 );
366} 448}
367 449
368sub size_allocate { 450sub size_allocate {
369 my ($self, $w, $h) = @_; 451 my ($self, $x, $y, $w, $h) = @_;
370 452
371 $self->{w} = $w; 453 $self->_size_allocate ($x, $y, $w, $h) or return;
372 $self->{h} = $h;
373 454
374 $self->child->size_allocate ($w, $h); 455 $self->child->size_allocate (0, 0, $w, $h);
375 456
376 $self->render_chld; 457 $self->render_chld;
377} 458}
378 459
379sub _draw { 460sub _draw {
383 464
384 my $tex = $self->{texture} 465 my $tex = $self->{texture}
385 or return; 466 or return;
386 467
387 glEnable GL_BLEND; 468 glEnable GL_BLEND;
469 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
388 glEnable GL_TEXTURE_2D; 470 glEnable GL_TEXTURE_2D;
389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
390 472
391 $tex->draw_quad (0, 0, $w, $h); 473 $tex->draw_quad (0, 0, $w, $h);
392 474
394 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
395} 477}
396 478
397############################################################################# 479#############################################################################
398 480
399package CFClient::Widget::Frame; 481package CFClient::UI::Frame;
400 482
401our @ISA = CFClient::Widget::Bin::; 483our @ISA = CFClient::UI::Bin::;
402 484
403use SDL::OpenGL; 485use SDL::OpenGL;
404 486
405sub size_request { 487sub size_request {
406 my ($self) = @_; 488 my ($self) = @_;
411 493
412 map { $_ + 4 } $chld->size_request; 494 map { $_ + 4 } $chld->size_request;
413} 495}
414 496
415sub size_allocate { 497sub size_allocate {
416 my ($self, $w, $h) = @_; 498 my ($self, $x, $y, $w, $h) = @_;
417 499
418 $self->{w} = $w; 500 $self->_size_allocate ($x, $y, $w, $h) or return;
419 $self->{h} = $h;
420 501
421 $self->child->size_allocate ($w - 4, $h - 4); 502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
422 $self->child->move (2, 2);
423} 503}
424 504
425sub _draw { 505sub _draw {
426 my ($self) = @_; 506 my ($self) = @_;
427 507
440 $chld->draw; 520 $chld->draw;
441} 521}
442 522
443############################################################################# 523#############################################################################
444 524
445package CFClient::Widget::FancyFrame; 525package CFClient::UI::FancyFrame;
446 526
447our @ISA = CFClient::Widget::Bin::; 527our @ISA = CFClient::UI::Bin::;
448 528
449use SDL::OpenGL; 529use SDL::OpenGL;
450 530
451my @tex = 531my @tex =
452 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
455sub size_request { 535sub size_request {
456 my ($self) = @_; 536 my ($self) = @_;
457 537
458 my ($w, $h) = $self->SUPER::size_request; 538 my ($w, $h) = $self->SUPER::size_request;
459 539
460 $h += $tex[1]->{height}; 540 $h += $tex[1]->{h};
461 $h += $tex[4]->{height}; 541 $h += $tex[4]->{h};
462 $w += $tex[2]->{width}; 542 $w += $tex[2]->{w};
463 $w += $tex[3]->{width}; 543 $w += $tex[3]->{w};
464 544
465 ($w, $h) 545 ($w, $h)
466} 546}
467 547
468sub size_allocate { 548sub size_allocate {
469 my ($self, $w, $h) = @_; 549 my ($self, $x, $y, $w, $h) = @_;
470 550
471 $self->SUPER::size_allocate ($w, $h); 551 $self->_size_allocate ($x, $y, $w, $h) or return;
472 552
473 $h -= $tex[1]->{height}; 553 $h -= $tex[1]->{h};
474 $h -= $tex[4]->{height}; 554 $h -= $tex[4]->{h};
475 $w -= $tex[2]->{width}; 555 $w -= $tex[2]->{w};
476 $w -= $tex[3]->{width}; 556 $w -= $tex[3]->{w};
477 557
478 $h = $h < 0 ? 0 : $h; 558 $h = $h < 0 ? 0 : $h;
479 $w = $w < 0 ? 0 : $w; 559 $w = $w < 0 ? 0 : $w;
480 560
481 my $child = $self->child; 561 my $child = $self->child;
482 562
483 $child->size_allocate ($w, $h); 563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
484 $child->move ($tex[3]->{width}, $tex[1]->{height});
485} 564}
486 565
487sub _draw { 566sub _draw {
488 my ($self) = @_; 567 my ($self) = @_;
489 568
490 my ($w, $h) = ($self->{w}, $self->{h}); 569 my ($w, $h) = ($self->{w}, $self->{h});
491 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
492 571
493 glEnable GL_BLEND; 572 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
494 glEnable GL_TEXTURE_2D; 574 glEnable GL_TEXTURE_2D;
495 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
497 576
498 my $top = $tex[1]; 577 my $top = $tex[1];
499 $top->draw_quad (0, 0, $w, $top->{height}); 578 $top->draw_quad (0, 0, $w, $top->{h});
500 579
501 my $left = $tex[3]; 580 my $left = $tex[3];
502 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 581 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
503 582
504 my $right = $tex[2]; 583 my $right = $tex[2];
505 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 584 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
506 585
507 my $bottom = $tex[4]; 586 my $bottom = $tex[4];
508 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
509 588
510 my $bg = $tex[0]; 589 my $bg = $tex[0];
511 glBindTexture GL_TEXTURE_2D, $bg->{name}; 590 glBindTexture GL_TEXTURE_2D, $bg->{name};
512 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
513 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
514 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
515 594
516 my $rep_x = $cw / $bg->{width}; 595 my $rep_x = $cw / $bg->{w};
517 my $rep_y = $ch / $bg->{height}; 596 my $rep_y = $ch / $bg->{h};
518 597
519 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
520 599
521 glDisable GL_BLEND; 600 glDisable GL_BLEND;
522 glDisable GL_TEXTURE_2D; 601 glDisable GL_TEXTURE_2D;
523 602
524 $self->child->draw; 603 $self->child->draw;
525 604
526} 605}
527 606
528############################################################################# 607#############################################################################
529 608
530package CFClient::Widget::Table; 609package CFClient::UI::Table;
531 610
532our @ISA = CFClient::Widget::Bin::; 611our @ISA = CFClient::UI::Base::;
612
613use List::Util qw(max sum);
533 614
534use SDL::OpenGL; 615use SDL::OpenGL;
535 616
536sub add { 617sub add {
537 my ($self, $x, $y, $chld) = @_; 618 my ($self, $x, $y, $chld) = @_;
538 my $old_chld = $self->{children}[$y][$x];
539 619
540 $self->{children}[$y][$x] = $chld; 620 $self->{children}[$y][$x] = $chld;
541 $chld->set_parent ($self); 621 $chld->set_parent ($self);
622
623 $self->{w} = $self->{h} = -1;
542 $self->update; 624 $self->update;
543} 625}
544 626
545sub max_row_height { 627sub get_wh {
546 my ($self, $row) = @_; 628 my ($self) = @_;
547 629
548 my $hs = 0; 630 my (@w, @h);
549 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 631
632 for my $y (0 .. $#{$self->{children}}) {
550 my $c = $self->{children}->[$row]->[$xi]; 633 my $row = $self->{children}[$y]
551 if ($c) { 634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
552 my ($w, $h) = $c->size_request; 639 my ($w, $h) = $widget->size_request;
553 if ($hs < $h) { $hs = $h } 640
641 $w[$x] = max $w[$x], $w;
642 $h[$y] = max $h[$y], $h;
554 } 643 }
555 } 644 }
556 return $hs;
557}
558 645
559sub max_col_width { 646 (\@w, \@h)
647}
648
649sub size_request {
560 my ($self, $col) = @_; 650 my ($self) = @_;
561 651
652 my ($ws, $hs) = $self->get_wh;
653
654 (
655 (List::Util::sum @$ws),
656 (List::Util::sum @$hs),
657 )
658}
659
660sub size_allocate {
661 my ($self, $x, $y, $w, $h) = @_;
662
663 $self->_size_allocate ($x, $y, $w, $h) or return;
664
665 my ($ws, $hs) = $self->get_wh;
666
667 my $req_w = List::Util::sum @$ws;
668 my $req_h = List::Util::sum @$hs;
669
670 # linearly scale sizes
671 $_ *= $req_w / $w for @$ws;
672 $_ *= $req_h / $h for @$hs;
673
674 my $y;
675
676 for my $r (0 .. $#{$self->{children}}) {
677 my $row = $self->{children}[$r]
678 or next;
679
562 my $ws = 0; 680 my $x = 0;
563 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 681 my $row_h = $hs->[$r];
564 my $c = ($self->{children}->[$yi] || [])->[$col]; 682
565 if ($c) { 683 for my $c (0 .. $#$row) {
566 my ($w, $h) = $c->size_request; 684 my $widget = $row->[$c]
567 if ($ws < $w) { $ws = $w } 685 or next;
686
687 my $col_w = $ws->[$c];
688
689 $widget->size_allocate ($x, $y, $col_w, $row_h);
690
691 $x += $col_w;
568 } 692 }
569 }
570 return $ws;
571}
572 693
573sub size_request { 694 $y += $row_h;
574 my ($self) = @_;
575
576 my ($hs, $ws) = (0, 0);
577
578 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
579 $hs += $self->max_row_height ($yi);
580 } 695 }
581 696
582 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
583 my $wm = 0;
584 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
585 $wm += $self->max_col_width ($xi)
586 }
587 if ($ws < $wm) { $ws = $wm }
588 }
589
590 return ($ws, $hs);
591} 697}
592 698
593sub _draw { 699sub _draw {
594 my ($self) = @_; 700 my ($self) = @_;
595 701
596 my $y = 0; 702 for (grep $_, @{$self->{children}}) {
597 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 703 $_->draw for grep $_, @$_;
598 my $x = 0;
599
600 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
601
602 my $c = $self->{children}->[$yi]->[$xi];
603 if ($c) {
604 $c->move ($x, $y, 0); #TODO: Move to size_request
605 $c->draw if $c;
606 }
607
608 $x += $self->max_col_width ($xi);
609 }
610
611 $y += $self->max_row_height ($yi);
612 } 704 }
613} 705}
614 706
615############################################################################# 707#############################################################################
616 708
617package CFClient::Widget::VBox; 709package CFClient::UI::VBox;
618 710
619our @ISA = CFClient::Widget::Container::; 711our @ISA = CFClient::UI::Container::;
620 712
621use SDL::OpenGL; 713use SDL::OpenGL;
622 714
623sub size_request { 715sub size_request {
624 my ($self) = @_; 716 my ($self) = @_;
630 (List::Util::sum map $_->[1], @alloc), 722 (List::Util::sum map $_->[1], @alloc),
631 ) 723 )
632} 724}
633 725
634sub size_allocate { 726sub size_allocate {
635 my ($self, $w, $h) = @_; 727 my ($self, $x, $y, $w, $h) = @_;
636 728
637 $self->w ($w); 729 $self->_size_allocate ($x, $y, $w, $h) or return;
638 $self->h ($h);
639 730
640 my $exp; 731 return unless $self->{h};
641 my @oth; 732
642 # find expand widget 733 my $children = $self->{children};
643 for (@{$self->{children}}) { 734
644 if ($_->{expand}) { 735 my @h = map +($_->size_request)[1], @$children;
645 $exp = $_; 736
646 last; 737 my $req_h = List::Util::sum @h;
738
739 if ($req_h > $h) {
740 # ah well, not enough space
741 $_ = $h[$_] * $h / $req_h for @h;
742 } else {
743 my @exp = grep $_->{expand}, @$children;
744 @exp = @$children unless @exp;
745
746 my %exp = map +($_ => 1), @exp;
747
748 for (0 .. $#$children) {
749 my $child = $children->[$_];
750
751 my $alloc_h = $h[$_];
752 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
753 $h[$_] = $alloc_h;
647 } 754 }
648 push @oth, $_;
649 }
650
651 my ($ow, $oh);
652
653 # get sizes of other widgets
654 for (@oth) {
655 my ($w, $h) = $_->size_request;
656 $oh += $h;
657 if ($ow < $w) { $ow = $w }
658 } 755 }
659 756
660 my $y = 0; 757 my $y = 0;
661 for (@{$self->{children}}) { 758 for (0 .. $#$children) {
662 $_->move (0, $y); 759 my $child = $children->[$_];
663 760 my $h = $h[$_];
664 if ($_ == $exp) {
665 $_->size_allocate ($w, $h - $oh);
666 $y += $h - $oh;
667 } else {
668 my ($cw, $h) = $_->size_request;
669 $_->size_allocate ($w, $h); 761 $child->size_allocate (0, $y, $w, $h);
762
670 $y += $h; 763 $y += $h;
671 }
672 } 764 }
673} 765}
674 766
675############################################################################# 767#############################################################################
676 768
677package CFClient::Widget::Label; 769package CFClient::UI::Label;
678 770
679our @ISA = CFClient::Widget::; 771our @ISA = CFClient::UI::Base::;
680 772
681use SDL::OpenGL; 773use SDL::OpenGL;
682 774
683sub new { 775sub new {
684 my ($class, %arg) = @_; 776 my ($class, %arg) = @_;
685 777
686 # TODO: color, and make height, xyz etc. optional
687 my $self = $class->SUPER::new ( 778 my $self = $class->SUPER::new (
688 color => [1, 1, 1], 779 fg => [1, 1, 1],
689 height => $::FONTSIZE, 780 height => $::FONTSIZE,
690 text => "", 781 text => "",
782 align => -1,
691 layout => new CFClient::Layout, 783 layout => new CFClient::Layout,
692 %arg 784 %arg
693 ); 785 );
694 786
695 $self->set_text ($self->{text}); 787 $self->set_text ($self->{text});
696 788
697 $self 789 $self
698} 790}
699 791
792sub escape_text {
793 local $_ = $_[1];
794
795 s/&/&amp;/g;
796 s/>/&gt;/g;
797 s/</&lt;/g;
798
799 $_[1]
800}
801
700sub set_text { 802sub set_text {
701 my ($self, $text) = @_; 803 my ($self, $text) = @_;
702 804
703 $self->{text} = $text; 805 $self->{text} = $text;
704 $self->{layout}->set_markup ($text); 806 $self->{layout}->set_markup ($text);
705 807
706 delete $self->{texture}; 808 delete $self->{texture};
809 $self->update;
707} 810}
708 811
709sub get_text { 812sub get_text {
710 my ($self, $text) = @_; 813 my ($self, $text) = @_;
711 814
716 my ($self) = @_; 819 my ($self) = @_;
717 820
718 $self->{layout}->set_width; 821 $self->{layout}->set_width;
719 $self->{layout}->set_height ($self->{height}); 822 $self->{layout}->set_height ($self->{height});
720 $self->{layout}->size 823 $self->{layout}->size
721# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
722# ( 825# (
723# $self->{texture}{width}, 826# $self->{texture}{w},
724# $self->{texture}{height}, 827# $self->{texture}{h},
725# ) 828# )
726# } else { 829# } else {
727# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
728# 831#
729# ($w, $h) 832# ($w, $h)
730# } 833# }
731} 834}
732 835
733sub size_allocate { 836sub size_allocate {
734 my ($self, $w, $h) = @_; 837 my ($self, $x, $y, $w, $h) = @_;
735 838
736 $self->SUPER::size_allocate ($w, $h); 839 $self->_size_allocate ($x, $y, $w, $h) or return;
840
737 delete $self->{texture}; 841 delete $self->{texture};
842}
843
844sub update {
845 my ($self) = @_;
846
847 delete $self->{texture};
848 $self->SUPER::update;
738} 849}
739 850
740sub _draw { 851sub _draw {
741 my ($self) = @_; 852 my ($self) = @_;
742 853
743 my $tex = $self->{texture} ||= do { 854 my $tex = $self->{texture} ||= do {
744 $self->{layout}->set_width ($self->{w}); 855 $self->{layout}->set_width ($self->{w});
745 new_from_layout CFClient::Texture $self->{layout}; 856 new_from_layout CFClient::Texture $self->{layout}
746 }; 857 };
747 858
748 glEnable GL_BLEND; 859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
749 glEnable GL_TEXTURE_2D; 861 glEnable GL_TEXTURE_2D;
750 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
752 863
753 glColor @{$self->{color}}; 864 glColor @{$self->{fg}};
754 865
866 my $x =
867 $self->{align} < 0 ? 0
868 : $self->{align} > 0 ? $self->{w} - $tex->{w}
869 : ($self->{w} - $tex->{w}) * 0.5;
870
755 $tex->draw_quad (0, 0); 871 $tex->draw_quad ($x, 0);
756 872
873 glDisable GL_TEXTURE_2D;
757 glDisable GL_BLEND; 874 glDisable GL_BLEND;
758 glDisable GL_TEXTURE_2D;
759} 875}
760 876
761############################################################################# 877#############################################################################
762 878
763package CFClient::Widget::Entry; 879package CFClient::UI::Entry;
764 880
765our @ISA = CFClient::Widget::Label::; 881our @ISA = CFClient::UI::Label::;
766 882
767use SDL; 883use SDL;
768use SDL::OpenGL; 884use SDL::OpenGL;
769 885
886sub new {
887 my $class = shift;
888
889 $class->SUPER::new (
890 fg => [1, 1, 1],
891 bg => [0, 0, 0, 0.4],
892 active_bg => [1, 1, 1],
893 active_fg => [0, 0, 0],
894 @_
895 )
896}
897
898sub _set_text {
899 my ($self, $text) = @_;
900
901 $self->{last_activity} = $::NOW;
902
903 $self->{text} = $text;
904 $self->{layout}->set_width ($self->{w});
905
906 $text =~ s/./*/g if $self->{hidden};
907
908
909 $self->{layout}->set_markup ($self->escape_text ($text));
910
911 $text = substr $text, 0, $self->{cursor};
912 utf8::encode $text;
913
914 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
915}
916
917sub size_request {
918 my ($self) = @_;
919
920 my ($w, $h) = $self->SUPER::size_request;
921
922 ($w + 1, $h) # add 1 for cursor
923}
924
925sub size_allocate {
926 my ($self, $x, $y, $w, $h) = @_;
927
928 $self->SUPER::size_allocate ($x, $y, $w, $h);
929
930 $self->_set_text ($self->{text});
931}
932
933sub set_text {
934 my ($self, $text) = @_;
935
936 $self->{cursor} = length $text;
937 $self->_set_text ($text);
938 $self->update;
939}
940
770sub key_down { 941sub key_down {
771 my ($self, $ev) = @_; 942 my ($self, $ev) = @_;
772 943
773 my $mod = $ev->key_mod; 944 my $mod = $ev->key_mod;
774 my $sym = $ev->key_sym; 945 my $sym = $ev->key_sym;
776 my $uni = $ev->key_unicode; 947 my $uni = $ev->key_unicode;
777 948
778 my $text = $self->get_text; 949 my $text = $self->get_text;
779 950
780 if ($sym == SDLK_BACKSPACE) { 951 if ($sym == SDLK_BACKSPACE) {
781 substr $text, -1, 1, ''; 952 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
953 } elsif ($sym == SDLK_DELETE) {
954 substr $text, $self->{cursor}, 1, "";
955 } elsif ($sym == SDLK_LEFT) {
956 --$self->{cursor} if $self->{cursor};
957 } elsif ($sym == SDLK_RIGHT) {
958 ++$self->{cursor} if $self->{cursor} < length $self->{text};
782 } elsif ($uni) { 959 } elsif ($uni) {
783 $text .= chr $uni; 960 substr $text, $self->{cursor}++, 0, chr $uni;
784 } 961 }
785 962
786 $self->set_text ($text); 963 $self->_set_text ($text);
964 $self->update;
965}
966
967sub focus_in {
968 my ($self) = @_;
969
970 $self->{last_activity} = $::NOW;
971
972 $self->SUPER::focus_in;
787} 973}
788 974
789sub button_down { 975sub button_down {
790 my ($self, $ev) = @_; 976 my ($self, $ev, $x, $y) = @_;
791 977
792 $self->focus_in; 978 $self->SUPER::button_down ($ev, $x, $y);
979
980 my $idx = $self->{layout}->xy_to_index ($x, $y);
981
982 # byte-index to char-index
983 my $text = $self->{layout};
984 utf8::encode $text;
985 $self->{cursor} = length substr $text, 0, $idx;
986
987 $self->_set_text ($self->{text});
988 $self->update;
793} 989}
794 990
795sub mouse_motion { 991sub mouse_motion {
796 my ($self, $ev, $x, $y) = @_; 992 my ($self, $ev, $x, $y) = @_;
797 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 993# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
798} 994}
799 995
800sub _draw { 996sub _draw {
801 my ($self) = @_; 997 my ($self) = @_;
802 998
999 local $self->{fg} = $self->{fg};
1000
803 if ($FOCUS == $self) { 1001 if ($FOCUS == $self) {
804 glColor 1, 1, 1; 1002 glColor @{$self->{active_bg}};
1003 $self->{fg} = $self->{active_fg};
805 } else { 1004 } else {
806 glColor 0.7, 0.7, 0.7; 1005 glColor @{$self->{bg}};
807 } 1006 }
808 1007
809 glBegin GL_QUADS; 1008 glBegin GL_QUADS;
810 glVertex 0 , 0; 1009 glVertex 0 , 0;
811 glVertex 0 , $self->{h} - 1; 1010 glVertex 0 , $self->{h};
812 glVertex $self->{w} - 1, $self->{h} - 1; 1011 glVertex $self->{w}, $self->{h};
813 glVertex $self->{w} - 1, 0; 1012 glVertex $self->{w}, 0;
814 glEnd; 1013 glEnd;
815 1014
816 $self->SUPER::_draw; 1015 $self->SUPER::_draw;
817}
818 1016
819############################################################################# 1017 #TODO: force update every cursor change :(
1018 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1019 glColor @{$self->{fg}};
1020 glBegin GL_LINES;
1021 glVertex $self->{cur_x}, $self->{cur_y};
1022 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1023 glEnd;
1024 }
1025}
820 1026
1027#############################################################################
1028
1029package CFClient::UI::Slider;
1030
1031use strict;
1032
1033use SDL::OpenGL;
1034
1035our @ISA = CFClient::UI::DrawBG::;
1036
1037sub size_request {
1038 my ($self) = @_;
1039
1040 my $w = 50;
1041 my $h = 10;
1042
1043 $self->{vertical} ? ($h, $w) : ($w, $h)
1044}
1045
1046sub new {
1047 my $class = shift;
1048
1049 # range [value, low, high, page]
1050
1051 $class->SUPER::new (
1052 fg => [1, 1, 1],
1053 active_fg => [0, 0, 0],
1054 range => [0, 0, 100, 10],
1055 vertical => 1,
1056 @_
1057 )
1058}
1059
1060sub button_down {
1061 my ($self, $ev, $x, $y) = @_;
1062
1063 $self->SUPER::button_down ($ev, $x, $y);
1064 $self->mouse_motion ($ev, $x, $y);
1065}
1066
1067sub mouse_motion {
1068 my ($self, $ev, $x, $y) = @_;
1069
1070 if ($GRAB == $self) {
1071 my ($value, $lo, $hi, $page) = @{$self->{range}};
1072
1073 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1074
1075 $x = $x * ($hi - $lo) / $w + $lo;
1076 $x = $lo if $x < $lo;
1077 $x = $hi - $page if $x > $hi - $page;
1078 $self->{range}[0] = $x;
1079
1080 $self->emit (changed => $x);
1081 $self->update;
1082 }
1083}
1084
1085sub _draw {
1086 my ($self) = @_;
1087
1088 $self->SUPER::_draw ();
1089
1090 my ($w, $h) = @$self{qw(w h)};
1091
1092 if ($self->{vertical}) {
1093 # draw a vertical slider like a rotated horizontal slider
1094
1095 glRotate 90, 0, 0, 1;
1096 glTranslate 0, -$self->{w}, 0;
1097
1098 ($w, $h) = ($h, $w);
1099 }
1100
1101 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1102 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1103
1104 my ($value, $lo, $hi, $page) = @{$self->{range}};
1105
1106 $page = int $page * $w / ($hi - $lo);
1107 $value = int +($value - $lo) * $w / ($hi - $lo);
1108
1109 $w -= $page;
1110 $page &= ~1;
1111 glTranslate $page * 0.5, 0, 0;
1112
1113 glColor @$fg;
1114 glBegin GL_LINES;
1115 glVertex 0, 0; glVertex 0, $h;
1116 glVertex $w - 1, 0; glVertex $w - 1, $h;
1117 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1118 glEnd;
1119
1120 my $knob_a = $value - $page * 0.5;
1121 my $knob_b = $value + $page * 0.5;
1122
1123 glBegin GL_QUADS;
1124 glColor @$fg;
1125 glVertex $knob_a, 0;
1126 glVertex $knob_a, $h;
1127 glVertex $knob_b, $h;
1128 glVertex $knob_b, 0;
1129
1130 if ($knob_a < $knob_b - 2) {
1131 glColor @$bg;
1132 glVertex $knob_a + 1, 1;
1133 glVertex $knob_a + 1, $h - 1;
1134 glVertex $knob_b - 1, $h - 1;
1135 glVertex $knob_b - 1, 1;
1136 }
1137 glEnd;
1138}
1139
1140#############################################################################
1141
821package CFClient::Widget::MapWidget; 1142package CFClient::UI::MapWidget;
822 1143
823use strict; 1144use strict;
824 1145
825use List::Util qw(min max); 1146use List::Util qw(min max);
826 1147
827use SDL; 1148use SDL;
828use SDL::OpenGL; 1149use SDL::OpenGL;
829use SDL::OpenGL::Constants;
830 1150
831our @ISA = CFClient::Widget::; 1151our @ISA = CFClient::UI::Base::;
832 1152
833sub new { 1153sub new {
834 my $class = shift; 1154 my $class = shift;
835 1155
836 $class->SUPER::new ( 1156 $class->SUPER::new (
856 1176
857sub update { 1177sub update {
858 my ($self) = @_; 1178 my ($self) = @_;
859 1179
860 $self->{need_update} = 1; 1180 $self->{need_update} = 1;
1181 $self->SUPER::update;
861} 1182}
862 1183
863sub _draw { 1184sub _draw {
864 my ($self) = @_; 1185 my ($self) = @_;
865 1186
888 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1209 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
889 } 1210 }
890 1211
891 glEnable GL_TEXTURE_2D; 1212 glEnable GL_TEXTURE_2D;
892 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
894 1216
895 my $sw4 = ($sw + 3) & ~3; 1217 my $sw4 = ($sw + 3) & ~3;
896 my $darkness = "\x00" x ($sw4 * $sh); 1218 my $darkness = "\x00" x ($sw4 * $sh);
897 1219
910 } 1232 }
911 1233
912 for my $num (grep $_, @$cell[1,2,3]) { 1234 for my $num (grep $_, @$cell[1,2,3]) {
913 my $tex = $::CONN->{face}[$num]{texture} || next; 1235 my $tex = $::CONN->{face}[$num]{texture} || next;
914 1236
915 my $w = $tex->{width}; 1237 my ($w, $h) = @$tex{qw(w h)};
916 my $h = $tex->{height};
917 1238
918 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); 1239 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
919 } 1240 }
920 } 1241 }
921 } 1242 }
928# 1249#
929# $lighting = $pb->get_pixels; 1250# $lighting = $pb->get_pixels;
930# $lighting =~ s/(.)../$1/gs; 1251# $lighting =~ s/(.)../$1/gs;
931# } 1252# }
932 1253
933 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
934 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
935 1255
936 $darkness = new CFClient::Texture 1256 $darkness = new CFClient::Texture
937 width => $sw4, 1257 w => $sw4,
938 height => $sh, 1258 h => $sh,
939 data => $darkness, 1259 data => $darkness,
940 internalformat => GL_ALPHA, 1260 internalformat => GL_ALPHA,
941 format => GL_ALPHA; 1261 format => GL_ALPHA;
942 1262
943 glColor 0.45, 0.45, 0.45, 1; 1263 glColor 0.45, 0.45, 0.45, 1;
1003 } 1323 }
1004} 1324}
1005 1325
1006############################################################################# 1326#############################################################################
1007 1327
1008package CFClient::Widget::Animator; 1328package CFClient::UI::Animator;
1009 1329
1010use SDL::OpenGL; 1330use SDL::OpenGL;
1011 1331
1012our @ISA = CFClient::Widget::Bin::; 1332our @ISA = CFClient::UI::Bin::;
1013 1333
1014sub moveto { 1334sub moveto {
1015 my ($self, $x, $y) = @_; 1335 my ($self, $x, $y) = @_;
1016 1336
1017 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1337 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1045 glPopMatrix; 1365 glPopMatrix;
1046} 1366}
1047 1367
1048############################################################################# 1368#############################################################################
1049 1369
1050package CFClient::Widget::Toplevel; 1370package CFClient::UI::Toplevel;
1051 1371
1052our @ISA = CFClient::Widget::Container::; 1372our @ISA = CFClient::UI::Container::;
1053 1373
1054sub size_request { 1374sub size_request {
1055 ($::WIDTH, $::HEIGHT) 1375 ($::WIDTH, $::HEIGHT)
1056} 1376}
1057 1377
1058sub size_allocate { 1378sub size_allocate {
1059 my ($self, $w, $h) = @_; 1379 my ($self, $x, $y, $w, $h) = @_;
1060 1380
1061 $self->SUPER::size_allocate ($w, $h); 1381 $self->_size_allocate ($x, $y, $w, $h);
1062 1382
1063 $_->size_allocate ($_->size_request) 1383 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1064 for @{$self->{children}}; 1384 for @{$self->{children}};
1065} 1385}
1066 1386
1067sub translate { 1387sub translate {
1068 my ($self, $x, $y) = @_; 1388 my ($self, $x, $y) = @_;
1071} 1391}
1072 1392
1073sub update { 1393sub update {
1074 my ($self) = @_; 1394 my ($self) = @_;
1075 1395
1076 $self->size_allocate ($self->size_request); 1396 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1077 ::refresh (); 1397 ::refresh ();
1078} 1398}
1079 1399
1080sub add { 1400sub add {
1081 my ($self, $widget) = @_; 1401 my ($self, $widget) = @_;
1082 1402
1083 $self->SUPER::add ($widget); 1403 $self->SUPER::add ($widget);
1084 1404
1085 $widget->size_allocate ($widget->size_request); 1405 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1086} 1406}
1087 1407
1088sub draw { 1408sub draw {
1089 my ($self) = @_; 1409 my ($self) = @_;
1090 1410
1091 $self->_draw; 1411 $self->_draw;
1092} 1412}
1093 1413
1094############################################################################# 1414#############################################################################
1095 1415
1096package CFClient::Widget; 1416package CFClient::UI;
1097 1417
1098$TOPLEVEL = new CFClient::Widget::Toplevel; 1418$TOPLEVEL = new CFClient::UI::Toplevel;
1099 1419
11001 14201
1101 1421

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines