… | |
… | |
39 | $self->{x} = $x; |
39 | $self->{x} = $x; |
40 | $self->{y} = $y; |
40 | $self->{y} = $y; |
41 | $self->{z} = $z if defined $z; |
41 | $self->{z} = $z if defined $z; |
42 | } |
42 | } |
43 | |
43 | |
|
|
44 | sub needs_redraw { |
|
|
45 | 0 |
|
|
46 | } |
|
|
47 | |
44 | sub size_request { |
48 | sub size_request { |
45 | die "size_request is abtract"; |
49 | die "size_request is abtract"; |
46 | } |
50 | } |
47 | |
51 | |
48 | sub focus_in { |
52 | sub focus_in { |
… | |
… | |
110 | |
114 | |
111 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
115 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
112 | |
116 | |
113 | sub _draw { die "Containers can't be drawn!" } |
117 | sub _draw { die "Containers can't be drawn!" } |
114 | |
118 | |
115 | package Crossfire::Client::Widget::Frame; |
119 | package Crossfire::Client::Widget::Window; |
116 | |
120 | |
117 | our @ISA = Crossfire::Client::Widget::Container::; |
121 | our @ISA = Crossfire::Client::Widget::Container::; |
118 | |
122 | |
119 | use SDL::OpenGL; |
123 | use SDL::OpenGL; |
|
|
124 | |
|
|
125 | sub add { |
|
|
126 | my ($self, $chld) = @_; |
|
|
127 | $self->SUPER::add ($chld); |
|
|
128 | $self->render_chld; #TODO: Move this to the size_request event propably? |
|
|
129 | } |
|
|
130 | |
|
|
131 | sub render_chld { |
|
|
132 | my ($self) = @_; |
|
|
133 | my $chld = $self->get; |
|
|
134 | my ($w, $h) = $self->size_request; |
|
|
135 | |
|
|
136 | $self->{texture} = |
|
|
137 | Crossfire::Client::Texture->new_from_opengl ( |
|
|
138 | $w, $h, sub { $chld->draw } |
|
|
139 | ); |
|
|
140 | $self->{texture}->upload; |
|
|
141 | } |
120 | |
142 | |
121 | sub size_request { |
143 | sub size_request { |
122 | my ($self) = @_; |
144 | my ($self) = @_; |
123 | my $chld = $self->get |
145 | my $chld = $self->get |
124 | or return (0, 0); |
146 | or return (0, 0); |
|
|
147 | $chld->size_request |
|
|
148 | } |
|
|
149 | |
|
|
150 | sub _draw { |
|
|
151 | my ($self) = @_; |
|
|
152 | |
|
|
153 | my ($w, $h) = $self->size_request;#TODO# use width/height of texture |
|
|
154 | |
|
|
155 | my $tex = $self->{texture} |
|
|
156 | or return; |
|
|
157 | |
|
|
158 | glEnable GL_BLEND; |
|
|
159 | glEnable GL_TEXTURE_2D; |
|
|
160 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
161 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
162 | |
|
|
163 | glColor 1, 0, 1; |
|
|
164 | |
|
|
165 | glBegin GL_QUADS; |
|
|
166 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
167 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
168 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
169 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
170 | glEnd; |
|
|
171 | |
|
|
172 | glDisable GL_BLEND; |
|
|
173 | glDisable GL_TEXTURE_2D; |
|
|
174 | } |
|
|
175 | |
|
|
176 | package Crossfire::Client::Widget::Frame; |
|
|
177 | |
|
|
178 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
179 | |
|
|
180 | use SDL::OpenGL; |
|
|
181 | |
|
|
182 | sub size_request { |
|
|
183 | my ($self) = @_; |
|
|
184 | my $chld = $self->get |
|
|
185 | or return (0, 0); |
|
|
186 | |
|
|
187 | $chld->move (2, 2); |
|
|
188 | |
125 | map { $_ + 4 } $chld->size_request; |
189 | map { $_ + 4 } $chld->size_request; |
126 | } |
190 | } |
127 | |
191 | |
128 | sub _draw { |
192 | sub _draw { |
129 | my ($self) = @_; |
193 | my ($self) = @_; |
130 | |
194 | |
131 | my $chld = $self->get; |
195 | my $chld = $self->get; |
132 | |
196 | |
133 | my ($w, $h) = $chld->size_request; |
197 | my ($w, $h) = $chld->size_request; |
134 | |
198 | |
135 | glColor 1, 0, 0; |
|
|
136 | glBegin GL_QUADS; |
199 | glBegin GL_QUADS; |
|
|
200 | glColor 0, 0, 0; |
137 | glTexCoord 0, 0; glVertex 0 , 0; |
201 | glTexCoord 0, 0; glVertex 0 , 0; |
138 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
202 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
139 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
203 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
140 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
204 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
141 | glEnd; |
205 | glEnd; |
142 | |
206 | |
143 | glPushMatrix; |
|
|
144 | glTranslate (2, 2, 0); |
|
|
145 | $chld->_draw; |
207 | $chld->draw; |
146 | glPopMatrix; |
|
|
147 | } |
208 | } |
|
|
209 | |
|
|
210 | package Crossfire::Client::Widget::FancyFrame; |
|
|
211 | |
|
|
212 | our @ISA = Crossfire::Client::Widget::Frame::; |
|
|
213 | |
|
|
214 | use SDL::OpenGL; |
|
|
215 | |
|
|
216 | #TODO: implement themed frame |
148 | |
217 | |
149 | package Crossfire::Client::Widget::Table; |
218 | package Crossfire::Client::Widget::Table; |
150 | |
219 | |
151 | our @ISA = Crossfire::Client::Widget::Container::; |
220 | our @ISA = Crossfire::Client::Widget::Container::; |
152 | |
221 | |
… | |
… | |
212 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
281 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
213 | my $x = 0; |
282 | my $x = 0; |
214 | |
283 | |
215 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
284 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
216 | |
285 | |
217 | glPushMatrix; |
|
|
218 | glTranslate ($x, $y, 0); |
|
|
219 | 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 |
220 | $c->_draw if $c; |
289 | $c->draw if $c; |
221 | glPopMatrix; |
290 | } |
222 | |
291 | |
223 | $x += $self->max_col_width ($xi); |
292 | $x += $self->max_col_width ($xi); |
224 | } |
293 | } |
225 | |
294 | |
226 | $y += $self->max_row_height ($yi); |
295 | $y += $self->max_row_height ($yi); |
… | |
… | |
254 | sub _draw { |
323 | sub _draw { |
255 | my ($self) = @_; |
324 | my ($self) = @_; |
256 | |
325 | |
257 | my ($x, $y); |
326 | my ($x, $y); |
258 | for (@{$self->{childs} || []}) { |
327 | for (@{$self->{childs} || []}) { |
259 | glPushMatrix; |
328 | $_->move (0, $y, 0); #TODO: move to size_request |
260 | glTranslate (0, $y, 0); |
|
|
261 | $_->_draw; |
329 | $_->draw; |
262 | glPopMatrix; |
|
|
263 | my ($w, $h) = $_->size_request; |
330 | my ($w, $h) = $_->size_request; |
264 | $y += $h; |
331 | $y += $h; |
265 | } |
332 | } |
266 | } |
333 | } |
267 | |
334 | |
… | |
… | |
270 | our @ISA = Crossfire::Client::Widget::; |
337 | our @ISA = Crossfire::Client::Widget::; |
271 | |
338 | |
272 | use SDL::OpenGL; |
339 | use SDL::OpenGL; |
273 | |
340 | |
274 | sub new { |
341 | sub new { |
275 | my ($class, $x, $y, $z, $ttf, $text) = @_; |
342 | my ($class, $x, $y, $z, $height, $text) = @_; |
276 | |
343 | |
277 | 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); |
278 | |
345 | |
279 | $self->set_text ($text); |
346 | $self->set_text ($text); |
280 | |
347 | |
281 | $self |
348 | $self |
282 | } |
349 | } |
283 | |
350 | |
284 | sub set_text { |
351 | sub set_text { |
285 | my ($self, $text) = @_; |
352 | my ($self, $text) = @_; |
|
|
353 | |
|
|
354 | $self->{text} = $text; |
|
|
355 | |
286 | $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}; |
287 | } |
357 | } |
288 | |
358 | |
289 | sub get_text { |
359 | sub get_text { |
290 | my ($self, $text) = @_; |
360 | my ($self, $text) = @_; |
|
|
361 | |
291 | $self->{text} |
362 | $self->{text} |
292 | } |
363 | } |
293 | |
364 | |
294 | sub size_request { |
365 | sub size_request { |
295 | my ($self) = @_; |
366 | my ($self) = @_; |
… | |
… | |
305 | |
376 | |
306 | my $tex = $self->{texture}; |
377 | my $tex = $self->{texture}; |
307 | |
378 | |
308 | glEnable GL_BLEND; |
379 | glEnable GL_BLEND; |
309 | glEnable GL_TEXTURE_2D; |
380 | glEnable GL_TEXTURE_2D; |
|
|
381 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
310 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
382 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
311 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
383 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
312 | |
384 | |
313 | glColor 1, 1, 1; |
385 | glColor 1, 1, 1, 0.8; |
314 | |
386 | |
315 | glBegin GL_QUADS; |
387 | glBegin GL_QUADS; |
316 | glTexCoord 0, 0; glVertex 0 , 0; |
388 | glTexCoord 0, 0; glVertex 0 , 0; |
317 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
389 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
318 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
390 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
… | |
… | |
321 | |
393 | |
322 | glDisable GL_BLEND; |
394 | glDisable GL_BLEND; |
323 | glDisable GL_TEXTURE_2D; |
395 | glDisable GL_TEXTURE_2D; |
324 | } |
396 | } |
325 | |
397 | |
|
|
398 | package Crossfire::Client::Widget::TextEntry; |
|
|
399 | |
|
|
400 | our @ISA = Crossfire::Client::Widget::Label::; |
|
|
401 | |
|
|
402 | use SDL; |
|
|
403 | use SDL::OpenGL; |
|
|
404 | |
|
|
405 | sub 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 |
326 | package Crossfire::Client::Widget::TextView; |
427 | package Crossfire::Client::Widget::TextView; |
327 | |
428 | |
328 | use strict; |
429 | use strict; |
329 | |
430 | |
330 | our @ISA = qw/Crossfire::Client::Widget/; |
431 | our @ISA = qw/Crossfire::Client::Widget/; |
331 | |
432 | |
332 | use SDL::OpenGL; |
433 | use SDL::OpenGL; |
333 | use SDL::OpenGL::Constants; |
434 | use SDL::OpenGL::Constants; |
|
|
435 | |
|
|
436 | sub 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 | #} |
334 | |
459 | |
335 | sub add_line { |
460 | sub add_line { |
336 | my ($self, $line) = @_; |
461 | my ($self, $line) = @_; |
337 | push @{$self->{lines}}, $line; |
462 | push @{$self->{lines}}, $line; |
338 | } |
|
|
339 | |
463 | |
|
|
464 | push @{$self->{txt_lines}}, |
|
|
465 | new_from_text Crossfire::Client::Texture $line, $self->{txt_height}; |
|
|
466 | } |
|
|
467 | |
|
|
468 | sub 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 | |
340 | sub _draw { |
482 | sub draw_line { |
341 | my ($self) = @_; |
483 | my ($self, $tex, $y) = @_; |
342 | |
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 | |
|
|
497 | sub _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; |
343 | } |
512 | } |
344 | |
513 | |
345 | package Crossfire::Client::Widget::MapWidget; |
514 | package Crossfire::Client::Widget::MapWidget; |
346 | |
515 | |
347 | use strict; |
516 | use strict; |
348 | |
517 | |
349 | our @ISA = qw/Crossfire::Client::Widget/; |
518 | use List::Util qw(min max); |
350 | |
519 | |
351 | use SDL; |
520 | use SDL; |
352 | use SDL::OpenGL; |
521 | use SDL::OpenGL; |
353 | use SDL::OpenGL::Constants; |
522 | use SDL::OpenGL::Constants; |
|
|
523 | |
|
|
524 | our @ISA = Crossfire::Client::Widget::; |
354 | |
525 | |
355 | sub key_down { |
526 | sub key_down { |
356 | print "MAPKEYDOWN\n"; |
527 | print "MAPKEYDOWN\n"; |
357 | } |
528 | } |
358 | |
529 | |
359 | sub key_up { |
530 | sub key_up { |
360 | } |
531 | } |
361 | |
532 | |
362 | sub _draw { |
533 | sub _draw { |
|
|
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 | } |
|
|
557 | |
363 | glEnable GL_TEXTURE_2D; |
558 | glEnable GL_TEXTURE_2D; |
364 | glEnable GL_BLEND; |
559 | glEnable GL_BLEND; |
365 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
560 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
366 | |
561 | |
367 | my $map = $::CONN->{map}; |
562 | for my $x (0 .. $sw - 1) { |
368 | |
|
|
369 | for my $x (0 .. $::CONN->{mapw} - 1) { |
|
|
370 | for my $y (0 .. $::CONN->{maph} - 1) { |
563 | for my $y (0 .. $sh - 1) { |
371 | |
564 | |
372 | my $cell = $map->[$x][$y] |
565 | my $cell = $map->[$x + $xofs][$y + $yofs] |
373 | or next; |
566 | or next; |
374 | |
567 | |
375 | my $darkness = $cell->[3] * (1 / 255); |
568 | my $darkness = $cell->[0] * (1 / 255); |
|
|
569 | if ($darkness < 0) { |
|
|
570 | glColor 0.3, 0.3, 0.3; |
|
|
571 | } else { |
376 | glColor $darkness, $darkness, $darkness; |
572 | glColor $darkness, $darkness, $darkness; |
|
|
573 | } |
377 | |
574 | |
378 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
575 | for my $num (grep $_, @$cell[1,2,3]) { |
379 | my $tex = $::CONN->{face}[$num]{texture} || next; |
576 | my $tex = $::CONN->{face}[$num]{texture} || next; |
380 | |
577 | |
381 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
578 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
382 | |
579 | |
383 | my $w = $tex->{width}; |
580 | my $w = $tex->{width}; |