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.56 by root, Mon Apr 10 22:16:34 2006 UTC vs.
Revision 1.72 by root, Tue Apr 11 18:00:45 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines