… | |
… | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | use Crossfire::Client; |
10 | use Crossfire::Client; |
11 | |
11 | |
12 | our $FOCUS; # the widget with current focus |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
13 | |
|
|
14 | our $TOPLEVEL; |
|
|
15 | our $BUTTON_STATE; |
13 | |
16 | |
14 | # class methods for events |
17 | # class methods for events |
15 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
18 | sub feed_sdl_key_down_event { |
16 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
19 | $FOCUS->key_down ($_[0]) if $FOCUS; |
|
|
20 | } |
|
|
21 | |
|
|
22 | sub feed_sdl_key_up_event { |
|
|
23 | $FOCUS->key_up ($_[0]) if $FOCUS; |
|
|
24 | } |
|
|
25 | |
17 | sub feed_sdl_button_down_event { } |
26 | sub feed_sdl_button_down_event { |
|
|
27 | my ($ev) = @_; |
|
|
28 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
29 | |
|
|
30 | if (!$BUTTON_STATE) { |
|
|
31 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
|
|
32 | |
|
|
33 | $GRAB = $widget; |
|
|
34 | $GRAB->update if $GRAB; |
|
|
35 | } |
|
|
36 | |
|
|
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
|
|
38 | |
|
|
39 | $GRAB->button_down ($ev) if $GRAB; |
|
|
40 | } |
|
|
41 | |
18 | sub feed_sdl_button_up_event { } |
42 | sub feed_sdl_button_up_event { |
|
|
43 | my ($ev) = @_; |
|
|
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
45 | |
|
|
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
47 | |
|
|
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
49 | |
|
|
50 | if (!$BUTTON_STATE) { |
|
|
51 | my $grab = $GRAB; undef $GRAB; |
|
|
52 | $grab->update if $grab; |
|
|
53 | $GRAB->update if $GRAB; |
|
|
54 | } |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub feed_sdl_motion_event { |
|
|
58 | my ($ev) = @_; |
|
|
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
60 | |
|
|
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
62 | |
|
|
63 | if ($widget != $HOVER) { |
|
|
64 | my $hover = $HOVER; $HOVER = $widget; |
|
|
65 | |
|
|
66 | $hover->update if $hover; |
|
|
67 | $HOVER->update if $HOVER; |
|
|
68 | } |
|
|
69 | |
|
|
70 | $HOVER->mouse_motion ($ev) if $HOVER; |
|
|
71 | } |
19 | |
72 | |
20 | sub new { |
73 | sub new { |
21 | my $class = shift; |
74 | my $class = shift; |
22 | |
75 | |
23 | bless { @_ }, $class |
76 | bless { @_ }, $class |
… | |
… | |
45 | $self->{w} = $w; |
98 | $self->{w} = $w; |
46 | $self->{h} = $h; |
99 | $self->{h} = $h; |
47 | } |
100 | } |
48 | |
101 | |
49 | sub focus_in { |
102 | sub focus_in { |
50 | my ($widget) = @_; |
103 | my ($self) = @_; |
51 | $FOCUS = $widget; |
104 | |
|
|
105 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
106 | $focus->update if $focus; |
|
|
107 | $FOCUS->update; |
52 | } |
108 | } |
53 | |
109 | |
54 | sub focus_out { |
110 | sub focus_out { |
55 | my ($widget) = @_; |
111 | my ($self) = @_; |
56 | } |
|
|
57 | |
112 | |
58 | sub key_down { |
113 | return unless $FOCUS == $self; |
59 | my ($widget, $sdlev) = @_; |
|
|
60 | } |
|
|
61 | |
114 | |
62 | sub key_up { |
115 | my $focus = $FOCUS; undef $FOCUS; |
63 | my ($widget, $sdlev) = @_; |
116 | $focus->update if $focus; #? |
64 | } |
117 | } |
65 | |
118 | |
|
|
119 | sub mouse_motion { } |
|
|
120 | sub button_up { } |
66 | sub button_down { |
121 | sub button_down { } |
67 | my ($widget, $sdlev) = @_; |
122 | sub key_down { } |
68 | } |
123 | sub key_up { } |
69 | |
124 | |
70 | sub button_up { |
|
|
71 | my ($widget, $sdlev) = @_; |
|
|
72 | } |
|
|
73 | |
|
|
74 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
125 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
75 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
126 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
76 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
127 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
77 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
128 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
78 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
129 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
79 | |
130 | |
80 | sub draw { |
131 | sub draw { |
81 | my ($self) = @_; |
132 | my ($self) = @_; |
82 | |
133 | |
83 | glPushMatrix; |
134 | glPushMatrix; |
84 | glTranslate $self->{x}, $self->{y}, 0; |
135 | glTranslate $self->{x}, $self->{y}, 0; |
85 | $self->_draw; |
136 | $self->_draw; |
|
|
137 | if ($self == $HOVER) { |
|
|
138 | glColor 1, 1, 1, 0.4; |
|
|
139 | glEnable GL_BLEND; |
|
|
140 | glBegin GL_QUADS; |
|
|
141 | glVertex 0 , 0; |
|
|
142 | glVertex $self->{w}, 0; |
|
|
143 | glVertex $self->{w}, $self->{h}; |
|
|
144 | glVertex 0 , $self->{h}; |
|
|
145 | glEnd; |
|
|
146 | glDisable GL_BLEND; |
|
|
147 | } |
86 | glPopMatrix; |
148 | glPopMatrix; |
87 | } |
149 | } |
88 | |
150 | |
89 | sub _draw { |
151 | sub _draw { |
90 | my ($self) = @_; |
152 | my ($self) = @_; |
… | |
… | |
162 | |
224 | |
163 | @{$self->{children}} = |
225 | @{$self->{children}} = |
164 | sort { $a->{z} <=> $b->{z} } |
226 | sort { $a->{z} <=> $b->{z} } |
165 | @{$self->{children}}, $chld; |
227 | @{$self->{children}}, $chld; |
166 | |
228 | |
167 | $self->size_allocate ($self->{w}, $self->{h}); |
229 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
230 | if $self->{w}; #TODO: check for "realised state" |
168 | } |
231 | } |
169 | |
232 | |
170 | sub remove { |
233 | sub remove { |
171 | my ($self, $widget) = @_; |
234 | my ($self, $widget) = @_; |
172 | |
235 | |
… | |
… | |
176 | } |
239 | } |
177 | |
240 | |
178 | sub find_widget { |
241 | sub find_widget { |
179 | my ($self, $x, $y) = @_; |
242 | my ($self, $x, $y) = @_; |
180 | |
243 | |
|
|
244 | $x -= $self->{x}; |
|
|
245 | $y -= $self->{y}; |
|
|
246 | |
181 | my $res; |
247 | my $res; |
182 | |
248 | |
183 | for (@{ $self->{children} }) { |
249 | for (reverse @{ $self->{children} }) { |
184 | $res = $_->find_widget ($x, $y) |
250 | $res = $_->find_widget ($x, $y) |
185 | and return $res; |
251 | and return $res; |
186 | } |
252 | } |
187 | |
253 | |
188 | () |
254 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
189 | } |
|
|
190 | |
|
|
191 | sub size_request { |
|
|
192 | my ($self) = @_; |
|
|
193 | |
|
|
194 | my ($hs, $ws) = (0, 0); |
|
|
195 | for (@{$self->{children} || []}) { |
|
|
196 | my ($w, $h) = $_->size_request; |
|
|
197 | $hs += $h; |
|
|
198 | if ($ws < $w) { $ws = $w } |
|
|
199 | } |
|
|
200 | |
|
|
201 | return ($ws, $hs); |
|
|
202 | } |
255 | } |
203 | |
256 | |
204 | sub _draw { |
257 | sub _draw { |
205 | my ($self) = @_; |
258 | my ($self) = @_; |
206 | |
259 | |
… | |
… | |
223 | my ($self, $w, $h) = @_; |
276 | my ($self, $w, $h) = @_; |
224 | |
277 | |
225 | $self->SUPER::size_allocate ($w, $h); |
278 | $self->SUPER::size_allocate ($w, $h); |
226 | $self->{children}[0]->size_allocate ($w, $h) |
279 | $self->{children}[0]->size_allocate ($w, $h) |
227 | if $self->{children}[0] |
280 | if $self->{children}[0] |
228 | } |
|
|
229 | |
|
|
230 | ############################################################################# |
|
|
231 | |
|
|
232 | package Crossfire::Client::Widget::Toplevel; |
|
|
233 | |
|
|
234 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
235 | |
|
|
236 | sub update { |
|
|
237 | my ($self) = @_; |
|
|
238 | |
|
|
239 | ::refresh (); |
|
|
240 | } |
|
|
241 | |
|
|
242 | sub add { |
|
|
243 | my ($self, $widget) = @_; |
|
|
244 | |
|
|
245 | $self->SUPER::add ($widget); |
|
|
246 | |
|
|
247 | $widget->size_allocate ($widget->size_request); |
|
|
248 | } |
281 | } |
249 | |
282 | |
250 | ############################################################################# |
283 | ############################################################################# |
251 | |
284 | |
252 | package Crossfire::Client::Widget::Window; |
285 | package Crossfire::Client::Widget::Window; |
… | |
… | |
299 | or return; |
332 | or return; |
300 | |
333 | |
301 | glEnable GL_BLEND; |
334 | glEnable GL_BLEND; |
302 | glEnable GL_TEXTURE_2D; |
335 | glEnable GL_TEXTURE_2D; |
303 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
304 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
305 | |
337 | |
306 | glBegin GL_QUADS; |
338 | $tex->draw_quad (0, 0, $w, $h); |
307 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
308 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
309 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
310 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
311 | glEnd; |
|
|
312 | |
339 | |
313 | glDisable GL_BLEND; |
340 | glDisable GL_BLEND; |
314 | glDisable GL_TEXTURE_2D; |
341 | glDisable GL_TEXTURE_2D; |
315 | } |
342 | } |
316 | |
343 | |
… | |
… | |
349 | |
376 | |
350 | my ($w, $h) = $chld->size_request; |
377 | my ($w, $h) = $chld->size_request; |
351 | |
378 | |
352 | glBegin GL_QUADS; |
379 | glBegin GL_QUADS; |
353 | glColor 0, 0, 0; |
380 | glColor 0, 0, 0; |
354 | glTexCoord 0, 0; glVertex 0 , 0; |
381 | glVertex 0 , 0; |
355 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
382 | glVertex 0 , $h + 4; |
356 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
383 | glVertex $w + 4 , $h + 4; |
357 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
384 | glVertex $w + 4 , 0; |
358 | glEnd; |
385 | glEnd; |
359 | |
386 | |
360 | $chld->draw; |
387 | $chld->draw; |
361 | } |
388 | } |
362 | |
389 | |
… | |
… | |
395 | $w -= $tex[2]->{width}; |
422 | $w -= $tex[2]->{width}; |
396 | $w -= $tex[3]->{width}; |
423 | $w -= $tex[3]->{width}; |
397 | |
424 | |
398 | $h = $h < 0 ? 0 : $h; |
425 | $h = $h < 0 ? 0 : $h; |
399 | $w = $w < 0 ? 0 : $w; |
426 | $w = $w < 0 ? 0 : $w; |
400 | warn "CHILD:$w $h\n"; |
427 | |
401 | $self->child->size_allocate ($w, $h); |
428 | $self->child->size_allocate ($w, $h); |
402 | $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
429 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
403 | } |
430 | } |
404 | |
431 | |
405 | sub _draw { |
432 | sub _draw { |
406 | my ($self) = @_; |
433 | my ($self) = @_; |
407 | |
434 | |
408 | my ($w, $h) = ($self->w, $self->h); |
435 | my ($w, $h) = ($self->{w}, $self->{h}); |
409 | my ($cw, $ch) = ($self->child->w, $self->child->h); |
436 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
410 | |
437 | |
411 | glEnable GL_BLEND; |
438 | glEnable GL_BLEND; |
412 | glEnable GL_TEXTURE_2D; |
439 | glEnable GL_TEXTURE_2D; |
413 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
440 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
414 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
441 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
415 | |
442 | |
416 | my $top = $tex[1]; |
443 | my $top = $tex[1]; |
417 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
444 | $top->draw_quad (0, 0, $w, $top->{height}); |
418 | |
|
|
419 | glBegin GL_QUADS; |
|
|
420 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
421 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
422 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
423 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
424 | glEnd; |
|
|
425 | |
445 | |
426 | my $left = $tex[3]; |
446 | my $left = $tex[3]; |
427 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
447 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
428 | |
|
|
429 | glBegin GL_QUADS; |
|
|
430 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
431 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
432 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
433 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
434 | glEnd; |
|
|
435 | |
448 | |
436 | my $right = $tex[2]; |
449 | my $right = $tex[2]; |
437 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
450 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
438 | |
|
|
439 | glBegin GL_QUADS; |
|
|
440 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
441 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
442 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
443 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
444 | glEnd; |
|
|
445 | |
451 | |
446 | my $bottom = $tex[4]; |
452 | my $bottom = $tex[4]; |
447 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
453 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
448 | |
|
|
449 | glBegin GL_QUADS; |
|
|
450 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
451 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
452 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
453 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
454 | glEnd; |
|
|
455 | |
454 | |
456 | my $bg = $tex[0]; |
455 | my $bg = $tex[0]; |
457 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
456 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
458 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
458 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
460 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
461 | |
460 | |
462 | my $rep_x = $cw / $bg->{width}; |
461 | my $rep_x = $cw / $bg->{width}; |
463 | my $rep_y = $ch / $bg->{height}; |
462 | my $rep_y = $ch / $bg->{height}; |
464 | |
463 | |
465 | glBegin GL_QUADS; |
464 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
466 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
467 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
468 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
469 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
470 | glEnd; |
|
|
471 | |
465 | |
472 | glDisable GL_BLEND; |
466 | glDisable GL_BLEND; |
473 | glDisable GL_TEXTURE_2D; |
467 | glDisable GL_TEXTURE_2D; |
474 | |
468 | |
475 | $self->child->draw; |
469 | $self->child->draw; |
… | |
… | |
568 | package Crossfire::Client::Widget::VBox; |
562 | package Crossfire::Client::Widget::VBox; |
569 | |
563 | |
570 | our @ISA = Crossfire::Client::Widget::Container::; |
564 | our @ISA = Crossfire::Client::Widget::Container::; |
571 | |
565 | |
572 | use SDL::OpenGL; |
566 | use SDL::OpenGL; |
|
|
567 | |
|
|
568 | sub size_request { |
|
|
569 | my ($self) = @_; |
|
|
570 | |
|
|
571 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
572 | |
|
|
573 | ( |
|
|
574 | (List::Util::max map $_->[0], @alloc), |
|
|
575 | (List::Util::sum map $_->[1], @alloc), |
|
|
576 | ) |
|
|
577 | } |
573 | |
578 | |
574 | sub size_allocate { |
579 | sub size_allocate { |
575 | my ($self, $w, $h) = @_; |
580 | my ($self, $w, $h) = @_; |
576 | |
581 | |
577 | $self->w ($w); |
582 | $self->w ($w); |
… | |
… | |
610 | $y += $h; |
615 | $y += $h; |
611 | } |
616 | } |
612 | } |
617 | } |
613 | } |
618 | } |
614 | |
619 | |
615 | sub _draw { |
|
|
616 | my ($self) = @_; |
|
|
617 | |
|
|
618 | my ($x, $y); |
|
|
619 | for (@{$self->{children} || []}) { |
|
|
620 | $_->draw; |
|
|
621 | $y += $_->h; |
|
|
622 | } |
|
|
623 | } |
|
|
624 | |
|
|
625 | ############################################################################# |
620 | ############################################################################# |
626 | |
621 | |
627 | package Crossfire::Client::Widget::Label; |
622 | package Crossfire::Client::Widget::Label; |
628 | |
623 | |
629 | our @ISA = Crossfire::Client::Widget::; |
624 | our @ISA = Crossfire::Client::Widget::; |
… | |
… | |
631 | use SDL::OpenGL; |
626 | use SDL::OpenGL; |
632 | |
627 | |
633 | sub new { |
628 | sub new { |
634 | my ($class, $x, $y, $z, $height, $text) = @_; |
629 | my ($class, $x, $y, $z, $height, $text) = @_; |
635 | |
630 | |
|
|
631 | $height ||= $::FONTSIZE; |
|
|
632 | |
636 | # TODO: color, and make height, xyz etc. optional |
633 | # TODO: color, and make height, xyz etc. optional |
637 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
634 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
638 | |
635 | |
639 | $self->set_text ($text); |
636 | $self->set_text ($text); |
640 | |
637 | |
… | |
… | |
657 | } |
654 | } |
658 | |
655 | |
659 | sub size_request { |
656 | sub size_request { |
660 | my ($self) = @_; |
657 | my ($self) = @_; |
661 | |
658 | |
662 | ( |
659 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
660 | ( |
663 | $self->{texture}{width}, |
661 | $self->{texture}{width}, |
664 | $self->{texture}{height}, |
662 | $self->{texture}{height}, |
|
|
663 | ) |
|
|
664 | } else { |
|
|
665 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
|
|
666 | |
|
|
667 | ($w, $h) |
665 | ) |
668 | } |
|
|
669 | |
666 | } |
670 | } |
667 | |
671 | |
668 | sub _draw { |
672 | sub _draw { |
669 | my ($self) = @_; |
673 | my ($self) = @_; |
670 | |
674 | |
… | |
… | |
672 | |
676 | |
673 | glEnable GL_BLEND; |
677 | glEnable GL_BLEND; |
674 | glEnable GL_TEXTURE_2D; |
678 | glEnable GL_TEXTURE_2D; |
675 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
676 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
680 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
677 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
678 | |
681 | |
679 | glColor 1, 0, 0, 1; # TODO color |
682 | glColor 1, 0, 0, 1; # TODO color |
680 | |
683 | |
681 | glBegin GL_QUADS; |
684 | $tex->draw_quad (0, 0); |
682 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
683 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
684 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
685 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
686 | glEnd; |
|
|
687 | |
685 | |
688 | glDisable GL_BLEND; |
686 | glDisable GL_BLEND; |
689 | glDisable GL_TEXTURE_2D; |
687 | glDisable GL_TEXTURE_2D; |
690 | } |
688 | } |
691 | |
689 | |
692 | ############################################################################# |
690 | ############################################################################# |
693 | |
691 | |
694 | package Crossfire::Client::Widget::TextEntry; |
692 | package Crossfire::Client::Widget::Entry; |
695 | |
693 | |
696 | our @ISA = Crossfire::Client::Widget::Label::; |
694 | our @ISA = Crossfire::Client::Widget::Label::; |
697 | |
695 | |
698 | use SDL; |
696 | use SDL; |
699 | use SDL::OpenGL; |
697 | use SDL::OpenGL; |
… | |
… | |
702 | my ($self, $ev) = @_; |
700 | my ($self, $ev) = @_; |
703 | |
701 | |
704 | my $mod = $ev->key_mod; |
702 | my $mod = $ev->key_mod; |
705 | my $sym = $ev->key_sym; |
703 | my $sym = $ev->key_sym; |
706 | |
704 | |
707 | $ev->set_unicode (1); |
|
|
708 | my $uni = $ev->key_unicode; |
705 | my $uni = $ev->key_unicode; |
709 | |
706 | |
710 | my $text = $self->get_text; |
707 | my $text = $self->get_text; |
711 | |
708 | |
712 | if ($sym == SDLK_BACKSPACE) { |
709 | if ($sym == SDLK_BACKSPACE) { |
713 | substr $text, -1, 1, ''; |
710 | substr $text, -1, 1, ''; |
714 | |
|
|
715 | } elsif ($uni) { |
711 | } elsif ($uni) { |
716 | $text .= chr $uni; |
712 | $text .= chr $uni; |
|
|
713 | print "$uni <$text>\n";#d# |
717 | } |
714 | } |
|
|
715 | |
718 | $self->set_text ($text); |
716 | $self->set_text ($text); |
|
|
717 | } |
|
|
718 | |
|
|
719 | sub button_down { |
|
|
720 | my ($self, $ev) = @_; |
|
|
721 | |
|
|
722 | $self->focus_in; |
|
|
723 | } |
|
|
724 | |
|
|
725 | sub _draw { |
|
|
726 | my ($self) = @_; |
|
|
727 | |
|
|
728 | if ($FOCUS == $self) { |
|
|
729 | glColor 1, 1, 1; |
|
|
730 | } else { |
|
|
731 | glColor 0.7, 0.7, 0.7; |
|
|
732 | } |
|
|
733 | |
|
|
734 | glBegin GL_QUADS; |
|
|
735 | glVertex 0 , 0; |
|
|
736 | glVertex 0 , $self->{h} - 1; |
|
|
737 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
738 | glVertex $self->{w} - 1, 0; |
|
|
739 | glEnd; |
|
|
740 | |
|
|
741 | $self->SUPER::_draw; |
719 | } |
742 | } |
720 | |
743 | |
721 | ############################################################################# |
744 | ############################################################################# |
722 | |
745 | |
723 | package Crossfire::Client::Widget::MapWidget; |
746 | package Crossfire::Client::Widget::MapWidget; |
… | |
… | |
738 | |
761 | |
739 | sub key_up { |
762 | sub key_up { |
740 | } |
763 | } |
741 | |
764 | |
742 | sub size_request { |
765 | sub size_request { |
743 | |
766 | ( |
744 | } |
767 | 1 + int $::WIDTH / 32, |
745 | |
768 | 1 + int $::HEIGHT / 32, |
746 | sub size_allocate { |
769 | ) |
747 | } |
770 | } |
748 | |
771 | |
749 | sub _draw { |
772 | sub _draw { |
750 | my ($self) = @_; |
773 | my ($self) = @_; |
751 | |
774 | |
… | |
… | |
784 | my $cell = $map->[$x + $xofs][$y + $yofs] |
807 | my $cell = $map->[$x + $xofs][$y + $yofs] |
785 | or next; |
808 | or next; |
786 | |
809 | |
787 | my $darkness = $cell->[0] * (1 / 255); |
810 | my $darkness = $cell->[0] * (1 / 255); |
788 | if ($darkness < 0) { |
811 | if ($darkness < 0) { |
789 | $darkness = 0.15; |
812 | $darkness = $cell->[1] ? 0.1 : 0; |
790 | } |
813 | } |
791 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
814 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
792 | |
815 | |
793 | for my $num (grep $_, @$cell[1,2,3]) { |
816 | for my $num (grep $_, @$cell[1,2,3]) { |
794 | my $tex = $::CONN->{face}[$num]{texture} || next; |
817 | my $tex = $::CONN->{face}[$num]{texture} || next; |
795 | |
818 | |
796 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
797 | |
|
|
798 | my $w = $tex->{width}; |
819 | my $w = $tex->{width}; |
799 | my $h = $tex->{height}; |
820 | my $h = $tex->{height}; |
800 | |
821 | |
801 | my $px = ($x + 1) * 32 - $w; |
822 | my $px = ($x + 1) * 32 - $w; |
802 | my $py = ($y + 1) * 32 - $h; |
823 | my $py = ($y + 1) * 32 - $h; |
803 | |
824 | |
804 | glBegin GL_QUADS; |
825 | $tex->draw_quad ($px, $py, $w, $h); |
805 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
806 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
807 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
808 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
809 | glEnd; |
|
|
810 | } |
826 | } |
811 | } |
827 | } |
812 | } |
828 | } |
813 | |
829 | |
814 | # if (1) { # higher quality darkness |
830 | # if (1) { # higher quality darkness |
… | |
… | |
823 | |
839 | |
824 | $lighting = new Crossfire::Client::Texture |
840 | $lighting = new Crossfire::Client::Texture |
825 | width => $sw4, |
841 | width => $sw4, |
826 | height => $sh, |
842 | height => $sh, |
827 | data => $lighting, |
843 | data => $lighting, |
828 | internalformat => GL_ALPHA4, |
844 | internalformat => GL_ALPHA, |
829 | format => GL_ALPHA; |
845 | format => GL_ALPHA; |
830 | |
846 | |
831 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
847 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
832 | glColor 0, 0, 0, 0.75; |
848 | glColor 0.45, 0.45, 0.45, 1; |
833 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
849 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
834 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
850 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
835 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
851 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
836 | glBegin GL_QUADS; |
852 | |
837 | glTexCoord 0, 0; glVertex 0 , 0; |
853 | $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
838 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
839 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
840 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
841 | glEnd; |
|
|
842 | |
854 | |
843 | glDisable GL_TEXTURE_2D; |
855 | glDisable GL_TEXTURE_2D; |
844 | glDisable GL_BLEND; |
856 | glDisable GL_BLEND; |
845 | } |
857 | } |
846 | |
858 | |
… | |
… | |
905 | |
917 | |
906 | sub moveto { |
918 | sub moveto { |
907 | my ($self, $x, $y) = @_; |
919 | my ($self, $x, $y) = @_; |
908 | |
920 | |
909 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
921 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
910 | $self->{speed} = 0.2; |
922 | $self->{speed} = 0.001; |
911 | $self->{time} = 1; |
923 | $self->{time} = 1; |
912 | |
924 | |
913 | ::animation_start $self; |
925 | ::animation_start $self; |
914 | } |
926 | } |
915 | |
927 | |
916 | sub animate { |
928 | sub animate { |
917 | my ($self, $interval) = @_; |
929 | my ($self, $interval) = @_; |
|
|
930 | |
|
|
931 | printf "%5.2f\n", 1 / $interval;#d# |
918 | |
932 | |
919 | $self->{time} -= $interval * $self->{speed}; |
933 | $self->{time} -= $interval * $self->{speed}; |
920 | if ($self->{time} <= 0) { |
934 | if ($self->{time} <= 0) { |
921 | $self->{time} = 0; |
935 | $self->{time} = 0; |
922 | ::animation_stop $self; |
936 | ::animation_stop $self; |
… | |
… | |
930 | |
944 | |
931 | sub _draw { |
945 | sub _draw { |
932 | my ($self) = @_; |
946 | my ($self) = @_; |
933 | |
947 | |
934 | glPushMatrix; |
948 | glPushMatrix; |
935 | glRotate $self->{time} * 10000, 0, 1, 0; |
949 | glRotate $self->{time} * 1000, 0, 1, 0; |
936 | $self->{children}[0]->draw; |
950 | $self->{children}[0]->draw; |
937 | glPopMatrix; |
951 | glPopMatrix; |
938 | } |
952 | } |
939 | |
953 | |
940 | 1; |
954 | ############################################################################# |
941 | |
955 | |
|
|
956 | package Crossfire::Client::Widget::Toplevel; |
|
|
957 | |
|
|
958 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
959 | |
|
|
960 | sub size_request { |
|
|
961 | ($::WIDTH, $::HEIGHT) |
|
|
962 | } |
|
|
963 | |
|
|
964 | sub size_allocate { |
|
|
965 | my ($self, $w, $h) = @_; |
|
|
966 | |
|
|
967 | $self->SUPER::size_allocate ($w, $h); |
|
|
968 | |
|
|
969 | $_->size_allocate ($_->size_request) |
|
|
970 | for @{$self->{children}}; |
|
|
971 | } |
|
|
972 | |
|
|
973 | sub update { |
|
|
974 | my ($self) = @_; |
|
|
975 | |
|
|
976 | $self->size_allocate ($self->size_request); |
|
|
977 | ::refresh (); |
|
|
978 | } |
|
|
979 | |
|
|
980 | sub add { |
|
|
981 | my ($self, $widget) = @_; |
|
|
982 | |
|
|
983 | $self->SUPER::add ($widget); |
|
|
984 | |
|
|
985 | $widget->size_allocate ($widget->size_request); |
|
|
986 | } |
|
|
987 | |
|
|
988 | sub draw { |
|
|
989 | my ($self) = @_; |
|
|
990 | |
|
|
991 | $self->_draw; |
|
|
992 | } |
|
|
993 | |
|
|
994 | ############################################################################# |
|
|
995 | |
|
|
996 | package Crossfire::Client::Widget; |
|
|
997 | |
|
|
998 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
|
|
999 | |
|
|
1000 | 1 |
|
|
1001 | |