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.46 by root, Sun Apr 9 22:21:02 2006 UTC

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
10use Crossfire::Client;
9 11
10our $FOCUS; # the widget with current focus 12our $FOCUS; # the widget with current focus
11 13
12# class methods for events 14# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
37 Carp::confess "size_request is abtract"; 39 Carp::confess "size_request is abtract";
38} 40}
39 41
40sub size_allocate { 42sub size_allocate {
41 my ($self, $w, $h) = @_; 43 my ($self, $w, $h) = @_;
44
42 $self->w ($w); 45 $self->{w} = $w;
43 $self->h ($h); 46 $self->{h} = $h;
44} 47}
45 48
46sub focus_in { 49sub focus_in {
47 my ($widget) = @_; 50 my ($widget) = @_;
48 $FOCUS = $widget; 51 $FOCUS = $widget;
134 my ($self) = @_; 137 my ($self) = @_;
135 138
136 #$self->deactivate; 139 #$self->deactivate;
137} 140}
138 141
142#############################################################################
143
139package Crossfire::Client::Widget::Container; 144package Crossfire::Client::Widget::Container;
140 145
141our @ISA = Crossfire::Client::Widget::; 146our @ISA = Crossfire::Client::Widget::;
142 147
143sub new { 148sub new {
157 162
158 @{$self->{children}} = 163 @{$self->{children}} =
159 sort { $a->{z} <=> $b->{z} } 164 sort { $a->{z} <=> $b->{z} }
160 @{$self->{children}}, $chld; 165 @{$self->{children}}, $chld;
161 166
162 $self->size_allocate ($self->{w}, $self->{h}); 167 $self->size_allocate ($self->{w}, $self->{h})
168 if $self->{w}; #TODO: check for "realised state"
163} 169}
164 170
165sub remove { 171sub remove {
166 my ($self, $widget) = @_; 172 my ($self, $widget) = @_;
167 173
171} 177}
172 178
173sub find_widget { 179sub find_widget {
174 my ($self, $x, $y) = @_; 180 my ($self, $x, $y) = @_;
175 181
182 $x -= $self->{x};
183 $y -= $self->{y};
184
176 my $res; 185 my $res;
177 186
178 for (@{ $self->{children} }) { 187 for (reverse @{ $self->{children} }) {
179 $res = $_->find_widget ($x, $y) 188 $res = $_->find_widget ($x, $y)
180 and return $res; 189 and return $res;
181 } 190 }
182 191
183 () 192 $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} 193}
198 194
199sub _draw { 195sub _draw {
200 my ($self) = @_; 196 my ($self) = @_;
201 197
202 $_->draw for @{$self->{children}}; 198 $_->draw for @{$self->{children}};
203} 199}
204 200
201#############################################################################
202
205package Crossfire::Client::Widget::Bin; 203package Crossfire::Client::Widget::Bin;
206 204
207our @ISA = Crossfire::Client::Widget::Container::; 205our @ISA = Crossfire::Client::Widget::Container::;
208 206
209sub get { $_[0]->{children}[0] } 207sub child { $_[0]->{children}[0] }
210 208
211sub size_request { 209sub size_request {
212 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 210 $_[0]{children}[0]->size_request if $_[0]{children}[0];
213} 211}
214 212
215sub size_allocate { 213sub size_allocate {
216 my ($self, $w, $h) = @_; 214 my ($self, $w, $h) = @_;
215
217 $self->SUPER::size_allocate ($w, $h); 216 $self->SUPER::size_allocate ($w, $h);
218 $self->{children}[0]->size_allocate ($w, $h) 217 $self->{children}[0]->size_allocate ($w, $h)
219 if $self->{children}[0] 218 if $self->{children}[0]
220} 219}
221 220
221#############################################################################
222
222package Crossfire::Client::Widget::Toplevel; 223package Crossfire::Client::Widget::Toplevel;
223 224
224our @ISA = Crossfire::Client::Widget::Container::; 225our @ISA = Crossfire::Client::Widget::Container::;
225 226
226sub update { 227sub update {
227 my ($self) = @_; 228 my ($self) = @_;
228 229
229 ::refresh (); 230 ::refresh ();
230} 231}
231 232
233sub add {
234 my ($self, $widget) = @_;
235
236 $self->SUPER::add ($widget);
237
238 $widget->size_allocate ($widget->size_request);
239}
240
241#############################################################################
242
232package Crossfire::Client::Widget::Window; 243package Crossfire::Client::Widget::Window;
233 244
234our @ISA = Crossfire::Client::Widget::Bin::; 245our @ISA = Crossfire::Client::Widget::Bin::;
235 246
236use SDL::OpenGL; 247use SDL::OpenGL;
237 248
238sub add { 249sub new {
239 my ($self, $chld) = @_; 250 my ($class, $x, $y, $z, $w, $h) = @_;
240 warn "ADD $chld\n";
241 $self->SUPER::add ($chld);
242 $chld->set_parent ($self);
243}
244 251
245sub remove { 252 my $self = $class->SUPER::new;
246 my ($self) = @_; 253
247 # TODO FIXME: removing a child from a window will crash, see render_chld 254 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
248 # $self->update;
249} 255}
250 256
251sub update { 257sub update {
252 my ($self) = @_; 258 my ($self) = @_;
259
253 $self->render_chld; 260 $self->render_chld;
261 $self->SUPER::update;
254} 262}
255 263
256sub render_chld { 264sub render_chld {
257 my ($self) = @_; 265 my ($self) = @_;
258 my $chld = $self->get;
259 my ($w, $h) = $self->size_request;
260 266
261 require Carp;
262 Carp::cluck "RENDERCHI $w $h";
263 warn "RENDERCHI $w $h\n";
264 $self->{texture} = 267 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 268 Crossfire::Client::Texture->new_from_opengl (
266 $w, $h, sub { $chld->draw } 269 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 270 );
268 $self->{texture}->upload;
269}
270
271sub size_request {
272 my ($self) = @_;
273 ($self->w, $self->h)
274} 271}
275 272
276sub size_allocate { 273sub size_allocate {
277 my ($self, $w, $h) = @_; 274 my ($self, $w, $h) = @_;
278 275
279 $self->w ($w); 276 $self->{w} = $w;
280 $self->h ($h); 277 $self->{h} = $h;
278
281 $self->get->size_allocate ($w, $h); 279 $self->child->size_allocate ($w, $h);
282 280
283 $self->update; #TODO: Move this to the size_request event propably? 281 $self->render_chld;
284} 282}
285 283
286sub _draw { 284sub _draw {
287 my ($self) = @_; 285 my ($self) = @_;
288 286
305 303
306 glDisable GL_BLEND; 304 glDisable GL_BLEND;
307 glDisable GL_TEXTURE_2D; 305 glDisable GL_TEXTURE_2D;
308} 306}
309 307
308#############################################################################
309
310package Crossfire::Client::Widget::Frame; 310package Crossfire::Client::Widget::Frame;
311 311
312our @ISA = Crossfire::Client::Widget::Bin::; 312our @ISA = Crossfire::Client::Widget::Bin::;
313 313
314use SDL::OpenGL; 314use SDL::OpenGL;
315 315
316sub size_request { 316sub size_request {
317 my ($self) = @_; 317 my ($self) = @_;
318 my $chld = $self->get 318 my $chld = $self->child
319 or return (0, 0); 319 or return (0, 0);
320 320
321 $chld->move (2, 2); 321 $chld->move (2, 2);
322 322
323 map { $_ + 4 } $chld->size_request; 323 map { $_ + 4 } $chld->size_request;
324} 324}
325 325
326sub size_allocate { 326sub size_allocate {
327 my ($self, $w, $h) = @_; 327 my ($self, $w, $h) = @_;
328 328
329 $self->w ($w); 329 $self->{w} = $w;
330 $self->h ($h); 330 $self->{h} = $h;
331 331
332 $self->get->size_allocate ($w - 4, $h - 4); 332 $self->child->size_allocate ($w - 4, $h - 4);
333 $self->get->move (2, 2); 333 $self->child->move (2, 2);
334} 334}
335 335
336sub _draw { 336sub _draw {
337 my ($self) = @_; 337 my ($self) = @_;
338 338
339 my $chld = $self->get; 339 my $chld = $self->child;
340 340
341 my ($w, $h) = $chld->size_request; 341 my ($w, $h) = $chld->size_request;
342 342
343 glBegin GL_QUADS; 343 glBegin GL_QUADS;
344 glColor 0, 0, 0; 344 glColor 0, 0, 0;
349 glEnd; 349 glEnd;
350 350
351 $chld->draw; 351 $chld->draw;
352} 352}
353 353
354#############################################################################
355
354package Crossfire::Client::Widget::FancyFrame; 356package Crossfire::Client::Widget::FancyFrame;
355 357
356our @ISA = Crossfire::Client::Widget::Frame::; 358our @ISA = Crossfire::Client::Widget::Bin::;
357 359
358use SDL::OpenGL; 360use SDL::OpenGL;
359 361
360sub new { 362my @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 $_ } 363 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/ 364 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 365
371sub size_request { 366sub size_request {
372 my ($self) = @_; 367 my ($self) = @_;
368
373 my ($w, $h) = $self->get->size_request; 369 my ($w, $h) = $self->SUPER::size_request;
374 370
375 $h += $self->{txts}->[1]->{height}; 371 $h += $tex[1]->{height};
376 $h += $self->{txts}->[4]->{height}; 372 $h += $tex[4]->{height};
377 $w += $self->{txts}->[2]->{width}; 373 $w += $tex[2]->{width};
378 $w += $self->{txts}->[3]->{width}; 374 $w += $tex[3]->{width};
379 375
380 ($w, $h) 376 ($w, $h)
381} 377}
382 378
383sub size_allocate { 379sub size_allocate {
384 my ($self, $w, $h) = @_; 380 my ($self, $w, $h) = @_;
385 381
386 $self->w ($w); 382 $self->SUPER::size_allocate ($w, $h);
387 $self->h ($h); 383
388 $h -= $self->{txts}->[1]->{height}; 384 $h -= $tex[1]->{height};
389 $h -= $self->{txts}->[4]->{height}; 385 $h -= $tex[4]->{height};
390 $w -= $self->{txts}->[2]->{width}; 386 $w -= $tex[2]->{width};
391 $w -= $self->{txts}->[3]->{width}; 387 $w -= $tex[3]->{width};
392 388
393 $h = $h < 0 ? 0 : $h; 389 $h = $h < 0 ? 0 : $h;
394 $w = $w < 0 ? 0 : $w; 390 $w = $w < 0 ? 0 : $w;
395 warn "CHILD:$w $h\n"; 391
396 $self->get->size_allocate ($w, $h); 392 $self->child->size_allocate ($w, $h);
397 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 393 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
398} 394}
399 395
400sub _draw { 396sub _draw {
401 my ($self) = @_; 397 my ($self) = @_;
402 398
403 my ($w, $h) = ($self->w, $self->h); 399 my ($w, $h) = ($self->{w}, $self->{h});
404 my ($cw, $ch) = ($self->get->w, $self->get->h); 400 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
405 401
406 glEnable GL_BLEND; 402 glEnable GL_BLEND;
407 glEnable GL_TEXTURE_2D; 403 glEnable GL_TEXTURE_2D;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 404 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 405 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
410 406
411 my $top = $self->{txts}->[1]; 407 my $top = $tex[1];
412 glBindTexture GL_TEXTURE_2D, $top->{name}; 408 glBindTexture GL_TEXTURE_2D, $top->{name};
413 409
414 glBegin GL_QUADS; 410 glBegin GL_QUADS;
415 glTexCoord 0, 0; glVertex 0 , 0; 411 glTexCoord 0, 0; glVertex 0 , 0;
416 glTexCoord 0, 1; glVertex 0 , $top->{height}; 412 glTexCoord 0, 1; glVertex 0 , $top->{height};
417 glTexCoord 1, 1; glVertex $w , $top->{height}; 413 glTexCoord 1, 1; glVertex $w , $top->{height};
418 glTexCoord 1, 0; glVertex $w , 0; 414 glTexCoord 1, 0; glVertex $w , 0;
419 glEnd; 415 glEnd;
420 416
421 my $left = $self->{txts}->[3]; 417 my $left = $tex[3];
422 glBindTexture GL_TEXTURE_2D, $left->{name}; 418 glBindTexture GL_TEXTURE_2D, $left->{name};
423 419
424 glBegin GL_QUADS; 420 glBegin GL_QUADS;
425 glTexCoord 0, 0; glVertex 0 , $top->{height}; 421 glTexCoord 0, 0; glVertex 0 , $top->{height};
426 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 422 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
427 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 423 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
428 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 424 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
429 glEnd; 425 glEnd;
430 426
431 my $right = $self->{txts}->[2]; 427 my $right = $tex[2];
432 glBindTexture GL_TEXTURE_2D, $right->{name}; 428 glBindTexture GL_TEXTURE_2D, $right->{name};
433 429
434 glBegin GL_QUADS; 430 glBegin GL_QUADS;
435 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 431 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
436 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 432 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
437 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 433 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
438 glTexCoord 1, 0; glVertex $w , $top->{height}; 434 glTexCoord 1, 0; glVertex $w , $top->{height};
439 glEnd; 435 glEnd;
440 436
441 my $bottom = $self->{txts}->[4]; 437 my $bottom = $tex[4];
442 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 438 glBindTexture GL_TEXTURE_2D, $bottom->{name};
443 439
444 glBegin GL_QUADS; 440 glBegin GL_QUADS;
445 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 441 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
446 glTexCoord 0, 1; glVertex 0 , $h; 442 glTexCoord 0, 1; glVertex 0 , $h;
447 glTexCoord 1, 1; glVertex $w , $h; 443 glTexCoord 1, 1; glVertex $w , $h;
448 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 444 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
449 glEnd; 445 glEnd;
450 446
451 my $bg = $self->{txts}->[0]; 447 my $bg = $tex[0];
452 glBindTexture GL_TEXTURE_2D, $bg->{name}; 448 glBindTexture GL_TEXTURE_2D, $bg->{name};
453 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 449 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
454 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 450 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
455 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 451 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
456 452
465 glEnd; 461 glEnd;
466 462
467 glDisable GL_BLEND; 463 glDisable GL_BLEND;
468 glDisable GL_TEXTURE_2D; 464 glDisable GL_TEXTURE_2D;
469 465
470 $self->get->draw; 466 $self->child->draw;
471 467
472} 468}
469
470#############################################################################
473 471
474package Crossfire::Client::Widget::Table; 472package Crossfire::Client::Widget::Table;
475 473
476our @ISA = Crossfire::Client::Widget::Bin::; 474our @ISA = Crossfire::Client::Widget::Bin::;
477 475
554 552
555 $y += $self->max_row_height ($yi); 553 $y += $self->max_row_height ($yi);
556 } 554 }
557} 555}
558 556
557#############################################################################
558
559package Crossfire::Client::Widget::VBox; 559package Crossfire::Client::Widget::VBox;
560 560
561our @ISA = Crossfire::Client::Widget::Container::; 561our @ISA = Crossfire::Client::Widget::Container::;
562 562
563use SDL::OpenGL; 563use SDL::OpenGL;
564
565sub size_request {
566 my ($self) = @_;
567
568 my @alloc = map [$_->size_request], @{$self->{children}};
569
570 (
571 (List::Util::max map $_->[0], @alloc),
572 (List::Util::sum map $_->[1], @alloc),
573 )
574}
564 575
565sub size_allocate { 576sub size_allocate {
566 my ($self, $w, $h) = @_; 577 my ($self, $w, $h) = @_;
567 578
568 $self->w ($w); 579 $self->w ($w);
601 $y += $h; 612 $y += $h;
602 } 613 }
603 } 614 }
604} 615}
605 616
606sub _draw { 617#############################################################################
607 my ($self) = @_;
608
609 my ($x, $y);
610 for (@{$self->{children} || []}) {
611 $_->draw;
612 $y += $_->h;
613 }
614}
615 618
616package Crossfire::Client::Widget::Label; 619package Crossfire::Client::Widget::Label;
617 620
618our @ISA = Crossfire::Client::Widget::; 621our @ISA = Crossfire::Client::Widget::;
619 622
676 679
677 glDisable GL_BLEND; 680 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
679} 682}
680 683
684#############################################################################
685
681package Crossfire::Client::Widget::TextEntry; 686package Crossfire::Client::Widget::TextEntry;
682 687
683our @ISA = Crossfire::Client::Widget::Label::; 688our @ISA = Crossfire::Client::Widget::Label::;
684 689
685use SDL; 690use SDL;
702 } elsif ($uni) { 707 } elsif ($uni) {
703 $text .= chr $uni; 708 $text .= chr $uni;
704 } 709 }
705 $self->set_text ($text); 710 $self->set_text ($text);
706} 711}
712
713#############################################################################
707 714
708package Crossfire::Client::Widget::MapWidget; 715package Crossfire::Client::Widget::MapWidget;
709 716
710use strict; 717use strict;
711 718
878 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 885 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
879 $::CONN->send ("command run_stop"); 886 $::CONN->send ("command run_stop");
880 } 887 }
881} 888}
882 889
890#############################################################################
891
883package Crossfire::Client::Widget::Animator; 892package Crossfire::Client::Widget::Animator;
884 893
885use SDL::OpenGL; 894use SDL::OpenGL;
886 895
887our @ISA = Crossfire::Client::Widget::Bin::; 896our @ISA = Crossfire::Client::Widget::Bin::;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines