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.54 by root, Mon Apr 10 11:57:17 2006 UTC vs.
Revision 1.75 by root, Tue Apr 11 20:44:49 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines