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.69 by root, Tue Apr 11 17:32:14 2006 UTC vs.
Revision 1.78 by root, Tue Apr 11 22:49: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) = @_;
155 return unless $self->{h} && $self->{w}; 176 return unless $self->{h} && $self->{w};
156 177
157 glPushMatrix; 178 glPushMatrix;
158 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
159 $self->_draw; 180 $self->_draw;
181 glPopMatrix;
182
160 if ($self == $HOVER) { 183 if ($self == $HOVER) {
161 glColor 1, 1, 1, 0.4; 184 my ($x, $y) = @$self{qw(x y)};
185
186 glColor 0, 0, 1, 0.2;
162 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
163 glBegin GL_QUADS; 189 glBegin GL_QUADS;
164 glVertex 0 , 0; 190 glVertex $x , $y;
165 glVertex $self->{w}, 0; 191 glVertex $x + $self->{w}, $y;
166 glVertex $self->{w}, $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
167 glVertex 0 , $self->{h}; 193 glVertex $x , $y + $self->{h};
168 glEnd; 194 glEnd;
169 glDisable GL_BLEND; 195 glDisable GL_BLEND;
170 } 196 }
171 glPopMatrix;
172} 197}
173 198
174sub _draw { 199sub _draw {
175 my ($self) = @_; 200 my ($self) = @_;
176 201
177 warn "no draw defined for $self\n"; 202 warn "no draw defined for $self\n";
178}
179
180sub bbox {
181 my ($self) = @_;
182 my ($w, $h) = $self->size_request;
183 (
184 $self->{x},
185 $self->{y},
186 $self->{x} = $w,
187 $self->{y} = $h
188 )
189} 203}
190 204
191sub find_widget { 205sub find_widget {
192 my ($self, $x, $y) = @_; 206 my ($self, $x, $y) = @_;
193 207
196 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 210 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
197 211
198 () 212 ()
199} 213}
200 214
201sub del_parent { $_[0]->{parent} = undef }
202
203sub set_parent { 215sub set_parent {
204 my ($self, $par) = @_; 216 my ($self, $par) = @_;
205 217
206 $self->{parent} = $par; 218 $self->{parent} = $par;
207 Scalar::Util::weaken $self->{parent}; 219 Scalar::Util::weaken $self->{parent};
216 228
217 $self->{parent}->update 229 $self->{parent}->update
218 if $self->{parent}; 230 if $self->{parent};
219} 231}
220 232
233sub connect {
234 my ($self, $signal, $cb) = @_;
235
236 push @{ $self->{cb}{$signal} }, $cb;
237}
238
239sub emit {
240 my ($self, $signal, @args) = @_;
241
242 $_->($self, @args)
243 for @{$self->{cb}{$signal} || []};
244}
245
221sub DESTROY { 246sub DESTROY {
222 my ($self) = @_; 247 my ($self) = @_;
223 248
224 #$self->deactivate; 249 #$self->deactivate;
225} 250}
226 251
227############################################################################# 252#############################################################################
228 253
229package CFClient::Widget::DrawBG; 254package CFClient::UI::DrawBG;
230 255
231our @ISA = CFClient::Widget::; 256our @ISA = CFClient::UI::Base::;
232 257
233use strict; 258use strict;
234use SDL::OpenGL; 259use SDL::OpenGL;
235 260
236sub new { 261sub new {
237 my $class = shift; 262 my $class = shift;
238 263
239 # range [value, low, high, page] 264 # range [value, low, high, page]
240 265
241 $class->SUPER::new ( 266 $class->SUPER::new (
242 bg => [0, 0, 0, 0.4], 267 bg => [0, 0, 0, 0.2],
243 active_bg => [1, 1, 1], 268 active_bg => [1, 1, 1],
244 @_ 269 @_
245 ) 270 )
246} 271}
247 272
248sub _draw { 273sub _draw {
249 my ($self) = @_; 274 my ($self) = @_;
250 275
251 my ($w, $h) = @$self{qw(w h)}; 276 my ($w, $h) = @$self{qw(w h)};
252 277
278 glEnable GL_BLEND;
279 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
253 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 280 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
281
254 glBegin GL_QUADS; 282 glBegin GL_QUADS;
255 glVertex 0 , 0; 283 glVertex 0 , 0;
256 glVertex 0 , $h; 284 glVertex 0 , $h;
257 glVertex $w, $h; 285 glVertex $w, $h;
258 glVertex $w, 0; 286 glVertex $w, 0;
259 glEnd; 287 glEnd;
260}
261 288
262############################################################################# 289 glDisable GL_BLEND;
290}
263 291
292#############################################################################
293
264package CFClient::Widget::Empty; 294package CFClient::UI::Empty;
265 295
266our @ISA = CFClient::Widget::; 296our @ISA = CFClient::UI::Base::;
267 297
268sub size_request { 298sub size_request {
269 (0, 0) 299 (0, 0)
270} 300}
271 301
272sub draw { } 302sub draw { }
273 303
274############################################################################# 304#############################################################################
275 305
276package CFClient::Widget::Container; 306package CFClient::UI::Container;
277 307
278our @ISA = CFClient::Widget::; 308our @ISA = CFClient::UI::Base::;
279 309
280sub new { 310sub new {
281 my ($class, %arg) = @_; 311 my ($class, %arg) = @_;
282 312
283 my $children = delete $arg{children} || []; 313 my $children = delete $arg{children} || [];
287 317
288 $self 318 $self
289} 319}
290 320
291sub add { 321sub add {
292 my ($self, $chld, $expand) = @_; 322 my ($self, $chld) = @_;
293 323
294 $chld->{expand} = $expand;
295 $chld->set_parent ($self); 324 $chld->set_parent ($self);
296 325
297 $self->{children} = [ 326 $self->{children} = [
298 sort { $a->{z} <=> $b->{z} } 327 sort { $a->{z} <=> $b->{z} }
299 @{$self->{children}}, $chld 328 @{$self->{children}}, $chld
300 ]; 329 ];
301 330
302 $self->size_allocate ($self->{w}, $self->{h}) 331 $self->{w} = $self->{h} = -1;
303 if $self->{w}; #TODO: check for "realised state" 332 $self->update;
304} 333}
305 334
306sub remove { 335sub remove {
307 my ($self, $widget) = @_; 336 my ($self, $widget) = @_;
308 337
309 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 338 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
310 339
311 $self->size_allocate ($self->{w}, $self->{h}); 340 $self->size_allocate (0, 0, $self->{w}, $self->{h});
312} 341}
313 342
314sub find_widget { 343sub find_widget {
315 my ($self, $x, $y) = @_; 344 my ($self, $x, $y) = @_;
316 345
333 $_->draw for @{$self->{children}}; 362 $_->draw for @{$self->{children}};
334} 363}
335 364
336############################################################################# 365#############################################################################
337 366
338package CFClient::Widget::Bin; 367package CFClient::UI::Bin;
339 368
340our @ISA = CFClient::Widget::Container::; 369our @ISA = CFClient::UI::Container::;
341 370
342sub new { 371sub new {
343 my ($class, %arg) = @_; 372 my ($class, %arg) = @_;
344 373
345 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; 374 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
346 375
347 $class->SUPER::new (children => [$child], %arg) 376 $class->SUPER::new (children => [$child], %arg)
348} 377}
349 378
350sub add { 379sub add {
358sub remove { 387sub remove {
359 my ($self, $widget) = @_; 388 my ($self, $widget) = @_;
360 389
361 $self->SUPER::remove ($widget); 390 $self->SUPER::remove ($widget);
362 391
363 $self->{children} = [new CFClient::Widget::Empty] 392 $self->{children} = [new CFClient::UI::Empty]
364 unless @{$self->{children}}; 393 unless @{$self->{children}};
365} 394}
366 395
367sub child { $_[0]->{children}[0] } 396sub child { $_[0]->{children}[0] }
368 397
369sub size_request { 398sub size_request {
370 $_[0]{children}[0]->size_request 399 $_[0]{children}[0]->size_request
371} 400}
372 401
373sub size_allocate { 402sub size_allocate {
374 my ($self, $w, $h) = @_; 403 my ($self, $x, $y, $w, $h) = @_;
375 404
376 return unless $self->{w} != $w || $self->{h} != $h; 405 $self->_size_allocate ($x, $y, $w, $h) or return;
377 406
378 $self->SUPER::size_allocate ($w, $h);
379 $self->{children}[0]->size_allocate ($w, $h); 407 $self->{children}[0]->size_allocate (0, 0, $w, $h);
380} 408}
381 409
382############################################################################# 410#############################################################################
383 411
384package CFClient::Widget::Window; 412package CFClient::UI::Window;
385 413
386our @ISA = CFClient::Widget::Bin::; 414our @ISA = CFClient::UI::Bin::;
387 415
388use SDL::OpenGL; 416use SDL::OpenGL;
389 417
390sub new { 418sub new {
391 my ($class, %arg) = @_; 419 my ($class, %arg) = @_;
409 $self->{w}, $self->{h}, sub { $self->child->draw } 437 $self->{w}, $self->{h}, sub { $self->child->draw }
410 ); 438 );
411} 439}
412 440
413sub size_allocate { 441sub size_allocate {
414 my ($self, $w, $h) = @_; 442 my ($self, $x, $y, $w, $h) = @_;
415 443
416 return unless $self->{w} != $w || $self->{h} != $h; 444 $self->_size_allocate ($x, $y, $w, $h) or return;
417 445
418 $self->{w} = $w;
419 $self->{h} = $h;
420
421 $self->child->size_allocate ($w, $h); 446 $self->child->size_allocate (0, 0, $w, $h);
422 447
423 $self->render_chld; 448 $self->render_chld;
424} 449}
425 450
426sub _draw { 451sub _draw {
430 455
431 my $tex = $self->{texture} 456 my $tex = $self->{texture}
432 or return; 457 or return;
433 458
434 glEnable GL_BLEND; 459 glEnable GL_BLEND;
460 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
435 glEnable GL_TEXTURE_2D; 461 glEnable GL_TEXTURE_2D;
436 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 462 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
437 463
438 $tex->draw_quad (0, 0, $w, $h); 464 $tex->draw_quad (0, 0, $w, $h);
439 465
441 glDisable GL_TEXTURE_2D; 467 glDisable GL_TEXTURE_2D;
442} 468}
443 469
444############################################################################# 470#############################################################################
445 471
446package CFClient::Widget::Frame; 472package CFClient::UI::Frame;
447 473
448our @ISA = CFClient::Widget::Bin::; 474our @ISA = CFClient::UI::Bin::;
449 475
450use SDL::OpenGL; 476use SDL::OpenGL;
451 477
452sub size_request { 478sub size_request {
453 my ($self) = @_; 479 my ($self) = @_;
458 484
459 map { $_ + 4 } $chld->size_request; 485 map { $_ + 4 } $chld->size_request;
460} 486}
461 487
462sub size_allocate { 488sub size_allocate {
463 my ($self, $w, $h) = @_; 489 my ($self, $x, $y, $w, $h) = @_;
464 490
465 return unless $self->{w} != $w || $self->{h} != $h; 491 $self->_size_allocate ($x, $y, $w, $h) or return;
466 492
467 $self->{w} = $w;
468 $self->{h} = $h;
469
470 $self->child->size_allocate ($w - 4, $h - 4); 493 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
471 $self->child->move (2, 2);
472} 494}
473 495
474sub _draw { 496sub _draw {
475 my ($self) = @_; 497 my ($self) = @_;
476 498
489 $chld->draw; 511 $chld->draw;
490} 512}
491 513
492############################################################################# 514#############################################################################
493 515
494package CFClient::Widget::FancyFrame; 516package CFClient::UI::FancyFrame;
495 517
496our @ISA = CFClient::Widget::Bin::; 518our @ISA = CFClient::UI::Bin::;
497 519
498use SDL::OpenGL; 520use SDL::OpenGL;
499 521
500my @tex = 522my @tex =
501 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 523 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
502 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 524 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
503 525
504sub size_request { 526sub size_request {
505 my ($self) = @_; 527 my ($self) = @_;
506 528
529 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
530
507 my ($w, $h) = $self->SUPER::size_request; 531 my ($w, $h) = $self->SUPER::size_request;
508 532
509 $h += $tex[1]->{height}; 533 $h += $tex[1]->{h};
510 $h += $tex[4]->{height}; 534 $h += $tex[4]->{h};
511 $w += $tex[2]->{width}; 535 $w += $tex[2]->{w};
512 $w += $tex[3]->{width}; 536 $w += $tex[3]->{w};
513 537
514 ($w, $h) 538 ($w, $h)
515} 539}
516 540
517sub size_allocate { 541sub size_allocate {
518 my ($self, $w, $h) = @_; 542 my ($self, $x, $y, $w, $h) = @_;
519 543
520 return unless $self->{w} != $w || $self->{h} != $h; 544 $self->_size_allocate ($x, $y, $w, $h) or return;
521 545
522 $self->SUPER::size_allocate ($w, $h);
523
524 $h -= $tex[1]->{height}; 546 $h -= $tex[1]{h};
525 $h -= $tex[4]->{height}; 547 $h -= $tex[4]{h};
526 $w -= $tex[2]->{width}; 548 $w -= $tex[2]{w};
527 $w -= $tex[3]->{width}; 549 $w -= $tex[3]{w};
528 550
529 $h = $h < 0 ? 0 : $h; 551 $h = $h < 0 ? 0 : $h;
530 $w = $w < 0 ? 0 : $w; 552 $w = $w < 0 ? 0 : $w;
531 553
532 my $child = $self->child; 554 my $child = $self->child;
533 555
534 $child->size_allocate ($w, $h); 556 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
535 $child->move ($tex[3]->{width}, $tex[1]->{height}); 557}
558
559sub button_down {
560 my ($self, $ev, $x, $y) = @_;
561
562 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
563 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
564
565 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
566 my ($bw, $bh) = ($self->{w}, $self->{h});
567
568 $self->{motion} = sub {
569 my ($ev, $x, $y) = @_;
570
571 ($x, $y) = ($ev->motion_x, $ev->motion_y);
572
573 $self->{user_w} = $bw + $x - $ox;
574 $self->{user_h} = $bh + $y - $oy;
575 $self->update;
576 };
577
578 } elsif ($x >= 0 && $x < $self->{w}
579 && $y >= 0 && $y < $tex[1]{h}) {
580
581 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
582 my ($bx, $by) = ($self->{x}, $self->{y});
583
584 $self->{motion} = sub {
585 my ($ev, $x, $y) = @_;
586
587 ($x, $y) = ($ev->motion_x, $ev->motion_y);
588
589 $self->move ($bx + $x - $ox, $by + $y - $oy);
590 $self->update;
591 };
592 }
593}
594
595sub button_up {
596 my ($self, $ev, $x, $y) = @_;
597
598 delete $self->{motion};
599}
600
601sub mouse_motion {
602 my ($self, $ev, $x, $y) = @_;
603
604 $self->{motion}->($ev, $x, $y) if $self->{motion};
536} 605}
537 606
538sub _draw { 607sub _draw {
539 my ($self) = @_; 608 my ($self) = @_;
540 609
541 my ($w, $h) = ($self->{w}, $self->{h}); 610 my ($w, $h) = ($self->{w}, $self->{h});
542 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 611 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
543 612
544 glEnable GL_BLEND; 613 glEnable GL_BLEND;
614 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
545 glEnable GL_TEXTURE_2D; 615 glEnable GL_TEXTURE_2D;
546 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
547 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
548 617
549 my $top = $tex[1]; 618 my $top = $tex[1];
550 $top->draw_quad (0, 0, $w, $top->{height}); 619 $top->draw_quad (0, 0, $w, $top->{h});
551 620
552 my $left = $tex[3]; 621 my $left = $tex[3];
553 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 622 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
554 623
555 my $right = $tex[2]; 624 my $right = $tex[2];
556 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 625 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
557 626
558 my $bottom = $tex[4]; 627 my $bottom = $tex[4];
559 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 628 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
560 629
561 my $bg = $tex[0]; 630 my $bg = $tex[0];
631
562 glBindTexture GL_TEXTURE_2D, $bg->{name}; 632 glBindTexture GL_TEXTURE_2D, $bg->{name};
563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 633 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
564 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 634 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
565 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 635 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
566 636
567 my $rep_x = $cw / $bg->{width}; 637 my $rep_x = $cw / $bg->{w};
568 my $rep_y = $ch / $bg->{height}; 638 my $rep_y = $ch / $bg->{h};
569 639
570 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 640 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
571 641
642 glDisable GL_TEXTURE_2D;
572 glDisable GL_BLEND; 643 glDisable GL_BLEND;
573 glDisable GL_TEXTURE_2D;
574 644
575 $self->child->draw; 645 $self->child->draw;
576 646
577} 647}
578 648
579############################################################################# 649#############################################################################
580 650
581package CFClient::Widget::Table; 651package CFClient::UI::Table;
582 652
583our @ISA = CFClient::Widget::Bin::; 653our @ISA = CFClient::UI::Base::;
654
655use List::Util qw(max sum);
584 656
585use SDL::OpenGL; 657use SDL::OpenGL;
658
659sub new {
660 my $class = shift;
661
662 $class->SUPER::new (
663 col_expand => [],
664 @_
665 )
666}
586 667
587sub add { 668sub add {
588 my ($self, $x, $y, $chld) = @_; 669 my ($self, $x, $y, $chld) = @_;
589 my $old_chld = $self->{children}[$y][$x];
590 670
591 $self->{children}[$y][$x] = $chld; 671 $self->{children}[$y][$x] = $chld;
592 $chld->set_parent ($self); 672 $chld->set_parent ($self);
673
674 $self->{w} = $self->{h} = -1;
593 $self->update; 675 $self->update;
594} 676}
595 677
596sub max_row_height { 678sub get_wh {
597 my ($self, $row) = @_; 679 my ($self) = @_;
598 680
599 my $hs = 0; 681 my (@w, @h);
600 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 682
683 for my $y (0 .. $#{$self->{children}}) {
601 my $c = $self->{children}->[$row]->[$xi]; 684 my $row = $self->{children}[$y]
602 if ($c) { 685 or next;
686
687 for my $x (0 .. $#$row) {
688 my $widget = $row->[$x]
689 or next;
603 my ($w, $h) = $c->size_request; 690 my ($w, $h) = $widget->size_request;
604 if ($hs < $h) { $hs = $h } 691
692 $w[$x] = max $w[$x], $w;
693 $h[$y] = max $h[$y], $h;
605 } 694 }
606 } 695 }
607 return $hs;
608}
609 696
610sub max_col_width { 697 (\@w, \@h)
698}
699
700sub size_request {
611 my ($self, $col) = @_; 701 my ($self) = @_;
612 702
703 my ($ws, $hs) = $self->get_wh;
704
705 (
706 (sum @$ws),
707 (sum @$hs),
708 )
709}
710
711sub size_allocate {
712 my ($self, $x, $y, $w, $h) = @_;
713
714 $self->_size_allocate ($x, $y, $w, $h) or return;
715
716 my ($ws, $hs) = $self->get_wh;
717
718 my $req_w = sum @$ws;
719 my $req_h = sum @$hs;
720
721 # TODO: nicer code && do row_expand
722 my @col_expand = @{$self->{col_expand}};
723 @col_expand = (1) x @$ws unless @col_expand;
724 my $col_expand = (sum @col_expand) || 1;
725
726 # linearly scale sizes
727 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
728 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
729
730 my $y;
731
732 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r]
734 or next;
735
613 my $ws = 0; 736 my $x = 0;
614 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 737 my $row_h = $hs->[$r];
615 my $c = ($self->{children}->[$yi] || [])->[$col]; 738
616 if ($c) { 739 for my $c (0 .. $#$row) {
617 my ($w, $h) = $c->size_request; 740 my $widget = $row->[$c]
618 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;
619 } 748 }
749
750 $y += $row_h;
620 } 751 }
621 return $ws; 752
622} 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::;
623 786
624sub size_request { 787sub size_request {
625 my ($self) = @_; 788 my ($self) = @_;
626 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
627 my ($hs, $ws) = (0, 0); 805 ($h, $w) = ($w, $h);
628 806
629 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 807 my $children = $self->{children};
630 $hs += $self->max_row_height ($yi);
631 }
632 808
633 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 809 my @h = map +($_->size_request)[0], @$children;
634 my $wm = 0; 810
635 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 811 my $req_h = List::Util::sum @h;
636 $wm += $self->max_col_width ($xi) 812
813 if ($req_h > $h) {
814 # ah well, not enough space
815 $_ *= $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;
637 } 826 }
638 if ($ws < $wm) { $ws = $wm }
639 } 827 }
640
641 return ($ws, $hs);
642}
643
644sub _draw {
645 my ($self) = @_;
646 828
647 my $y = 0; 829 my $y = 0;
648 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 830 for (0 .. $#$children) {
831 my $child = $children->[$_];
649 my $x = 0; 832 my $h = $h[$_];
833 $child->size_allocate ($y, 0, $h, $w);
650 834
651 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 835 $y += $h;
652
653 my $c = $self->{children}->[$yi]->[$xi];
654 if ($c) {
655 $c->move ($x, $y, 0); #TODO: Move to size_request
656 $c->draw if $c;
657 }
658
659 $x += $self->max_col_width ($xi);
660 }
661
662 $y += $self->max_row_height ($yi);
663 } 836 }
664} 837}
665 838
666############################################################################# 839#############################################################################
667 840
668package CFClient::Widget::VBox; 841package CFClient::UI::VBox;
669 842
843# TODO: wrap into common Box base class
844
670our @ISA = CFClient::Widget::Container::; 845our @ISA = CFClient::UI::Container::;
671
672use SDL::OpenGL;
673 846
674sub size_request { 847sub size_request {
675 my ($self) = @_; 848 my ($self) = @_;
676 849
677 my @alloc = map [$_->size_request], @{$self->{children}}; 850 my @alloc = map [$_->size_request], @{$self->{children}};
681 (List::Util::sum map $_->[1], @alloc), 854 (List::Util::sum map $_->[1], @alloc),
682 ) 855 )
683} 856}
684 857
685sub size_allocate { 858sub size_allocate {
686 my ($self, $w, $h) = @_; 859 my ($self, $x, $y, $w, $h) = @_;
687 860
688 return unless $self->{w} != $w || $self->{h} != $h; 861 $self->_size_allocate ($x, $y, $w, $h) or return;
689
690 $self->{w} = $w;
691 $self->{h} = $h;
692 862
693 return unless $self->{h}; 863 return unless $self->{h};
694 864
695 my $children = $self->{children}; 865 my $children = $self->{children};
696 866
698 868
699 my $req_h = List::Util::sum @h; 869 my $req_h = List::Util::sum @h;
700 870
701 if ($req_h > $h) { 871 if ($req_h > $h) {
702 # ah well, not enough space 872 # ah well, not enough space
703 $_ = $h[$_] * $h / $req_h for @h; 873 $_ *= $h / $req_h for @h;
704 } else { 874 } else {
705 my @exp = grep $_->{expand}, @$children; 875 my $exp = List::Util::sum map $_->{expand}, @$children;
706 @exp = @$children unless @exp; 876 $exp ||= 1;
707 877
708 my %exp = map +($_ => 1), @exp;
709
710 for (0 .. $#$children) { 878 for (0 .. $#$children) {
711 my $child = $children->[$_]; 879 my $child = $children->[$_];
712 880
713 my $alloc_h = $h[$_]; 881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
714 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
715 $h[$_] = $alloc_h;
716 } 882 }
717 } 883 }
718 884
719 my $y = 0; 885 my $y = 0;
720 for (0 .. $#$children) { 886 for (0 .. $#$children) {
721 my $child = $children->[$_]; 887 my $child = $children->[$_];
722 my $h = $h[$_]; 888 my $h = $h[$_];
723 $child->move (0, $y);
724 $child->size_allocate ($w, $h); 889 $child->size_allocate (0, $y, $w, $h);
725 890
726 $y += $h; 891 $y += $h;
727 } 892 }
728} 893}
729 894
730############################################################################# 895#############################################################################
731 896
732package CFClient::Widget::Label; 897package CFClient::UI::Label;
733 898
734our @ISA = CFClient::Widget::; 899our @ISA = CFClient::UI::Base::;
735 900
736use SDL::OpenGL; 901use SDL::OpenGL;
737 902
738sub new { 903sub new {
739 my ($class, %arg) = @_; 904 my ($class, %arg) = @_;
740 905
741 my $self = $class->SUPER::new ( 906 my $self = $class->SUPER::new (
742 fg => [1, 1, 1], 907 fg => [1, 1, 1],
743 height => $::FONTSIZE, 908 height => $::FONTSIZE,
744 text => "", 909 text => "",
910 align => -1,
911 padding => 2,
745 layout => new CFClient::Layout, 912 layout => new CFClient::Layout,
746 %arg 913 %arg
747 ); 914 );
748 915
749 $self->set_text ($self->{text}); 916 $self->set_text ($self->{text});
750 917
780sub size_request { 947sub size_request {
781 my ($self) = @_; 948 my ($self) = @_;
782 949
783 $self->{layout}->set_width; 950 $self->{layout}->set_width;
784 $self->{layout}->set_height ($self->{height}); 951 $self->{layout}->set_height ($self->{height});
785 $self->{layout}->size 952 my ($w, $h) = $self->{layout}->size;
786# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 953
787# ( 954 (
788# $self->{texture}{width}, 955 $w + $self->{padding} * 2,
789# $self->{texture}{height}, 956 $h + $self->{padding} * 2,
790# ) 957 )
791# } else {
792# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
793#
794# ($w, $h)
795# }
796} 958}
797 959
798sub size_allocate { 960sub size_allocate {
799 my ($self, $w, $h) = @_; 961 my ($self, $x, $y, $w, $h) = @_;
800 962
801 return unless $self->{w} != $w || $self->{h} != $h; 963 $self->_size_allocate ($x, $y, $w, $h) or return;
802 964
803 $self->SUPER::size_allocate ($w, $h);
804 delete $self->{texture}; 965 delete $self->{texture};
805} 966}
806 967
807sub update { 968sub update {
808 my ($self) = @_; 969 my ($self) = @_;
814sub _draw { 975sub _draw {
815 my ($self) = @_; 976 my ($self) = @_;
816 977
817 my $tex = $self->{texture} ||= do { 978 my $tex = $self->{texture} ||= do {
818 $self->{layout}->set_width ($self->{w}); 979 $self->{layout}->set_width ($self->{w});
980 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{height});
819 new_from_layout CFClient::Texture $self->{layout}; 981 new_from_layout CFClient::Texture $self->{layout}
820 }; 982 };
821 983
822 glEnable GL_BLEND; 984 glEnable GL_BLEND;
985 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
823 glEnable GL_TEXTURE_2D; 986 glEnable GL_TEXTURE_2D;
824 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
825 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 987 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
826 988
827 glColor @{$self->{fg}}; 989 glColor @{$self->{fg}};
828 990
829 $tex->draw_quad (0, 0); 991 my $x =
992 $self->{align} < 0 ? $self->{padding}
993 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
994 : ($self->{w} - $tex->{w}) * 0.5;
830 995
996 $tex->draw_quad ($x, $self->{padding});
997
998 glDisable GL_TEXTURE_2D;
831 glDisable GL_BLEND; 999 glDisable GL_BLEND;
832 glDisable GL_TEXTURE_2D;
833} 1000}
834 1001
835############################################################################# 1002#############################################################################
836 1003
837package CFClient::Widget::Entry; 1004package CFClient::UI::Entry;
838 1005
839our @ISA = CFClient::Widget::Label::; 1006our @ISA = CFClient::UI::Label::;
840 1007
841use SDL; 1008use SDL;
842use SDL::OpenGL; 1009use SDL::OpenGL;
843 1010
844sub new { 1011sub new {
845 my $class = shift; 1012 my $class = shift;
846 1013
847 $class->SUPER::new ( 1014 $class->SUPER::new (
848 fg => [1, 1, 1], 1015 fg => [1, 1, 1],
849 bg => [0, 0, 0, 0.4], 1016 bg => [0, 0, 0, 0.2],
850 active_bg => [1, 1, 1], 1017 active_bg => [1, 1, 1],
851 active_fg => [0, 0, 0], 1018 active_fg => [0, 0, 0],
852 @_ 1019 @_
853 ) 1020 )
854} 1021}
858 1025
859 $self->{last_activity} = $::NOW; 1026 $self->{last_activity} = $::NOW;
860 1027
861 $self->{text} = $text; 1028 $self->{text} = $text;
862 $self->{layout}->set_width ($self->{w}); 1029 $self->{layout}->set_width ($self->{w});
1030
1031 $text =~ s/./*/g if $self->{hidden};
1032
863 $self->{layout}->set_markup ($self->escape_text ($text)); 1033 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
864 1034
865 $text = substr $text, 0, $self->{cursor}; 1035 $text = substr $text, 0, $self->{cursor};
866 utf8::encode $text; 1036 utf8::encode $text;
867 1037
868 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1038 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
875 1045
876 ($w + 1, $h) # add 1 for cursor 1046 ($w + 1, $h) # add 1 for cursor
877} 1047}
878 1048
879sub size_allocate { 1049sub size_allocate {
880 my ($self, $w, $h) = @_; 1050 my ($self, $x, $y, $w, $h) = @_;
881 1051
882 return unless $self->{w} != $w || $self->{h} != $h;
883
884 $self->SUPER::size_allocate ($w, $h); 1052 $self->SUPER::size_allocate ($x, $y, $w, $h);
885 1053
886 $self->_set_text ($self->{text}); 1054 $self->_set_text ($self->{text});
887} 1055}
888 1056
889sub set_text { 1057sub set_text {
910 substr $text, $self->{cursor}, 1, ""; 1078 substr $text, $self->{cursor}, 1, "";
911 } elsif ($sym == SDLK_LEFT) { 1079 } elsif ($sym == SDLK_LEFT) {
912 --$self->{cursor} if $self->{cursor}; 1080 --$self->{cursor} if $self->{cursor};
913 } elsif ($sym == SDLK_RIGHT) { 1081 } elsif ($sym == SDLK_RIGHT) {
914 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1082 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1083 } elsif ($sym == SDLK_HOME) {
1084 $self->{cursor} = 0;
1085 } elsif ($sym == SDLK_END) {
1086 $self->{cursor} = length $text;
915 } elsif ($uni) { 1087 } elsif ($uni) {
916 substr $text, $self->{cursor}++, 0, chr $uni; 1088 substr $text, $self->{cursor}++, 0, chr $uni;
917 } 1089 }
918 1090
919 $self->_set_text ($text); 1091 $self->_set_text ($text);
934 $self->SUPER::button_down ($ev, $x, $y); 1106 $self->SUPER::button_down ($ev, $x, $y);
935 1107
936 my $idx = $self->{layout}->xy_to_index ($x, $y); 1108 my $idx = $self->{layout}->xy_to_index ($x, $y);
937 1109
938 # byte-index to char-index 1110 # byte-index to char-index
939 my $text = $self->{layout}; 1111 my $text = $self->{text};
940 utf8::encode $text; 1112 utf8::encode $text;
941 $self->{cursor} = length substr $text, 0, $idx; 1113 $self->{cursor} = length substr $text, 0, $idx;
942 1114
943 $self->_set_text ($self->{text}); 1115 $self->_set_text ($self->{text});
944 $self->update; 1116 $self->update;
959 $self->{fg} = $self->{active_fg}; 1131 $self->{fg} = $self->{active_fg};
960 } else { 1132 } else {
961 glColor @{$self->{bg}}; 1133 glColor @{$self->{bg}};
962 } 1134 }
963 1135
1136 glEnable GL_BLEND;
1137 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
964 glBegin GL_QUADS; 1138 glBegin GL_QUADS;
965 glVertex 0 , 0; 1139 glVertex 0 , 0;
966 glVertex 0 , $self->{h}; 1140 glVertex 0 , $self->{h};
967 glVertex $self->{w}, $self->{h}; 1141 glVertex $self->{w}, $self->{h};
968 glVertex $self->{w}, 0; 1142 glVertex $self->{w}, 0;
969 glEnd; 1143 glEnd;
1144 glDisable GL_BLEND;
970 1145
971 $self->SUPER::_draw; 1146 $self->SUPER::_draw;
972 1147
973 #TODO: force update every cursor change :( 1148 #TODO: force update every cursor change :(
974 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1149 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
980 } 1155 }
981} 1156}
982 1157
983############################################################################# 1158#############################################################################
984 1159
985package CFClient::Widget::Slider; 1160package CFClient::UI::Slider;
986 1161
987use strict; 1162use strict;
988 1163
989use SDL::OpenGL; 1164use SDL::OpenGL;
990use SDL::OpenGL::Constants;
991 1165
992our @ISA = CFClient::Widget::DrawBG::; 1166our @ISA = CFClient::UI::DrawBG::;
993
994sub size_request {
995 my ($self) = @_;
996
997 my $w = 20;
998 my $h = 10;
999
1000 $self->{vertical} ? ($h, $w) : ($w, $h)
1001}
1002 1167
1003sub new { 1168sub new {
1004 my $class = shift; 1169 my $class = shift;
1005 1170
1006 # range [value, low, high, page] 1171 # range [value, low, high, page]
1007 1172
1008 $class->SUPER::new ( 1173 my $self = $class->SUPER::new (
1009 fg => [1, 1, 1], 1174 fg => [1, 1, 1],
1010 active_fg => [0, 0, 0], 1175 active_fg => [0, 0, 0],
1011 range => [0, 0, 100, 10], 1176 range => [0, 0, 100, 10],
1177 req_w => 40,
1178 req_h => 10,
1012 vertical => 0, 1179 vertical => 0,
1013 @_ 1180 @_
1014 ) 1181 );
1182
1183 $self
1184}
1185
1186sub size_request {
1187 my ($self) = @_;
1188
1189 my $w = $self->{req_w};
1190 my $h = $self->{req_h};
1191
1192 $self->{vertical} ? ($h, $w) : ($w, $h)
1015} 1193}
1016 1194
1017sub button_down { 1195sub button_down {
1018 my ($self, $ev, $x, $y) = @_; 1196 my ($self, $ev, $x, $y) = @_;
1019 1197
1025 my ($self, $ev, $x, $y) = @_; 1203 my ($self, $ev, $x, $y) = @_;
1026 1204
1027 if ($GRAB == $self) { 1205 if ($GRAB == $self) {
1028 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1206 my ($value, $lo, $hi, $page) = @{$self->{range}};
1029 1207
1208 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1209
1030 $x = $x * ($hi - $lo) / $self->{w} + $lo; 1210 $x = $x * ($hi - $lo) / $w + $lo;
1031 $x = $lo if $x < $lo; 1211 $x = $lo if $x < $lo;
1032 $x = $hi - $page if $x > $hi - $page; 1212 $x = $hi - $page if $x > $hi - $page;
1033 $self->{range}[0] = $x; 1213 $self->{range}[0] = $x;
1034 1214
1035 $self->{changed}($x) if $self->{changed}; 1215 $self->emit (changed => $x);
1036 $self->update; 1216 $self->update;
1037 } 1217 }
1038} 1218}
1039 1219
1040sub _draw { 1220sub _draw {
1041 my ($self) = @_; 1221 my ($self) = @_;
1042 1222
1043 $self->SUPER::_draw (); 1223 $self->SUPER::_draw ();
1044
1045 glPushMatrix;
1046 1224
1047 my ($w, $h) = @$self{qw(w h)}; 1225 my ($w, $h) = @$self{qw(w h)};
1048 1226
1049 if ($self->{vertical}) { 1227 if ($self->{vertical}) {
1050 # draw a vertical slider like a rotated horizontal slider 1228 # draw a vertical slider like a rotated horizontal slider
1051 1229
1052 glTranslate 0, $self->{w};
1053 glRotate 90, 0, 0, 1; 1230 glRotate 90, 0, 0, 1;
1231 glTranslate 0, -$self->{w}, 0;
1054 1232
1055 ($w, $h) = ($h, $w); 1233 ($w, $h) = ($h, $w);
1056 } 1234 }
1057 1235
1058 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1236 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1090 glVertex $knob_a + 1, $h - 1; 1268 glVertex $knob_a + 1, $h - 1;
1091 glVertex $knob_b - 1, $h - 1; 1269 glVertex $knob_b - 1, $h - 1;
1092 glVertex $knob_b - 1, 1; 1270 glVertex $knob_b - 1, 1;
1093 } 1271 }
1094 glEnd; 1272 glEnd;
1095
1096 glPopMatrix;
1097} 1273}
1098 1274
1099############################################################################# 1275#############################################################################
1100 1276
1101package CFClient::Widget::MapWidget; 1277package CFClient::UI::MapWidget;
1102 1278
1103use strict; 1279use strict;
1104 1280
1105use List::Util qw(min max); 1281use List::Util qw(min max);
1106 1282
1107use SDL; 1283use SDL;
1108use SDL::OpenGL; 1284use SDL::OpenGL;
1109use SDL::OpenGL::Constants;
1110 1285
1111our @ISA = CFClient::Widget::; 1286our @ISA = CFClient::UI::Base::;
1112 1287
1113sub new { 1288sub new {
1114 my $class = shift; 1289 my $class = shift;
1115 1290
1116 $class->SUPER::new ( 1291 $class->SUPER::new (
1127sub key_up { 1302sub key_up {
1128} 1303}
1129 1304
1130sub size_request { 1305sub size_request {
1131 ( 1306 (
1132 1 + int $::WIDTH / 32, 1307 1 + 32 * int $::WIDTH / 32,
1133 1 + int $::HEIGHT / 32, 1308 1 + 32 * int $::HEIGHT / 32,
1134 ) 1309 )
1135} 1310}
1136 1311
1137sub update { 1312sub update {
1138 my ($self) = @_; 1313 my ($self) = @_;
1139 1314
1140 $self->{need_update} = 1; 1315 $self->{need_update} = 1;
1316 $self->SUPER::update;
1141} 1317}
1142 1318
1143sub _draw { 1319sub draw {
1144 my ($self) = @_; 1320 my ($self) = @_;
1145 1321
1146 if (delete $self->{need_update}) { 1322 if (delete $self->{need_update}) {
1147 glNewList $self->{list}, GL_COMPILE; 1323 glNewList $self->{list}, GL_COMPILE;
1148 1324
1168 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1344 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1169 } 1345 }
1170 1346
1171 glEnable GL_TEXTURE_2D; 1347 glEnable GL_TEXTURE_2D;
1172 glEnable GL_BLEND; 1348 glEnable GL_BLEND;
1349 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1173 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1174 1351
1175 my $sw4 = ($sw + 3) & ~3; 1352 my $sw4 = ($sw + 3) & ~3;
1176 my $darkness = "\x00" x ($sw4 * $sh); 1353 my $darkness = "\x00" x ($sw4 * $sh);
1177 1354
1190 } 1367 }
1191 1368
1192 for my $num (grep $_, @$cell[1,2,3]) { 1369 for my $num (grep $_, @$cell[1,2,3]) {
1193 my $tex = $::CONN->{face}[$num]{texture} || next; 1370 my $tex = $::CONN->{face}[$num]{texture} || next;
1194 1371
1195 my $w = $tex->{width}; 1372 my ($w, $h) = @$tex{qw(w h)};
1196 my $h = $tex->{height};
1197 1373
1198 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); 1374 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1199 } 1375 }
1200 } 1376 }
1201 } 1377 }
1208# 1384#
1209# $lighting = $pb->get_pixels; 1385# $lighting = $pb->get_pixels;
1210# $lighting =~ s/(.)../$1/gs; 1386# $lighting =~ s/(.)../$1/gs;
1211# } 1387# }
1212 1388
1213 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1214 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1215 1390
1216 $darkness = new CFClient::Texture 1391 $darkness = new CFClient::Texture
1217 width => $sw4, 1392 w => $sw4,
1218 height => $sh, 1393 h => $sh,
1219 data => $darkness, 1394 data => $darkness,
1220 internalformat => GL_ALPHA, 1395 internalformat => GL_ALPHA,
1221 format => GL_ALPHA; 1396 format => GL_ALPHA;
1222 1397
1223 glColor 0.45, 0.45, 0.45, 1; 1398 glColor 0.45, 0.45, 0.45, 1;
1283 } 1458 }
1284} 1459}
1285 1460
1286############################################################################# 1461#############################################################################
1287 1462
1288package CFClient::Widget::Animator; 1463package CFClient::UI::Animator;
1289 1464
1290use SDL::OpenGL; 1465use SDL::OpenGL;
1291 1466
1292our @ISA = CFClient::Widget::Bin::; 1467our @ISA = CFClient::UI::Bin::;
1293 1468
1294sub moveto { 1469sub moveto {
1295 my ($self, $x, $y) = @_; 1470 my ($self, $x, $y) = @_;
1296 1471
1297 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1472 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1325 glPopMatrix; 1500 glPopMatrix;
1326} 1501}
1327 1502
1328############################################################################# 1503#############################################################################
1329 1504
1330package CFClient::Widget::Toplevel; 1505package CFClient::UI::Toplevel;
1331 1506
1332our @ISA = CFClient::Widget::Container::; 1507our @ISA = CFClient::UI::Container::;
1333 1508
1334sub size_request { 1509sub size_request {
1335 ($::WIDTH, $::HEIGHT) 1510 ($::WIDTH, $::HEIGHT)
1336} 1511}
1337 1512
1338sub size_allocate { 1513sub size_allocate {
1339 my ($self, $w, $h) = @_; 1514 my ($self, $x, $y, $w, $h) = @_;
1340 1515
1341 $self->SUPER::size_allocate ($w, $h); 1516 $self->_size_allocate ($x, $y, $w, $h);
1342 1517
1343 $_->size_allocate ($_->size_request) 1518 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1344 for @{$self->{children}}; 1519 for @{$self->{children}};
1345} 1520}
1346 1521
1347sub translate { 1522sub translate {
1348 my ($self, $x, $y) = @_; 1523 my ($self, $x, $y) = @_;
1351} 1526}
1352 1527
1353sub update { 1528sub update {
1354 my ($self) = @_; 1529 my ($self) = @_;
1355 1530
1356 $self->size_allocate ($self->size_request); 1531 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1357 ::refresh (); 1532 ::refresh ();
1358} 1533}
1359 1534
1360sub add { 1535sub add {
1361 my ($self, $widget) = @_; 1536 my ($self, $widget) = @_;
1362 1537
1363 $self->SUPER::add ($widget); 1538 $self->SUPER::add ($widget);
1364 1539
1365 $widget->size_allocate ($widget->size_request); 1540 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1366} 1541}
1367 1542
1368sub draw { 1543sub draw {
1369 my ($self) = @_; 1544 my ($self) = @_;
1370 1545
1371 $self->_draw; 1546 $self->_draw;
1372} 1547}
1373 1548
1374############################################################################# 1549#############################################################################
1375 1550
1376package CFClient::Widget; 1551package CFClient::UI;
1377 1552
1378$TOPLEVEL = new CFClient::Widget::Toplevel; 1553$TOPLEVEL = new CFClient::UI::Toplevel;
1379 1554
13801 15551
1381 1556

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines