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.40 by root, Sun Apr 9 21:41:11 2006 UTC vs.
Revision 1.72 by root, Tue Apr 11 18:00:45 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines