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.16 by root, Fri Apr 7 23:16:29 2006 UTC vs.
Revision 1.75 by root, Tue Apr 11 20:44:49 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4
5use Scalar::Util ();
6use List::Util ();
7
8use CFClient;
9
10our ($FOCUS, $HOVER, $GRAB); # various widgets
11
12our $TOPLEVEL;
13our $BUTTON_STATE;
14
15# class methods for events
16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS;
18}
19
20sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS;
22}
23
24sub feed_sdl_button_down_event {
25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
27
28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y);
30
31 $GRAB = $widget;
32 $GRAB->update if $GRAB;
33 }
34
35 $BUTTON_STATE |= 1 << ($ev->button - 1);
36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
38}
39
40sub feed_sdl_button_up_event {
41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49
50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71}
72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
78
4use SDL::OpenGL; 79use SDL::OpenGL;
5use SDL::OpenGL::Constants;
6
7our $FOCUS; # the widget with current focus
8our @ACTIVE_WIDGETS;
9
10# class methods for events
11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
14sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS }
15 80
16sub new { 81sub new {
17 my $class = shift; 82 my $class = shift;
18 83
19 bless { @_ }, $class 84 bless {
85 x => 0,
86 y => 0,
87 z => 0,
88 w => -1,
89 h => -1,
90 @_
91 }, $class
20} 92}
21 93
22sub activate { 94sub move {
23 push @ACTIVE_WIDGETS, $_[0]; 95 my ($self, $x, $y, $z) = @_;
96 $self->{x} = $x;
97 $self->{y} = $y;
98 $self->{z} = $z if defined $z;
24} 99}
25 100
26sub deactivate { 101sub needs_redraw {
27 @ACTIVE_WIDGETS = 102 0
28 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] }
30 @ACTIVE_WIDGETS;
31} 103}
32 104
33sub size_request { 105sub size_request {
106 require Carp;
34 die "size_request is abtract"; 107 Carp::confess "size_request is abtract";
108}
109
110sub _size_allocate {
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;
117
118 $self->{w} = $w;
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);
128}
129
130# translate global koordinates to local coordinate system
131sub translate {
132 my ($self, $x, $y) = @_;
133
134 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
35} 135}
36 136
37sub focus_in { 137sub focus_in {
38 my ($widget) = @_; 138 my ($self) = @_;
39 $FOCUS = $widget; 139
140 return if $FOCUS == $self;
141
142 my $focus = $FOCUS; $FOCUS = $self;
143 $focus->update if $focus;
144 $FOCUS->update;
40} 145}
41 146
42sub focus_out { 147sub focus_out {
43 my ($widget) = @_; 148 my ($self) = @_;
44}
45 149
150 return unless $FOCUS == $self;
151
152 my $focus = $FOCUS; undef $FOCUS;
153 $focus->update if $focus; #?
154}
155
156sub mouse_motion { }
157sub button_up { }
46sub key_down { 158sub key_down { }
47 my ($widget, $sdlev) = @_;
48}
49
50sub key_up { 159sub key_up { }
51 my ($widget, $sdlev) = @_;
52}
53 160
54sub button_down { 161sub button_down {
55 my ($widget, $sdlev) = @_; 162 my ($self, $ev, $x, $y) = @_;
56}
57 163
58sub button_up { 164 $self->focus_in;
59 my ($widget, $sdlev) = @_;
60} 165}
61 166
62sub w { $_[0]->{w} } 167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
63sub h { $_[0]->{h} } 168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
64sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
65sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
66sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
67 172
68sub draw { 173sub draw {
69 my ($self) = @_; 174 my ($self) = @_;
175
176 return unless $self->{h} && $self->{w};
70 177
71 glPushMatrix; 178 glPushMatrix;
72 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
73 $self->_draw; 180 $self->_draw;
74 glPopMatrix; 181 glPopMatrix;
182
183 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)};
185
186 glColor 1, 1, 1, 0.1;
187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS;
190 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y;
192 glVertex $x + $self->{w}, $y + $self->{h};
193 glVertex $x , $y + $self->{h};
194 glEnd;
195 glDisable GL_BLEND;
196 }
75} 197}
76 198
77sub _draw { 199sub _draw {
78 my ($widget) = @_; 200 my ($self) = @_;
201
202 warn "no draw defined for $self\n";
79} 203}
80 204
81sub bbox { 205sub bbox {
82 my ($widget) = @_; 206 my ($self) = @_;
207 my ($w, $h) = $self->size_request;
208 (
209 $self->{x},
210 $self->{y},
211 $self->{x} = $w,
212 $self->{y} = $h
213 )
83} 214}
84 215
85package Crossfire::Client::Widget::Container; 216sub find_widget {
217 my ($self, $x, $y) = @_;
86 218
87our @ISA = Crossfire::Client::Widget::; 219 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
88 222
223 ()
224}
225
226sub del_parent { $_[0]->{parent} = undef }
227
228sub set_parent {
229 my ($self, $par) = @_;
230
231 $self->{parent} = $par;
232 Scalar::Util::weaken $self->{parent};
233}
234
235sub get_parent {
236 $_[0]->{parent}
237}
238
239sub update {
240 my ($self) = @_;
241
242 $self->{parent}->update
243 if $self->{parent};
244}
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
259sub DESTROY {
260 my ($self) = @_;
261
262 #$self->deactivate;
263}
264
265#############################################################################
266
267package CFClient::UI::DrawBG;
268
269our @ISA = CFClient::UI::Base::;
270
271use strict;
89use SDL::OpenGL; 272use SDL::OpenGL;
90 273
91sub add { $_[0]->{child} = $_[1] } 274sub new {
92sub get { $_[0]->{child} } 275 my $class = shift;
93 276
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 277 # range [value, low, high, page]
95 278
96sub _draw { die "Containers can't be drawn!" } 279 $class->SUPER::new (
280 bg => [0, 0, 0, 0.4],
281 active_bg => [1, 1, 1],
282 @_
283 )
284}
97 285
98package Crossfire::Client::Widget::Frame; 286sub _draw {
287 my ($self) = @_;
99 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
314package CFClient::UI::Container;
315
316our @ISA = CFClient::UI::Base::;
317
318sub new {
319 my ($class, %arg) = @_;
320
321 my $children = delete $arg{children} || [];
322
323 my $self = $class->SUPER::new (children => [], %arg);
324 $self->add ($_) for @$children;
325
326 $self
327}
328
329sub add {
330 my ($self, $chld, $expand) = @_;
331
332 $chld->{expand} = $expand;
333 $chld->set_parent ($self);
334
335 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld
338 ];
339
340 $self->{w} = $self->{h} = -1;
341 $self->update;
342}
343
344sub remove {
345 my ($self, $widget) = @_;
346
347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
348
349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
350}
351
352sub find_widget {
353 my ($self, $x, $y) = @_;
354
355 $x -= $self->{x};
356 $y -= $self->{y};
357
358 my $res;
359
360 for (reverse @{ $self->{children} }) {
361 $res = $_->find_widget ($x, $y)
362 and return $res;
363 }
364
365 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
366}
367
368sub _draw {
369 my ($self) = @_;
370
371 $_->draw for @{$self->{children}};
372}
373
374#############################################################################
375
376package CFClient::UI::Bin;
377
100our @ISA = Crossfire::Client::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}
404
405sub child { $_[0]->{children}[0] }
406
407sub size_request {
408 $_[0]{children}[0]->size_request
409}
410
411sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_;
413
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h);
417}
418
419#############################################################################
420
421package CFClient::UI::Window;
422
423our @ISA = CFClient::UI::Bin::;
101 424
102use SDL::OpenGL; 425use SDL::OpenGL;
103 426
427sub new {
428 my ($class, %arg) = @_;
429
430 my $self = $class->SUPER::new (%arg);
431}
432
433sub update {
434 my ($self) = @_;
435
436 # we want to do this delayed...
437 $self->render_chld;
438 $self->SUPER::update;
439}
440
441sub render_chld {
442 my ($self) = @_;
443
444 $self->{texture} =
445 CFClient::Texture->new_from_opengl (
446 $self->{w}, $self->{h}, sub { $self->child->draw }
447 );
448}
449
450sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_;
452
453 $self->_size_allocate ($x, $y, $w, $h) or return;
454
455 $self->child->size_allocate (0, 0, $w, $h);
456
457 $self->render_chld;
458}
459
460sub _draw {
461 my ($self) = @_;
462
463 my ($w, $h) = ($self->w, $self->h);
464
465 my $tex = $self->{texture}
466 or return;
467
468 glEnable GL_BLEND;
469 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
470 glEnable GL_TEXTURE_2D;
471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
472
473 $tex->draw_quad (0, 0, $w, $h);
474
475 glDisable GL_BLEND;
476 glDisable GL_TEXTURE_2D;
477}
478
479#############################################################################
480
481package CFClient::UI::Frame;
482
483our @ISA = CFClient::UI::Bin::;
484
485use SDL::OpenGL;
486
104sub size_request { 487sub size_request {
105 my ($self) = @_; 488 my ($self) = @_;
106 my $chld = $self->get 489 my $chld = $self->child
107 or return (0, 0); 490 or return (0, 0);
491
492 $chld->move (2, 2);
493
108 map { $_ + 4 } $chld->size_request; 494 map { $_ + 4 } $chld->size_request;
109} 495}
110 496
497sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_;
499
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
503}
504
111sub _draw { 505sub _draw {
112 my ($self) = @_; 506 my ($self) = @_;
113 507
114 my $chld = $self->get; 508 my $chld = $self->child;
115 509
116 my ($w, $h) = $chld->size_request; 510 my ($w, $h) = $chld->size_request;
117 511
118 glColor 1, 0, 0;
119 glBegin GL_QUADS; 512 glBegin GL_QUADS;
120 glTexCoord 0, 0; glVertex 0 , 0; 513 glColor 0, 0, 0;
514 glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4; 515 glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 516 glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0; 517 glVertex $w + 4 , 0;
124 glEnd; 518 glEnd;
125 519
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw; 520 $chld->draw;
129 glPopMatrix;
130} 521}
131 522
132package Crossfire::Client::Widget::Table; 523#############################################################################
133 524
134our @ISA = Crossfire::Client::Widget::Container::; 525package CFClient::UI::FancyFrame;
526
527our @ISA = CFClient::UI::Bin::;
528
529use SDL::OpenGL;
530
531my @tex =
532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
533 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
534
535sub size_request {
536 my ($self) = @_;
537
538 my ($w, $h) = $self->SUPER::size_request;
539
540 $h += $tex[1]->{h};
541 $h += $tex[4]->{h};
542 $w += $tex[2]->{w};
543 $w += $tex[3]->{w};
544
545 ($w, $h)
546}
547
548sub size_allocate {
549 my ($self, $x, $y, $w, $h) = @_;
550
551 $self->_size_allocate ($x, $y, $w, $h) or return;
552
553 $h -= $tex[1]->{h};
554 $h -= $tex[4]->{h};
555 $w -= $tex[2]->{w};
556 $w -= $tex[3]->{w};
557
558 $h = $h < 0 ? 0 : $h;
559 $w = $w < 0 ? 0 : $w;
560
561 my $child = $self->child;
562
563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
564}
565
566sub _draw {
567 my ($self) = @_;
568
569 my ($w, $h) = ($self->{w}, $self->{h});
570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
571
572 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
574 glEnable GL_TEXTURE_2D;
575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
576
577 my $top = $tex[1];
578 $top->draw_quad (0, 0, $w, $top->{h});
579
580 my $left = $tex[3];
581 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
582
583 my $right = $tex[2];
584 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
585
586 my $bottom = $tex[4];
587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
588
589 my $bg = $tex[0];
590 glBindTexture GL_TEXTURE_2D, $bg->{name};
591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
594
595 my $rep_x = $cw / $bg->{w};
596 my $rep_y = $ch / $bg->{h};
597
598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
599
600 glDisable GL_BLEND;
601 glDisable GL_TEXTURE_2D;
602
603 $self->child->draw;
604
605}
606
607#############################################################################
608
609package CFClient::UI::Table;
610
611our @ISA = CFClient::UI::Base::;
612
613use List::Util qw(max sum);
135 614
136use SDL::OpenGL; 615use SDL::OpenGL;
137 616
138sub add { 617sub add {
139 my ($self, $x, $y, $chld) = @_; 618 my ($self, $x, $y, $chld) = @_;
619
140 $self->{childs}[$y][$x] = $chld; 620 $self->{children}[$y][$x] = $chld;
141} 621 $chld->set_parent ($self);
142 622
143sub max_row_height { 623 $self->{w} = $self->{h} = -1;
624 $self->update;
625}
626
627sub get_wh {
144 my ($self, $row) = @_; 628 my ($self) = @_;
145 629
146 my $hs = 0; 630 my (@w, @h);
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 631
148 my $c = $self->{childs}->[$row]->[$xi]; 632 for my $y (0 .. $#{$self->{children}}) {
633 my $row = $self->{children}[$y]
634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
149 my ($w, $h) = $c->size_request if $c; 639 my ($w, $h) = $widget->size_request;
150 if ($hs < $h) { $hs = $h }
151 }
152 return $hs;
153}
154 640
155sub max_col_width { 641 $w[$x] = max $w[$x], $w;
156 my ($self, $col) = @_; 642 $h[$y] = max $h[$y], $h;
157 643 }
158 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col];
161 my ($w, $h) = $c->size_request if $c;
162 if ($ws < $w) { $ws = $w }
163 } 644 }
164 return $ws; 645
646 (\@w, \@h)
165} 647}
166 648
167sub size_request { 649sub size_request {
168 my ($self) = @_; 650 my ($self) = @_;
169 651
170 my ($hs, $ws) = (0, 0); 652 my ($ws, $hs) = $self->get_wh;
171 653
172 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
173 $hs += $self->max_row_height ($yi);
174 } 654 (
655 (List::Util::sum @$ws),
656 (List::Util::sum @$hs),
657 )
658}
175 659
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 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
177 my $wm = 0; 680 my $x = 0;
178 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 681 my $row_h = $hs->[$r];
179 $wm += $self->max_col_width ($xi) 682
683 for my $c (0 .. $#$row) {
684 my $widget = $row->[$c]
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;
180 } 692 }
181 if ($ws < $wm) { $ws = $wm }
182 }
183 693
184 return ($ws, $hs); 694 $y += $row_h;
695 }
696
185} 697}
186 698
187sub _draw { 699sub _draw {
188 my ($self) = @_; 700 my ($self) = @_;
189 701
702 for (grep $_, @{$self->{children}}) {
703 $_->draw for grep $_, @$_;
704 }
705}
706
707#############################################################################
708
709package CFClient::UI::VBox;
710
711our @ISA = CFClient::UI::Container::;
712
713use SDL::OpenGL;
714
715sub size_request {
716 my ($self) = @_;
717
718 my @alloc = map [$_->size_request], @{$self->{children}};
719
720 (
721 (List::Util::max map $_->[0], @alloc),
722 (List::Util::sum map $_->[1], @alloc),
723 )
724}
725
726sub size_allocate {
727 my ($self, $x, $y, $w, $h) = @_;
728
729 $self->_size_allocate ($x, $y, $w, $h) or return;
730
731 return unless $self->{h};
732
733 my $children = $self->{children};
734
735 my @h = map +($_->size_request)[1], @$children;
736
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;
754 }
755 }
756
190 my $y = 0; 757 my $y = 0;
191 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 758 for (0 .. $#$children) {
759 my $child = $children->[$_];
192 my $x = 0; 760 my $h = $h[$_];
761 $child->size_allocate (0, $y, $w, $h);
193 762
194 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 763 $y += $h;
195
196 glPushMatrix;
197 glTranslate ($x, $y, 0);
198 my $c = $self->{childs}->[$yi]->[$xi];
199 $c->_draw if $c;
200 glPopMatrix;
201
202 $x += $self->max_col_width ($xi);
203 }
204
205 $y += $self->max_row_height ($yi);
206 } 764 }
207} 765}
208 766
209package Crossfire::Client::Widget::VBox; 767#############################################################################
210 768
211our @ISA = Crossfire::Client::Widget::Container::; 769package CFClient::UI::Label;
770
771our @ISA = CFClient::UI::Base::;
212 772
213use SDL::OpenGL; 773use SDL::OpenGL;
214 774
215sub add {
216 my ($self, $chld) = @_;
217 push @{$self->{childs}}, $chld;
218}
219
220sub size_request {
221 my ($self) = @_;
222
223 my ($hs, $ws) = (0, 0);
224 for (@{$self->{childs} || []}) {
225 my ($w, $h) = $_->size_request;
226 $hs += $h;
227 if ($ws < $w) { $ws = $w }
228 }
229
230 return ($ws, $hs);
231}
232
233sub _draw {
234 my ($self) = @_;
235
236 my ($x, $y);
237 for (@{$self->{childs} || []}) {
238 glPushMatrix;
239 glTranslate (0, $y, 0);
240 $_->_draw;
241 glPopMatrix;
242 my ($w, $h) = $_->size_request;
243 $y += $h;
244 }
245}
246
247package Crossfire::Client::Widget::Label;
248
249our @ISA = Crossfire::Client::Widget::;
250
251use SDL::OpenGL;
252
253sub new { 775sub new {
254 my ($class, $x, $y, $z, $ttf, $text) = @_; 776 my ($class, %arg) = @_;
255 777
256 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 778 my $self = $class->SUPER::new (
779 fg => [1, 1, 1],
780 height => $::FONTSIZE,
781 text => "",
782 align => -1,
783 layout => new CFClient::Layout,
784 %arg
785 );
257 786
258 $self->set_text ($text); 787 $self->set_text ($self->{text});
259 788
260 $self 789 $self
790}
791
792sub escape_text {
793 local $_ = $_[1];
794
795 s/&/&amp;/g;
796 s/>/&gt;/g;
797 s/</&lt;/g;
798
799 $_[1]
261} 800}
262 801
263sub set_text { 802sub set_text {
264 my ($self, $text) = @_; 803 my ($self, $text) = @_;
265 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 804
805 $self->{text} = $text;
806 $self->{layout}->set_markup ($text);
807
808 delete $self->{texture};
809 $self->update;
266} 810}
267 811
268sub get_text { 812sub get_text {
269 my ($self, $text) = @_; 813 my ($self, $text) = @_;
814
270 $self->{text} 815 $self->{text}
271} 816}
272 817
273sub size_request { 818sub size_request {
274 my ($self) = @_; 819 my ($self) = @_;
275 820
276 ( 821 $self->{layout}->set_width;
822 $self->{layout}->set_height ($self->{height});
823 $self->{layout}->size
824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
825# (
277 $self->{texture}{width}, 826# $self->{texture}{w},
278 $self->{texture}{height}, 827# $self->{texture}{h},
279 ) 828# )
829# } else {
830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
831#
832# ($w, $h)
833# }
834}
835
836sub size_allocate {
837 my ($self, $x, $y, $w, $h) = @_;
838
839 $self->_size_allocate ($x, $y, $w, $h) or return;
840
841 delete $self->{texture};
842}
843
844sub update {
845 my ($self) = @_;
846
847 delete $self->{texture};
848 $self->SUPER::update;
280} 849}
281 850
282sub _draw { 851sub _draw {
283 my ($self) = @_; 852 my ($self) = @_;
284 853
285 my $tex = $self->{texture}; 854 my $tex = $self->{texture} ||= do {
286 855 $self->{layout}->set_width ($self->{w});
287 $self->{x}--; 856 new_from_layout CFClient::Texture $self->{layout}
857 };
288 858
289 glEnable GL_BLEND; 859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
290 glEnable GL_TEXTURE_2D; 861 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 863
294 glColor 1, 1, 1; 864 glColor @{$self->{fg}};
295 865
296 glBegin GL_QUADS; 866 my $x =
297 glTexCoord 0, 0; glVertex 0 , 0; 867 $self->{align} < 0 ? 0
298 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 868 : $self->{align} > 0 ? $self->{w} - $tex->{w}
299 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 869 : ($self->{w} - $tex->{w}) * 0.5;
300 glTexCoord 1, 0; glVertex $tex->{width}, 0;
301 glEnd;
302 870
871 $tex->draw_quad ($x, 0);
872
873 glDisable GL_TEXTURE_2D;
303 glDisable GL_BLEND; 874 glDisable GL_BLEND;
304 glDisable GL_TEXTURE_2D;
305} 875}
306 876
307package Crossfire::Client::Widget::TextView; 877#############################################################################
308 878
309use strict; 879package CFClient::UI::Entry;
310 880
311our @ISA = qw/Crossfire::Client::Widget/; 881our @ISA = CFClient::UI::Label::;
312
313use SDL::OpenGL;
314use SDL::OpenGL::Constants;
315
316sub add_line {
317 my ($self, $line) = @_;
318 push @{$self->{lines}}, $line;
319}
320
321sub _draw {
322 my ($self) = @_;
323
324}
325
326package Crossfire::Client::Widget::MapWidget;
327
328use strict;
329
330our @ISA = qw/Crossfire::Client::Widget/;
331 882
332use SDL; 883use SDL;
333use SDL::OpenGL; 884use SDL::OpenGL;
334use SDL::OpenGL::Constants; 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
941sub key_down {
942 my ($self, $ev) = @_;
943
944 my $mod = $ev->key_mod;
945 my $sym = $ev->key_sym;
946
947 my $uni = $ev->key_unicode;
948
949 my $text = $self->get_text;
950
951 if ($sym == SDLK_BACKSPACE) {
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};
959 } elsif ($uni) {
960 substr $text, $self->{cursor}++, 0, chr $uni;
961 }
962
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;
973}
974
975sub button_down {
976 my ($self, $ev, $x, $y) = @_;
977
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;
989}
990
991sub mouse_motion {
992 my ($self, $ev, $x, $y) = @_;
993# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
994}
995
996sub _draw {
997 my ($self) = @_;
998
999 local $self->{fg} = $self->{fg};
1000
1001 if ($FOCUS == $self) {
1002 glColor @{$self->{active_bg}};
1003 $self->{fg} = $self->{active_fg};
1004 } else {
1005 glColor @{$self->{bg}};
1006 }
1007
1008 glBegin GL_QUADS;
1009 glVertex 0 , 0;
1010 glVertex 0 , $self->{h};
1011 glVertex $self->{w}, $self->{h};
1012 glVertex $self->{w}, 0;
1013 glEnd;
1014
1015 $self->SUPER::_draw;
1016
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}
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
1142package CFClient::UI::MapWidget;
1143
1144use strict;
1145
1146use List::Util qw(min max);
1147
1148use SDL;
1149use SDL::OpenGL;
1150
1151our @ISA = CFClient::UI::Base::;
1152
1153sub new {
1154 my $class = shift;
1155
1156 $class->SUPER::new (
1157 z => -1,
1158 list => (glGenLists 1),
1159 @_
1160 )
1161}
335 1162
336sub key_down { 1163sub key_down {
337 print "MAPKEYDOWN\n"; 1164 print "MAPKEYDOWN\n";
338} 1165}
339 1166
340sub key_up { 1167sub key_up {
341} 1168}
342 1169
1170sub size_request {
1171 (
1172 1 + int $::WIDTH / 32,
1173 1 + int $::HEIGHT / 32,
1174 )
1175}
1176
1177sub update {
1178 my ($self) = @_;
1179
1180 $self->{need_update} = 1;
1181 $self->SUPER::update;
1182}
1183
343sub _draw { 1184sub _draw {
344 glEnable GL_TEXTURE_2D; 1185 my ($self) = @_;
345 glEnable GL_BLEND;
346 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
347 1186
1187 if (delete $self->{need_update}) {
1188 glNewList $self->{list}, GL_COMPILE;
1189
1190 my $mx = $::CONN->{mapx};
1191 my $my = $::CONN->{mapy};
1192
348 my $map = $::CONN->{map}; 1193 my $map = $::CONN->{map};
349 1194
350 for my $x (0 .. $::CONN->{mapw} - 1) { 1195 my ($xofs, $yofs);
351 for my $y (0 .. $::CONN->{maph} - 1) {
352 1196
353 my $cell = $map->[$x][$y] 1197 my $sw = 1 + int $::WIDTH / 32;
1198 my $sh = 1 + int $::HEIGHT / 32;
1199
1200 if ($::CONN->{mapw} > $sw) {
1201 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1202 } else {
1203 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1204 }
1205
1206 if ($::CONN->{maph} > $sh) {
1207 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1208 } else {
1209 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1210 }
1211
1212 glEnable GL_TEXTURE_2D;
1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1216
1217 my $sw4 = ($sw + 3) & ~3;
1218 my $darkness = "\x00" x ($sw4 * $sh);
1219
1220 for my $x (0 .. $sw - 1) {
1221 my $row = $map->[$x + $xofs];
1222 for my $y (0 .. $sh - 1) {
1223
1224 my $cell = $row->[$y + $yofs]
354 or next; 1225 or next;
355 1226
356 my $darkness = $cell->[3] * (1 / 255); 1227 my $dark = $cell->[0];
357 glColor $darkness, $darkness, $darkness; 1228 if ($dark < 0) {
1229 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1230 } else {
1231 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1232 }
358 1233
359 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 1234 for my $num (grep $_, @$cell[1,2,3]) {
360 my $tex = $::CONN->{face}[$num]{texture} || next; 1235 my $tex = $::CONN->{face}[$num]{texture} || next;
1236
1237 my ($w, $h) = @$tex{qw(w h)};
361 1238
362 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1239 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
363
364 glBegin GL_QUADS;
365 glTexCoord 0, 0; glVertex $x * 32 , $y * 32;
366 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32;
367 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32;
368 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32;
369 glEnd; 1240 }
370 } 1241 }
371 } 1242 }
1243
1244# if (1) { # higher quality darkness
1245# $lighting =~ s/(.)/$1$1$1/gs;
1246# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1247#
1248# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1249#
1250# $lighting = $pb->get_pixels;
1251# $lighting =~ s/(.)../$1/gs;
372 } 1252# }
373 1253
1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1255
1256 $darkness = new CFClient::Texture
1257 w => $sw4,
1258 h => $sh,
1259 data => $darkness,
1260 internalformat => GL_ALPHA,
1261 format => GL_ALPHA;
1262
1263 glColor 0.45, 0.45, 0.45, 1;
1264 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1265
374 glDisable GL_TEXTURE_2D; 1266 glDisable GL_TEXTURE_2D;
375 glDisable GL_BLEND; 1267 glDisable GL_BLEND;
1268
1269 glEndList;
1270 }
1271
1272 glCallList $self->{list};
376} 1273}
377 1274
378my %DIR = ( 1275my %DIR = (
379 SDLK_KP8, [1, "north"], 1276 SDLK_KP8, [1, "north"],
380 SDLK_KP9, [2, "northest"], 1277 SDLK_KP9, [2, "northeast"],
381 SDLK_KP6, [3, "east"], 1278 SDLK_KP6, [3, "east"],
382 SDLK_KP3, [4, "southeast"], 1279 SDLK_KP3, [4, "southeast"],
383 SDLK_KP2, [5, "south"], 1280 SDLK_KP2, [5, "south"],
384 SDLK_KP1, [6, "southwest"], 1281 SDLK_KP1, [6, "southwest"],
385 SDLK_KP4, [7, "west"], 1282 SDLK_KP4, [7, "west"],
386 SDLK_KP7, [8, "northwest"], 1283 SDLK_KP7, [8, "northwest"],
1284
1285 SDLK_UP, [1, "north"],
1286 SDLK_RIGHT, [3, "east"],
1287 SDLK_DOWN, [5, "south"],
1288 SDLK_LEFT, [7, "west"],
387); 1289);
388 1290
389sub key_down { 1291sub key_down {
390 my ($self, $ev) = @_; 1292 my ($self, $ev) = @_;
391 1293
394 1296
395 if ($sym == SDLK_KP5) { 1297 if ($sym == SDLK_KP5) {
396 $::CONN->send ("command stay fire"); 1298 $::CONN->send ("command stay fire");
397 } elsif (exists $DIR{$sym}) { 1299 } elsif (exists $DIR{$sym}) {
398 if ($mod & KMOD_SHIFT) { 1300 if ($mod & KMOD_SHIFT) {
1301 $self->{shft}++;
399 $::CONN->send ("command fire $DIR{$sym}[0]"); 1302 $::CONN->send ("command fire $DIR{$sym}[0]");
400 } elsif ($mod & KMOD_CTRL) { 1303 } elsif ($mod & KMOD_CTRL) {
1304 $self->{ctrl}++;
401 $::CONN->send ("command run $DIR{$sym}[0]"); 1305 $::CONN->send ("command run $DIR{$sym}[0]");
402 } else { 1306 } else {
1307 $::CONN->send ("command $DIR{$sym}[1]");
403 } 1308 }
404 } 1309 }
405} 1310}
406 1311
407sub key_up { 1312sub key_up {
408 my ($self, $ev) = @_; 1313 my ($self, $ev) = @_;
409 1314
410 my $mod = $ev->key_mod; 1315 my $mod = $ev->key_mod;
411 my $sym = $ev->key_sym; 1316 my $sym = $ev->key_sym;
412 1317
413 if (exists $DIR{$sym}) { 1318 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
414 if ($mod & KMOD_SHIFT) {
415 $::CONN->send ("command fire_stop"); 1319 $::CONN->send ("command fire_stop");
416 } else { 1320 }
1321 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
417 $::CONN->send ("command run_stop"); 1322 $::CONN->send ("command run_stop");
1323 }
1324}
1325
1326#############################################################################
1327
1328package CFClient::UI::Animator;
1329
1330use SDL::OpenGL;
1331
1332our @ISA = CFClient::UI::Bin::;
1333
1334sub moveto {
1335 my ($self, $x, $y) = @_;
1336
1337 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1338 $self->{speed} = 0.001;
1339 $self->{time} = 1;
1340
1341 ::animation_start $self;
1342}
1343
1344sub animate {
1345 my ($self, $interval) = @_;
1346
1347 $self->{time} -= $interval * $self->{speed};
1348 if ($self->{time} <= 0) {
1349 $self->{time} = 0;
1350 ::animation_stop $self;
1351 }
1352
1353 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
418 } 1354
419 } 1355 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
1356 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
420} 1357}
421 1358
4221; 1359sub _draw {
1360 my ($self) = @_;
423 1361
1362 glPushMatrix;
1363 glRotate $self->{time} * 1000, 0, 1, 0;
1364 $self->{children}[0]->draw;
1365 glPopMatrix;
1366}
1367
1368#############################################################################
1369
1370package CFClient::UI::Toplevel;
1371
1372our @ISA = CFClient::UI::Container::;
1373
1374sub size_request {
1375 ($::WIDTH, $::HEIGHT)
1376}
1377
1378sub size_allocate {
1379 my ($self, $x, $y, $w, $h) = @_;
1380
1381 $self->_size_allocate ($x, $y, $w, $h);
1382
1383 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1384 for @{$self->{children}};
1385}
1386
1387sub translate {
1388 my ($self, $x, $y) = @_;
1389
1390 ($x, $y)
1391}
1392
1393sub update {
1394 my ($self) = @_;
1395
1396 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1397 ::refresh ();
1398}
1399
1400sub add {
1401 my ($self, $widget) = @_;
1402
1403 $self->SUPER::add ($widget);
1404
1405 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1406}
1407
1408sub draw {
1409 my ($self) = @_;
1410
1411 $self->_draw;
1412}
1413
1414#############################################################################
1415
1416package CFClient::UI;
1417
1418$TOPLEVEL = new CFClient::UI::Toplevel;
1419
14201
1421

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines