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.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
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 { @_ }, $class 84 bless {
85 x => 0,
86 y => 0,
87 z => 0,
88 @_
89 }, $class
79} 90}
80 91
81sub move { 92sub move {
82 my ($self, $x, $y, $z) = @_; 93 my ($self, $x, $y, $z) = @_;
83 $self->{x} = $x; 94 $self->{x} = $x;
109} 120}
110 121
111sub focus_in { 122sub focus_in {
112 my ($self) = @_; 123 my ($self) = @_;
113 124
125 return if $FOCUS == $self;
126
114 my $focus = $FOCUS; $FOCUS = $self; 127 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus; 128 $focus->update if $focus;
116 $FOCUS->update; 129 $FOCUS->update;
117} 130}
118 131
125 $focus->update if $focus; #? 138 $focus->update if $focus; #?
126} 139}
127 140
128sub mouse_motion { } 141sub mouse_motion { }
129sub button_up { } 142sub button_up { }
130sub button_down { }
131sub key_down { } 143sub key_down { }
132sub key_up { } 144sub key_up { }
145
146sub button_down {
147 my ($self, $ev, $x, $y) = @_;
148
149 $self->focus_in;
150}
133 151
134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 152sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 153sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 154sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 155sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 156sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
139 157
140sub draw { 158sub draw {
141 my ($self) = @_; 159 my ($self) = @_;
142 160
161 return unless $self->{h} && $self->{w};
162
143 glPushMatrix; 163 glPushMatrix;
144 glTranslate $self->{x}, $self->{y}, 0; 164 glTranslate $self->{x}, $self->{y}, 0;
145 $self->_draw; 165 $self->_draw;
166 glPopMatrix;
167
146 if ($self == $HOVER) { 168 if ($self == $HOVER) {
169 my ($x, $y) = @$self{qw(x y)};
170
147 glColor 1, 1, 1, 0.4; 171 glColor 1, 1, 1, 0.1;
148 glEnable GL_BLEND; 172 glEnable GL_BLEND;
173 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
149 glBegin GL_QUADS; 174 glBegin GL_QUADS;
150 glVertex 0 , 0; 175 glVertex $x , $y;
151 glVertex $self->{w}, 0; 176 glVertex $x + $self->{w}, $y;
152 glVertex $self->{w}, $self->{h}; 177 glVertex $x + $self->{w}, $y + $self->{h};
153 glVertex 0 , $self->{h}; 178 glVertex $x , $y + $self->{h};
154 glEnd; 179 glEnd;
155 glDisable GL_BLEND; 180 glDisable GL_BLEND;
156 } 181 }
157 glPopMatrix;
158} 182}
159 183
160sub _draw { 184sub _draw {
161 my ($self) = @_; 185 my ($self) = @_;
162 186
202 226
203 $self->{parent}->update 227 $self->{parent}->update
204 if $self->{parent}; 228 if $self->{parent};
205} 229}
206 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
207sub DESTROY { 244sub DESTROY {
208 my ($self) = @_; 245 my ($self) = @_;
209 246
210 #$self->deactivate; 247 #$self->deactivate;
211} 248}
212 249
213############################################################################# 250#############################################################################
214 251
215package CFClient::Widget::Container; 252package CFClient::UI::DrawBG;
216 253
217our @ISA = CFClient::Widget::; 254our @ISA = CFClient::UI::Base::;
255
256use strict;
257use SDL::OpenGL;
218 258
219sub new { 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
287package CFClient::UI::Empty;
288
289our @ISA = CFClient::UI::Base::;
290
291sub size_request {
292 (0, 0)
293}
294
295sub draw { }
296
297#############################################################################
298
299package CFClient::UI::Container;
300
301our @ISA = CFClient::UI::Base::;
302
303sub new {
220 my ($class, @widgets) = @_; 304 my ($class, %arg) = @_;
221 305
306 my $children = delete $arg{children} || [];
307
222 my $self = $class->SUPER::new (children => []); 308 my $self = $class->SUPER::new (children => [], %arg);
223 $self->add ($_) for @widgets; 309 $self->add ($_) for @$children;
224 310
225 $self 311 $self
226} 312}
227 313
228sub add { 314sub add {
229 my ($self, $chld, $expand) = @_; 315 my ($self, $chld, $expand) = @_;
230 316
231 $chld->{expand} = $expand; 317 $chld->{expand} = $expand;
232 $chld->set_parent ($self); 318 $chld->set_parent ($self);
233 319
234 @{$self->{children}} = 320 $self->{children} = [
235 sort { $a->{z} <=> $b->{z} } 321 sort { $a->{z} <=> $b->{z} }
236 @{$self->{children}}, $chld; 322 @{$self->{children}}, $chld
323 ];
237 324
238 $self->size_allocate ($self->{w}, $self->{h}) 325 $self->size_allocate ($self->{w}, $self->{h})
239 if $self->{w}; #TODO: check for "realised state" 326 if $self->{w}; #TODO: check for "realised state"
240} 327}
241 328
269 $_->draw for @{$self->{children}}; 356 $_->draw for @{$self->{children}};
270} 357}
271 358
272############################################################################# 359#############################################################################
273 360
274package CFClient::Widget::Bin; 361package CFClient::UI::Bin;
275 362
276our @ISA = CFClient::Widget::Container::; 363our @ISA = CFClient::UI::Container::;
364
365sub new {
366 my ($class, %arg) = @_;
367
368 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
369
370 $class->SUPER::new (children => [$child], %arg)
371}
372
373sub add {
374 my ($self, $widget) = @_;
375
376 $self->{children} = [];
377
378 $self->SUPER::add ($widget);
379}
380
381sub remove {
382 my ($self, $widget) = @_;
383
384 $self->SUPER::remove ($widget);
385
386 $self->{children} = [new CFClient::UI::Empty]
387 unless @{$self->{children}};
388}
277 389
278sub child { $_[0]->{children}[0] } 390sub child { $_[0]->{children}[0] }
279 391
280sub size_request { 392sub size_request {
281 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 393 $_[0]{children}[0]->size_request
282} 394}
283 395
284sub size_allocate { 396sub size_allocate {
285 my ($self, $w, $h) = @_; 397 my ($self, $w, $h) = @_;
286 398
399 return unless $self->{w} != $w || $self->{h} != $h;
400
287 $self->SUPER::size_allocate ($w, $h); 401 $self->SUPER::size_allocate ($w, $h);
288 $self->{children}[0]->size_allocate ($w, $h) 402 $self->{children}[0]->size_allocate ($w, $h);
289 if $self->{children}[0]
290} 403}
291 404
292############################################################################# 405#############################################################################
293 406
294package CFClient::Widget::Window; 407package CFClient::UI::Window;
295 408
296our @ISA = CFClient::Widget::Bin::; 409our @ISA = CFClient::UI::Bin::;
297 410
298use SDL::OpenGL; 411use SDL::OpenGL;
299 412
300sub new { 413sub new {
301 my ($class, $x, $y, $z, $w, $h) = @_; 414 my ($class, %arg) = @_;
302 415
303 my $self = $class->SUPER::new; 416 my $self = $class->SUPER::new (%arg);
304
305 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
306} 417}
307 418
308sub update { 419sub update {
309 my ($self) = @_; 420 my ($self) = @_;
310 421
422 # we want to do this delayed...
311 $self->render_chld; 423 $self->render_chld;
312 $self->SUPER::update; 424 $self->SUPER::update;
313} 425}
314 426
315sub render_chld { 427sub render_chld {
322} 434}
323 435
324sub size_allocate { 436sub size_allocate {
325 my ($self, $w, $h) = @_; 437 my ($self, $w, $h) = @_;
326 438
439 return unless $self->{w} != $w || $self->{h} != $h;
440
327 $self->{w} = $w; 441 $self->{w} = $w;
328 $self->{h} = $h; 442 $self->{h} = $h;
329 443
330 $self->child->size_allocate ($w, $h); 444 $self->child->size_allocate ($w, $h);
331 445
339 453
340 my $tex = $self->{texture} 454 my $tex = $self->{texture}
341 or return; 455 or return;
342 456
343 glEnable GL_BLEND; 457 glEnable GL_BLEND;
458 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
344 glEnable GL_TEXTURE_2D; 459 glEnable GL_TEXTURE_2D;
345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 460 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
346 461
347 $tex->draw_quad (0, 0, $w, $h); 462 $tex->draw_quad (0, 0, $w, $h);
348 463
350 glDisable GL_TEXTURE_2D; 465 glDisable GL_TEXTURE_2D;
351} 466}
352 467
353############################################################################# 468#############################################################################
354 469
355package CFClient::Widget::Frame; 470package CFClient::UI::Frame;
356 471
357our @ISA = CFClient::Widget::Bin::; 472our @ISA = CFClient::UI::Bin::;
358 473
359use SDL::OpenGL; 474use SDL::OpenGL;
360 475
361sub size_request { 476sub size_request {
362 my ($self) = @_; 477 my ($self) = @_;
369} 484}
370 485
371sub size_allocate { 486sub size_allocate {
372 my ($self, $w, $h) = @_; 487 my ($self, $w, $h) = @_;
373 488
489 return unless $self->{w} != $w || $self->{h} != $h;
490
374 $self->{w} = $w; 491 $self->{w} = $w;
375 $self->{h} = $h; 492 $self->{h} = $h;
376 493
377 $self->child->size_allocate ($w - 4, $h - 4); 494 $self->child->size_allocate ($w - 4, $h - 4);
378 $self->child->move (2, 2); 495 $self->child->move (2, 2);
396 $chld->draw; 513 $chld->draw;
397} 514}
398 515
399############################################################################# 516#############################################################################
400 517
401package CFClient::Widget::FancyFrame; 518package CFClient::UI::FancyFrame;
402 519
403our @ISA = CFClient::Widget::Bin::; 520our @ISA = CFClient::UI::Bin::;
404 521
405use SDL::OpenGL; 522use SDL::OpenGL;
406 523
407my @tex = 524my @tex =
408 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 525 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
411sub size_request { 528sub size_request {
412 my ($self) = @_; 529 my ($self) = @_;
413 530
414 my ($w, $h) = $self->SUPER::size_request; 531 my ($w, $h) = $self->SUPER::size_request;
415 532
416 $h += $tex[1]->{height}; 533 $h += $tex[1]->{h};
417 $h += $tex[4]->{height}; 534 $h += $tex[4]->{h};
418 $w += $tex[2]->{width}; 535 $w += $tex[2]->{w};
419 $w += $tex[3]->{width}; 536 $w += $tex[3]->{w};
420 537
421 ($w, $h) 538 ($w, $h)
422} 539}
423 540
424sub size_allocate { 541sub size_allocate {
425 my ($self, $w, $h) = @_; 542 my ($self, $w, $h) = @_;
426 543
544 return unless $self->{w} != $w || $self->{h} != $h;
545
427 $self->SUPER::size_allocate ($w, $h); 546 $self->SUPER::size_allocate ($w, $h);
428 547
429 $h -= $tex[1]->{height}; 548 $h -= $tex[1]->{h};
430 $h -= $tex[4]->{height}; 549 $h -= $tex[4]->{h};
431 $w -= $tex[2]->{width}; 550 $w -= $tex[2]->{w};
432 $w -= $tex[3]->{width}; 551 $w -= $tex[3]->{w};
433 552
434 $h = $h < 0 ? 0 : $h; 553 $h = $h < 0 ? 0 : $h;
435 $w = $w < 0 ? 0 : $w; 554 $w = $w < 0 ? 0 : $w;
436 555
556 my $child = $self->child;
557
437 $self->child->size_allocate ($w, $h); 558 $child->size_allocate ($w, $h);
438 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 559 $child->move ($tex[3]->{w}, $tex[1]->{h});
439} 560}
440 561
441sub _draw { 562sub _draw {
442 my ($self) = @_; 563 my ($self) = @_;
443 564
444 my ($w, $h) = ($self->{w}, $self->{h}); 565 my ($w, $h) = ($self->{w}, $self->{h});
445 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 566 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
446 567
447 glEnable GL_BLEND; 568 glEnable GL_BLEND;
569 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
448 glEnable GL_TEXTURE_2D; 570 glEnable GL_TEXTURE_2D;
449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 571 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
451 572
452 my $top = $tex[1]; 573 my $top = $tex[1];
453 $top->draw_quad (0, 0, $w, $top->{height}); 574 $top->draw_quad (0, 0, $w, $top->{h});
454 575
455 my $left = $tex[3]; 576 my $left = $tex[3];
456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 577 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
457 578
458 my $right = $tex[2]; 579 my $right = $tex[2];
459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 580 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
460 581
461 my $bottom = $tex[4]; 582 my $bottom = $tex[4];
462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 583 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
463 584
464 my $bg = $tex[0]; 585 my $bg = $tex[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 586 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 587 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 588 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 589 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 590
470 my $rep_x = $cw / $bg->{width}; 591 my $rep_x = $cw / $bg->{w};
471 my $rep_y = $ch / $bg->{height}; 592 my $rep_y = $ch / $bg->{h};
472 593
473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 594 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
474 595
475 glDisable GL_BLEND; 596 glDisable GL_BLEND;
476 glDisable GL_TEXTURE_2D; 597 glDisable GL_TEXTURE_2D;
477 598
478 $self->child->draw; 599 $self->child->draw;
479 600
480} 601}
481 602
482############################################################################# 603#############################################################################
483 604
484package CFClient::Widget::Table; 605package CFClient::UI::Table;
485 606
486our @ISA = CFClient::Widget::Bin::; 607our @ISA = CFClient::UI::Base::;
487 608
488use SDL::OpenGL; 609use SDL::OpenGL;
489 610
490sub add { 611sub add {
491 my ($self, $x, $y, $chld) = @_; 612 my ($self, $x, $y, $chld) = @_;
566 } 687 }
567} 688}
568 689
569############################################################################# 690#############################################################################
570 691
571package CFClient::Widget::VBox; 692package CFClient::UI::VBox;
572 693
573our @ISA = CFClient::Widget::Container::; 694our @ISA = CFClient::UI::Container::;
574 695
575use SDL::OpenGL; 696use SDL::OpenGL;
576 697
577sub size_request { 698sub size_request {
578 my ($self) = @_; 699 my ($self) = @_;
586} 707}
587 708
588sub size_allocate { 709sub size_allocate {
589 my ($self, $w, $h) = @_; 710 my ($self, $w, $h) = @_;
590 711
712 return unless $self->{w} != $w || $self->{h} != $h;
713
591 $self->w ($w); 714 $self->{w} = $w;
592 $self->h ($h); 715 $self->{h} = $h;
593 716
594 my $exp; 717 return unless $self->{h};
595 my @oth; 718
596 # find expand widget 719 my $children = $self->{children};
597 for (@{$self->{children}}) { 720
598 if ($_->{expand}) { 721 my @h = map +($_->size_request)[1], @$children;
599 $exp = $_; 722
600 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;
601 } 740 }
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 } 741 }
613 742
614 my $y = 0; 743 my $y = 0;
615 for (@{$self->{children}}) { 744 for (0 .. $#$children) {
745 my $child = $children->[$_];
746 my $h = $h[$_];
616 $_->move (0, $y); 747 $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); 748 $child->size_allocate ($w, $h);
749
624 $y += $h; 750 $y += $h;
625 }
626 } 751 }
627} 752}
628 753
629############################################################################# 754#############################################################################
630 755
631package CFClient::Widget::Label; 756package CFClient::UI::Label;
632 757
633our @ISA = CFClient::Widget::; 758our @ISA = CFClient::UI::Base::;
634 759
635use SDL::OpenGL; 760use SDL::OpenGL;
636 761
637sub new { 762sub new {
638 my ($class, $x, $y, $z, $height, $text) = @_; 763 my ($class, %arg) = @_;
639 764
640 $height ||= $::FONTSIZE;
641
642 # TODO: color, and make height, xyz etc. optional
643 my $self = $class->SUPER::new ( 765 my $self = $class->SUPER::new (
644 x => $x, 766 fg => [1, 1, 1],
645 y => $y, 767 height => $::FONTSIZE,
646 z => $z, 768 text => "",
647 height => $height, 769 align => -1,
648 layout => new CFClient::Layout, 770 layout => new CFClient::Layout,
771 %arg
649 ); 772 );
650 773
651 $self->set_text ($text); 774 $self->set_text ($self->{text});
652 775
653 $self 776 $self
777}
778
779sub escape_text {
780 local $_ = $_[1];
781
782 s/&/&amp;/g;
783 s/>/&gt;/g;
784 s/</&lt;/g;
785
786 $_[1]
654} 787}
655 788
656sub set_text { 789sub set_text {
657 my ($self, $text) = @_; 790 my ($self, $text) = @_;
658 791
659 $self->{text} = $text; 792 $self->{text} = $text;
660 $self->{layout}->set_markup ($text); 793 $self->{layout}->set_markup ($text);
661 794
662 delete $self->{texture}; 795 delete $self->{texture};
796 $self->update;
663} 797}
664 798
665sub get_text { 799sub get_text {
666 my ($self, $text) = @_; 800 my ($self, $text) = @_;
667 801
670 804
671sub size_request { 805sub size_request {
672 my ($self) = @_; 806 my ($self) = @_;
673 807
674 $self->{layout}->set_width; 808 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height});
675 $self->{layout}->size 810 $self->{layout}->size
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
677# ( 812# (
678# $self->{texture}{width}, 813# $self->{texture}{w},
679# $self->{texture}{height}, 814# $self->{texture}{h},
680# ) 815# )
681# } else { 816# } else {
682# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
683# 818#
684# ($w, $h) 819# ($w, $h)
685# } 820# }
686} 821}
687 822
688sub size_allocate { 823sub size_allocate {
689 my ($self, $w, $h) = @_; 824 my ($self, $w, $h) = @_;
690 825
826 return unless $self->{w} != $w || $self->{h} != $h;
827
691 $self->SUPER::size_allocate ($w, $h); 828 $self->SUPER::size_allocate ($w, $h);
692 delete $self->{texture}; 829 delete $self->{texture};
693} 830}
694 831
832sub update {
833 my ($self) = @_;
834
835 delete $self->{texture};
836 $self->SUPER::update;
837}
838
695sub _draw { 839sub _draw {
696 my ($self) = @_; 840 my ($self) = @_;
697 841
698 my $tex = $self->{texture} ||= do { 842 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w}); 843 $self->{layout}->set_width ($self->{w});
700 new_from_layout CFClient::Texture $self->{layout}; 844 new_from_layout CFClient::Texture $self->{layout}
701 }; 845 };
702 846
703 glEnable GL_BLEND; 847 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
704 glEnable GL_TEXTURE_2D; 849 glEnable GL_TEXTURE_2D;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 851
708 glColor 1, 0, 0, 1; # TODO color 852 glColor @{$self->{fg}};
709 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
710 $tex->draw_quad (0, 0); 859 $tex->draw_quad ($x, 0);
711 860
861 glDisable GL_TEXTURE_2D;
712 glDisable GL_BLEND; 862 glDisable GL_BLEND;
713 glDisable GL_TEXTURE_2D; 863 glGetError and die;
714} 864}
715 865
716############################################################################# 866#############################################################################
717 867
718package CFClient::Widget::Entry; 868package CFClient::UI::Entry;
719 869
720our @ISA = CFClient::Widget::Label::; 870our @ISA = CFClient::UI::Label::;
721 871
722use SDL; 872use SDL;
723use SDL::OpenGL; 873use SDL::OpenGL;
724 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
725sub key_down { 932sub key_down {
726 my ($self, $ev) = @_; 933 my ($self, $ev) = @_;
727 934
728 my $mod = $ev->key_mod; 935 my $mod = $ev->key_mod;
729 my $sym = $ev->key_sym; 936 my $sym = $ev->key_sym;
731 my $uni = $ev->key_unicode; 938 my $uni = $ev->key_unicode;
732 939
733 my $text = $self->get_text; 940 my $text = $self->get_text;
734 941
735 if ($sym == SDLK_BACKSPACE) { 942 if ($sym == SDLK_BACKSPACE) {
736 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};
737 } elsif ($uni) { 950 } elsif ($uni) {
738 $text .= chr $uni; 951 substr $text, $self->{cursor}++, 0, chr $uni;
739 } 952 }
740 953
741 $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;
742} 964}
743 965
744sub button_down { 966sub button_down {
745 my ($self, $ev) = @_; 967 my ($self, $ev, $x, $y) = @_;
746 968
747 $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;
748} 980}
749 981
750sub mouse_motion { 982sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_; 983 my ($self, $ev, $x, $y) = @_;
752 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#
753} 985}
754 986
755sub _draw { 987sub _draw {
756 my ($self) = @_; 988 my ($self) = @_;
757 989
990 local $self->{fg} = $self->{fg};
991
758 if ($FOCUS == $self) { 992 if ($FOCUS == $self) {
759 glColor 1, 1, 1; 993 glColor @{$self->{active_bg}};
994 $self->{fg} = $self->{active_fg};
760 } else { 995 } else {
761 glColor 0.7, 0.7, 0.7; 996 glColor @{$self->{bg}};
762 } 997 }
763 998
764 glBegin GL_QUADS; 999 glBegin GL_QUADS;
765 glVertex 0 , 0; 1000 glVertex 0 , 0;
766 glVertex 0 , $self->{h} - 1; 1001 glVertex 0 , $self->{h};
767 glVertex $self->{w} - 1, $self->{h} - 1; 1002 glVertex $self->{w}, $self->{h};
768 glVertex $self->{w} - 1, 0; 1003 glVertex $self->{w}, 0;
769 glEnd; 1004 glEnd;
770 1005
771 $self->SUPER::_draw; 1006 $self->SUPER::_draw;
772}
773 1007
774############################################################################# 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}
775 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
776package CFClient::Widget::MapWidget; 1133package CFClient::UI::MapWidget;
777 1134
778use strict; 1135use strict;
779 1136
780use List::Util qw(min max); 1137use List::Util qw(min max);
781 1138
782use SDL; 1139use SDL;
783use SDL::OpenGL; 1140use SDL::OpenGL;
784use SDL::OpenGL::Constants;
785 1141
786our @ISA = CFClient::Widget::; 1142our @ISA = CFClient::UI::Base::;
1143
1144sub new {
1145 my $class = shift;
1146
1147 $class->SUPER::new (
1148 z => -1,
1149 list => (glGenLists 1),
1150 @_
1151 )
1152}
787 1153
788sub key_down { 1154sub key_down {
789 print "MAPKEYDOWN\n"; 1155 print "MAPKEYDOWN\n";
790} 1156}
791 1157
797 1 + int $::WIDTH / 32, 1163 1 + int $::WIDTH / 32,
798 1 + int $::HEIGHT / 32, 1164 1 + int $::HEIGHT / 32,
799 ) 1165 )
800} 1166}
801 1167
1168sub update {
1169 my ($self) = @_;
1170
1171 $self->{need_update} = 1;
1172 $self->SUPER::update;
1173}
1174
802sub _draw { 1175sub _draw {
803 my ($self) = @_; 1176 my ($self) = @_;
804 1177
1178 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE;
1180
805 my $mx = $::CONN->{mapx}; 1181 my $mx = $::CONN->{mapx};
806 my $my = $::CONN->{mapy}; 1182 my $my = $::CONN->{mapy};
807 1183
808 my $map = $::CONN->{map}; 1184 my $map = $::CONN->{map};
809 1185
810 my ($xofs, $yofs); 1186 my ($xofs, $yofs);
811 1187
812 my $sw = 1 + int $::WIDTH / 32; 1188 my $sw = 1 + int $::WIDTH / 32;
813 my $sh = 1 + int $::HEIGHT / 32; 1189 my $sh = 1 + int $::HEIGHT / 32;
814 1190
815 if ($::CONN->{mapw} > $sw) { 1191 if ($::CONN->{mapw} > $sw) {
816 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1192 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
817 } else { 1193 } else {
818 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1194 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
819 } 1195 }
820 1196
821 if ($::CONN->{maph} > $sh) { 1197 if ($::CONN->{maph} > $sh) {
822 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1198 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
823 } else { 1199 } else {
824 $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};
825 } 1201 }
826 1202
827 glEnable GL_TEXTURE_2D; 1203 glEnable GL_TEXTURE_2D;
828 glEnable GL_BLEND; 1204 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
829 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
830 1207
831 my $sw4 = ($sw + 3) & ~3; 1208 my $sw4 = ($sw + 3) & ~3;
832 my $darkness = "\x00" x ($sw4 * $sh); 1209 my $darkness = "\x00" x ($sw4 * $sh);
833 1210
834 for my $x (0 .. $sw - 1) { 1211 for my $x (0 .. $sw - 1) {
835 my $row = $map->[$x + $xofs]; 1212 my $row = $map->[$x + $xofs];
836 for my $y (0 .. $sh - 1) { 1213 for my $y (0 .. $sh - 1) {
837 1214
838 my $cell = $row->[$y + $yofs] 1215 my $cell = $row->[$y + $yofs]
839 or next; 1216 or next;
840 1217
841 my $dark = $cell->[0]; 1218 my $dark = $cell->[0];
842 if ($dark < 0) { 1219 if ($dark < 0) {
843 substr $darkness, $y * $sw4 + $x, 1, chr 224; 1220 substr $darkness, $y * $sw4 + $x, 1, chr 224;
844 } else { 1221 } else {
845 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 1222 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
846 } 1223 }
847 1224
848 for my $num (grep $_, @$cell[1,2,3]) { 1225 for my $num (grep $_, @$cell[1,2,3]) {
849 my $tex = $::CONN->{face}[$num]{texture} || next; 1226 my $tex = $::CONN->{face}[$num]{texture} || next;
850 1227
851 my $w = $tex->{width}; 1228 my ($w, $h) = @$tex{qw(w h)};
852 my $h = $tex->{height};
853 1229
854 $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);
1231 }
855 } 1232 }
856 } 1233 }
857 }
858 1234
859# if (1) { # higher quality darkness 1235# if (1) { # higher quality darkness
860# $lighting =~ s/(.)/$1$1$1/gs; 1236# $lighting =~ s/(.)/$1$1$1/gs;
861# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 1237# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
862# 1238#
864# 1240#
865# $lighting = $pb->get_pixels; 1241# $lighting = $pb->get_pixels;
866# $lighting =~ s/(.)../$1/gs; 1242# $lighting =~ s/(.)../$1/gs;
867# } 1243# }
868 1244
869 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
870 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1245 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
871 1246
872 $darkness = new CFClient::Texture 1247 $darkness = new CFClient::Texture
873 width => $sw4, 1248 w => $sw4,
874 height => $sh, 1249 h => $sh,
875 data => $darkness, 1250 data => $darkness,
876 internalformat => GL_ALPHA, 1251 internalformat => GL_ALPHA,
877 format => GL_ALPHA; 1252 format => GL_ALPHA;
878 1253
879 glColor 0.45, 0.45, 0.45, 1; 1254 glColor 0.45, 0.45, 0.45, 1;
880 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 1255 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
881 1256
882 glDisable GL_TEXTURE_2D; 1257 glDisable GL_TEXTURE_2D;
883 glDisable GL_BLEND; 1258 glDisable GL_BLEND;
1259
1260 glEndList;
1261 }
1262
1263 glCallList $self->{list};
884} 1264}
885 1265
886my %DIR = ( 1266my %DIR = (
887 SDLK_KP8, [1, "north"], 1267 SDLK_KP8, [1, "north"],
888 SDLK_KP9, [2, "northeast"], 1268 SDLK_KP9, [2, "northeast"],
934 } 1314 }
935} 1315}
936 1316
937############################################################################# 1317#############################################################################
938 1318
939package CFClient::Widget::Animator; 1319package CFClient::UI::Animator;
940 1320
941use SDL::OpenGL; 1321use SDL::OpenGL;
942 1322
943our @ISA = CFClient::Widget::Bin::; 1323our @ISA = CFClient::UI::Bin::;
944 1324
945sub moveto { 1325sub moveto {
946 my ($self, $x, $y) = @_; 1326 my ($self, $x, $y) = @_;
947 1327
948 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1328 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
976 glPopMatrix; 1356 glPopMatrix;
977} 1357}
978 1358
979############################################################################# 1359#############################################################################
980 1360
981package CFClient::Widget::Toplevel; 1361package CFClient::UI::Toplevel;
982 1362
983our @ISA = CFClient::Widget::Container::; 1363our @ISA = CFClient::UI::Container::;
984 1364
985sub size_request { 1365sub size_request {
986 ($::WIDTH, $::HEIGHT) 1366 ($::WIDTH, $::HEIGHT)
987} 1367}
988 1368
1022 $self->_draw; 1402 $self->_draw;
1023} 1403}
1024 1404
1025############################################################################# 1405#############################################################################
1026 1406
1027package CFClient::Widget; 1407package CFClient::UI;
1028 1408
1029$TOPLEVEL = new CFClient::Widget::Toplevel; 1409$TOPLEVEL = new CFClient::UI::Toplevel;
1030 1410
10311 14111
1032 1412

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines