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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines