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.67 by root, Tue Apr 11 14:36:59 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;
37 Carp::confess "size_request is abtract"; 99 Carp::confess "size_request is abtract";
38} 100}
39 101
40sub size_allocate { 102sub size_allocate {
41 my ($self, $w, $h) = @_; 103 my ($self, $w, $h) = @_;
104
42 $self->w ($w); 105 $self->{w} = $w;
43 $self->h ($h); 106 $self->{h} = $h;
107}
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});
44} 114}
45 115
46sub focus_in { 116sub focus_in {
47 my ($widget) = @_; 117 my ($self) = @_;
48 $FOCUS = $widget; 118
119 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus;
121 $FOCUS->update;
49} 122}
50 123
51sub focus_out { 124sub focus_out {
52 my ($widget) = @_; 125 my ($self) = @_;
53}
54 126
55sub key_down { 127 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 128
59sub key_up { 129 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 130 $focus->update if $focus; #?
61} 131}
62 132
133sub mouse_motion { }
134sub button_up { }
63sub button_down { 135sub button_down { }
64 my ($widget, $sdlev) = @_; 136sub key_down { }
65} 137sub key_up { }
66 138
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 144
77sub draw { 145sub draw {
78 my ($self) = @_; 146 my ($self) = @_;
79 147
80 glPushMatrix; 148 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 149 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 150 $self->_draw;
151 if ($self == $HOVER) {
152 glColor 1, 1, 1, 0.4;
153 glEnable GL_BLEND;
154 glBegin GL_QUADS;
155 glVertex 0 , 0;
156 glVertex $self->{w}, 0;
157 glVertex $self->{w}, $self->{h};
158 glVertex 0 , $self->{h};
159 glEnd;
160 glDisable GL_BLEND;
161 }
83 glPopMatrix; 162 glPopMatrix;
84} 163}
85 164
86sub _draw { 165sub _draw {
87 my ($self) = @_; 166 my ($self) = @_;
134 my ($self) = @_; 213 my ($self) = @_;
135 214
136 #$self->deactivate; 215 #$self->deactivate;
137} 216}
138 217
218#############################################################################
219
220package CFClient::Widget::Empty;
221
222our @ISA = CFClient::Widget::;
223
224sub size_request {
225 (0, 0)
226}
227
228sub draw { }
229
230#############################################################################
231
139package Crossfire::Client::Widget::Container; 232package CFClient::Widget::Container;
140 233
141our @ISA = Crossfire::Client::Widget::; 234our @ISA = CFClient::Widget::;
142 235
143sub new { 236sub new {
144 my ($class, @widgets) = @_; 237 my ($class, %arg) = @_;
145 238
239 my $children = delete $arg{children} || [];
240
146 my $self = $class->SUPER::new (children => []); 241 my $self = $class->SUPER::new (children => [], %arg);
147 $self->add ($_) for @widgets; 242 $self->add ($_) for @$children;
148 243
149 $self 244 $self
150} 245}
151 246
152sub add { 247sub add {
153 my ($self, $chld, $expand) = @_; 248 my ($self, $chld, $expand) = @_;
154 249
155 $chld->{expand} = $expand; 250 $chld->{expand} = $expand;
156 $chld->set_parent ($self); 251 $chld->set_parent ($self);
157 252
158 @{$self->{children}} = 253 $self->{children} = [
159 sort { $a->{z} <=> $b->{z} } 254 sort { $a->{z} <=> $b->{z} }
160 @{$self->{children}}, $chld; 255 @{$self->{children}}, $chld
256 ];
161 257
162 $self->size_allocate ($self->{w}, $self->{h}); 258 $self->size_allocate ($self->{w}, $self->{h})
259 if $self->{w}; #TODO: check for "realised state"
163} 260}
164 261
165sub remove { 262sub remove {
166 my ($self, $widget) = @_; 263 my ($self, $widget) = @_;
167 264
171} 268}
172 269
173sub find_widget { 270sub find_widget {
174 my ($self, $x, $y) = @_; 271 my ($self, $x, $y) = @_;
175 272
273 $x -= $self->{x};
274 $y -= $self->{y};
275
176 my $res; 276 my $res;
177 277
178 for (@{ $self->{children} }) { 278 for (reverse @{ $self->{children} }) {
179 $res = $_->find_widget ($x, $y) 279 $res = $_->find_widget ($x, $y)
180 and return $res; 280 and return $res;
181 } 281 }
182 282
183 () 283 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
184}
185
186sub 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} 284}
198 285
199sub _draw { 286sub _draw {
200 my ($self) = @_; 287 my ($self) = @_;
201 288
202 $_->draw for @{$self->{children}}; 289 $_->draw for @{$self->{children}};
203} 290}
204 291
292#############################################################################
293
205package Crossfire::Client::Widget::Bin; 294package CFClient::Widget::Bin;
206 295
207our @ISA = Crossfire::Client::Widget::Container::; 296our @ISA = CFClient::Widget::Container::;
208 297
298sub new {
299 my ($class, %arg) = @_;
300
301 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::;
302
303 $class->SUPER::new (children => [$child], %arg)
304}
305
306sub add {
307 my ($self, $widget) = @_;
308
309 $self->{children} = [];
310
311 $self->SUPER::add ($widget);
312}
313
314sub remove {
315 my ($self, $widget) = @_;
316
317 $self->SUPER::remove ($widget);
318
319 $self->{children} = [new CFClient::Widget::Empty]
320 unless @{$self->{children}};
321}
322
209sub get { $_[0]->{children}[0] } 323sub child { $_[0]->{children}[0] }
210 324
211sub size_request { 325sub size_request {
212 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 326 $_[0]{children}[0]->size_request if $_[0]{children}[0];
213} 327}
214 328
215sub size_allocate { 329sub size_allocate {
216 my ($self, $w, $h) = @_; 330 my ($self, $w, $h) = @_;
331
217 $self->SUPER::size_allocate ($w, $h); 332 $self->SUPER::size_allocate ($w, $h);
218 $self->{children}[0]->size_allocate ($w, $h) 333 $self->{children}[0]->size_allocate ($w, $h)
219 if $self->{children}[0] 334 if $self->{children}[0]
220} 335}
221 336
222package Crossfire::Client::Widget::Toplevel; 337#############################################################################
223 338
339package CFClient::Widget::Window;
340
224our @ISA = Crossfire::Client::Widget::Container::; 341our @ISA = CFClient::Widget::Bin::;
342
343use SDL::OpenGL;
344
345sub new {
346 my ($class, %arg) = @_;
347
348 my $self = $class->SUPER::new (%arg);
349}
225 350
226sub update { 351sub update {
227 my ($self) = @_; 352 my ($self) = @_;
228 353
229 ::refresh (); 354 # 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; 355 $self->render_chld;
356 $self->SUPER::update;
254} 357}
255 358
256sub render_chld { 359sub render_chld {
257 my ($self) = @_; 360 my ($self) = @_;
258 my $chld = $self->get;
259 my ($w, $h) = $self->size_request;
260 361
261 require Carp;
262 Carp::cluck "RENDERCHI $w $h";
263 warn "RENDERCHI $w $h\n";
264 $self->{texture} = 362 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 363 CFClient::Texture->new_from_opengl (
266 $w, $h, sub { $chld->draw } 364 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 365 );
268 $self->{texture}->upload;
269}
270
271sub size_request {
272 my ($self) = @_;
273 ($self->w, $self->h)
274} 366}
275 367
276sub size_allocate { 368sub size_allocate {
277 my ($self, $w, $h) = @_; 369 my ($self, $w, $h) = @_;
278 370
279 $self->w ($w); 371 $self->{w} = $w;
280 $self->h ($h); 372 $self->{h} = $h;
373
281 $self->get->size_allocate ($w, $h); 374 $self->child->size_allocate ($w, $h);
282 375
283 $self->update; #TODO: Move this to the size_request event propably? 376 $self->render_chld;
284} 377}
285 378
286sub _draw { 379sub _draw {
287 my ($self) = @_; 380 my ($self) = @_;
288 381
292 or return; 385 or return;
293 386
294 glEnable GL_BLEND; 387 glEnable GL_BLEND;
295 glEnable GL_TEXTURE_2D; 388 glEnable GL_TEXTURE_2D;
296 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
297 glBindTexture GL_TEXTURE_2D, $tex->{name};
298 390
299 glBegin GL_QUADS; 391 $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 392
306 glDisable GL_BLEND; 393 glDisable GL_BLEND;
307 glDisable GL_TEXTURE_2D; 394 glDisable GL_TEXTURE_2D;
308} 395}
309 396
397#############################################################################
398
310package Crossfire::Client::Widget::Frame; 399package CFClient::Widget::Frame;
311 400
312our @ISA = Crossfire::Client::Widget::Bin::; 401our @ISA = CFClient::Widget::Bin::;
313 402
314use SDL::OpenGL; 403use SDL::OpenGL;
315 404
316sub size_request { 405sub size_request {
317 my ($self) = @_; 406 my ($self) = @_;
318 my $chld = $self->get 407 my $chld = $self->child
319 or return (0, 0); 408 or return (0, 0);
320 409
321 $chld->move (2, 2); 410 $chld->move (2, 2);
322 411
323 map { $_ + 4 } $chld->size_request; 412 map { $_ + 4 } $chld->size_request;
324} 413}
325 414
326sub size_allocate { 415sub size_allocate {
327 my ($self, $w, $h) = @_; 416 my ($self, $w, $h) = @_;
328 417
329 $self->w ($w); 418 $self->{w} = $w;
330 $self->h ($h); 419 $self->{h} = $h;
331 420
332 $self->get->size_allocate ($w - 4, $h - 4); 421 $self->child->size_allocate ($w - 4, $h - 4);
333 $self->get->move (2, 2); 422 $self->child->move (2, 2);
334} 423}
335 424
336sub _draw { 425sub _draw {
337 my ($self) = @_; 426 my ($self) = @_;
338 427
339 my $chld = $self->get; 428 my $chld = $self->child;
340 429
341 my ($w, $h) = $chld->size_request; 430 my ($w, $h) = $chld->size_request;
342 431
343 glBegin GL_QUADS; 432 glBegin GL_QUADS;
344 glColor 0, 0, 0; 433 glColor 0, 0, 0;
345 glTexCoord 0, 0; glVertex 0 , 0; 434 glVertex 0 , 0;
346 glTexCoord 0, 1; glVertex 0 , $h + 4; 435 glVertex 0 , $h + 4;
347 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 436 glVertex $w + 4 , $h + 4;
348 glTexCoord 1, 0; glVertex $w + 4 , 0; 437 glVertex $w + 4 , 0;
349 glEnd; 438 glEnd;
350 439
351 $chld->draw; 440 $chld->draw;
352} 441}
353 442
443#############################################################################
444
354package Crossfire::Client::Widget::FancyFrame; 445package CFClient::Widget::FancyFrame;
355 446
356our @ISA = Crossfire::Client::Widget::Frame::; 447our @ISA = CFClient::Widget::Bin::;
357 448
358use SDL::OpenGL; 449use SDL::OpenGL;
359 450
360sub new { 451my @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 $_ } 452 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/ 453 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 454
371sub size_request { 455sub size_request {
372 my ($self) = @_; 456 my ($self) = @_;
457
373 my ($w, $h) = $self->get->size_request; 458 my ($w, $h) = $self->SUPER::size_request;
374 459
375 $h += $self->{txts}->[1]->{height}; 460 $h += $tex[1]->{height};
376 $h += $self->{txts}->[4]->{height}; 461 $h += $tex[4]->{height};
377 $w += $self->{txts}->[2]->{width}; 462 $w += $tex[2]->{width};
378 $w += $self->{txts}->[3]->{width}; 463 $w += $tex[3]->{width};
379 464
380 ($w, $h) 465 ($w, $h)
381} 466}
382 467
383sub size_allocate { 468sub size_allocate {
384 my ($self, $w, $h) = @_; 469 my ($self, $w, $h) = @_;
385 470
386 $self->w ($w); 471 $self->SUPER::size_allocate ($w, $h);
387 $self->h ($h); 472
388 $h -= $self->{txts}->[1]->{height}; 473 $h -= $tex[1]->{height};
389 $h -= $self->{txts}->[4]->{height}; 474 $h -= $tex[4]->{height};
390 $w -= $self->{txts}->[2]->{width}; 475 $w -= $tex[2]->{width};
391 $w -= $self->{txts}->[3]->{width}; 476 $w -= $tex[3]->{width};
392 477
393 $h = $h < 0 ? 0 : $h; 478 $h = $h < 0 ? 0 : $h;
394 $w = $w < 0 ? 0 : $w; 479 $w = $w < 0 ? 0 : $w;
395 warn "CHILD:$w $h\n"; 480
481 my $child = $self->child;
482
396 $self->get->size_allocate ($w, $h); 483 $child->size_allocate ($w, $h);
397 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 484 $child->move ($tex[3]->{width}, $tex[1]->{height});
398} 485}
399 486
400sub _draw { 487sub _draw {
401 my ($self) = @_; 488 my ($self) = @_;
402 489
403 my ($w, $h) = ($self->w, $self->h); 490 my ($w, $h) = ($self->{w}, $self->{h});
404 my ($cw, $ch) = ($self->get->w, $self->get->h); 491 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
405 492
406 glEnable GL_BLEND; 493 glEnable GL_BLEND;
407 glEnable GL_TEXTURE_2D; 494 glEnable GL_TEXTURE_2D;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 495 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
410 497
411 my $top = $self->{txts}->[1]; 498 my $top = $tex[1];
412 glBindTexture GL_TEXTURE_2D, $top->{name}; 499 $top->draw_quad (0, 0, $w, $top->{height});
413 500
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]; 501 my $left = $tex[3];
422 glBindTexture GL_TEXTURE_2D, $left->{name}; 502 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
423 503
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]; 504 my $right = $tex[2];
432 glBindTexture GL_TEXTURE_2D, $right->{name}; 505 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
433 506
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]; 507 my $bottom = $tex[4];
442 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 508 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
443 509
444 glBegin GL_QUADS; 510 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}; 511 glBindTexture GL_TEXTURE_2D, $bg->{name};
453 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 512 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
454 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 513 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
455 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 514 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
456 515
457 my $rep_x = $cw / $bg->{width}; 516 my $rep_x = $cw / $bg->{width};
458 my $rep_y = $ch / $bg->{height}; 517 my $rep_y = $ch / $bg->{height};
459 518
460 glBegin GL_QUADS; 519 $bg->draw_quad ($left->{width}, $top->{height}, $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 520
467 glDisable GL_BLEND; 521 glDisable GL_BLEND;
468 glDisable GL_TEXTURE_2D; 522 glDisable GL_TEXTURE_2D;
469 523
470 $self->get->draw; 524 $self->child->draw;
471 525
472} 526}
473 527
528#############################################################################
529
474package Crossfire::Client::Widget::Table; 530package CFClient::Widget::Table;
475 531
476our @ISA = Crossfire::Client::Widget::Bin::; 532our @ISA = CFClient::Widget::Bin::;
477 533
478use SDL::OpenGL; 534use SDL::OpenGL;
479 535
480sub add { 536sub add {
481 my ($self, $x, $y, $chld) = @_; 537 my ($self, $x, $y, $chld) = @_;
554 610
555 $y += $self->max_row_height ($yi); 611 $y += $self->max_row_height ($yi);
556 } 612 }
557} 613}
558 614
615#############################################################################
616
559package Crossfire::Client::Widget::VBox; 617package CFClient::Widget::VBox;
560 618
561our @ISA = Crossfire::Client::Widget::Container::; 619our @ISA = CFClient::Widget::Container::;
562 620
563use SDL::OpenGL; 621use SDL::OpenGL;
622
623sub size_request {
624 my ($self) = @_;
625
626 my @alloc = map [$_->size_request], @{$self->{children}};
627
628 (
629 (List::Util::max map $_->[0], @alloc),
630 (List::Util::sum map $_->[1], @alloc),
631 )
632}
564 633
565sub size_allocate { 634sub size_allocate {
566 my ($self, $w, $h) = @_; 635 my ($self, $w, $h) = @_;
567 636
568 $self->w ($w); 637 $self->w ($w);
601 $y += $h; 670 $y += $h;
602 } 671 }
603 } 672 }
604} 673}
605 674
606sub _draw { 675#############################################################################
607 my ($self) = @_;
608 676
609 my ($x, $y);
610 for (@{$self->{children} || []}) {
611 $_->draw;
612 $y += $_->h;
613 }
614}
615
616package Crossfire::Client::Widget::Label; 677package CFClient::Widget::Label;
617 678
618our @ISA = Crossfire::Client::Widget::; 679our @ISA = CFClient::Widget::;
619 680
620use SDL::OpenGL; 681use SDL::OpenGL;
621 682
622sub new { 683sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 684 my ($class, %arg) = @_;
624 685
625 # TODO: color, and make height, xyz etc. optional 686 # TODO: color, and make height, xyz etc. optional
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 687 my $self = $class->SUPER::new (
688 color => [1, 1, 1],
689 height => $::FONTSIZE,
690 text => "",
691 layout => new CFClient::Layout,
692 %arg
693 );
627 694
628 $self->set_text ($text); 695 $self->set_text ($self->{text});
629 696
630 $self 697 $self
631} 698}
632 699
633sub set_text { 700sub set_text {
634 my ($self, $text) = @_; 701 my ($self, $text) = @_;
635 702
636 $self->{text} = $text; 703 $self->{text} = $text;
637 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 704 $self->{layout}->set_markup ($text);
638 705
639 $self->update; 706 delete $self->{texture};
640} 707}
641 708
642sub get_text { 709sub get_text {
643 my ($self, $text) = @_; 710 my ($self, $text) = @_;
644 711
646} 713}
647 714
648sub size_request { 715sub size_request {
649 my ($self) = @_; 716 my ($self) = @_;
650 717
651 ( 718 $self->{layout}->set_width;
719 $self->{layout}->set_height ($self->{height});
720 $self->{layout}->size
721# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
722# (
652 $self->{texture}{width}, 723# $self->{texture}{width},
653 $self->{texture}{height}, 724# $self->{texture}{height},
654 ) 725# )
726# } else {
727# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
728#
729# ($w, $h)
730# }
731}
732
733sub size_allocate {
734 my ($self, $w, $h) = @_;
735
736 $self->SUPER::size_allocate ($w, $h);
737 delete $self->{texture};
655} 738}
656 739
657sub _draw { 740sub _draw {
658 my ($self) = @_; 741 my ($self) = @_;
659 742
660 my $tex = $self->{texture}; 743 my $tex = $self->{texture} ||= do {
744 $self->{layout}->set_width ($self->{w});
745 new_from_layout CFClient::Texture $self->{layout};
746 };
661 747
662 glEnable GL_BLEND; 748 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 749 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 750 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
666 glBindTexture GL_TEXTURE_2D, $tex->{name};
667 752
668 glColor 1, 0, 0, 1; # TODO color 753 glColor @{$self->{color}};
669 754
670 glBegin GL_QUADS; 755 $tex->draw_quad (0, 0);
671 glTexCoord 0, 0; glVertex 0 , 0;
672 glTexCoord 0, 1; glVertex 0 , $tex->{height};
673 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
674 glTexCoord 1, 0; glVertex $tex->{width}, 0;
675 glEnd;
676 756
677 glDisable GL_BLEND; 757 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 758 glDisable GL_TEXTURE_2D;
679} 759}
680 760
761#############################################################################
762
681package Crossfire::Client::Widget::TextEntry; 763package CFClient::Widget::Entry;
682 764
683our @ISA = Crossfire::Client::Widget::Label::; 765our @ISA = CFClient::Widget::Label::;
684 766
685use SDL; 767use SDL;
686use SDL::OpenGL; 768use SDL::OpenGL;
687 769
688sub key_down { 770sub key_down {
689 my ($self, $ev) = @_; 771 my ($self, $ev) = @_;
690 772
691 my $mod = $ev->key_mod; 773 my $mod = $ev->key_mod;
692 my $sym = $ev->key_sym; 774 my $sym = $ev->key_sym;
693 775
694 $ev->set_unicode (1);
695 my $uni = $ev->key_unicode; 776 my $uni = $ev->key_unicode;
696 777
697 my $text = $self->get_text; 778 my $text = $self->get_text;
698 779
699 if ($sym == SDLK_BACKSPACE) { 780 if ($sym == SDLK_BACKSPACE) {
700 substr $text, -1, 1, ''; 781 substr $text, -1, 1, '';
701
702 } elsif ($uni) { 782 } elsif ($uni) {
703 $text .= chr $uni; 783 $text .= chr $uni;
704 } 784 }
785
705 $self->set_text ($text); 786 $self->set_text ($text);
706} 787}
707 788
789sub button_down {
790 my ($self, $ev) = @_;
791
792 $self->focus_in;
793}
794
795sub mouse_motion {
796 my ($self, $ev, $x, $y) = @_;
797 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
798}
799
800sub _draw {
801 my ($self) = @_;
802
803 if ($FOCUS == $self) {
804 glColor 1, 1, 1;
805 } else {
806 glColor 0.7, 0.7, 0.7;
807 }
808
809 glBegin GL_QUADS;
810 glVertex 0 , 0;
811 glVertex 0 , $self->{h} - 1;
812 glVertex $self->{w} - 1, $self->{h} - 1;
813 glVertex $self->{w} - 1, 0;
814 glEnd;
815
816 $self->SUPER::_draw;
817}
818
819#############################################################################
820
708package Crossfire::Client::Widget::MapWidget; 821package CFClient::Widget::MapWidget;
709 822
710use strict; 823use strict;
711 824
712use List::Util qw(min max); 825use List::Util qw(min max);
713 826
714use SDL; 827use SDL;
715use SDL::OpenGL; 828use SDL::OpenGL;
716use SDL::OpenGL::Constants; 829use SDL::OpenGL::Constants;
717 830
718our @ISA = Crossfire::Client::Widget::; 831our @ISA = CFClient::Widget::;
832
833sub new {
834 my $class = shift;
835
836 $class->SUPER::new (
837 z => -1,
838 list => (glGenLists 1),
839 @_
840 )
841}
719 842
720sub key_down { 843sub key_down {
721 print "MAPKEYDOWN\n"; 844 print "MAPKEYDOWN\n";
722} 845}
723 846
724sub key_up { 847sub key_up {
725} 848}
726 849
727sub size_request { 850sub size_request {
728 851 (
852 1 + int $::WIDTH / 32,
853 1 + int $::HEIGHT / 32,
854 )
729} 855}
730 856
731sub size_allocate { 857sub update {
858 my ($self) = @_;
859
860 $self->{need_update} = 1;
732} 861}
733 862
734sub _draw { 863sub _draw {
735 my ($self) = @_; 864 my ($self) = @_;
736 865
866 if (delete $self->{need_update}) {
867 glNewList $self->{list}, GL_COMPILE;
868
737 my $mx = $::CONN->{mapx}; 869 my $mx = $::CONN->{mapx};
738 my $my = $::CONN->{mapy}; 870 my $my = $::CONN->{mapy};
739 871
740 my $map = $::CONN->{map}; 872 my $map = $::CONN->{map};
741 873
742 my ($xofs, $yofs); 874 my ($xofs, $yofs);
743 875
744 my $sw = 1 + int $::WIDTH / 32; 876 my $sw = 1 + int $::WIDTH / 32;
745 my $sh = 1 + int $::HEIGHT / 32; 877 my $sh = 1 + int $::HEIGHT / 32;
746 878
747 if ($::CONN->{mapw} > $sw) { 879 if ($::CONN->{mapw} > $sw) {
748 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 880 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
749 } else { 881 } else {
750 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 882 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
751 } 883 }
752 884
753 if ($::CONN->{maph} > $sh) { 885 if ($::CONN->{maph} > $sh) {
754 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 886 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
755 } else { 887 } else {
756 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 888 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
757 } 889 }
758 890
759 glEnable GL_TEXTURE_2D; 891 glEnable GL_TEXTURE_2D;
760 glEnable GL_BLEND; 892 glEnable GL_BLEND;
761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
762 894
763 my $sw4 = ($sw + 3) & ~3; 895 my $sw4 = ($sw + 3) & ~3;
764 my $lighting = "\x00" x ($sw4 * $sh); 896 my $darkness = "\x00" x ($sw4 * $sh);
765 897
766 for my $x (0 .. $sw - 1) { 898 for my $x (0 .. $sw - 1) {
899 my $row = $map->[$x + $xofs];
767 for my $y (0 .. $sh - 1) { 900 for my $y (0 .. $sh - 1) {
768 901
769 my $cell = $map->[$x + $xofs][$y + $yofs] 902 my $cell = $row->[$y + $yofs]
770 or next; 903 or next;
771 904
772 my $darkness = $cell->[0] * (1 / 255); 905 my $dark = $cell->[0];
773 if ($darkness < 0) { 906 if ($dark < 0) {
774 $darkness = 0.15; 907 substr $darkness, $y * $sw4 + $x, 1, chr 224;
908 } else {
909 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
775 } 910 }
776 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
777 911
778 for my $num (grep $_, @$cell[1,2,3]) { 912 for my $num (grep $_, @$cell[1,2,3]) {
779 my $tex = $::CONN->{face}[$num]{texture} || next; 913 my $tex = $::CONN->{face}[$num]{texture} || next;
780 914
781 glBindTexture GL_TEXTURE_2D, $tex->{name};
782
783 my $w = $tex->{width}; 915 my $w = $tex->{width};
784 my $h = $tex->{height}; 916 my $h = $tex->{height};
785 917
786 my $px = ($x + 1) * 32 - $w; 918 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
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; 919 }
795 } 920 }
796 } 921 }
797 }
798 922
799# if (1) { # higher quality darkness 923# if (1) { # higher quality darkness
800# $lighting =~ s/(.)/$1$1$1/gs; 924# $lighting =~ s/(.)/$1$1$1/gs;
801# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 925# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
802# 926#
804# 928#
805# $lighting = $pb->get_pixels; 929# $lighting = $pb->get_pixels;
806# $lighting =~ s/(.)../$1/gs; 930# $lighting =~ s/(.)../$1/gs;
807# } 931# }
808 932
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; 933 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; 934 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 935
936 $darkness = new CFClient::Texture
937 width => $sw4,
938 height => $sh,
939 data => $darkness,
940 internalformat => GL_ALPHA,
941 format => GL_ALPHA;
942
943 glColor 0.45, 0.45, 0.45, 1;
944 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
945
828 glDisable GL_TEXTURE_2D; 946 glDisable GL_TEXTURE_2D;
829 glDisable GL_BLEND; 947 glDisable GL_BLEND;
948
949 glEndList;
950 }
951
952 glCallList $self->{list};
830} 953}
831 954
832my %DIR = ( 955my %DIR = (
833 SDLK_KP8, [1, "north"], 956 SDLK_KP8, [1, "north"],
834 SDLK_KP9, [2, "northeast"], 957 SDLK_KP9, [2, "northeast"],
878 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1001 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
879 $::CONN->send ("command run_stop"); 1002 $::CONN->send ("command run_stop");
880 } 1003 }
881} 1004}
882 1005
1006#############################################################################
1007
883package Crossfire::Client::Widget::Animator; 1008package CFClient::Widget::Animator;
884 1009
885use SDL::OpenGL; 1010use SDL::OpenGL;
886 1011
887our @ISA = Crossfire::Client::Widget::Bin::; 1012our @ISA = CFClient::Widget::Bin::;
888 1013
889sub moveto { 1014sub moveto {
890 my ($self, $x, $y) = @_; 1015 my ($self, $x, $y) = @_;
891 1016
892 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1017 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
893 $self->{speed} = 0.2; 1018 $self->{speed} = 0.001;
894 $self->{time} = 1; 1019 $self->{time} = 1;
895 1020
896 ::animation_start $self; 1021 ::animation_start $self;
897} 1022}
898 1023
913 1038
914sub _draw { 1039sub _draw {
915 my ($self) = @_; 1040 my ($self) = @_;
916 1041
917 glPushMatrix; 1042 glPushMatrix;
918 glRotate $self->{time} * 10000, 0, 1, 0; 1043 glRotate $self->{time} * 1000, 0, 1, 0;
919 $self->{children}[0]->draw; 1044 $self->{children}[0]->draw;
920 glPopMatrix; 1045 glPopMatrix;
921} 1046}
922 1047
9231; 1048#############################################################################
924 1049
1050package CFClient::Widget::Toplevel;
1051
1052our @ISA = CFClient::Widget::Container::;
1053
1054sub size_request {
1055 ($::WIDTH, $::HEIGHT)
1056}
1057
1058sub size_allocate {
1059 my ($self, $w, $h) = @_;
1060
1061 $self->SUPER::size_allocate ($w, $h);
1062
1063 $_->size_allocate ($_->size_request)
1064 for @{$self->{children}};
1065}
1066
1067sub translate {
1068 my ($self, $x, $y) = @_;
1069
1070 ($x, $y)
1071}
1072
1073sub update {
1074 my ($self) = @_;
1075
1076 $self->size_allocate ($self->size_request);
1077 ::refresh ();
1078}
1079
1080sub add {
1081 my ($self, $widget) = @_;
1082
1083 $self->SUPER::add ($widget);
1084
1085 $widget->size_allocate ($widget->size_request);
1086}
1087
1088sub draw {
1089 my ($self) = @_;
1090
1091 $self->_draw;
1092}
1093
1094#############################################################################
1095
1096package CFClient::Widget;
1097
1098$TOPLEVEL = new CFClient::Widget::Toplevel;
1099
11001
1101

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines