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.70 by root, Tue Apr 11 17:33:43 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) = @_;
155 return unless $self->{h} && $self->{w}; 176 return unless $self->{h} && $self->{w};
156 177
157 glPushMatrix; 178 glPushMatrix;
158 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
159 $self->_draw; 180 $self->_draw;
181 glPopMatrix;
182
160 if ($self == $HOVER) { 183 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)};
185
161 glColor 1, 1, 1, 0.4; 186 glColor 1, 1, 1, 0.1;
162 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
163 glBegin GL_QUADS; 189 glBegin GL_QUADS;
164 glVertex 0 , 0; 190 glVertex $x , $y;
165 glVertex $self->{w}, 0; 191 glVertex $x + $self->{w}, $y;
166 glVertex $self->{w}, $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
167 glVertex 0 , $self->{h}; 193 glVertex $x , $y + $self->{h};
168 glEnd; 194 glEnd;
169 glDisable GL_BLEND; 195 glDisable GL_BLEND;
170 } 196 }
171 glPopMatrix;
172} 197}
173 198
174sub _draw { 199sub _draw {
175 my ($self) = @_; 200 my ($self) = @_;
176 201
216 241
217 $self->{parent}->update 242 $self->{parent}->update
218 if $self->{parent}; 243 if $self->{parent};
219} 244}
220 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
221sub DESTROY { 259sub DESTROY {
222 my ($self) = @_; 260 my ($self) = @_;
223 261
224 #$self->deactivate; 262 #$self->deactivate;
225} 263}
226 264
227############################################################################# 265#############################################################################
228 266
229package CFClient::Widget::DrawBG; 267package CFClient::UI::DrawBG;
230 268
231our @ISA = CFClient::Widget::; 269our @ISA = CFClient::UI::Base::;
232 270
233use strict; 271use strict;
234use SDL::OpenGL; 272use SDL::OpenGL;
235 273
236sub new { 274sub new {
259 glEnd; 297 glEnd;
260} 298}
261 299
262############################################################################# 300#############################################################################
263 301
264package CFClient::Widget::Empty; 302package CFClient::UI::Empty;
265 303
266our @ISA = CFClient::Widget::; 304our @ISA = CFClient::UI::Base::;
267 305
268sub size_request { 306sub size_request {
269 (0, 0) 307 (0, 0)
270} 308}
271 309
272sub draw { } 310sub draw { }
273 311
274############################################################################# 312#############################################################################
275 313
276package CFClient::Widget::Container; 314package CFClient::UI::Container;
277 315
278our @ISA = CFClient::Widget::; 316our @ISA = CFClient::UI::Base::;
279 317
280sub new { 318sub new {
281 my ($class, %arg) = @_; 319 my ($class, %arg) = @_;
282 320
283 my $children = delete $arg{children} || []; 321 my $children = delete $arg{children} || [];
297 $self->{children} = [ 335 $self->{children} = [
298 sort { $a->{z} <=> $b->{z} } 336 sort { $a->{z} <=> $b->{z} }
299 @{$self->{children}}, $chld 337 @{$self->{children}}, $chld
300 ]; 338 ];
301 339
302 $self->size_allocate ($self->{w}, $self->{h}) 340 $self->{w} = $self->{h} = -1;
303 if $self->{w}; #TODO: check for "realised state" 341 $self->update;
304} 342}
305 343
306sub remove { 344sub remove {
307 my ($self, $widget) = @_; 345 my ($self, $widget) = @_;
308 346
309 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
310 348
311 $self->size_allocate ($self->{w}, $self->{h}); 349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
312} 350}
313 351
314sub find_widget { 352sub find_widget {
315 my ($self, $x, $y) = @_; 353 my ($self, $x, $y) = @_;
316 354
333 $_->draw for @{$self->{children}}; 371 $_->draw for @{$self->{children}};
334} 372}
335 373
336############################################################################# 374#############################################################################
337 375
338package CFClient::Widget::Bin; 376package CFClient::UI::Bin;
339 377
340our @ISA = CFClient::Widget::Container::; 378our @ISA = CFClient::UI::Container::;
341 379
342sub new { 380sub new {
343 my ($class, %arg) = @_; 381 my ($class, %arg) = @_;
344 382
345 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; 383 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
346 384
347 $class->SUPER::new (children => [$child], %arg) 385 $class->SUPER::new (children => [$child], %arg)
348} 386}
349 387
350sub add { 388sub add {
358sub remove { 396sub remove {
359 my ($self, $widget) = @_; 397 my ($self, $widget) = @_;
360 398
361 $self->SUPER::remove ($widget); 399 $self->SUPER::remove ($widget);
362 400
363 $self->{children} = [new CFClient::Widget::Empty] 401 $self->{children} = [new CFClient::UI::Empty]
364 unless @{$self->{children}}; 402 unless @{$self->{children}};
365} 403}
366 404
367sub child { $_[0]->{children}[0] } 405sub child { $_[0]->{children}[0] }
368 406
369sub size_request { 407sub size_request {
370 $_[0]{children}[0]->size_request 408 $_[0]{children}[0]->size_request
371} 409}
372 410
373sub size_allocate { 411sub size_allocate {
374 my ($self, $w, $h) = @_; 412 my ($self, $x, $y, $w, $h) = @_;
375 413
376 return unless $self->{w} != $w || $self->{h} != $h; 414 $self->_size_allocate ($x, $y, $w, $h) or return;
377 415
378 $self->SUPER::size_allocate ($w, $h);
379 $self->{children}[0]->size_allocate ($w, $h); 416 $self->{children}[0]->size_allocate (0, 0, $w, $h);
380} 417}
381 418
382############################################################################# 419#############################################################################
383 420
384package CFClient::Widget::Window; 421package CFClient::UI::Window;
385 422
386our @ISA = CFClient::Widget::Bin::; 423our @ISA = CFClient::UI::Bin::;
387 424
388use SDL::OpenGL; 425use SDL::OpenGL;
389 426
390sub new { 427sub new {
391 my ($class, %arg) = @_; 428 my ($class, %arg) = @_;
409 $self->{w}, $self->{h}, sub { $self->child->draw } 446 $self->{w}, $self->{h}, sub { $self->child->draw }
410 ); 447 );
411} 448}
412 449
413sub size_allocate { 450sub size_allocate {
414 my ($self, $w, $h) = @_; 451 my ($self, $x, $y, $w, $h) = @_;
415 452
416 return unless $self->{w} != $w || $self->{h} != $h; 453 $self->_size_allocate ($x, $y, $w, $h) or return;
417 454
418 $self->{w} = $w;
419 $self->{h} = $h;
420
421 $self->child->size_allocate ($w, $h); 455 $self->child->size_allocate (0, 0, $w, $h);
422 456
423 $self->render_chld; 457 $self->render_chld;
424} 458}
425 459
426sub _draw { 460sub _draw {
430 464
431 my $tex = $self->{texture} 465 my $tex = $self->{texture}
432 or return; 466 or return;
433 467
434 glEnable GL_BLEND; 468 glEnable GL_BLEND;
469 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
435 glEnable GL_TEXTURE_2D; 470 glEnable GL_TEXTURE_2D;
436 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
437 472
438 $tex->draw_quad (0, 0, $w, $h); 473 $tex->draw_quad (0, 0, $w, $h);
439 474
441 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
442} 477}
443 478
444############################################################################# 479#############################################################################
445 480
446package CFClient::Widget::Frame; 481package CFClient::UI::Frame;
447 482
448our @ISA = CFClient::Widget::Bin::; 483our @ISA = CFClient::UI::Bin::;
449 484
450use SDL::OpenGL; 485use SDL::OpenGL;
451 486
452sub size_request { 487sub size_request {
453 my ($self) = @_; 488 my ($self) = @_;
458 493
459 map { $_ + 4 } $chld->size_request; 494 map { $_ + 4 } $chld->size_request;
460} 495}
461 496
462sub size_allocate { 497sub size_allocate {
463 my ($self, $w, $h) = @_; 498 my ($self, $x, $y, $w, $h) = @_;
464 499
465 return unless $self->{w} != $w || $self->{h} != $h; 500 $self->_size_allocate ($x, $y, $w, $h) or return;
466 501
467 $self->{w} = $w;
468 $self->{h} = $h;
469
470 $self->child->size_allocate ($w - 4, $h - 4); 502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
471 $self->child->move (2, 2);
472} 503}
473 504
474sub _draw { 505sub _draw {
475 my ($self) = @_; 506 my ($self) = @_;
476 507
489 $chld->draw; 520 $chld->draw;
490} 521}
491 522
492############################################################################# 523#############################################################################
493 524
494package CFClient::Widget::FancyFrame; 525package CFClient::UI::FancyFrame;
495 526
496our @ISA = CFClient::Widget::Bin::; 527our @ISA = CFClient::UI::Bin::;
497 528
498use SDL::OpenGL; 529use SDL::OpenGL;
499 530
500my @tex = 531my @tex =
501 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
504sub size_request { 535sub size_request {
505 my ($self) = @_; 536 my ($self) = @_;
506 537
507 my ($w, $h) = $self->SUPER::size_request; 538 my ($w, $h) = $self->SUPER::size_request;
508 539
509 $h += $tex[1]->{height}; 540 $h += $tex[1]->{h};
510 $h += $tex[4]->{height}; 541 $h += $tex[4]->{h};
511 $w += $tex[2]->{width}; 542 $w += $tex[2]->{w};
512 $w += $tex[3]->{width}; 543 $w += $tex[3]->{w};
513 544
514 ($w, $h) 545 ($w, $h)
515} 546}
516 547
517sub size_allocate { 548sub size_allocate {
518 my ($self, $w, $h) = @_; 549 my ($self, $x, $y, $w, $h) = @_;
519 550
520 return unless $self->{w} != $w || $self->{h} != $h; 551 $self->_size_allocate ($x, $y, $w, $h) or return;
521 552
522 $self->SUPER::size_allocate ($w, $h);
523
524 $h -= $tex[1]->{height}; 553 $h -= $tex[1]->{h};
525 $h -= $tex[4]->{height}; 554 $h -= $tex[4]->{h};
526 $w -= $tex[2]->{width}; 555 $w -= $tex[2]->{w};
527 $w -= $tex[3]->{width}; 556 $w -= $tex[3]->{w};
528 557
529 $h = $h < 0 ? 0 : $h; 558 $h = $h < 0 ? 0 : $h;
530 $w = $w < 0 ? 0 : $w; 559 $w = $w < 0 ? 0 : $w;
531 560
532 my $child = $self->child; 561 my $child = $self->child;
533 562
534 $child->size_allocate ($w, $h); 563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
535 $child->move ($tex[3]->{width}, $tex[1]->{height});
536} 564}
537 565
538sub _draw { 566sub _draw {
539 my ($self) = @_; 567 my ($self) = @_;
540 568
541 my ($w, $h) = ($self->{w}, $self->{h}); 569 my ($w, $h) = ($self->{w}, $self->{h});
542 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
543 571
544 glEnable GL_BLEND; 572 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
545 glEnable GL_TEXTURE_2D; 574 glEnable GL_TEXTURE_2D;
546 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
547 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
548 576
549 my $top = $tex[1]; 577 my $top = $tex[1];
550 $top->draw_quad (0, 0, $w, $top->{height}); 578 $top->draw_quad (0, 0, $w, $top->{h});
551 579
552 my $left = $tex[3]; 580 my $left = $tex[3];
553 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 581 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
554 582
555 my $right = $tex[2]; 583 my $right = $tex[2];
556 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 584 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
557 585
558 my $bottom = $tex[4]; 586 my $bottom = $tex[4];
559 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
560 588
561 my $bg = $tex[0]; 589 my $bg = $tex[0];
562 glBindTexture GL_TEXTURE_2D, $bg->{name}; 590 glBindTexture GL_TEXTURE_2D, $bg->{name};
563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
564 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
565 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
566 594
567 my $rep_x = $cw / $bg->{width}; 595 my $rep_x = $cw / $bg->{w};
568 my $rep_y = $ch / $bg->{height}; 596 my $rep_y = $ch / $bg->{h};
569 597
570 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
571 599
572 glDisable GL_BLEND; 600 glDisable GL_BLEND;
573 glDisable GL_TEXTURE_2D; 601 glDisable GL_TEXTURE_2D;
574 602
575 $self->child->draw; 603 $self->child->draw;
576 604
577} 605}
578 606
579############################################################################# 607#############################################################################
580 608
581package CFClient::Widget::Table; 609package CFClient::UI::Table;
582 610
583our @ISA = CFClient::Widget::Bin::; 611our @ISA = CFClient::UI::Base::;
612
613use List::Util qw(max sum);
584 614
585use SDL::OpenGL; 615use SDL::OpenGL;
586 616
587sub add { 617sub add {
588 my ($self, $x, $y, $chld) = @_; 618 my ($self, $x, $y, $chld) = @_;
589 my $old_chld = $self->{children}[$y][$x];
590 619
591 $self->{children}[$y][$x] = $chld; 620 $self->{children}[$y][$x] = $chld;
592 $chld->set_parent ($self); 621 $chld->set_parent ($self);
622
623 $self->{w} = $self->{h} = -1;
593 $self->update; 624 $self->update;
594} 625}
595 626
596sub max_row_height { 627sub get_wh {
597 my ($self, $row) = @_; 628 my ($self) = @_;
598 629
599 my $hs = 0; 630 my (@w, @h);
600 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 631
632 for my $y (0 .. $#{$self->{children}}) {
601 my $c = $self->{children}->[$row]->[$xi]; 633 my $row = $self->{children}[$y]
602 if ($c) { 634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
603 my ($w, $h) = $c->size_request; 639 my ($w, $h) = $widget->size_request;
604 if ($hs < $h) { $hs = $h } 640
641 $w[$x] = max $w[$x], $w;
642 $h[$y] = max $h[$y], $h;
605 } 643 }
606 } 644 }
607 return $hs;
608}
609 645
610sub max_col_width { 646 (\@w, \@h)
647}
648
649sub size_request {
611 my ($self, $col) = @_; 650 my ($self) = @_;
612 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
613 my $ws = 0; 680 my $x = 0;
614 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 681 my $row_h = $hs->[$r];
615 my $c = ($self->{children}->[$yi] || [])->[$col]; 682
616 if ($c) { 683 for my $c (0 .. $#$row) {
617 my ($w, $h) = $c->size_request; 684 my $widget = $row->[$c]
618 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;
619 } 692 }
620 }
621 return $ws;
622}
623 693
624sub size_request { 694 $y += $row_h;
625 my ($self) = @_;
626
627 my ($hs, $ws) = (0, 0);
628
629 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
630 $hs += $self->max_row_height ($yi);
631 } 695 }
632 696
633 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
634 my $wm = 0;
635 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
636 $wm += $self->max_col_width ($xi)
637 }
638 if ($ws < $wm) { $ws = $wm }
639 }
640
641 return ($ws, $hs);
642} 697}
643 698
644sub _draw { 699sub _draw {
645 my ($self) = @_; 700 my ($self) = @_;
646 701
647 my $y = 0; 702 for (grep $_, @{$self->{children}}) {
648 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 703 $_->draw for grep $_, @$_;
649 my $x = 0;
650
651 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
652
653 my $c = $self->{children}->[$yi]->[$xi];
654 if ($c) {
655 $c->move ($x, $y, 0); #TODO: Move to size_request
656 $c->draw if $c;
657 }
658
659 $x += $self->max_col_width ($xi);
660 }
661
662 $y += $self->max_row_height ($yi);
663 } 704 }
664} 705}
665 706
666############################################################################# 707#############################################################################
667 708
668package CFClient::Widget::VBox; 709package CFClient::UI::VBox;
669 710
670our @ISA = CFClient::Widget::Container::; 711our @ISA = CFClient::UI::Container::;
671 712
672use SDL::OpenGL; 713use SDL::OpenGL;
673 714
674sub size_request { 715sub size_request {
675 my ($self) = @_; 716 my ($self) = @_;
681 (List::Util::sum map $_->[1], @alloc), 722 (List::Util::sum map $_->[1], @alloc),
682 ) 723 )
683} 724}
684 725
685sub size_allocate { 726sub size_allocate {
686 my ($self, $w, $h) = @_; 727 my ($self, $x, $y, $w, $h) = @_;
687 728
688 return unless $self->{w} != $w || $self->{h} != $h; 729 $self->_size_allocate ($x, $y, $w, $h) or return;
689
690 $self->{w} = $w;
691 $self->{h} = $h;
692 730
693 return unless $self->{h}; 731 return unless $self->{h};
694 732
695 my $children = $self->{children}; 733 my $children = $self->{children};
696 734
718 756
719 my $y = 0; 757 my $y = 0;
720 for (0 .. $#$children) { 758 for (0 .. $#$children) {
721 my $child = $children->[$_]; 759 my $child = $children->[$_];
722 my $h = $h[$_]; 760 my $h = $h[$_];
723 $child->move (0, $y);
724 $child->size_allocate ($w, $h); 761 $child->size_allocate (0, $y, $w, $h);
725 762
726 $y += $h; 763 $y += $h;
727 } 764 }
728} 765}
729 766
730############################################################################# 767#############################################################################
731 768
732package CFClient::Widget::Label; 769package CFClient::UI::Label;
733 770
734our @ISA = CFClient::Widget::; 771our @ISA = CFClient::UI::Base::;
735 772
736use SDL::OpenGL; 773use SDL::OpenGL;
737 774
738sub new { 775sub new {
739 my ($class, %arg) = @_; 776 my ($class, %arg) = @_;
740 777
741 my $self = $class->SUPER::new ( 778 my $self = $class->SUPER::new (
742 fg => [1, 1, 1], 779 fg => [1, 1, 1],
743 height => $::FONTSIZE, 780 height => $::FONTSIZE,
744 text => "", 781 text => "",
782 align => -1,
745 layout => new CFClient::Layout, 783 layout => new CFClient::Layout,
746 %arg 784 %arg
747 ); 785 );
748 786
749 $self->set_text ($self->{text}); 787 $self->set_text ($self->{text});
781 my ($self) = @_; 819 my ($self) = @_;
782 820
783 $self->{layout}->set_width; 821 $self->{layout}->set_width;
784 $self->{layout}->set_height ($self->{height}); 822 $self->{layout}->set_height ($self->{height});
785 $self->{layout}->size 823 $self->{layout}->size
786# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
787# ( 825# (
788# $self->{texture}{width}, 826# $self->{texture}{w},
789# $self->{texture}{height}, 827# $self->{texture}{h},
790# ) 828# )
791# } else { 829# } else {
792# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
793# 831#
794# ($w, $h) 832# ($w, $h)
795# } 833# }
796} 834}
797 835
798sub size_allocate { 836sub size_allocate {
799 my ($self, $w, $h) = @_; 837 my ($self, $x, $y, $w, $h) = @_;
800 838
801 return unless $self->{w} != $w || $self->{h} != $h; 839 $self->_size_allocate ($x, $y, $w, $h) or return;
802 840
803 $self->SUPER::size_allocate ($w, $h);
804 delete $self->{texture}; 841 delete $self->{texture};
805} 842}
806 843
807sub update { 844sub update {
808 my ($self) = @_; 845 my ($self) = @_;
814sub _draw { 851sub _draw {
815 my ($self) = @_; 852 my ($self) = @_;
816 853
817 my $tex = $self->{texture} ||= do { 854 my $tex = $self->{texture} ||= do {
818 $self->{layout}->set_width ($self->{w}); 855 $self->{layout}->set_width ($self->{w});
819 new_from_layout CFClient::Texture $self->{layout}; 856 new_from_layout CFClient::Texture $self->{layout}
820 }; 857 };
821 858
822 glEnable GL_BLEND; 859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
823 glEnable GL_TEXTURE_2D; 861 glEnable GL_TEXTURE_2D;
824 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
825 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
826 863
827 glColor @{$self->{fg}}; 864 glColor @{$self->{fg}};
828 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
829 $tex->draw_quad (0, 0); 871 $tex->draw_quad ($x, 0);
830 872
873 glDisable GL_TEXTURE_2D;
831 glDisable GL_BLEND; 874 glDisable GL_BLEND;
832 glDisable GL_TEXTURE_2D;
833} 875}
834 876
835############################################################################# 877#############################################################################
836 878
837package CFClient::Widget::Entry; 879package CFClient::UI::Entry;
838 880
839our @ISA = CFClient::Widget::Label::; 881our @ISA = CFClient::UI::Label::;
840 882
841use SDL; 883use SDL;
842use SDL::OpenGL; 884use SDL::OpenGL;
843 885
844sub new { 886sub new {
858 900
859 $self->{last_activity} = $::NOW; 901 $self->{last_activity} = $::NOW;
860 902
861 $self->{text} = $text; 903 $self->{text} = $text;
862 $self->{layout}->set_width ($self->{w}); 904 $self->{layout}->set_width ($self->{w});
905
906 $text =~ s/./*/g if $self->{hidden};
907
908
863 $self->{layout}->set_markup ($self->escape_text ($text)); 909 $self->{layout}->set_markup ($self->escape_text ($text));
864 910
865 $text = substr $text, 0, $self->{cursor}; 911 $text = substr $text, 0, $self->{cursor};
866 utf8::encode $text; 912 utf8::encode $text;
867 913
875 921
876 ($w + 1, $h) # add 1 for cursor 922 ($w + 1, $h) # add 1 for cursor
877} 923}
878 924
879sub size_allocate { 925sub size_allocate {
880 my ($self, $w, $h) = @_; 926 my ($self, $x, $y, $w, $h) = @_;
881 927
882 return unless $self->{w} != $w || $self->{h} != $h;
883
884 $self->SUPER::size_allocate ($w, $h); 928 $self->SUPER::size_allocate ($x, $y, $w, $h);
885 929
886 $self->_set_text ($self->{text}); 930 $self->_set_text ($self->{text});
887} 931}
888 932
889sub set_text { 933sub set_text {
980 } 1024 }
981} 1025}
982 1026
983############################################################################# 1027#############################################################################
984 1028
985package CFClient::Widget::Slider; 1029package CFClient::UI::Slider;
986 1030
987use strict; 1031use strict;
988 1032
989use SDL::OpenGL; 1033use SDL::OpenGL;
990use SDL::OpenGL::Constants;
991 1034
992our @ISA = CFClient::Widget::DrawBG::; 1035our @ISA = CFClient::UI::DrawBG::;
993 1036
994sub size_request { 1037sub size_request {
995 my ($self) = @_; 1038 my ($self) = @_;
996 1039
997 my $w = 20; 1040 my $w = 50;
998 my $h = 10; 1041 my $h = 10;
999 1042
1000 $self->{vertical} ? ($h, $w) : ($w, $h) 1043 $self->{vertical} ? ($h, $w) : ($w, $h)
1001} 1044}
1002 1045
1025 my ($self, $ev, $x, $y) = @_; 1068 my ($self, $ev, $x, $y) = @_;
1026 1069
1027 if ($GRAB == $self) { 1070 if ($GRAB == $self) {
1028 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1071 my ($value, $lo, $hi, $page) = @{$self->{range}};
1029 1072
1073 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1074
1030 $x = $x * ($hi - $lo) / $self->{w} + $lo; 1075 $x = $x * ($hi - $lo) / $w + $lo;
1031 $x = $lo if $x < $lo; 1076 $x = $lo if $x < $lo;
1032 $x = $hi - $page if $x > $hi - $page; 1077 $x = $hi - $page if $x > $hi - $page;
1033 $self->{range}[0] = $x; 1078 $self->{range}[0] = $x;
1034 1079
1035 $self->{changed}($x) if $self->{changed}; 1080 $self->emit (changed => $x);
1036 $self->update; 1081 $self->update;
1037 } 1082 }
1038} 1083}
1039 1084
1040sub _draw { 1085sub _draw {
1041 my ($self) = @_; 1086 my ($self) = @_;
1042 1087
1043 $self->SUPER::_draw (); 1088 $self->SUPER::_draw ();
1044
1045 glPushMatrix;
1046 1089
1047 my ($w, $h) = @$self{qw(w h)}; 1090 my ($w, $h) = @$self{qw(w h)};
1048 1091
1049 if ($self->{vertical}) { 1092 if ($self->{vertical}) {
1050 # draw a vertical slider like a rotated horizontal slider 1093 # draw a vertical slider like a rotated horizontal slider
1051 1094
1052 glTranslate 0, $self->{w};
1053 glRotate 90, 0, 0, 1; 1095 glRotate 90, 0, 0, 1;
1096 glTranslate 0, -$self->{w}, 0;
1054 1097
1055 ($w, $h) = ($h, $w); 1098 ($w, $h) = ($h, $w);
1056 } 1099 }
1057 1100
1058 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1101 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1090 glVertex $knob_a + 1, $h - 1; 1133 glVertex $knob_a + 1, $h - 1;
1091 glVertex $knob_b - 1, $h - 1; 1134 glVertex $knob_b - 1, $h - 1;
1092 glVertex $knob_b - 1, 1; 1135 glVertex $knob_b - 1, 1;
1093 } 1136 }
1094 glEnd; 1137 glEnd;
1095
1096 glPopMatrix;
1097} 1138}
1098 1139
1099############################################################################# 1140#############################################################################
1100 1141
1101package CFClient::Widget::MapWidget; 1142package CFClient::UI::MapWidget;
1102 1143
1103use strict; 1144use strict;
1104 1145
1105use List::Util qw(min max); 1146use List::Util qw(min max);
1106 1147
1107use SDL; 1148use SDL;
1108use SDL::OpenGL; 1149use SDL::OpenGL;
1109use SDL::OpenGL::Constants;
1110 1150
1111our @ISA = CFClient::Widget::; 1151our @ISA = CFClient::UI::Base::;
1112 1152
1113sub new { 1153sub new {
1114 my $class = shift; 1154 my $class = shift;
1115 1155
1116 $class->SUPER::new ( 1156 $class->SUPER::new (
1136 1176
1137sub update { 1177sub update {
1138 my ($self) = @_; 1178 my ($self) = @_;
1139 1179
1140 $self->{need_update} = 1; 1180 $self->{need_update} = 1;
1181 $self->SUPER::update;
1141} 1182}
1142 1183
1143sub _draw { 1184sub _draw {
1144 my ($self) = @_; 1185 my ($self) = @_;
1145 1186
1168 $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};
1169 } 1210 }
1170 1211
1171 glEnable GL_TEXTURE_2D; 1212 glEnable GL_TEXTURE_2D;
1172 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1173 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1174 1216
1175 my $sw4 = ($sw + 3) & ~3; 1217 my $sw4 = ($sw + 3) & ~3;
1176 my $darkness = "\x00" x ($sw4 * $sh); 1218 my $darkness = "\x00" x ($sw4 * $sh);
1177 1219
1190 } 1232 }
1191 1233
1192 for my $num (grep $_, @$cell[1,2,3]) { 1234 for my $num (grep $_, @$cell[1,2,3]) {
1193 my $tex = $::CONN->{face}[$num]{texture} || next; 1235 my $tex = $::CONN->{face}[$num]{texture} || next;
1194 1236
1195 my $w = $tex->{width}; 1237 my ($w, $h) = @$tex{qw(w h)};
1196 my $h = $tex->{height};
1197 1238
1198 $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);
1199 } 1240 }
1200 } 1241 }
1201 } 1242 }
1208# 1249#
1209# $lighting = $pb->get_pixels; 1250# $lighting = $pb->get_pixels;
1210# $lighting =~ s/(.)../$1/gs; 1251# $lighting =~ s/(.)../$1/gs;
1211# } 1252# }
1212 1253
1213 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1214 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1215 1255
1216 $darkness = new CFClient::Texture 1256 $darkness = new CFClient::Texture
1217 width => $sw4, 1257 w => $sw4,
1218 height => $sh, 1258 h => $sh,
1219 data => $darkness, 1259 data => $darkness,
1220 internalformat => GL_ALPHA, 1260 internalformat => GL_ALPHA,
1221 format => GL_ALPHA; 1261 format => GL_ALPHA;
1222 1262
1223 glColor 0.45, 0.45, 0.45, 1; 1263 glColor 0.45, 0.45, 0.45, 1;
1283 } 1323 }
1284} 1324}
1285 1325
1286############################################################################# 1326#############################################################################
1287 1327
1288package CFClient::Widget::Animator; 1328package CFClient::UI::Animator;
1289 1329
1290use SDL::OpenGL; 1330use SDL::OpenGL;
1291 1331
1292our @ISA = CFClient::Widget::Bin::; 1332our @ISA = CFClient::UI::Bin::;
1293 1333
1294sub moveto { 1334sub moveto {
1295 my ($self, $x, $y) = @_; 1335 my ($self, $x, $y) = @_;
1296 1336
1297 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1337 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1325 glPopMatrix; 1365 glPopMatrix;
1326} 1366}
1327 1367
1328############################################################################# 1368#############################################################################
1329 1369
1330package CFClient::Widget::Toplevel; 1370package CFClient::UI::Toplevel;
1331 1371
1332our @ISA = CFClient::Widget::Container::; 1372our @ISA = CFClient::UI::Container::;
1333 1373
1334sub size_request { 1374sub size_request {
1335 ($::WIDTH, $::HEIGHT) 1375 ($::WIDTH, $::HEIGHT)
1336} 1376}
1337 1377
1338sub size_allocate { 1378sub size_allocate {
1339 my ($self, $w, $h) = @_; 1379 my ($self, $x, $y, $w, $h) = @_;
1340 1380
1341 $self->SUPER::size_allocate ($w, $h); 1381 $self->_size_allocate ($x, $y, $w, $h);
1342 1382
1343 $_->size_allocate ($_->size_request) 1383 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1344 for @{$self->{children}}; 1384 for @{$self->{children}};
1345} 1385}
1346 1386
1347sub translate { 1387sub translate {
1348 my ($self, $x, $y) = @_; 1388 my ($self, $x, $y) = @_;
1351} 1391}
1352 1392
1353sub update { 1393sub update {
1354 my ($self) = @_; 1394 my ($self) = @_;
1355 1395
1356 $self->size_allocate ($self->size_request); 1396 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1357 ::refresh (); 1397 ::refresh ();
1358} 1398}
1359 1399
1360sub add { 1400sub add {
1361 my ($self, $widget) = @_; 1401 my ($self, $widget) = @_;
1362 1402
1363 $self->SUPER::add ($widget); 1403 $self->SUPER::add ($widget);
1364 1404
1365 $widget->size_allocate ($widget->size_request); 1405 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1366} 1406}
1367 1407
1368sub draw { 1408sub draw {
1369 my ($self) = @_; 1409 my ($self) = @_;
1370 1410
1371 $self->_draw; 1411 $self->_draw;
1372} 1412}
1373 1413
1374############################################################################# 1414#############################################################################
1375 1415
1376package CFClient::Widget; 1416package CFClient::UI;
1377 1417
1378$TOPLEVEL = new CFClient::Widget::Toplevel; 1418$TOPLEVEL = new CFClient::UI::Toplevel;
1379 1419
13801 14201
1381 1421

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines