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.38 by root, Sun Apr 9 21:34:49 2006 UTC vs.
Revision 1.74 by root, Tue Apr 11 19:31:18 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines