1 | package Crossfire::Client::Widget; |
1 | package CFClient::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
6 | |
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 CFClient; |
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, $GRAB->translate ($x, $y)) 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 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
|
|
51 | |
|
|
52 | if (!$BUTTON_STATE) { |
|
|
53 | my $grab = $GRAB; undef $GRAB; |
|
|
54 | $grab->update if $grab; |
|
|
55 | $GRAB->update if $GRAB; |
|
|
56 | } |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub feed_sdl_motion_event { |
|
|
60 | my ($ev) = @_; |
|
|
61 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
62 | |
|
|
63 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
64 | |
|
|
65 | if ($widget != $HOVER) { |
|
|
66 | my $hover = $HOVER; $HOVER = $widget; |
|
|
67 | |
|
|
68 | $hover->update if $hover; |
|
|
69 | $HOVER->update if $HOVER; |
|
|
70 | } |
|
|
71 | |
|
|
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
|
|
73 | } |
19 | |
74 | |
20 | sub new { |
75 | sub new { |
21 | my $class = shift; |
76 | my $class = shift; |
22 | |
77 | |
23 | bless { @_ }, $class |
78 | bless { @_ }, $class |
… | |
… | |
44 | |
99 | |
45 | $self->{w} = $w; |
100 | $self->{w} = $w; |
46 | $self->{h} = $h; |
101 | $self->{h} = $h; |
47 | } |
102 | } |
48 | |
103 | |
|
|
104 | # translate global koordinates to local coordinate system |
|
|
105 | sub translate { |
|
|
106 | my ($self, $x, $y) = @_; |
|
|
107 | |
|
|
108 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
|
|
109 | } |
|
|
110 | |
49 | sub focus_in { |
111 | sub focus_in { |
50 | my ($widget) = @_; |
112 | my ($self) = @_; |
51 | $FOCUS = $widget; |
113 | |
|
|
114 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
115 | $focus->update if $focus; |
|
|
116 | $FOCUS->update; |
52 | } |
117 | } |
53 | |
118 | |
54 | sub focus_out { |
119 | sub focus_out { |
55 | my ($widget) = @_; |
120 | my ($self) = @_; |
56 | } |
|
|
57 | |
121 | |
58 | sub key_down { |
122 | return unless $FOCUS == $self; |
59 | my ($widget, $sdlev) = @_; |
|
|
60 | } |
|
|
61 | |
123 | |
62 | sub key_up { |
124 | my $focus = $FOCUS; undef $FOCUS; |
63 | my ($widget, $sdlev) = @_; |
125 | $focus->update if $focus; #? |
64 | } |
126 | } |
65 | |
127 | |
|
|
128 | sub mouse_motion { } |
|
|
129 | sub button_up { } |
66 | sub button_down { |
130 | sub button_down { } |
67 | my ($widget, $sdlev) = @_; |
131 | sub key_down { } |
68 | } |
132 | sub key_up { } |
69 | |
133 | |
70 | sub button_up { |
|
|
71 | my ($widget, $sdlev) = @_; |
|
|
72 | } |
|
|
73 | |
|
|
74 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
134 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
75 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
135 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
76 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
136 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
77 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
137 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
78 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
138 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
79 | |
139 | |
80 | sub draw { |
140 | sub draw { |
81 | my ($self) = @_; |
141 | my ($self) = @_; |
82 | |
142 | |
83 | glPushMatrix; |
143 | glPushMatrix; |
84 | glTranslate $self->{x}, $self->{y}, 0; |
144 | glTranslate $self->{x}, $self->{y}, 0; |
85 | $self->_draw; |
145 | $self->_draw; |
|
|
146 | if ($self == $HOVER) { |
|
|
147 | glColor 1, 1, 1, 0.4; |
|
|
148 | glEnable GL_BLEND; |
|
|
149 | glBegin GL_QUADS; |
|
|
150 | glVertex 0 , 0; |
|
|
151 | glVertex $self->{w}, 0; |
|
|
152 | glVertex $self->{w}, $self->{h}; |
|
|
153 | glVertex 0 , $self->{h}; |
|
|
154 | glEnd; |
|
|
155 | glDisable GL_BLEND; |
|
|
156 | } |
86 | glPopMatrix; |
157 | glPopMatrix; |
87 | } |
158 | } |
88 | |
159 | |
89 | sub _draw { |
160 | sub _draw { |
90 | my ($self) = @_; |
161 | my ($self) = @_; |
… | |
… | |
139 | #$self->deactivate; |
210 | #$self->deactivate; |
140 | } |
211 | } |
141 | |
212 | |
142 | ############################################################################# |
213 | ############################################################################# |
143 | |
214 | |
144 | package Crossfire::Client::Widget::Container; |
215 | package CFClient::Widget::Container; |
145 | |
216 | |
146 | our @ISA = Crossfire::Client::Widget::; |
217 | our @ISA = CFClient::Widget::; |
147 | |
218 | |
148 | sub new { |
219 | sub new { |
149 | my ($class, @widgets) = @_; |
220 | my ($class, %arg) = @_; |
|
|
221 | |
|
|
222 | my $children = delete $arg{children} || []; |
150 | |
223 | |
151 | my $self = $class->SUPER::new (children => []); |
224 | my $self = $class->SUPER::new (children => []); |
152 | $self->add ($_) for @widgets; |
225 | $self->add ($_) for @$children; |
153 | |
226 | |
154 | $self |
227 | $self |
155 | } |
228 | } |
156 | |
229 | |
157 | sub add { |
230 | sub add { |
… | |
… | |
177 | } |
250 | } |
178 | |
251 | |
179 | sub find_widget { |
252 | sub find_widget { |
180 | my ($self, $x, $y) = @_; |
253 | my ($self, $x, $y) = @_; |
181 | |
254 | |
|
|
255 | $x -= $self->{x}; |
|
|
256 | $y -= $self->{y}; |
|
|
257 | |
182 | my $res; |
258 | my $res; |
183 | |
259 | |
184 | for (@{ $self->{children} }) { |
260 | for (reverse @{ $self->{children} }) { |
185 | $res = $_->find_widget ($x, $y) |
261 | $res = $_->find_widget ($x, $y) |
186 | and return $res; |
262 | and return $res; |
187 | } |
263 | } |
188 | |
264 | |
189 | () |
265 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
190 | } |
266 | } |
191 | |
267 | |
192 | sub _draw { |
268 | sub _draw { |
193 | my ($self) = @_; |
269 | my ($self) = @_; |
194 | |
270 | |
195 | $_->draw for @{$self->{children}}; |
271 | $_->draw for @{$self->{children}}; |
196 | } |
272 | } |
197 | |
273 | |
198 | ############################################################################# |
274 | ############################################################################# |
199 | |
275 | |
200 | package Crossfire::Client::Widget::Bin; |
276 | package CFClient::Widget::Bin; |
201 | |
277 | |
202 | our @ISA = Crossfire::Client::Widget::Container::; |
278 | our @ISA = CFClient::Widget::Container::; |
203 | |
279 | |
204 | sub child { $_[0]->{children}[0] } |
280 | sub child { $_[0]->{children}[0] } |
205 | |
281 | |
206 | sub size_request { |
282 | sub size_request { |
207 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
283 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
215 | if $self->{children}[0] |
291 | if $self->{children}[0] |
216 | } |
292 | } |
217 | |
293 | |
218 | ############################################################################# |
294 | ############################################################################# |
219 | |
295 | |
220 | package Crossfire::Client::Widget::Toplevel; |
296 | package CFClient::Widget::Window; |
221 | |
297 | |
222 | our @ISA = Crossfire::Client::Widget::Container::; |
298 | our @ISA = CFClient::Widget::Bin::; |
|
|
299 | |
|
|
300 | use SDL::OpenGL; |
|
|
301 | |
|
|
302 | sub new { |
|
|
303 | my ($class, %arg) = @_; |
|
|
304 | |
|
|
305 | my $self = $class->SUPER::new (%arg); |
|
|
306 | } |
223 | |
307 | |
224 | sub update { |
308 | sub update { |
225 | my ($self) = @_; |
309 | my ($self) = @_; |
226 | |
310 | |
227 | ::refresh (); |
311 | # we want to do this delayed... |
228 | } |
|
|
229 | |
|
|
230 | sub add { |
|
|
231 | my ($self, $widget) = @_; |
|
|
232 | |
|
|
233 | $self->SUPER::add ($widget); |
|
|
234 | |
|
|
235 | $widget->size_allocate ($widget->size_request); |
|
|
236 | } |
|
|
237 | |
|
|
238 | ############################################################################# |
|
|
239 | |
|
|
240 | package Crossfire::Client::Widget::Window; |
|
|
241 | |
|
|
242 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
243 | |
|
|
244 | use SDL::OpenGL; |
|
|
245 | |
|
|
246 | sub new { |
|
|
247 | my ($class, $x, $y, $z, $w, $h) = @_; |
|
|
248 | |
|
|
249 | my $self = $class->SUPER::new; |
|
|
250 | |
|
|
251 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
252 | } |
|
|
253 | |
|
|
254 | sub update { |
|
|
255 | my ($self) = @_; |
|
|
256 | |
|
|
257 | $self->render_chld; |
312 | $self->render_chld; |
258 | $self->SUPER::update; |
313 | $self->SUPER::update; |
259 | } |
314 | } |
260 | |
315 | |
261 | sub render_chld { |
316 | sub render_chld { |
262 | my ($self) = @_; |
317 | my ($self) = @_; |
263 | |
318 | |
264 | $self->{texture} = |
319 | $self->{texture} = |
265 | Crossfire::Client::Texture->new_from_opengl ( |
320 | CFClient::Texture->new_from_opengl ( |
266 | $self->{w}, $self->{h}, sub { $self->child->draw } |
321 | $self->{w}, $self->{h}, sub { $self->child->draw } |
267 | ); |
322 | ); |
268 | } |
323 | } |
269 | |
324 | |
270 | sub size_allocate { |
325 | sub size_allocate { |
… | |
… | |
287 | or return; |
342 | or return; |
288 | |
343 | |
289 | glEnable GL_BLEND; |
344 | glEnable GL_BLEND; |
290 | glEnable GL_TEXTURE_2D; |
345 | glEnable GL_TEXTURE_2D; |
291 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
346 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
292 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
293 | |
347 | |
294 | glBegin GL_QUADS; |
348 | $tex->draw_quad (0, 0, $w, $h); |
295 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
296 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
297 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
298 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
299 | glEnd; |
|
|
300 | |
349 | |
301 | glDisable GL_BLEND; |
350 | glDisable GL_BLEND; |
302 | glDisable GL_TEXTURE_2D; |
351 | glDisable GL_TEXTURE_2D; |
303 | } |
352 | } |
304 | |
353 | |
305 | ############################################################################# |
354 | ############################################################################# |
306 | |
355 | |
307 | package Crossfire::Client::Widget::Frame; |
356 | package CFClient::Widget::Frame; |
308 | |
357 | |
309 | our @ISA = Crossfire::Client::Widget::Bin::; |
358 | our @ISA = CFClient::Widget::Bin::; |
310 | |
359 | |
311 | use SDL::OpenGL; |
360 | use SDL::OpenGL; |
312 | |
361 | |
313 | sub size_request { |
362 | sub size_request { |
314 | my ($self) = @_; |
363 | my ($self) = @_; |
… | |
… | |
337 | |
386 | |
338 | my ($w, $h) = $chld->size_request; |
387 | my ($w, $h) = $chld->size_request; |
339 | |
388 | |
340 | glBegin GL_QUADS; |
389 | glBegin GL_QUADS; |
341 | glColor 0, 0, 0; |
390 | glColor 0, 0, 0; |
342 | glTexCoord 0, 0; glVertex 0 , 0; |
391 | glVertex 0 , 0; |
343 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
392 | glVertex 0 , $h + 4; |
344 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
393 | glVertex $w + 4 , $h + 4; |
345 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
394 | glVertex $w + 4 , 0; |
346 | glEnd; |
395 | glEnd; |
347 | |
396 | |
348 | $chld->draw; |
397 | $chld->draw; |
349 | } |
398 | } |
350 | |
399 | |
351 | ############################################################################# |
400 | ############################################################################# |
352 | |
401 | |
353 | package Crossfire::Client::Widget::FancyFrame; |
402 | package CFClient::Widget::FancyFrame; |
354 | |
403 | |
355 | our @ISA = Crossfire::Client::Widget::Bin::; |
404 | our @ISA = CFClient::Widget::Bin::; |
356 | |
405 | |
357 | use SDL::OpenGL; |
406 | use SDL::OpenGL; |
358 | |
407 | |
359 | my @tex = |
408 | my @tex = |
360 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
409 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
361 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
410 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
362 | |
411 | |
363 | sub size_request { |
412 | sub size_request { |
364 | my ($self) = @_; |
413 | my ($self) = @_; |
365 | |
414 | |
… | |
… | |
400 | glEnable GL_TEXTURE_2D; |
449 | glEnable GL_TEXTURE_2D; |
401 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
450 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
402 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
451 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
403 | |
452 | |
404 | my $top = $tex[1]; |
453 | my $top = $tex[1]; |
405 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
454 | $top->draw_quad (0, 0, $w, $top->{height}); |
406 | |
|
|
407 | glBegin GL_QUADS; |
|
|
408 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
409 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
410 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
411 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
412 | glEnd; |
|
|
413 | |
455 | |
414 | my $left = $tex[3]; |
456 | my $left = $tex[3]; |
415 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
457 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
416 | |
|
|
417 | glBegin GL_QUADS; |
|
|
418 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
419 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
420 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
421 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
422 | glEnd; |
|
|
423 | |
458 | |
424 | my $right = $tex[2]; |
459 | my $right = $tex[2]; |
425 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
460 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
426 | |
|
|
427 | glBegin GL_QUADS; |
|
|
428 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
429 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
430 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
431 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
432 | glEnd; |
|
|
433 | |
461 | |
434 | my $bottom = $tex[4]; |
462 | my $bottom = $tex[4]; |
435 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
463 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
436 | |
|
|
437 | glBegin GL_QUADS; |
|
|
438 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
439 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
440 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
441 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
442 | glEnd; |
|
|
443 | |
464 | |
444 | my $bg = $tex[0]; |
465 | my $bg = $tex[0]; |
445 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
466 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
446 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
447 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
448 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
469 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
449 | |
470 | |
450 | my $rep_x = $cw / $bg->{width}; |
471 | my $rep_x = $cw / $bg->{width}; |
451 | my $rep_y = $ch / $bg->{height}; |
472 | my $rep_y = $ch / $bg->{height}; |
452 | |
473 | |
453 | glBegin GL_QUADS; |
474 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
454 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
455 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
456 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
457 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
458 | glEnd; |
|
|
459 | |
475 | |
460 | glDisable GL_BLEND; |
476 | glDisable GL_BLEND; |
461 | glDisable GL_TEXTURE_2D; |
477 | glDisable GL_TEXTURE_2D; |
462 | |
478 | |
463 | $self->child->draw; |
479 | $self->child->draw; |
464 | |
480 | |
465 | } |
481 | } |
466 | |
482 | |
467 | ############################################################################# |
483 | ############################################################################# |
468 | |
484 | |
469 | package Crossfire::Client::Widget::Table; |
485 | package CFClient::Widget::Table; |
470 | |
486 | |
471 | our @ISA = Crossfire::Client::Widget::Bin::; |
487 | our @ISA = CFClient::Widget::Bin::; |
472 | |
488 | |
473 | use SDL::OpenGL; |
489 | use SDL::OpenGL; |
474 | |
490 | |
475 | sub add { |
491 | sub add { |
476 | my ($self, $x, $y, $chld) = @_; |
492 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
551 | } |
567 | } |
552 | } |
568 | } |
553 | |
569 | |
554 | ############################################################################# |
570 | ############################################################################# |
555 | |
571 | |
556 | package Crossfire::Client::Widget::VBox; |
572 | package CFClient::Widget::VBox; |
557 | |
573 | |
558 | our @ISA = Crossfire::Client::Widget::Container::; |
574 | our @ISA = CFClient::Widget::Container::; |
559 | |
575 | |
560 | use SDL::OpenGL; |
576 | use SDL::OpenGL; |
561 | |
577 | |
562 | sub size_request { |
578 | sub size_request { |
563 | my ($self) = @_; |
579 | my ($self) = @_; |
… | |
… | |
611 | } |
627 | } |
612 | } |
628 | } |
613 | |
629 | |
614 | ############################################################################# |
630 | ############################################################################# |
615 | |
631 | |
616 | package Crossfire::Client::Widget::Label; |
632 | package CFClient::Widget::Label; |
617 | |
633 | |
618 | our @ISA = Crossfire::Client::Widget::; |
634 | our @ISA = CFClient::Widget::; |
619 | |
635 | |
620 | use SDL::OpenGL; |
636 | use SDL::OpenGL; |
621 | |
637 | |
622 | sub new { |
638 | sub new { |
623 | my ($class, $x, $y, $z, $height, $text) = @_; |
639 | my ($class, %arg) = @_; |
624 | |
640 | |
625 | # TODO: color, and make height, xyz etc. optional |
641 | # TODO: color, and make height, xyz etc. optional |
626 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
642 | my $self = $class->SUPER::new ( |
|
|
643 | color => [1, 1, 1], |
|
|
644 | height => $::FONTSIZE, |
|
|
645 | text => "", |
|
|
646 | layout => new CFClient::Layout, |
|
|
647 | %arg |
|
|
648 | ); |
627 | |
649 | |
628 | $self->set_text ($text); |
650 | $self->set_text ($self->{text}); |
629 | |
651 | |
630 | $self |
652 | $self |
631 | } |
653 | } |
632 | |
654 | |
633 | sub set_text { |
655 | sub set_text { |
634 | my ($self, $text) = @_; |
656 | my ($self, $text) = @_; |
635 | |
657 | |
636 | $self->{text} = $text; |
658 | $self->{text} = $text; |
637 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
659 | $self->{layout}->set_markup ($text); |
638 | |
660 | |
639 | $self->update; |
661 | delete $self->{texture}; |
640 | } |
662 | } |
641 | |
663 | |
642 | sub get_text { |
664 | sub get_text { |
643 | my ($self, $text) = @_; |
665 | my ($self, $text) = @_; |
644 | |
666 | |
… | |
… | |
646 | } |
668 | } |
647 | |
669 | |
648 | sub size_request { |
670 | sub size_request { |
649 | my ($self) = @_; |
671 | my ($self) = @_; |
650 | |
672 | |
651 | ( |
673 | $self->{layout}->set_width; |
|
|
674 | $self->{layout}->set_height ($self->{height}); |
|
|
675 | $self->{layout}->size |
|
|
676 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
677 | # ( |
652 | $self->{texture}{width}, |
678 | # $self->{texture}{width}, |
653 | $self->{texture}{height}, |
679 | # $self->{texture}{height}, |
654 | ) |
680 | # ) |
|
|
681 | # } else { |
|
|
682 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
|
|
683 | # |
|
|
684 | # ($w, $h) |
|
|
685 | # } |
|
|
686 | } |
|
|
687 | |
|
|
688 | sub size_allocate { |
|
|
689 | my ($self, $w, $h) = @_; |
|
|
690 | |
|
|
691 | $self->SUPER::size_allocate ($w, $h); |
|
|
692 | delete $self->{texture}; |
655 | } |
693 | } |
656 | |
694 | |
657 | sub _draw { |
695 | sub _draw { |
658 | my ($self) = @_; |
696 | my ($self) = @_; |
659 | |
697 | |
660 | my $tex = $self->{texture}; |
698 | my $tex = $self->{texture} ||= do { |
|
|
699 | $self->{layout}->set_width ($self->{w}); |
|
|
700 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
701 | }; |
661 | |
702 | |
662 | glEnable GL_BLEND; |
703 | glEnable GL_BLEND; |
663 | glEnable GL_TEXTURE_2D; |
704 | glEnable GL_TEXTURE_2D; |
664 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
705 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
665 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
706 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
666 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
667 | |
707 | |
668 | glColor 1, 0, 0, 1; # TODO color |
708 | glColor @{$self->{color}}; |
669 | |
709 | |
670 | glBegin GL_QUADS; |
710 | $tex->draw_quad (0, 0); |
671 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
672 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
673 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
674 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
675 | glEnd; |
|
|
676 | |
711 | |
677 | glDisable GL_BLEND; |
712 | glDisable GL_BLEND; |
678 | glDisable GL_TEXTURE_2D; |
713 | glDisable GL_TEXTURE_2D; |
679 | } |
714 | } |
680 | |
715 | |
681 | ############################################################################# |
716 | ############################################################################# |
682 | |
717 | |
683 | package Crossfire::Client::Widget::TextEntry; |
718 | package CFClient::Widget::Entry; |
684 | |
719 | |
685 | our @ISA = Crossfire::Client::Widget::Label::; |
720 | our @ISA = CFClient::Widget::Label::; |
686 | |
721 | |
687 | use SDL; |
722 | use SDL; |
688 | use SDL::OpenGL; |
723 | use SDL::OpenGL; |
689 | |
724 | |
690 | sub key_down { |
725 | sub key_down { |
691 | my ($self, $ev) = @_; |
726 | my ($self, $ev) = @_; |
692 | |
727 | |
693 | my $mod = $ev->key_mod; |
728 | my $mod = $ev->key_mod; |
694 | my $sym = $ev->key_sym; |
729 | my $sym = $ev->key_sym; |
695 | |
730 | |
696 | $ev->set_unicode (1); |
|
|
697 | my $uni = $ev->key_unicode; |
731 | my $uni = $ev->key_unicode; |
698 | |
732 | |
699 | my $text = $self->get_text; |
733 | my $text = $self->get_text; |
700 | |
734 | |
701 | if ($sym == SDLK_BACKSPACE) { |
735 | if ($sym == SDLK_BACKSPACE) { |
702 | substr $text, -1, 1, ''; |
736 | substr $text, -1, 1, ''; |
703 | |
|
|
704 | } elsif ($uni) { |
737 | } elsif ($uni) { |
705 | $text .= chr $uni; |
738 | $text .= chr $uni; |
706 | } |
739 | } |
|
|
740 | |
707 | $self->set_text ($text); |
741 | $self->set_text ($text); |
708 | } |
742 | } |
709 | |
743 | |
710 | ############################################################################# |
744 | sub button_down { |
|
|
745 | my ($self, $ev) = @_; |
711 | |
746 | |
|
|
747 | $self->focus_in; |
|
|
748 | } |
|
|
749 | |
|
|
750 | sub mouse_motion { |
|
|
751 | my ($self, $ev, $x, $y) = @_; |
|
|
752 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
|
|
753 | } |
|
|
754 | |
|
|
755 | sub _draw { |
|
|
756 | my ($self) = @_; |
|
|
757 | |
|
|
758 | if ($FOCUS == $self) { |
|
|
759 | glColor 1, 1, 1; |
|
|
760 | } else { |
|
|
761 | glColor 0.7, 0.7, 0.7; |
|
|
762 | } |
|
|
763 | |
|
|
764 | glBegin GL_QUADS; |
|
|
765 | glVertex 0 , 0; |
|
|
766 | glVertex 0 , $self->{h} - 1; |
|
|
767 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
768 | glVertex $self->{w} - 1, 0; |
|
|
769 | glEnd; |
|
|
770 | |
|
|
771 | $self->SUPER::_draw; |
|
|
772 | } |
|
|
773 | |
|
|
774 | ############################################################################# |
|
|
775 | |
712 | package Crossfire::Client::Widget::MapWidget; |
776 | package CFClient::Widget::MapWidget; |
713 | |
777 | |
714 | use strict; |
778 | use strict; |
715 | |
779 | |
716 | use List::Util qw(min max); |
780 | use List::Util qw(min max); |
717 | |
781 | |
718 | use SDL; |
782 | use SDL; |
719 | use SDL::OpenGL; |
783 | use SDL::OpenGL; |
720 | use SDL::OpenGL::Constants; |
784 | use SDL::OpenGL::Constants; |
721 | |
785 | |
722 | our @ISA = Crossfire::Client::Widget::; |
786 | our @ISA = CFClient::Widget::; |
|
|
787 | |
|
|
788 | sub new { |
|
|
789 | my $class = shift; |
|
|
790 | |
|
|
791 | $class->SUPER::new (z => -1, @_) |
|
|
792 | } |
723 | |
793 | |
724 | sub key_down { |
794 | sub key_down { |
725 | print "MAPKEYDOWN\n"; |
795 | print "MAPKEYDOWN\n"; |
726 | } |
796 | } |
727 | |
797 | |
728 | sub key_up { |
798 | sub key_up { |
729 | } |
799 | } |
730 | |
800 | |
731 | sub size_request { |
801 | sub size_request { |
732 | |
802 | ( |
733 | } |
803 | 1 + int $::WIDTH / 32, |
734 | |
804 | 1 + int $::HEIGHT / 32, |
735 | sub size_allocate { |
805 | ) |
736 | } |
806 | } |
737 | |
807 | |
738 | sub _draw { |
808 | sub _draw { |
739 | my ($self) = @_; |
809 | my ($self) = @_; |
740 | |
810 | |
… | |
… | |
763 | glEnable GL_TEXTURE_2D; |
833 | glEnable GL_TEXTURE_2D; |
764 | glEnable GL_BLEND; |
834 | glEnable GL_BLEND; |
765 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
835 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
766 | |
836 | |
767 | my $sw4 = ($sw + 3) & ~3; |
837 | my $sw4 = ($sw + 3) & ~3; |
768 | my $lighting = "\x00" x ($sw4 * $sh); |
838 | my $darkness = "\x00" x ($sw4 * $sh); |
769 | |
839 | |
770 | for my $x (0 .. $sw - 1) { |
840 | for my $x (0 .. $sw - 1) { |
|
|
841 | my $row = $map->[$x + $xofs]; |
771 | for my $y (0 .. $sh - 1) { |
842 | for my $y (0 .. $sh - 1) { |
772 | |
843 | |
773 | my $cell = $map->[$x + $xofs][$y + $yofs] |
844 | my $cell = $row->[$y + $yofs] |
774 | or next; |
845 | or next; |
775 | |
846 | |
776 | my $darkness = $cell->[0] * (1 / 255); |
847 | my $dark = $cell->[0]; |
777 | if ($darkness < 0) { |
848 | if ($dark < 0) { |
778 | $darkness = 0.15; |
849 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
850 | } else { |
|
|
851 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
779 | } |
852 | } |
780 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
781 | |
853 | |
782 | for my $num (grep $_, @$cell[1,2,3]) { |
854 | for my $num (grep $_, @$cell[1,2,3]) { |
783 | my $tex = $::CONN->{face}[$num]{texture} || next; |
855 | my $tex = $::CONN->{face}[$num]{texture} || next; |
784 | |
856 | |
785 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
786 | |
|
|
787 | my $w = $tex->{width}; |
857 | my $w = $tex->{width}; |
788 | my $h = $tex->{height}; |
858 | my $h = $tex->{height}; |
789 | |
859 | |
790 | my $px = ($x + 1) * 32 - $w; |
860 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
791 | my $py = ($y + 1) * 32 - $h; |
|
|
792 | |
|
|
793 | glBegin GL_QUADS; |
|
|
794 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
795 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
796 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
797 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
798 | glEnd; |
|
|
799 | } |
861 | } |
800 | } |
862 | } |
801 | } |
863 | } |
802 | |
864 | |
803 | # if (1) { # higher quality darkness |
865 | # if (1) { # higher quality darkness |
… | |
… | |
808 | # |
870 | # |
809 | # $lighting = $pb->get_pixels; |
871 | # $lighting = $pb->get_pixels; |
810 | # $lighting =~ s/(.)../$1/gs; |
872 | # $lighting =~ s/(.)../$1/gs; |
811 | # } |
873 | # } |
812 | |
874 | |
813 | $lighting = new Crossfire::Client::Texture |
875 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
876 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
877 | |
|
|
878 | $darkness = new CFClient::Texture |
814 | width => $sw4, |
879 | width => $sw4, |
815 | height => $sh, |
880 | height => $sh, |
816 | data => $lighting, |
881 | data => $darkness, |
817 | internalformat => GL_ALPHA4, |
882 | internalformat => GL_ALPHA, |
818 | format => GL_ALPHA; |
883 | format => GL_ALPHA; |
819 | |
884 | |
820 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
885 | glColor 0.45, 0.45, 0.45, 1; |
821 | glColor 0, 0, 0, 0.75; |
886 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
822 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
823 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
824 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
825 | glBegin GL_QUADS; |
|
|
826 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
827 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
828 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
829 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
830 | glEnd; |
|
|
831 | |
887 | |
832 | glDisable GL_TEXTURE_2D; |
888 | glDisable GL_TEXTURE_2D; |
833 | glDisable GL_BLEND; |
889 | glDisable GL_BLEND; |
834 | } |
890 | } |
835 | |
891 | |
… | |
… | |
884 | } |
940 | } |
885 | } |
941 | } |
886 | |
942 | |
887 | ############################################################################# |
943 | ############################################################################# |
888 | |
944 | |
889 | package Crossfire::Client::Widget::Animator; |
945 | package CFClient::Widget::Animator; |
890 | |
946 | |
891 | use SDL::OpenGL; |
947 | use SDL::OpenGL; |
892 | |
948 | |
893 | our @ISA = Crossfire::Client::Widget::Bin::; |
949 | our @ISA = CFClient::Widget::Bin::; |
894 | |
950 | |
895 | sub moveto { |
951 | sub moveto { |
896 | my ($self, $x, $y) = @_; |
952 | my ($self, $x, $y) = @_; |
897 | |
953 | |
898 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
954 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
899 | $self->{speed} = 0.2; |
955 | $self->{speed} = 0.001; |
900 | $self->{time} = 1; |
956 | $self->{time} = 1; |
901 | |
957 | |
902 | ::animation_start $self; |
958 | ::animation_start $self; |
903 | } |
959 | } |
904 | |
960 | |
… | |
… | |
919 | |
975 | |
920 | sub _draw { |
976 | sub _draw { |
921 | my ($self) = @_; |
977 | my ($self) = @_; |
922 | |
978 | |
923 | glPushMatrix; |
979 | glPushMatrix; |
924 | glRotate $self->{time} * 10000, 0, 1, 0; |
980 | glRotate $self->{time} * 1000, 0, 1, 0; |
925 | $self->{children}[0]->draw; |
981 | $self->{children}[0]->draw; |
926 | glPopMatrix; |
982 | glPopMatrix; |
927 | } |
983 | } |
928 | |
984 | |
929 | 1; |
985 | ############################################################################# |
930 | |
986 | |
|
|
987 | package CFClient::Widget::Toplevel; |
|
|
988 | |
|
|
989 | our @ISA = CFClient::Widget::Container::; |
|
|
990 | |
|
|
991 | sub size_request { |
|
|
992 | ($::WIDTH, $::HEIGHT) |
|
|
993 | } |
|
|
994 | |
|
|
995 | sub size_allocate { |
|
|
996 | my ($self, $w, $h) = @_; |
|
|
997 | |
|
|
998 | $self->SUPER::size_allocate ($w, $h); |
|
|
999 | |
|
|
1000 | $_->size_allocate ($_->size_request) |
|
|
1001 | for @{$self->{children}}; |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | sub translate { |
|
|
1005 | my ($self, $x, $y) = @_; |
|
|
1006 | |
|
|
1007 | ($x, $y) |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | sub update { |
|
|
1011 | my ($self) = @_; |
|
|
1012 | |
|
|
1013 | $self->size_allocate ($self->size_request); |
|
|
1014 | ::refresh (); |
|
|
1015 | } |
|
|
1016 | |
|
|
1017 | sub add { |
|
|
1018 | my ($self, $widget) = @_; |
|
|
1019 | |
|
|
1020 | $self->SUPER::add ($widget); |
|
|
1021 | |
|
|
1022 | $widget->size_allocate ($widget->size_request); |
|
|
1023 | } |
|
|
1024 | |
|
|
1025 | sub draw { |
|
|
1026 | my ($self) = @_; |
|
|
1027 | |
|
|
1028 | $self->_draw; |
|
|
1029 | } |
|
|
1030 | |
|
|
1031 | ############################################################################# |
|
|
1032 | |
|
|
1033 | package CFClient::Widget; |
|
|
1034 | |
|
|
1035 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
|
|
1036 | |
|
|
1037 | 1 |
|
|
1038 | |