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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines