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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines