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.64 by root, Tue Apr 11 13:38:22 2006 UTC vs.
Revision 1.75 by root, Tue Apr 11 20:44:49 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines