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.60 by root, Tue Apr 11 13:14:36 2006 UTC vs.
Revision 1.73 by root, Tue Apr 11 18:06:53 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines