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.31 by elmex, Sun Apr 9 00:43:11 2006 UTC vs.
Revision 1.74 by root, Tue Apr 11 19:31:18 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines