… | |
… | |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | our $FOCUS; # the widget with current focus |
10 | our $FOCUS; # the widget with current focus |
11 | our @ACTIVE_WIDGETS; |
11 | #our @ACTIVE_WIDGETS; |
12 | |
12 | |
13 | # class methods for events |
13 | # class methods for events |
14 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
14 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
15 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
15 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
16 | sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } |
16 | sub feed_sdl_button_down_event { } |
17 | sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } |
17 | sub feed_sdl_button_up_event { } |
18 | |
18 | |
19 | sub new { |
19 | sub new { |
20 | my $class = shift; |
20 | my $class = shift; |
21 | |
21 | |
22 | bless { @_ }, $class |
22 | bless { @_ }, $class |
23 | } |
23 | } |
24 | |
24 | |
25 | sub activate { |
25 | #sub activate { |
26 | push @ACTIVE_WIDGETS, $_[0]; |
26 | # push @ACTIVE_WIDGETS, $_[0]; |
27 | Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; |
27 | # Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; |
28 | } |
28 | #} |
29 | |
29 | |
30 | sub deactivate { |
30 | #sub deactivate { |
31 | @ACTIVE_WIDGETS = |
31 | # @ACTIVE_WIDGETS = |
32 | sort { $a->{z} <=> $b->{z} } |
32 | # sort { $a->{z} <=> $b->{z} } |
33 | grep { $_ && $_ != $_[0] } |
33 | # grep { $_ && $_ != $_[0] } |
34 | @ACTIVE_WIDGETS; |
34 | # @ACTIVE_WIDGETS; |
35 | } |
35 | #} |
36 | |
36 | |
37 | sub move { |
37 | sub move { |
38 | my ($self, $x, $y, $z) = @_; |
38 | my ($self, $x, $y, $z) = @_; |
39 | $self->{x} = $x; |
39 | $self->{x} = $x; |
40 | $self->{y} = $y; |
40 | $self->{y} = $y; |
… | |
… | |
92 | sub _draw { |
92 | sub _draw { |
93 | my ($widget) = @_; |
93 | my ($widget) = @_; |
94 | } |
94 | } |
95 | |
95 | |
96 | sub bbox { |
96 | sub bbox { |
97 | my ($widget) = @_; |
97 | my ($self) = @_; |
|
|
98 | my ($w, $h) = $self->size_request; |
|
|
99 | ( |
|
|
100 | $self->{x}, |
|
|
101 | $self->{y}, |
|
|
102 | $self->{x} = $w, |
|
|
103 | $self->{y} = $h |
|
|
104 | ) |
|
|
105 | } |
|
|
106 | |
|
|
107 | sub del_parent { $_[0]->{parent} = undef } |
|
|
108 | |
|
|
109 | sub set_parent { |
|
|
110 | my ($self, $par) = @_; |
|
|
111 | |
|
|
112 | $self->{parent} = $par; |
|
|
113 | Scalar::Util::weaken $self->{parent}; |
|
|
114 | } |
|
|
115 | |
|
|
116 | sub get_parent { |
|
|
117 | $_[0]->{parent} |
|
|
118 | } |
|
|
119 | |
|
|
120 | sub update { |
|
|
121 | my ($self) = @_; |
|
|
122 | |
|
|
123 | $self->{parent}->update |
|
|
124 | if $self->{parent}; |
98 | } |
125 | } |
99 | |
126 | |
100 | sub DESTROY { |
127 | sub DESTROY { |
101 | my ($self) = @_; |
128 | my ($self) = @_; |
102 | |
129 | |
103 | $self->deactivate; |
130 | #$self->deactivate; |
104 | } |
131 | } |
105 | |
132 | |
106 | package Crossfire::Client::Widget::Container; |
133 | package Crossfire::Client::Widget::Container; |
107 | |
134 | |
108 | our @ISA = Crossfire::Client::Widget::; |
135 | our @ISA = Crossfire::Client::Widget::; |
109 | |
136 | |
110 | use SDL::OpenGL; |
137 | use SDL::OpenGL; |
111 | |
138 | |
112 | sub add { $_[0]->{child} = $_[1] } |
139 | sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } |
113 | sub get { $_[0]->{child} } |
140 | sub get { $_[0]->{child} } |
|
|
141 | sub remove { |
|
|
142 | my ($self, $chld) = @_; |
|
|
143 | delete $self->{child} |
|
|
144 | if $self->{child} == $chld; |
|
|
145 | } |
114 | |
146 | |
115 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
147 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
116 | |
148 | |
117 | sub _draw { die "Containers can't be drawn!" } |
149 | sub _draw { die "Containers can't be drawn!" } |
|
|
150 | |
|
|
151 | package Crossfire::Client::Widget::Toplevel; |
|
|
152 | |
|
|
153 | our @ISA = Crossfire::Client::Widget::; |
|
|
154 | |
|
|
155 | use SDL::OpenGL; |
|
|
156 | |
|
|
157 | sub add { |
|
|
158 | my ($self, $chld) = @_; |
|
|
159 | |
|
|
160 | push @{$self->{childs}}, $chld; |
|
|
161 | @{$self->{childs}} = |
|
|
162 | sort { $a->{z} <=> $b->{z} } |
|
|
163 | @{$self->{childs}}; |
|
|
164 | |
|
|
165 | $chld->set_parent ($self); |
|
|
166 | } |
|
|
167 | |
|
|
168 | sub remove { |
|
|
169 | my ($self, $chld) = @_; |
|
|
170 | @{$self->{childs}} = |
|
|
171 | sort { $a->{z} <=> $b->{z} } |
|
|
172 | grep { $_ && $_ != $_[0] } |
|
|
173 | @{$self->{childs}} |
|
|
174 | } |
|
|
175 | |
|
|
176 | sub update { |
|
|
177 | my ($self) = @_; |
|
|
178 | ::refresh (); |
|
|
179 | } |
|
|
180 | |
|
|
181 | sub _draw { |
|
|
182 | my ($self) = @_; |
|
|
183 | |
|
|
184 | $_->draw for @{$self->{childs}}; |
|
|
185 | } |
118 | |
186 | |
119 | package Crossfire::Client::Widget::Window; |
187 | package Crossfire::Client::Widget::Window; |
120 | |
188 | |
121 | our @ISA = Crossfire::Client::Widget::Container::; |
189 | our @ISA = Crossfire::Client::Widget::Container::; |
122 | |
190 | |
123 | use SDL::OpenGL; |
191 | use SDL::OpenGL; |
124 | |
192 | |
125 | sub add { |
193 | sub add { |
126 | my ($self, $chld) = @_; |
194 | my ($self, $chld) = @_; |
127 | $self->SUPER::add ($chld); |
195 | $self->SUPER::add ($chld); |
|
|
196 | $chld->set_parent ($self); |
128 | $self->render_chld; #TODO: Move this to the size_request event propably? |
197 | $self->update; #TODO: Move this to the size_request event propably? |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub remove { |
|
|
201 | my ($self) = @_; |
|
|
202 | # TODO FIXME: removing a child from a window will crash, see render_chld |
|
|
203 | $self->update; |
|
|
204 | } |
|
|
205 | |
|
|
206 | sub update { |
|
|
207 | my ($self) = @_; |
|
|
208 | $self->render_chld; |
129 | } |
209 | } |
130 | |
210 | |
131 | sub render_chld { |
211 | sub render_chld { |
132 | my ($self) = @_; |
212 | my ($self) = @_; |
133 | my $chld = $self->get; |
213 | my $chld = $self->get; |
… | |
… | |
148 | } |
228 | } |
149 | |
229 | |
150 | sub _draw { |
230 | sub _draw { |
151 | my ($self) = @_; |
231 | my ($self) = @_; |
152 | |
232 | |
153 | my ($w, $h) = $self->size_request;#TODO# use widht/height of texture |
233 | my ($w, $h) = $self->size_request;#TODO# use width/height of texture |
|
|
234 | |
154 | my $tex = $self->{texture} |
235 | my $tex = $self->{texture} |
155 | or return; |
236 | or return; |
156 | |
237 | |
157 | glEnable GL_BLEND; |
238 | glEnable GL_BLEND; |
158 | glEnable GL_TEXTURE_2D; |
239 | glEnable GL_TEXTURE_2D; |
159 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
240 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
160 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
241 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
161 | |
242 | |
162 | glColor 1, 1, 1; |
243 | glColor 1, 0, 1; |
163 | |
244 | |
164 | glBegin GL_QUADS; |
245 | glBegin GL_QUADS; |
165 | glTexCoord 0, 0; glVertex 0, 0; |
246 | glTexCoord 0, 0; glVertex 0, 0; |
166 | glTexCoord 0, 1; glVertex 0, $h; |
247 | glTexCoord 0, 1; glVertex 0, $h; |
167 | glTexCoord 1, 1; glVertex $w, $h; |
248 | glTexCoord 1, 1; glVertex $w, $h; |
… | |
… | |
180 | |
261 | |
181 | sub size_request { |
262 | sub size_request { |
182 | my ($self) = @_; |
263 | my ($self) = @_; |
183 | my $chld = $self->get |
264 | my $chld = $self->get |
184 | or return (0, 0); |
265 | or return (0, 0); |
|
|
266 | |
|
|
267 | $chld->move (2, 2); |
|
|
268 | |
185 | map { $_ + 4 } $chld->size_request; |
269 | map { $_ + 4 } $chld->size_request; |
186 | } |
270 | } |
187 | |
271 | |
188 | sub _draw { |
272 | sub _draw { |
189 | my ($self) = @_; |
273 | my ($self) = @_; |
190 | |
274 | |
191 | my $chld = $self->get; |
275 | my $chld = $self->get; |
192 | |
276 | |
193 | $chld->move (2, 2, 0); #TODO:move to size_request |
|
|
194 | |
|
|
195 | my ($w, $h) = $chld->size_request; |
277 | my ($w, $h) = $chld->size_request; |
196 | |
278 | |
197 | glColor 1, 0, 0; |
|
|
198 | glBegin GL_QUADS; |
279 | glBegin GL_QUADS; |
|
|
280 | glColor 0, 0, 0; |
199 | glTexCoord 0, 0; glVertex 0 , 0; |
281 | glTexCoord 0, 0; glVertex 0 , 0; |
200 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
282 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
201 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
283 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
202 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
284 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
203 | glEnd; |
285 | glEnd; |
204 | |
286 | |
205 | $chld->draw; |
287 | $chld->draw; |
206 | } |
288 | } |
207 | |
289 | |
|
|
290 | package Crossfire::Client::Widget::FancyFrame; |
|
|
291 | |
|
|
292 | our @ISA = Crossfire::Client::Widget::Frame::; |
|
|
293 | |
|
|
294 | use SDL::OpenGL; |
|
|
295 | |
|
|
296 | #TODO: implement themed frame |
|
|
297 | |
208 | package Crossfire::Client::Widget::Table; |
298 | package Crossfire::Client::Widget::Table; |
209 | |
299 | |
210 | our @ISA = Crossfire::Client::Widget::Container::; |
300 | our @ISA = Crossfire::Client::Widget::Container::; |
211 | |
301 | |
212 | use SDL::OpenGL; |
302 | use SDL::OpenGL; |
213 | |
303 | |
214 | sub add { |
304 | sub add { |
215 | my ($self, $x, $y, $chld) = @_; |
305 | my ($self, $x, $y, $chld) = @_; |
|
|
306 | my $old_chld = $self->{childs}[$y][$x]; |
|
|
307 | |
216 | $self->{childs}[$y][$x] = $chld; |
308 | $self->{childs}[$y][$x] = $chld; |
|
|
309 | $chld->set_parent ($self); |
|
|
310 | $self->update; |
217 | } |
311 | } |
218 | |
312 | |
219 | sub max_row_height { |
313 | sub max_row_height { |
220 | my ($self, $row) = @_; |
314 | my ($self, $row) = @_; |
221 | |
315 | |
… | |
… | |
293 | use SDL::OpenGL; |
387 | use SDL::OpenGL; |
294 | |
388 | |
295 | sub add { |
389 | sub add { |
296 | my ($self, $chld) = @_; |
390 | my ($self, $chld) = @_; |
297 | push @{$self->{childs}}, $chld; |
391 | push @{$self->{childs}}, $chld; |
|
|
392 | $chld->set_parent ($self); |
|
|
393 | $self->update; |
298 | } |
394 | } |
299 | |
395 | |
300 | sub size_request { |
396 | sub size_request { |
301 | my ($self) = @_; |
397 | my ($self) = @_; |
302 | |
398 | |
… | |
… | |
327 | our @ISA = Crossfire::Client::Widget::; |
423 | our @ISA = Crossfire::Client::Widget::; |
328 | |
424 | |
329 | use SDL::OpenGL; |
425 | use SDL::OpenGL; |
330 | |
426 | |
331 | sub new { |
427 | sub new { |
332 | my ($class, $x, $y, $z, $ttf, $text) = @_; |
428 | my ($class, $x, $y, $z, $height, $text) = @_; |
333 | |
429 | |
|
|
430 | # TODO: color, and make height, xyz etc. optional |
334 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); |
431 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
335 | |
432 | |
336 | $self->set_text ($text); |
433 | $self->set_text ($text); |
337 | |
434 | |
338 | $self |
435 | $self |
339 | } |
436 | } |
340 | |
437 | |
341 | sub set_text { |
438 | sub set_text { |
342 | my ($self, $text) = @_; |
439 | my ($self, $text) = @_; |
|
|
440 | |
|
|
441 | $self->{text} = $text; |
343 | $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; |
442 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
|
|
443 | |
|
|
444 | $self->update; |
344 | } |
445 | } |
345 | |
446 | |
346 | sub get_text { |
447 | sub get_text { |
347 | my ($self, $text) = @_; |
448 | my ($self, $text) = @_; |
|
|
449 | |
348 | $self->{text} |
450 | $self->{text} |
349 | } |
451 | } |
350 | |
452 | |
351 | sub size_request { |
453 | sub size_request { |
352 | my ($self) = @_; |
454 | my ($self) = @_; |
… | |
… | |
362 | |
464 | |
363 | my $tex = $self->{texture}; |
465 | my $tex = $self->{texture}; |
364 | |
466 | |
365 | glEnable GL_BLEND; |
467 | glEnable GL_BLEND; |
366 | glEnable GL_TEXTURE_2D; |
468 | glEnable GL_TEXTURE_2D; |
|
|
469 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
367 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL; |
470 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
368 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
471 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
369 | |
472 | |
370 | glColor 1, 0, 1; |
473 | glColor 1, 1, 1, 0.6; # TODO color |
371 | |
474 | |
372 | glBegin GL_QUADS; |
475 | glBegin GL_QUADS; |
373 | glTexCoord 0, 0; glVertex 0 , 0; |
476 | glTexCoord 0, 0; glVertex 0 , 0; |
374 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
477 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
375 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
478 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
… | |
… | |
378 | |
481 | |
379 | glDisable GL_BLEND; |
482 | glDisable GL_BLEND; |
380 | glDisable GL_TEXTURE_2D; |
483 | glDisable GL_TEXTURE_2D; |
381 | } |
484 | } |
382 | |
485 | |
|
|
486 | package Crossfire::Client::Widget::TextEntry; |
|
|
487 | |
|
|
488 | our @ISA = Crossfire::Client::Widget::Label::; |
|
|
489 | |
|
|
490 | use SDL; |
|
|
491 | use SDL::OpenGL; |
|
|
492 | |
|
|
493 | sub key_down { |
|
|
494 | my ($self, $ev) = @_; |
|
|
495 | |
|
|
496 | my $mod = $ev->key_mod; |
|
|
497 | my $sym = $ev->key_sym; |
|
|
498 | |
|
|
499 | $ev->set_unicode (1); |
|
|
500 | my $uni = $ev->key_unicode; |
|
|
501 | |
|
|
502 | my $text = $self->get_text; |
|
|
503 | |
|
|
504 | if ($sym == SDLK_BACKSPACE) { |
|
|
505 | substr $text, -1, 1, ''; |
|
|
506 | |
|
|
507 | } elsif ($uni) { |
|
|
508 | $text .= chr $uni; |
|
|
509 | } |
|
|
510 | $self->set_text ($text); |
|
|
511 | } |
|
|
512 | |
|
|
513 | |
|
|
514 | # XXX: TextView isn't neccessary with pango multiline text rendering |
383 | package Crossfire::Client::Widget::TextView; |
515 | package Crossfire::Client::Widget::TextView; |
384 | |
516 | |
385 | use strict; |
517 | use strict; |
386 | |
518 | |
387 | our @ISA = qw/Crossfire::Client::Widget/; |
519 | our @ISA = qw/Crossfire::Client::Widget/; |
388 | |
520 | |
389 | use SDL::OpenGL; |
521 | use SDL::OpenGL; |
390 | use SDL::OpenGL::Constants; |
522 | use SDL::OpenGL::Constants; |
|
|
523 | |
|
|
524 | sub new { |
|
|
525 | my ($class, $text, $h) = @_; |
|
|
526 | my $self = $class->SUPER::new (); |
|
|
527 | |
|
|
528 | $self->{txt_height} = $h; |
|
|
529 | @{$self->{lines}} = split /\r?\n/, $text; |
|
|
530 | |
|
|
531 | for (split /\r?\n/, $text) { |
|
|
532 | $self->add_line ($_); |
|
|
533 | } |
|
|
534 | $self |
|
|
535 | } |
|
|
536 | |
|
|
537 | #sub render_lines { |
|
|
538 | # my ($self) = @_; |
|
|
539 | # |
|
|
540 | # $self->{txt_lines} = []; |
|
|
541 | # |
|
|
542 | # for (@{$self->{lines}}) { |
|
|
543 | # push @{$self->{txt_lines}}, |
|
|
544 | # new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_; |
|
|
545 | # } |
|
|
546 | #} |
391 | |
547 | |
392 | sub add_line { |
548 | sub add_line { |
393 | my ($self, $line) = @_; |
549 | my ($self, $line) = @_; |
394 | push @{$self->{lines}}, $line; |
550 | push @{$self->{lines}}, $line; |
395 | } |
|
|
396 | |
551 | |
|
|
552 | push @{$self->{txt_lines}}, |
|
|
553 | new_from_text Crossfire::Client::Texture $line, $self->{txt_height}; |
|
|
554 | } |
|
|
555 | |
|
|
556 | sub size_request { |
|
|
557 | my ($self) = @_; |
|
|
558 | |
|
|
559 | my $w = 0; |
|
|
560 | my $h = 0; |
|
|
561 | |
|
|
562 | for (@{$self->{txt_lines}}) { |
|
|
563 | if ($w < $_->{width}) { $w = $_->{width} } |
|
|
564 | $h += $_->{height}; |
|
|
565 | } |
|
|
566 | |
|
|
567 | return ($w, $h); |
|
|
568 | } |
|
|
569 | |
397 | sub _draw { |
570 | sub draw_line { |
398 | my ($self) = @_; |
571 | my ($self, $tex, $y) = @_; |
399 | |
572 | |
|
|
573 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
574 | |
|
|
575 | glColor 1, 0, 1; |
|
|
576 | |
|
|
577 | glBegin GL_QUADS; |
|
|
578 | glTexCoord 0, 0; glVertex 0 , $y; |
|
|
579 | glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; |
|
|
580 | glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; |
|
|
581 | glTexCoord 1, 0; glVertex $tex->{width}, $y; |
|
|
582 | glEnd; |
|
|
583 | } |
|
|
584 | |
|
|
585 | sub _draw { |
|
|
586 | my ($self) = @_; |
|
|
587 | |
|
|
588 | glEnable GL_BLEND; |
|
|
589 | glEnable GL_TEXTURE_2D; |
|
|
590 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL; |
|
|
591 | |
|
|
592 | my $l = 0; |
|
|
593 | for (@{$self->{txt_lines}}) { |
|
|
594 | $self->draw_line ($_, $l); |
|
|
595 | $l += $_->{height}; |
|
|
596 | } |
|
|
597 | |
|
|
598 | glDisable GL_BLEND; |
|
|
599 | glDisable GL_TEXTURE_2D; |
400 | } |
600 | } |
401 | |
601 | |
402 | package Crossfire::Client::Widget::MapWidget; |
602 | package Crossfire::Client::Widget::MapWidget; |
403 | |
603 | |
404 | use strict; |
604 | use strict; |