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.41 by root, Sun Apr 9 21:50:21 2006 UTC vs.
Revision 1.60 by root, Tue Apr 11 13:14:36 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
10use Crossfire::Client; 10use CFClient;
11 11
12our $FOCUS; # the widget with current focus 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
13 16
14# class methods for events 17# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
16sub 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
17sub 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
18sub 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}
19 74
20sub new { 75sub new {
21 my $class = shift; 76 my $class = shift;
22 77
23 bless { @_ }, $class 78 bless { @_ }, $class
44 99
45 $self->{w} = $w; 100 $self->{w} = $w;
46 $self->{h} = $h; 101 $self->{h} = $h;
47} 102}
48 103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
109}
110
49sub focus_in { 111sub focus_in {
50 my ($widget) = @_; 112 my ($self) = @_;
51 $FOCUS = $widget; 113
114 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus;
116 $FOCUS->update;
52} 117}
53 118
54sub focus_out { 119sub focus_out {
55 my ($widget) = @_; 120 my ($self) = @_;
56}
57 121
58sub key_down { 122 return unless $FOCUS == $self;
59 my ($widget, $sdlev) = @_;
60}
61 123
62sub key_up { 124 my $focus = $FOCUS; undef $FOCUS;
63 my ($widget, $sdlev) = @_; 125 $focus->update if $focus; #?
64} 126}
65 127
128sub mouse_motion { }
129sub button_up { }
66sub button_down { 130sub button_down { }
67 my ($widget, $sdlev) = @_; 131sub key_down { }
68} 132sub key_up { }
69 133
70sub button_up {
71 my ($widget, $sdlev) = @_;
72}
73
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 139
80sub draw { 140sub draw {
81 my ($self) = @_; 141 my ($self) = @_;
82 142
83 glPushMatrix; 143 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 144 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 145 $self->_draw;
146 if ($self == $HOVER) {
147 glColor 1, 1, 1, 0.4;
148 glEnable GL_BLEND;
149 glBegin GL_QUADS;
150 glVertex 0 , 0;
151 glVertex $self->{w}, 0;
152 glVertex $self->{w}, $self->{h};
153 glVertex 0 , $self->{h};
154 glEnd;
155 glDisable GL_BLEND;
156 }
86 glPopMatrix; 157 glPopMatrix;
87} 158}
88 159
89sub _draw { 160sub _draw {
90 my ($self) = @_; 161 my ($self) = @_;
139 #$self->deactivate; 210 #$self->deactivate;
140} 211}
141 212
142############################################################################# 213#############################################################################
143 214
144package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
145 216
146our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
147 218
148sub new { 219sub new {
149 my ($class, @widgets) = @_; 220 my ($class, @widgets) = @_;
150 221
151 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
162 233
163 @{$self->{children}} = 234 @{$self->{children}} =
164 sort { $a->{z} <=> $b->{z} } 235 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 236 @{$self->{children}}, $chld;
166 237
167 $self->size_allocate ($self->{w}, $self->{h}); 238 $self->size_allocate ($self->{w}, $self->{h})
239 if $self->{w}; #TODO: check for "realised state"
168} 240}
169 241
170sub remove { 242sub remove {
171 my ($self, $widget) = @_; 243 my ($self, $widget) = @_;
172 244
176} 248}
177 249
178sub find_widget { 250sub find_widget {
179 my ($self, $x, $y) = @_; 251 my ($self, $x, $y) = @_;
180 252
253 $x -= $self->{x};
254 $y -= $self->{y};
255
181 my $res; 256 my $res;
182 257
183 for (@{ $self->{children} }) { 258 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 259 $res = $_->find_widget ($x, $y)
185 and return $res; 260 and return $res;
186 } 261 }
187 262
188 () 263 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
189}
190
191sub size_request {
192 my ($self) = @_;
193
194 my ($hs, $ws) = (0, 0);
195 for (@{$self->{children} || []}) {
196 my ($w, $h) = $_->size_request;
197 $hs += $h;
198 if ($ws < $w) { $ws = $w }
199 }
200
201 return ($ws, $hs);
202} 264}
203 265
204sub _draw { 266sub _draw {
205 my ($self) = @_; 267 my ($self) = @_;
206 268
207 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
208} 270}
209 271
210############################################################################# 272#############################################################################
211 273
212package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
213 275
214our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
215 277
216sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
217 279
218sub size_request { 280sub size_request {
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
220} 282}
221 283
222sub size_allocate { 284sub size_allocate {
223 my ($self, $w, $h) = @_; 285 my ($self, $w, $h) = @_;
286
224 $self->SUPER::size_allocate ($w, $h); 287 $self->SUPER::size_allocate ($w, $h);
225 $self->{children}[0]->size_allocate ($w, $h) 288 $self->{children}[0]->size_allocate ($w, $h)
226 if $self->{children}[0] 289 if $self->{children}[0]
227} 290}
228 291
229############################################################################# 292#############################################################################
230 293
231package Crossfire::Client::Widget::Toplevel; 294package CFClient::Widget::Window;
232 295
233our @ISA = Crossfire::Client::Widget::Container::; 296our @ISA = CFClient::Widget::Bin::;
297
298use SDL::OpenGL;
299
300sub new {
301 my ($class, $x, $y, $z, $w, $h) = @_;
302
303 my $self = $class->SUPER::new;
304
305 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
306}
234 307
235sub update { 308sub update {
236 my ($self) = @_; 309 my ($self) = @_;
237 310
238 ::refresh ();
239}
240
241#############################################################################
242
243package Crossfire::Client::Widget::Window;
244
245our @ISA = Crossfire::Client::Widget::Bin::;
246
247use SDL::OpenGL;
248
249sub add {
250 my ($self, $chld) = @_;
251 warn "ADD $chld\n";
252 $self->SUPER::add ($chld);
253 $chld->set_parent ($self);
254}
255
256sub remove {
257 my ($self) = @_;
258 # TODO FIXME: removing a child from a window will crash, see render_chld
259 # $self->update;
260}
261
262sub update {
263 my ($self) = @_;
264 $self->render_chld; 311 $self->render_chld;
312 $self->SUPER::update;
265} 313}
266 314
267sub render_chld { 315sub render_chld {
268 my ($self) = @_; 316 my ($self) = @_;
269 my $chld = $self->get;
270 my ($w, $h) = $self->size_request;
271 317
272 require Carp;
273 Carp::cluck "RENDERCHI $w $h";
274 warn "RENDERCHI $w $h\n";
275 $self->{texture} = 318 $self->{texture} =
276 Crossfire::Client::Texture->new_from_opengl ( 319 CFClient::Texture->new_from_opengl (
277 $w, $h, sub { $chld->draw } 320 $self->{w}, $self->{h}, sub { $self->child->draw }
278 ); 321 );
279 $self->{texture}->upload;
280}
281
282sub size_request {
283 my ($self) = @_;
284 ($self->w, $self->h)
285} 322}
286 323
287sub size_allocate { 324sub size_allocate {
288 my ($self, $w, $h) = @_; 325 my ($self, $w, $h) = @_;
289 326
290 $self->w ($w); 327 $self->{w} = $w;
291 $self->h ($h); 328 $self->{h} = $h;
329
292 $self->get->size_allocate ($w, $h); 330 $self->child->size_allocate ($w, $h);
293 331
294 $self->update; #TODO: Move this to the size_request event propably? 332 $self->render_chld;
295} 333}
296 334
297sub _draw { 335sub _draw {
298 my ($self) = @_; 336 my ($self) = @_;
299 337
303 or return; 341 or return;
304 342
305 glEnable GL_BLEND; 343 glEnable GL_BLEND;
306 glEnable GL_TEXTURE_2D; 344 glEnable GL_TEXTURE_2D;
307 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
308 glBindTexture GL_TEXTURE_2D, $tex->{name};
309 346
310 glBegin GL_QUADS; 347 $tex->draw_quad (0, 0, $w, $h);
311 glTexCoord 0, 0; glVertex 0, 0;
312 glTexCoord 0, 1; glVertex 0, $h;
313 glTexCoord 1, 1; glVertex $w, $h;
314 glTexCoord 1, 0; glVertex $w, 0;
315 glEnd;
316 348
317 glDisable GL_BLEND; 349 glDisable GL_BLEND;
318 glDisable GL_TEXTURE_2D; 350 glDisable GL_TEXTURE_2D;
319} 351}
320 352
321############################################################################# 353#############################################################################
322 354
323package Crossfire::Client::Widget::Frame; 355package CFClient::Widget::Frame;
324 356
325our @ISA = Crossfire::Client::Widget::Bin::; 357our @ISA = CFClient::Widget::Bin::;
326 358
327use SDL::OpenGL; 359use SDL::OpenGL;
328 360
329sub size_request { 361sub size_request {
330 my ($self) = @_; 362 my ($self) = @_;
336 map { $_ + 4 } $chld->size_request; 368 map { $_ + 4 } $chld->size_request;
337} 369}
338 370
339sub size_allocate { 371sub size_allocate {
340 my ($self, $w, $h) = @_; 372 my ($self, $w, $h) = @_;
341 373
342 $self->SUPER::size_allocate ($w, $h); 374 $self->{w} = $w;
375 $self->{h} = $h;
343 376
344 $self->child->size_allocate ($w - 4, $h - 4); 377 $self->child->size_allocate ($w - 4, $h - 4);
345 $self->child->move (2, 2); 378 $self->child->move (2, 2);
346} 379}
347 380
352 385
353 my ($w, $h) = $chld->size_request; 386 my ($w, $h) = $chld->size_request;
354 387
355 glBegin GL_QUADS; 388 glBegin GL_QUADS;
356 glColor 0, 0, 0; 389 glColor 0, 0, 0;
357 glTexCoord 0, 0; glVertex 0 , 0; 390 glVertex 0 , 0;
358 glTexCoord 0, 1; glVertex 0 , $h + 4; 391 glVertex 0 , $h + 4;
359 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 392 glVertex $w + 4 , $h + 4;
360 glTexCoord 1, 0; glVertex $w + 4 , 0; 393 glVertex $w + 4 , 0;
361 glEnd; 394 glEnd;
362 395
363 $chld->draw; 396 $chld->draw;
364} 397}
365 398
366############################################################################# 399#############################################################################
367 400
368package Crossfire::Client::Widget::FancyFrame; 401package CFClient::Widget::FancyFrame;
369 402
370our @ISA = Crossfire::Client::Widget::Bin::; 403our @ISA = CFClient::Widget::Bin::;
371 404
372use SDL::OpenGL; 405use SDL::OpenGL;
373 406
374my @tex = 407my @tex =
375 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 408 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
376 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 409 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
377 410
378sub size_request { 411sub size_request {
379 my ($self) = @_; 412 my ($self) = @_;
380 413
398 $w -= $tex[2]->{width}; 431 $w -= $tex[2]->{width};
399 $w -= $tex[3]->{width}; 432 $w -= $tex[3]->{width};
400 433
401 $h = $h < 0 ? 0 : $h; 434 $h = $h < 0 ? 0 : $h;
402 $w = $w < 0 ? 0 : $w; 435 $w = $w < 0 ? 0 : $w;
403 warn "CHILD:$w $h\n"; 436
404 $self->child->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
405 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 438 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
406} 439}
407 440
408sub _draw { 441sub _draw {
409 my ($self) = @_; 442 my ($self) = @_;
410 443
411 my ($w, $h) = ($self->w, $self->h); 444 my ($w, $h) = ($self->{w}, $self->{h});
412 my ($cw, $ch) = ($self->child->w, $self->child->h); 445 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
413 446
414 glEnable GL_BLEND; 447 glEnable GL_BLEND;
415 glEnable GL_TEXTURE_2D; 448 glEnable GL_TEXTURE_2D;
416 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
418 451
419 my $top = $tex[1]; 452 my $top = $tex[1];
420 glBindTexture GL_TEXTURE_2D, $top->{name}; 453 $top->draw_quad (0, 0, $w, $top->{height});
421
422 glBegin GL_QUADS;
423 glTexCoord 0, 0; glVertex 0 , 0;
424 glTexCoord 0, 1; glVertex 0 , $top->{height};
425 glTexCoord 1, 1; glVertex $w , $top->{height};
426 glTexCoord 1, 0; glVertex $w , 0;
427 glEnd;
428 454
429 my $left = $tex[3]; 455 my $left = $tex[3];
430 glBindTexture GL_TEXTURE_2D, $left->{name}; 456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
431
432 glBegin GL_QUADS;
433 glTexCoord 0, 0; glVertex 0 , $top->{height};
434 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
435 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
436 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
437 glEnd;
438 457
439 my $right = $tex[2]; 458 my $right = $tex[2];
440 glBindTexture GL_TEXTURE_2D, $right->{name}; 459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
441
442 glBegin GL_QUADS;
443 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
444 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
445 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
446 glTexCoord 1, 0; glVertex $w , $top->{height};
447 glEnd;
448 460
449 my $bottom = $tex[4]; 461 my $bottom = $tex[4];
450 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
451
452 glBegin GL_QUADS;
453 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
454 glTexCoord 0, 1; glVertex 0 , $h;
455 glTexCoord 1, 1; glVertex $w , $h;
456 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
457 glEnd;
458 463
459 my $bg = $tex[0]; 464 my $bg = $tex[0];
460 glBindTexture GL_TEXTURE_2D, $bg->{name}; 465 glBindTexture GL_TEXTURE_2D, $bg->{name};
461 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
462 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
463 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
464 469
465 my $rep_x = $cw / $bg->{width}; 470 my $rep_x = $cw / $bg->{width};
466 my $rep_y = $ch / $bg->{height}; 471 my $rep_y = $ch / $bg->{height};
467 472
468 glBegin GL_QUADS; 473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
469 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
470 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
471 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
472 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
473 glEnd;
474 474
475 glDisable GL_BLEND; 475 glDisable GL_BLEND;
476 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
477 477
478 $self->child->draw; 478 $self->child->draw;
479 479
480} 480}
481 481
482############################################################################# 482#############################################################################
483 483
484package Crossfire::Client::Widget::Table; 484package CFClient::Widget::Table;
485 485
486our @ISA = Crossfire::Client::Widget::Bin::; 486our @ISA = CFClient::Widget::Bin::;
487 487
488use SDL::OpenGL; 488use SDL::OpenGL;
489 489
490sub add { 490sub add {
491 my ($self, $x, $y, $chld) = @_; 491 my ($self, $x, $y, $chld) = @_;
566 } 566 }
567} 567}
568 568
569############################################################################# 569#############################################################################
570 570
571package Crossfire::Client::Widget::VBox; 571package CFClient::Widget::VBox;
572 572
573our @ISA = Crossfire::Client::Widget::Container::; 573our @ISA = CFClient::Widget::Container::;
574 574
575use SDL::OpenGL; 575use SDL::OpenGL;
576
577sub size_request {
578 my ($self) = @_;
579
580 my @alloc = map [$_->size_request], @{$self->{children}};
581
582 (
583 (List::Util::max map $_->[0], @alloc),
584 (List::Util::sum map $_->[1], @alloc),
585 )
586}
576 587
577sub size_allocate { 588sub size_allocate {
578 my ($self, $w, $h) = @_; 589 my ($self, $w, $h) = @_;
579 590
580 $self->w ($w); 591 $self->w ($w);
613 $y += $h; 624 $y += $h;
614 } 625 }
615 } 626 }
616} 627}
617 628
618sub _draw {
619 my ($self) = @_;
620
621 my ($x, $y);
622 for (@{$self->{children} || []}) {
623 $_->draw;
624 $y += $_->h;
625 }
626}
627
628############################################################################# 629#############################################################################
629 630
630package Crossfire::Client::Widget::Label; 631package CFClient::Widget::Label;
631 632
632our @ISA = Crossfire::Client::Widget::; 633our @ISA = CFClient::Widget::;
633 634
634use SDL::OpenGL; 635use SDL::OpenGL;
635 636
636sub new { 637sub new {
637 my ($class, $x, $y, $z, $height, $text) = @_; 638 my ($class, $x, $y, $z, $height, $text) = @_;
638 639
640 $height ||= $::FONTSIZE;
641
639 # TODO: color, and make height, xyz etc. optional 642 # TODO: color, and make height, xyz etc. optional
640 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 643 my $self = $class->SUPER::new (
644 x => $x,
645 y => $y,
646 z => $z,
647 height => $height,
648 layout => new CFClient::Layout,
649 );
641 650
642 $self->set_text ($text); 651 $self->set_text ($text);
643 652
644 $self 653 $self
645} 654}
646 655
647sub set_text { 656sub set_text {
648 my ($self, $text) = @_; 657 my ($self, $text) = @_;
649 658
650 $self->{text} = $text; 659 $self->{text} = $text;
651 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 660 $self->{layout}->set_markup ($text);
652 661
653 $self->update; 662 delete $self->{texture};
654} 663}
655 664
656sub get_text { 665sub get_text {
657 my ($self, $text) = @_; 666 my ($self, $text) = @_;
658 667
660} 669}
661 670
662sub size_request { 671sub size_request {
663 my ($self) = @_; 672 my ($self) = @_;
664 673
665 ( 674 $self->{layout}->set_width;
675 $self->{layout}->size
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
677# (
666 $self->{texture}{width}, 678# $self->{texture}{width},
667 $self->{texture}{height}, 679# $self->{texture}{height},
668 ) 680# )
681# } else {
682# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
683#
684# ($w, $h)
685# }
686}
687
688sub size_allocate {
689 my ($self, $w, $h) = @_;
690
691 $self->SUPER::size_allocate ($w, $h);
692 delete $self->{texture};
669} 693}
670 694
671sub _draw { 695sub _draw {
672 my ($self) = @_; 696 my ($self) = @_;
673 697
674 my $tex = $self->{texture}; 698 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w});
700 new_from_layout CFClient::Texture $self->{layout};
701 };
675 702
676 glEnable GL_BLEND; 703 glEnable GL_BLEND;
677 glEnable GL_TEXTURE_2D; 704 glEnable GL_TEXTURE_2D;
678 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
679 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681 707
682 glColor 1, 0, 0, 1; # TODO color 708 glColor 1, 0, 0, 1; # TODO color
683 709
684 glBegin GL_QUADS; 710 $tex->draw_quad (0, 0);
685 glTexCoord 0, 0; glVertex 0 , 0;
686 glTexCoord 0, 1; glVertex 0 , $tex->{height};
687 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
688 glTexCoord 1, 0; glVertex $tex->{width}, 0;
689 glEnd;
690 711
691 glDisable GL_BLEND; 712 glDisable GL_BLEND;
692 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
693} 714}
694 715
695############################################################################# 716#############################################################################
696 717
697package Crossfire::Client::Widget::TextEntry; 718package CFClient::Widget::Entry;
698 719
699our @ISA = Crossfire::Client::Widget::Label::; 720our @ISA = CFClient::Widget::Label::;
700 721
701use SDL; 722use SDL;
702use SDL::OpenGL; 723use SDL::OpenGL;
703 724
704sub key_down { 725sub key_down {
705 my ($self, $ev) = @_; 726 my ($self, $ev) = @_;
706 727
707 my $mod = $ev->key_mod; 728 my $mod = $ev->key_mod;
708 my $sym = $ev->key_sym; 729 my $sym = $ev->key_sym;
709 730
710 $ev->set_unicode (1);
711 my $uni = $ev->key_unicode; 731 my $uni = $ev->key_unicode;
712 732
713 my $text = $self->get_text; 733 my $text = $self->get_text;
714 734
715 if ($sym == SDLK_BACKSPACE) { 735 if ($sym == SDLK_BACKSPACE) {
716 substr $text, -1, 1, ''; 736 substr $text, -1, 1, '';
717
718 } elsif ($uni) { 737 } elsif ($uni) {
719 $text .= chr $uni; 738 $text .= chr $uni;
720 } 739 }
740
721 $self->set_text ($text); 741 $self->set_text ($text);
722} 742}
723 743
724############################################################################# 744sub button_down {
745 my ($self, $ev) = @_;
725 746
747 $self->focus_in;
748}
749
750sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_;
752 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
753}
754
755sub _draw {
756 my ($self) = @_;
757
758 if ($FOCUS == $self) {
759 glColor 1, 1, 1;
760 } else {
761 glColor 0.7, 0.7, 0.7;
762 }
763
764 glBegin GL_QUADS;
765 glVertex 0 , 0;
766 glVertex 0 , $self->{h} - 1;
767 glVertex $self->{w} - 1, $self->{h} - 1;
768 glVertex $self->{w} - 1, 0;
769 glEnd;
770
771 $self->SUPER::_draw;
772}
773
774#############################################################################
775
726package Crossfire::Client::Widget::MapWidget; 776package CFClient::Widget::MapWidget;
727 777
728use strict; 778use strict;
729 779
730use List::Util qw(min max); 780use List::Util qw(min max);
731 781
732use SDL; 782use SDL;
733use SDL::OpenGL; 783use SDL::OpenGL;
734use SDL::OpenGL::Constants; 784use SDL::OpenGL::Constants;
735 785
736our @ISA = Crossfire::Client::Widget::; 786our @ISA = CFClient::Widget::;
737 787
738sub key_down { 788sub key_down {
739 print "MAPKEYDOWN\n"; 789 print "MAPKEYDOWN\n";
740} 790}
741 791
742sub key_up { 792sub key_up {
743} 793}
744 794
745sub size_request { 795sub size_request {
746 796 (
747} 797 1 + int $::WIDTH / 32,
748 798 1 + int $::HEIGHT / 32,
749sub size_allocate { 799 )
750} 800}
751 801
752sub _draw { 802sub _draw {
753 my ($self) = @_; 803 my ($self) = @_;
754 804
777 glEnable GL_TEXTURE_2D; 827 glEnable GL_TEXTURE_2D;
778 glEnable GL_BLEND; 828 glEnable GL_BLEND;
779 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 829 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
780 830
781 my $sw4 = ($sw + 3) & ~3; 831 my $sw4 = ($sw + 3) & ~3;
782 my $lighting = "\x00" x ($sw4 * $sh); 832 my $darkness = "\x00" x ($sw4 * $sh);
783 833
784 for my $x (0 .. $sw - 1) { 834 for my $x (0 .. $sw - 1) {
835 my $row = $map->[$x + $xofs];
785 for my $y (0 .. $sh - 1) { 836 for my $y (0 .. $sh - 1) {
786 837
787 my $cell = $map->[$x + $xofs][$y + $yofs] 838 my $cell = $row->[$y + $yofs]
788 or next; 839 or next;
789 840
790 my $darkness = $cell->[0] * (1 / 255); 841 my $dark = $cell->[0];
791 if ($darkness < 0) { 842 if ($dark < 0) {
792 $darkness = 0.15; 843 substr $darkness, $y * $sw4 + $x, 1, chr 224;
844 } else {
845 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
793 } 846 }
794 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
795 847
796 for my $num (grep $_, @$cell[1,2,3]) { 848 for my $num (grep $_, @$cell[1,2,3]) {
797 my $tex = $::CONN->{face}[$num]{texture} || next; 849 my $tex = $::CONN->{face}[$num]{texture} || next;
798 850
799 glBindTexture GL_TEXTURE_2D, $tex->{name};
800
801 my $w = $tex->{width}; 851 my $w = $tex->{width};
802 my $h = $tex->{height}; 852 my $h = $tex->{height};
803 853
804 my $px = ($x + 1) * 32 - $w; 854 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
805 my $py = ($y + 1) * 32 - $h;
806
807 glBegin GL_QUADS;
808 glTexCoord 0, 0; glVertex $px , $py;
809 glTexCoord 0, 1; glVertex $px , $py + $h;
810 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
811 glTexCoord 1, 0; glVertex $px + $w, $py;
812 glEnd;
813 } 855 }
814 } 856 }
815 } 857 }
816 858
817# if (1) { # higher quality darkness 859# if (1) { # higher quality darkness
822# 864#
823# $lighting = $pb->get_pixels; 865# $lighting = $pb->get_pixels;
824# $lighting =~ s/(.)../$1/gs; 866# $lighting =~ s/(.)../$1/gs;
825# } 867# }
826 868
827 $lighting = new Crossfire::Client::Texture 869 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
870 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
871
872 $darkness = new CFClient::Texture
828 width => $sw4, 873 width => $sw4,
829 height => $sh, 874 height => $sh,
830 data => $lighting, 875 data => $darkness,
831 internalformat => GL_ALPHA4, 876 internalformat => GL_ALPHA,
832 format => GL_ALPHA; 877 format => GL_ALPHA;
833 878
834 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 879 glColor 0.45, 0.45, 0.45, 1;
835 glColor 0, 0, 0, 0.75; 880 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
836 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
837 glBindTexture GL_TEXTURE_2D, $lighting->{name};
838 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
839 glBegin GL_QUADS;
840 glTexCoord 0, 0; glVertex 0 , 0;
841 glTexCoord 0, 1; glVertex 0 , $sh * 32;
842 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
843 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
844 glEnd;
845 881
846 glDisable GL_TEXTURE_2D; 882 glDisable GL_TEXTURE_2D;
847 glDisable GL_BLEND; 883 glDisable GL_BLEND;
848} 884}
849 885
898 } 934 }
899} 935}
900 936
901############################################################################# 937#############################################################################
902 938
903package Crossfire::Client::Widget::Animator; 939package CFClient::Widget::Animator;
904 940
905use SDL::OpenGL; 941use SDL::OpenGL;
906 942
907our @ISA = Crossfire::Client::Widget::Bin::; 943our @ISA = CFClient::Widget::Bin::;
908 944
909sub moveto { 945sub moveto {
910 my ($self, $x, $y) = @_; 946 my ($self, $x, $y) = @_;
911 947
912 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 948 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
913 $self->{speed} = 0.2; 949 $self->{speed} = 0.001;
914 $self->{time} = 1; 950 $self->{time} = 1;
915 951
916 ::animation_start $self; 952 ::animation_start $self;
917} 953}
918 954
933 969
934sub _draw { 970sub _draw {
935 my ($self) = @_; 971 my ($self) = @_;
936 972
937 glPushMatrix; 973 glPushMatrix;
938 glRotate $self->{time} * 10000, 0, 1, 0; 974 glRotate $self->{time} * 1000, 0, 1, 0;
939 $self->{children}[0]->draw; 975 $self->{children}[0]->draw;
940 glPopMatrix; 976 glPopMatrix;
941} 977}
942 978
9431; 979#############################################################################
944 980
981package CFClient::Widget::Toplevel;
982
983our @ISA = CFClient::Widget::Container::;
984
985sub size_request {
986 ($::WIDTH, $::HEIGHT)
987}
988
989sub size_allocate {
990 my ($self, $w, $h) = @_;
991
992 $self->SUPER::size_allocate ($w, $h);
993
994 $_->size_allocate ($_->size_request)
995 for @{$self->{children}};
996}
997
998sub translate {
999 my ($self, $x, $y) = @_;
1000
1001 ($x, $y)
1002}
1003
1004sub update {
1005 my ($self) = @_;
1006
1007 $self->size_allocate ($self->size_request);
1008 ::refresh ();
1009}
1010
1011sub add {
1012 my ($self, $widget) = @_;
1013
1014 $self->SUPER::add ($widget);
1015
1016 $widget->size_allocate ($widget->size_request);
1017}
1018
1019sub draw {
1020 my ($self) = @_;
1021
1022 $self->_draw;
1023}
1024
1025#############################################################################
1026
1027package CFClient::Widget;
1028
1029$TOPLEVEL = new CFClient::Widget::Toplevel;
1030
10311
1032

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines