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.56 by root, Mon Apr 10 22:16:34 2006 UTC

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 Crossfire::Client;
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) 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 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) if $HOVER;
71}
17 72
18sub new { 73sub new {
19 my $class = shift; 74 my $class = shift;
20 75
21 bless { @_ }, $class 76 bless { @_ }, $class
37 Carp::confess "size_request is abtract"; 92 Carp::confess "size_request is abtract";
38} 93}
39 94
40sub size_allocate { 95sub size_allocate {
41 my ($self, $w, $h) = @_; 96 my ($self, $w, $h) = @_;
97
42 $self->w ($w); 98 $self->{w} = $w;
43 $self->h ($h); 99 $self->{h} = $h;
44} 100}
45 101
46sub focus_in { 102sub focus_in {
47 my ($widget) = @_; 103 my ($self) = @_;
48 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
49} 108}
50 109
51sub focus_out { 110sub focus_out {
52 my ($widget) = @_; 111 my ($self) = @_;
53}
54 112
55sub key_down { 113 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 114
59sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
61} 117}
62 118
119sub mouse_motion { }
120sub button_up { }
63sub button_down { 121sub button_down { }
64 my ($widget, $sdlev) = @_; 122sub key_down { }
65} 123sub key_up { }
66 124
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 130
77sub draw { 131sub draw {
78 my ($self) = @_; 132 my ($self) = @_;
79 133
80 glPushMatrix; 134 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
83 glPopMatrix; 148 glPopMatrix;
84} 149}
85 150
86sub _draw { 151sub _draw {
87 my ($self) = @_; 152 my ($self) = @_;
134 my ($self) = @_; 199 my ($self) = @_;
135 200
136 #$self->deactivate; 201 #$self->deactivate;
137} 202}
138 203
204#############################################################################
205
139package Crossfire::Client::Widget::Container; 206package Crossfire::Client::Widget::Container;
140 207
141our @ISA = Crossfire::Client::Widget::; 208our @ISA = Crossfire::Client::Widget::;
142 209
143sub new { 210sub new {
157 224
158 @{$self->{children}} = 225 @{$self->{children}} =
159 sort { $a->{z} <=> $b->{z} } 226 sort { $a->{z} <=> $b->{z} }
160 @{$self->{children}}, $chld; 227 @{$self->{children}}, $chld;
161 228
162 $self->size_allocate ($self->{w}, $self->{h}); 229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
163} 231}
164 232
165sub remove { 233sub remove {
166 my ($self, $widget) = @_; 234 my ($self, $widget) = @_;
167 235
171} 239}
172 240
173sub find_widget { 241sub find_widget {
174 my ($self, $x, $y) = @_; 242 my ($self, $x, $y) = @_;
175 243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
176 my $res; 247 my $res;
177 248
178 for (@{ $self->{children} }) { 249 for (reverse @{ $self->{children} }) {
179 $res = $_->find_widget ($x, $y) 250 $res = $_->find_widget ($x, $y)
180 and return $res; 251 and return $res;
181 } 252 }
182 253
183 () 254 $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} 255}
198 256
199sub _draw { 257sub _draw {
200 my ($self) = @_; 258 my ($self) = @_;
201 259
202 $_->draw for @{$self->{children}}; 260 $_->draw for @{$self->{children}};
203} 261}
204 262
263#############################################################################
264
205package Crossfire::Client::Widget::Bin; 265package Crossfire::Client::Widget::Bin;
206 266
207our @ISA = Crossfire::Client::Widget::Container::; 267our @ISA = Crossfire::Client::Widget::Container::;
208 268
209sub get { $_[0]->{children}[0] } 269sub child { $_[0]->{children}[0] }
210 270
211sub size_request { 271sub size_request {
212 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
213} 273}
214 274
215sub size_allocate { 275sub size_allocate {
216 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
277
217 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
218 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
219 if $self->{children}[0] 280 if $self->{children}[0]
220} 281}
221 282
283#############################################################################
284
222package Crossfire::Client::Widget::Toplevel; 285package Crossfire::Client::Widget::Window;
223 286
224our @ISA = Crossfire::Client::Widget::Container::; 287our @ISA = Crossfire::Client::Widget::Bin::;
288
289use SDL::OpenGL;
290
291sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_;
293
294 my $self = $class->SUPER::new;
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
225 298
226sub update { 299sub update {
227 my ($self) = @_; 300 my ($self) = @_;
228 301
229 ::refresh ();
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; 302 $self->render_chld;
303 $self->SUPER::update;
254} 304}
255 305
256sub render_chld { 306sub render_chld {
257 my ($self) = @_; 307 my ($self) = @_;
258 my $chld = $self->get;
259 my ($w, $h) = $self->size_request;
260 308
261 require Carp;
262 Carp::cluck "RENDERCHI $w $h";
263 warn "RENDERCHI $w $h\n";
264 $self->{texture} = 309 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
266 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 312 );
268 $self->{texture}->upload;
269}
270
271sub size_request {
272 my ($self) = @_;
273 ($self->w, $self->h)
274} 313}
275 314
276sub size_allocate { 315sub size_allocate {
277 my ($self, $w, $h) = @_; 316 my ($self, $w, $h) = @_;
278 317
279 $self->w ($w); 318 $self->{w} = $w;
280 $self->h ($h); 319 $self->{h} = $h;
320
281 $self->get->size_allocate ($w, $h); 321 $self->child->size_allocate ($w, $h);
282 322
283 $self->update; #TODO: Move this to the size_request event propably? 323 $self->render_chld;
284} 324}
285 325
286sub _draw { 326sub _draw {
287 my ($self) = @_; 327 my ($self) = @_;
288 328
292 or return; 332 or return;
293 333
294 glEnable GL_BLEND; 334 glEnable GL_BLEND;
295 glEnable GL_TEXTURE_2D; 335 glEnable GL_TEXTURE_2D;
296 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
297 glBindTexture GL_TEXTURE_2D, $tex->{name};
298 337
299 glBegin GL_QUADS; 338 $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 339
306 glDisable GL_BLEND; 340 glDisable GL_BLEND;
307 glDisable GL_TEXTURE_2D; 341 glDisable GL_TEXTURE_2D;
308} 342}
309 343
344#############################################################################
345
310package Crossfire::Client::Widget::Frame; 346package Crossfire::Client::Widget::Frame;
311 347
312our @ISA = Crossfire::Client::Widget::Bin::; 348our @ISA = Crossfire::Client::Widget::Bin::;
313 349
314use SDL::OpenGL; 350use SDL::OpenGL;
315 351
316sub size_request { 352sub size_request {
317 my ($self) = @_; 353 my ($self) = @_;
318 my $chld = $self->get 354 my $chld = $self->child
319 or return (0, 0); 355 or return (0, 0);
320 356
321 $chld->move (2, 2); 357 $chld->move (2, 2);
322 358
323 map { $_ + 4 } $chld->size_request; 359 map { $_ + 4 } $chld->size_request;
324} 360}
325 361
326sub size_allocate { 362sub size_allocate {
327 my ($self, $w, $h) = @_; 363 my ($self, $w, $h) = @_;
328 364
329 $self->w ($w); 365 $self->{w} = $w;
330 $self->h ($h); 366 $self->{h} = $h;
331 367
332 $self->get->size_allocate ($w - 4, $h - 4); 368 $self->child->size_allocate ($w - 4, $h - 4);
333 $self->get->move (2, 2); 369 $self->child->move (2, 2);
334} 370}
335 371
336sub _draw { 372sub _draw {
337 my ($self) = @_; 373 my ($self) = @_;
338 374
339 my $chld = $self->get; 375 my $chld = $self->child;
340 376
341 my ($w, $h) = $chld->size_request; 377 my ($w, $h) = $chld->size_request;
342 378
343 glBegin GL_QUADS; 379 glBegin GL_QUADS;
344 glColor 0, 0, 0; 380 glColor 0, 0, 0;
345 glTexCoord 0, 0; glVertex 0 , 0; 381 glVertex 0 , 0;
346 glTexCoord 0, 1; glVertex 0 , $h + 4; 382 glVertex 0 , $h + 4;
347 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 383 glVertex $w + 4 , $h + 4;
348 glTexCoord 1, 0; glVertex $w + 4 , 0; 384 glVertex $w + 4 , 0;
349 glEnd; 385 glEnd;
350 386
351 $chld->draw; 387 $chld->draw;
352} 388}
353 389
390#############################################################################
391
354package Crossfire::Client::Widget::FancyFrame; 392package Crossfire::Client::Widget::FancyFrame;
355 393
356our @ISA = Crossfire::Client::Widget::Frame::; 394our @ISA = Crossfire::Client::Widget::Bin::;
357 395
358use SDL::OpenGL; 396use SDL::OpenGL;
359 397
360sub new { 398my @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 $_ } 399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
366 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 400 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 401
371sub size_request { 402sub size_request {
372 my ($self) = @_; 403 my ($self) = @_;
404
373 my ($w, $h) = $self->get->size_request; 405 my ($w, $h) = $self->SUPER::size_request;
374 406
375 $h += $self->{txts}->[1]->{height}; 407 $h += $tex[1]->{height};
376 $h += $self->{txts}->[4]->{height}; 408 $h += $tex[4]->{height};
377 $w += $self->{txts}->[2]->{width}; 409 $w += $tex[2]->{width};
378 $w += $self->{txts}->[3]->{width}; 410 $w += $tex[3]->{width};
379 411
380 ($w, $h) 412 ($w, $h)
381} 413}
382 414
383sub size_allocate { 415sub size_allocate {
384 my ($self, $w, $h) = @_; 416 my ($self, $w, $h) = @_;
385 417
386 $self->w ($w); 418 $self->SUPER::size_allocate ($w, $h);
387 $self->h ($h); 419
388 $h -= $self->{txts}->[1]->{height}; 420 $h -= $tex[1]->{height};
389 $h -= $self->{txts}->[4]->{height}; 421 $h -= $tex[4]->{height};
390 $w -= $self->{txts}->[2]->{width}; 422 $w -= $tex[2]->{width};
391 $w -= $self->{txts}->[3]->{width}; 423 $w -= $tex[3]->{width};
392 424
393 $h = $h < 0 ? 0 : $h; 425 $h = $h < 0 ? 0 : $h;
394 $w = $w < 0 ? 0 : $w; 426 $w = $w < 0 ? 0 : $w;
395 warn "CHILD:$w $h\n"; 427
396 $self->get->size_allocate ($w, $h); 428 $self->child->size_allocate ($w, $h);
397 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 429 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
398} 430}
399 431
400sub _draw { 432sub _draw {
401 my ($self) = @_; 433 my ($self) = @_;
402 434
403 my ($w, $h) = ($self->w, $self->h); 435 my ($w, $h) = ($self->{w}, $self->{h});
404 my ($cw, $ch) = ($self->get->w, $self->get->h); 436 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
405 437
406 glEnable GL_BLEND; 438 glEnable GL_BLEND;
407 glEnable GL_TEXTURE_2D; 439 glEnable GL_TEXTURE_2D;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 440 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
410 442
411 my $top = $self->{txts}->[1]; 443 my $top = $tex[1];
412 glBindTexture GL_TEXTURE_2D, $top->{name}; 444 $top->draw_quad (0, 0, $w, $top->{height});
413 445
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]; 446 my $left = $tex[3];
422 glBindTexture GL_TEXTURE_2D, $left->{name}; 447 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
423 448
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]; 449 my $right = $tex[2];
432 glBindTexture GL_TEXTURE_2D, $right->{name}; 450 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
433 451
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]; 452 my $bottom = $tex[4];
442 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 453 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
443 454
444 glBegin GL_QUADS; 455 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}; 456 glBindTexture GL_TEXTURE_2D, $bg->{name};
453 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
454 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
455 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
456 460
457 my $rep_x = $cw / $bg->{width}; 461 my $rep_x = $cw / $bg->{width};
458 my $rep_y = $ch / $bg->{height}; 462 my $rep_y = $ch / $bg->{height};
459 463
460 glBegin GL_QUADS; 464 $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 465
467 glDisable GL_BLEND; 466 glDisable GL_BLEND;
468 glDisable GL_TEXTURE_2D; 467 glDisable GL_TEXTURE_2D;
469 468
470 $self->get->draw; 469 $self->child->draw;
471 470
472} 471}
472
473#############################################################################
473 474
474package Crossfire::Client::Widget::Table; 475package Crossfire::Client::Widget::Table;
475 476
476our @ISA = Crossfire::Client::Widget::Bin::; 477our @ISA = Crossfire::Client::Widget::Bin::;
477 478
554 555
555 $y += $self->max_row_height ($yi); 556 $y += $self->max_row_height ($yi);
556 } 557 }
557} 558}
558 559
560#############################################################################
561
559package Crossfire::Client::Widget::VBox; 562package Crossfire::Client::Widget::VBox;
560 563
561our @ISA = Crossfire::Client::Widget::Container::; 564our @ISA = Crossfire::Client::Widget::Container::;
562 565
563use SDL::OpenGL; 566use SDL::OpenGL;
567
568sub size_request {
569 my ($self) = @_;
570
571 my @alloc = map [$_->size_request], @{$self->{children}};
572
573 (
574 (List::Util::max map $_->[0], @alloc),
575 (List::Util::sum map $_->[1], @alloc),
576 )
577}
564 578
565sub size_allocate { 579sub size_allocate {
566 my ($self, $w, $h) = @_; 580 my ($self, $w, $h) = @_;
567 581
568 $self->w ($w); 582 $self->w ($w);
601 $y += $h; 615 $y += $h;
602 } 616 }
603 } 617 }
604} 618}
605 619
606sub _draw { 620#############################################################################
607 my ($self) = @_;
608
609 my ($x, $y);
610 for (@{$self->{children} || []}) {
611 $_->draw;
612 $y += $_->h;
613 }
614}
615 621
616package Crossfire::Client::Widget::Label; 622package Crossfire::Client::Widget::Label;
617 623
618our @ISA = Crossfire::Client::Widget::; 624our @ISA = Crossfire::Client::Widget::;
619 625
620use SDL::OpenGL; 626use SDL::OpenGL;
621 627
622sub new { 628sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 629 my ($class, $x, $y, $z, $height, $text) = @_;
624 630
631 $height ||= $::FONTSIZE;
632
625 # TODO: color, and make height, xyz etc. optional 633 # TODO: color, and make height, xyz etc. optional
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
627 635
628 $self->set_text ($text); 636 $self->set_text ($text);
629 637
646} 654}
647 655
648sub size_request { 656sub size_request {
649 my ($self) = @_; 657 my ($self) = @_;
650 658
651 ( 659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 (
652 $self->{texture}{width}, 661 $self->{texture}{width},
653 $self->{texture}{height}, 662 $self->{texture}{height},
663 )
664 } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
666
667 ($w, $h)
654 ) 668 }
669
655} 670}
656 671
657sub _draw { 672sub _draw {
658 my ($self) = @_; 673 my ($self) = @_;
659 674
661 676
662 glEnable GL_BLEND; 677 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 678 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
666 glBindTexture GL_TEXTURE_2D, $tex->{name};
667 681
668 glColor 1, 0, 0, 1; # TODO color 682 glColor 1, 0, 0, 1; # TODO color
669 683
670 glBegin GL_QUADS; 684 $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 685
677 glDisable GL_BLEND; 686 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 687 glDisable GL_TEXTURE_2D;
679} 688}
680 689
690#############################################################################
691
681package Crossfire::Client::Widget::TextEntry; 692package Crossfire::Client::Widget::Entry;
682 693
683our @ISA = Crossfire::Client::Widget::Label::; 694our @ISA = Crossfire::Client::Widget::Label::;
684 695
685use SDL; 696use SDL;
686use SDL::OpenGL; 697use SDL::OpenGL;
689 my ($self, $ev) = @_; 700 my ($self, $ev) = @_;
690 701
691 my $mod = $ev->key_mod; 702 my $mod = $ev->key_mod;
692 my $sym = $ev->key_sym; 703 my $sym = $ev->key_sym;
693 704
694 $ev->set_unicode (1);
695 my $uni = $ev->key_unicode; 705 my $uni = $ev->key_unicode;
696 706
697 my $text = $self->get_text; 707 my $text = $self->get_text;
698 708
699 if ($sym == SDLK_BACKSPACE) { 709 if ($sym == SDLK_BACKSPACE) {
700 substr $text, -1, 1, ''; 710 substr $text, -1, 1, '';
701
702 } elsif ($uni) { 711 } elsif ($uni) {
703 $text .= chr $uni; 712 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
704 } 714 }
715
705 $self->set_text ($text); 716 $self->set_text ($text);
706} 717}
718
719sub button_down {
720 my ($self, $ev) = @_;
721
722 $self->focus_in;
723}
724
725sub _draw {
726 my ($self) = @_;
727
728 if ($FOCUS == $self) {
729 glColor 1, 1, 1;
730 } else {
731 glColor 0.7, 0.7, 0.7;
732 }
733
734 glBegin GL_QUADS;
735 glVertex 0 , 0;
736 glVertex 0 , $self->{h} - 1;
737 glVertex $self->{w} - 1, $self->{h} - 1;
738 glVertex $self->{w} - 1, 0;
739 glEnd;
740
741 $self->SUPER::_draw;
742}
743
744#############################################################################
707 745
708package Crossfire::Client::Widget::MapWidget; 746package Crossfire::Client::Widget::MapWidget;
709 747
710use strict; 748use strict;
711 749
723 761
724sub key_up { 762sub key_up {
725} 763}
726 764
727sub size_request { 765sub size_request {
728 766 (
729} 767 1 + int $::WIDTH / 32,
730 768 1 + int $::HEIGHT / 32,
731sub size_allocate { 769 )
732} 770}
733 771
734sub _draw { 772sub _draw {
735 my ($self) = @_; 773 my ($self) = @_;
736 774
769 my $cell = $map->[$x + $xofs][$y + $yofs] 807 my $cell = $map->[$x + $xofs][$y + $yofs]
770 or next; 808 or next;
771 809
772 my $darkness = $cell->[0] * (1 / 255); 810 my $darkness = $cell->[0] * (1 / 255);
773 if ($darkness < 0) { 811 if ($darkness < 0) {
774 $darkness = 0.15; 812 $darkness = $cell->[1] ? 0.1 : 0;
775 } 813 }
776 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
777 815
778 for my $num (grep $_, @$cell[1,2,3]) { 816 for my $num (grep $_, @$cell[1,2,3]) {
779 my $tex = $::CONN->{face}[$num]{texture} || next; 817 my $tex = $::CONN->{face}[$num]{texture} || next;
780 818
781 glBindTexture GL_TEXTURE_2D, $tex->{name};
782
783 my $w = $tex->{width}; 819 my $w = $tex->{width};
784 my $h = $tex->{height}; 820 my $h = $tex->{height};
785 821
786 my $px = ($x + 1) * 32 - $w; 822 my $px = ($x + 1) * 32 - $w;
787 my $py = ($y + 1) * 32 - $h; 823 my $py = ($y + 1) * 32 - $h;
788 824
789 glBegin GL_QUADS; 825 $tex->draw_quad ($px, $py, $w, $h);
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;
795 } 826 }
796 } 827 }
797 } 828 }
798 829
799# if (1) { # higher quality darkness 830# if (1) { # higher quality darkness
808 839
809 $lighting = new Crossfire::Client::Texture 840 $lighting = new Crossfire::Client::Texture
810 width => $sw4, 841 width => $sw4,
811 height => $sh, 842 height => $sh,
812 data => $lighting, 843 data => $lighting,
813 internalformat => GL_ALPHA4, 844 internalformat => GL_ALPHA,
814 format => GL_ALPHA; 845 format => GL_ALPHA;
815 846
816 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
817 glColor 0, 0, 0, 0.75; 848 glColor 0.45, 0.45, 0.45, 1;
818 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 849 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
819 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 850 glBindTexture GL_TEXTURE_2D, $lighting->{name};
820 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 851 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
821 glBegin GL_QUADS; 852
822 glTexCoord 0, 0; glVertex 0 , 0; 853 $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32);
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 854
828 glDisable GL_TEXTURE_2D; 855 glDisable GL_TEXTURE_2D;
829 glDisable GL_BLEND; 856 glDisable GL_BLEND;
830} 857}
831 858
878 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 905 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
879 $::CONN->send ("command run_stop"); 906 $::CONN->send ("command run_stop");
880 } 907 }
881} 908}
882 909
910#############################################################################
911
883package Crossfire::Client::Widget::Animator; 912package Crossfire::Client::Widget::Animator;
884 913
885use SDL::OpenGL; 914use SDL::OpenGL;
886 915
887our @ISA = Crossfire::Client::Widget::Bin::; 916our @ISA = Crossfire::Client::Widget::Bin::;
888 917
889sub moveto { 918sub moveto {
890 my ($self, $x, $y) = @_; 919 my ($self, $x, $y) = @_;
891 920
892 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
893 $self->{speed} = 0.2; 922 $self->{speed} = 0.001;
894 $self->{time} = 1; 923 $self->{time} = 1;
895 924
896 ::animation_start $self; 925 ::animation_start $self;
897} 926}
898 927
899sub animate { 928sub animate {
900 my ($self, $interval) = @_; 929 my ($self, $interval) = @_;
930
931 printf "%5.2f\n", 1 / $interval;#d#
901 932
902 $self->{time} -= $interval * $self->{speed}; 933 $self->{time} -= $interval * $self->{speed};
903 if ($self->{time} <= 0) { 934 if ($self->{time} <= 0) {
904 $self->{time} = 0; 935 $self->{time} = 0;
905 ::animation_stop $self; 936 ::animation_stop $self;
913 944
914sub _draw { 945sub _draw {
915 my ($self) = @_; 946 my ($self) = @_;
916 947
917 glPushMatrix; 948 glPushMatrix;
918 glRotate $self->{time} * 10000, 0, 1, 0; 949 glRotate $self->{time} * 1000, 0, 1, 0;
919 $self->{children}[0]->draw; 950 $self->{children}[0]->draw;
920 glPopMatrix; 951 glPopMatrix;
921} 952}
922 953
9231; 954#############################################################################
924 955
956package Crossfire::Client::Widget::Toplevel;
957
958our @ISA = Crossfire::Client::Widget::Container::;
959
960sub size_request {
961 ($::WIDTH, $::HEIGHT)
962}
963
964sub size_allocate {
965 my ($self, $w, $h) = @_;
966
967 $self->SUPER::size_allocate ($w, $h);
968
969 $_->size_allocate ($_->size_request)
970 for @{$self->{children}};
971}
972
973sub update {
974 my ($self) = @_;
975
976 $self->size_allocate ($self->size_request);
977 ::refresh ();
978}
979
980sub add {
981 my ($self, $widget) = @_;
982
983 $self->SUPER::add ($widget);
984
985 $widget->size_allocate ($widget->size_request);
986}
987
988sub draw {
989 my ($self) = @_;
990
991 $self->_draw;
992}
993
994#############################################################################
995
996package Crossfire::Client::Widget;
997
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
999
10001
1001

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines