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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines