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.22 by elmex, Sat Apr 8 18:18:09 2006 UTC vs.
Revision 1.31 by elmex, Sun Apr 9 00:43:11 2006 UTC

123use SDL::OpenGL; 123use SDL::OpenGL;
124 124
125sub add { 125sub add {
126 my ($self, $chld) = @_; 126 my ($self, $chld) = @_;
127 $self->SUPER::add ($chld); 127 $self->SUPER::add ($chld);
128 $self->render_chld; 128 $self->render_chld; #TODO: Move this to the size_request event propably?
129} 129}
130 130
131sub render_chld { 131sub render_chld {
132 my ($self) = @_; 132 my ($self) = @_;
133 my $chld = $self->get; 133 my $chld = $self->get;
134 my ($w, $h) = $self->size_request; 134 my ($w, $h) = $self->size_request;
135 135
136 $w = $h = 256;
137
138 $self->{texture} = 136 $self->{texture} =
139 Crossfire::Client::Texture->new_from_opengl ( 137 Crossfire::Client::Texture->new_from_opengl (
140 $w, $h, sub { 138 $w, $h, sub { $chld->draw }
141 glPushMatrix;
142 glLoadIdentity;
143 $chld->_draw;
144 glPopMatrix;
145 }
146 ); 139 );
147 $self->{texture}->upload; 140 $self->{texture}->upload;
148} 141}
149 142
150sub size_request { 143sub size_request {
155} 148}
156 149
157sub _draw { 150sub _draw {
158 my ($self) = @_; 151 my ($self) = @_;
159 152
160 my ($w, $h) = $self->size_request; 153 my ($w, $h) = $self->size_request;#TODO# use width/height of texture
154
161 my $tex = $self->{texture} 155 my $tex = $self->{texture}
162 or return; 156 or return;
163 157
164 glEnable GL_BLEND; 158 glEnable GL_BLEND;
165 glEnable GL_TEXTURE_2D; 159 glEnable GL_TEXTURE_2D;
166 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
167 glBindTexture GL_TEXTURE_2D, $tex->{name}; 161 glBindTexture GL_TEXTURE_2D, $tex->{name};
168 162
169 glColor 1, 1, 1; 163 glColor 1, 0, 1;
170 164
171 glBegin GL_QUADS; 165 glBegin GL_QUADS;
172 glTexCoord 0, 0; glVertex 0 , 0; 166 glTexCoord 0, 0; glVertex 0, 0;
173 glTexCoord 0, 1; glVertex 0 , $h; 167 glTexCoord 0, 1; glVertex 0, $h;
174 glTexCoord 1, 1; glVertex $w , $h; 168 glTexCoord 1, 1; glVertex $w, $h;
175 glTexCoord 1, 0; glVertex $w , 0; 169 glTexCoord 1, 0; glVertex $w, 0;
176 glEnd; 170 glEnd;
177 171
178 glDisable GL_BLEND; 172 glDisable GL_BLEND;
179 glDisable GL_TEXTURE_2D; 173 glDisable GL_TEXTURE_2D;
180} 174}
187 181
188sub size_request { 182sub size_request {
189 my ($self) = @_; 183 my ($self) = @_;
190 my $chld = $self->get 184 my $chld = $self->get
191 or return (0, 0); 185 or return (0, 0);
186
187 $chld->move (2, 2);
188
192 map { $_ + 4 } $chld->size_request; 189 map { $_ + 4 } $chld->size_request;
193} 190}
194 191
195sub _draw { 192sub _draw {
196 my ($self) = @_; 193 my ($self) = @_;
197 194
198 my $chld = $self->get; 195 my $chld = $self->get;
199 196
200 my ($w, $h) = $chld->size_request; 197 my ($w, $h) = $chld->size_request;
201 198
202 glColor 1, 0, 0;
203 glBegin GL_QUADS; 199 glBegin GL_QUADS;
200 glColor 0, 0, 0;
204 glTexCoord 0, 0; glVertex 0 , 0; 201 glTexCoord 0, 0; glVertex 0 , 0;
205 glTexCoord 0, 1; glVertex 0 , $h + 4; 202 glTexCoord 0, 1; glVertex 0 , $h + 4;
206 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 203 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
207 glTexCoord 1, 0; glVertex $w + 4 , 0; 204 glTexCoord 1, 0; glVertex $w + 4 , 0;
208 glEnd; 205 glEnd;
209 206
210 glPushMatrix;
211 glTranslate (2, 2, 0);
212 $chld->_draw; 207 $chld->draw;
213 glPopMatrix;
214} 208}
209
210package Crossfire::Client::Widget::FancyFrame;
211
212our @ISA = Crossfire::Client::Widget::Frame::;
213
214use SDL::OpenGL;
215
216#TODO: implement themed frame
215 217
216package Crossfire::Client::Widget::Table; 218package Crossfire::Client::Widget::Table;
217 219
218our @ISA = Crossfire::Client::Widget::Container::; 220our @ISA = Crossfire::Client::Widget::Container::;
219 221
279 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 281 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
280 my $x = 0; 282 my $x = 0;
281 283
282 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 284 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
283 285
284 glPushMatrix;
285 glTranslate ($x, $y, 0);
286 my $c = $self->{childs}->[$yi]->[$xi]; 286 my $c = $self->{childs}->[$yi]->[$xi];
287 if ($c) {
288 $c->move ($x, $y, 0); #TODO: Move to size_request
287 $c->_draw if $c; 289 $c->draw if $c;
288 glPopMatrix; 290 }
289 291
290 $x += $self->max_col_width ($xi); 292 $x += $self->max_col_width ($xi);
291 } 293 }
292 294
293 $y += $self->max_row_height ($yi); 295 $y += $self->max_row_height ($yi);
321sub _draw { 323sub _draw {
322 my ($self) = @_; 324 my ($self) = @_;
323 325
324 my ($x, $y); 326 my ($x, $y);
325 for (@{$self->{childs} || []}) { 327 for (@{$self->{childs} || []}) {
326 glPushMatrix; 328 $_->move (0, $y, 0); #TODO: move to size_request
327 glTranslate (0, $y, 0);
328 $_->_draw; 329 $_->draw;
329 glPopMatrix;
330 my ($w, $h) = $_->size_request; 330 my ($w, $h) = $_->size_request;
331 $y += $h; 331 $y += $h;
332 } 332 }
333} 333}
334 334
337our @ISA = Crossfire::Client::Widget::; 337our @ISA = Crossfire::Client::Widget::;
338 338
339use SDL::OpenGL; 339use SDL::OpenGL;
340 340
341sub new { 341sub new {
342 my ($class, $x, $y, $z, $ttf, $text) = @_; 342 my ($class, $x, $y, $z, $height, $text) = @_;
343 343
344 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 344 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
345 345
346 $self->set_text ($text); 346 $self->set_text ($text);
347 347
348 $self 348 $self
349} 349}
350 350
351sub set_text { 351sub set_text {
352 my ($self, $text) = @_; 352 my ($self, $text) = @_;
353
354 $self->{text} = $text;
355
353 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 356 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
354} 357}
355 358
356sub get_text { 359sub get_text {
357 my ($self, $text) = @_; 360 my ($self, $text) = @_;
361
358 $self->{text} 362 $self->{text}
359} 363}
360 364
361sub size_request { 365sub size_request {
362 my ($self) = @_; 366 my ($self) = @_;
372 376
373 my $tex = $self->{texture}; 377 my $tex = $self->{texture};
374 378
375 glEnable GL_BLEND; 379 glEnable GL_BLEND;
376 glEnable GL_TEXTURE_2D; 380 glEnable GL_TEXTURE_2D;
381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
377 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 382 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
378 glBindTexture GL_TEXTURE_2D, $tex->{name}; 383 glBindTexture GL_TEXTURE_2D, $tex->{name};
379 384
380 glColor 1, 1, 1; 385 glColor 1, 1, 1, 0.8;
381 386
382 glBegin GL_QUADS; 387 glBegin GL_QUADS;
383 glTexCoord 0, 0; glVertex 0 , 0; 388 glTexCoord 0, 0; glVertex 0 , 0;
384 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 389 glTexCoord 0, 1; glVertex 0 , $tex->{height};
385 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 390 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
388 393
389 glDisable GL_BLEND; 394 glDisable GL_BLEND;
390 glDisable GL_TEXTURE_2D; 395 glDisable GL_TEXTURE_2D;
391} 396}
392 397
398package Crossfire::Client::Widget::TextEntry;
399
400our @ISA = Crossfire::Client::Widget::Label::;
401
402use SDL;
403use SDL::OpenGL;
404
405sub key_down {
406 my ($self, $ev) = @_;
407
408 my $mod = $ev->key_mod;
409 my $sym = $ev->key_sym;
410
411 $ev->set_unicode (1);
412 my $uni = $ev->key_unicode;
413
414 my $text = $self->get_text;
415
416 if ($sym == SDLK_BACKSPACE) {
417 substr $text, -1, 1, '';
418
419 } elsif ($uni) {
420 $text .= chr $uni;
421 }
422 $self->set_text ($text);
423}
424
425
426# XXX: TextView isn't neccessary with pango multiline text rendering
393package Crossfire::Client::Widget::TextView; 427package Crossfire::Client::Widget::TextView;
394 428
395use strict; 429use strict;
396 430
397our @ISA = qw/Crossfire::Client::Widget/; 431our @ISA = qw/Crossfire::Client::Widget/;
398 432
399use SDL::OpenGL; 433use SDL::OpenGL;
400use SDL::OpenGL::Constants; 434use SDL::OpenGL::Constants;
435
436sub new {
437 my ($class, $text, $h) = @_;
438 my $self = $class->SUPER::new ();
439
440 $self->{txt_height} = $h;
441 @{$self->{lines}} = split /\r?\n/, $text;
442
443 for (split /\r?\n/, $text) {
444 $self->add_line ($_);
445 }
446 $self
447}
448
449#sub render_lines {
450# my ($self) = @_;
451#
452# $self->{txt_lines} = [];
453#
454# for (@{$self->{lines}}) {
455# push @{$self->{txt_lines}},
456# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
457# }
458#}
401 459
402sub add_line { 460sub add_line {
403 my ($self, $line) = @_; 461 my ($self, $line) = @_;
404 push @{$self->{lines}}, $line; 462 push @{$self->{lines}}, $line;
405}
406 463
464 push @{$self->{txt_lines}},
465 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
466}
467
468sub size_request {
469 my ($self) = @_;
470
471 my $w = 0;
472 my $h = 0;
473
474 for (@{$self->{txt_lines}}) {
475 if ($w < $_->{width}) { $w = $_->{width} }
476 $h += $_->{height};
477 }
478
479 return ($w, $h);
480}
481
407sub _draw { 482sub draw_line {
408 my ($self) = @_; 483 my ($self, $tex, $y) = @_;
409 484
485 glBindTexture GL_TEXTURE_2D, $tex->{name};
486
487 glColor 1, 0, 1;
488
489 glBegin GL_QUADS;
490 glTexCoord 0, 0; glVertex 0 , $y;
491 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height};
492 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height};
493 glTexCoord 1, 0; glVertex $tex->{width}, $y;
494 glEnd;
495}
496
497sub _draw {
498 my ($self) = @_;
499
500 glEnable GL_BLEND;
501 glEnable GL_TEXTURE_2D;
502 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
503
504 my $l = 0;
505 for (@{$self->{txt_lines}}) {
506 $self->draw_line ($_, $l);
507 $l += $_->{height};
508 }
509
510 glDisable GL_BLEND;
511 glDisable GL_TEXTURE_2D;
410} 512}
411 513
412package Crossfire::Client::Widget::MapWidget; 514package Crossfire::Client::Widget::MapWidget;
413 515
414use strict; 516use strict;
415 517
416our @ISA = qw/Crossfire::Client::Widget/; 518use List::Util qw(min max);
417 519
418use SDL; 520use SDL;
419use SDL::OpenGL; 521use SDL::OpenGL;
420use SDL::OpenGL::Constants; 522use SDL::OpenGL::Constants;
523
524our @ISA = Crossfire::Client::Widget::;
421 525
422sub key_down { 526sub key_down {
423 print "MAPKEYDOWN\n"; 527 print "MAPKEYDOWN\n";
424} 528}
425 529
426sub key_up { 530sub key_up {
427} 531}
428 532
429sub _draw { 533sub _draw {
430 my ($self) = @_; 534 my ($self) = @_;
535
536 my $mx = $::CONN->{mapx};
537 my $my = $::CONN->{mapy};
538
539 my $map = $::CONN->{map};
540
541 my ($xofs, $yofs);
542
543 my $sw = 1 + int $::WIDTH / 32;
544 my $sh = 1 + int $::HEIGHT / 32;
545
546 if ($::CONN->{mapw} > $sw) {
547 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
548 } else {
549 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
550 }
551
552 if ($::CONN->{maph} > $sh) {
553 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
554 } else {
555 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
556 }
431 557
432 glEnable GL_TEXTURE_2D; 558 glEnable GL_TEXTURE_2D;
433 glEnable GL_BLEND; 559 glEnable GL_BLEND;
434 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
435 561
436 my $map = $::CONN->{map}; 562 for my $x (0 .. $sw - 1) {
563 for my $y (0 .. $sh - 1) {
437 564
438 for my $x (0 .. int $::WIDTH / 32) { 565 my $cell = $map->[$x + $xofs][$y + $yofs]
439 for my $y (0 .. int $::HEIGHT / 32) {
440
441 my $cell = $map->[$x + $::CONN->{mapx}]
442 [$y + $::CONN->{mapy}]
443 or next; 566 or next;
444 567
445 my $darkness = $cell->[0] * (1 / 255); 568 my $darkness = $cell->[0] * (1 / 255);
446 if ($darkness < 0) { 569 if ($darkness < 0) {
447 $darkness = 0.5; 570 glColor 0.3, 0.3, 0.3;
571 } else {
572 glColor $darkness, $darkness, $darkness;
448 } 573 }
449 glColor $darkness, $darkness, $darkness;
450 574
451 for my $num (grep $_, @$cell[1,2,3]) { 575 for my $num (grep $_, @$cell[1,2,3]) {
452 my $tex = $::CONN->{face}[$num]{texture} || next; 576 my $tex = $::CONN->{face}[$num]{texture} || next;
453 577
454 glBindTexture GL_TEXTURE_2D, $tex->{name}; 578 glBindTexture GL_TEXTURE_2D, $tex->{name};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines