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 | |
|
|
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
13 | |
|
|
14 | our $TOPLEVEL; |
|
|
15 | our $BUTTON_STATE; |
11 | |
16 | |
12 | # class methods for events |
17 | # class methods for events |
13 | sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS } |
18 | sub feed_sdl_key_down_event { |
14 | 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 | |
15 | 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 | |
16 | 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 | } |
17 | |
74 | |
18 | sub new { |
75 | sub new { |
19 | my $class = shift; |
76 | my $class = shift; |
20 | |
77 | |
21 | bless { @_ }, $class |
78 | bless { @_ }, $class |
… | |
… | |
42 | |
99 | |
43 | $self->{w} = $w; |
100 | $self->{w} = $w; |
44 | $self->{h} = $h; |
101 | $self->{h} = $h; |
45 | } |
102 | } |
46 | |
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 | |
47 | sub focus_in { |
111 | sub focus_in { |
48 | my ($widget) = @_; |
112 | my ($self) = @_; |
49 | $::FOCUS = $widget; |
113 | |
|
|
114 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
115 | $focus->update if $focus; |
|
|
116 | $FOCUS->update; |
50 | } |
117 | } |
51 | |
118 | |
52 | sub focus_out { |
119 | sub focus_out { |
53 | my ($widget) = @_; |
120 | my ($self) = @_; |
54 | } |
|
|
55 | |
121 | |
56 | sub key_down { |
122 | return unless $FOCUS == $self; |
57 | my ($widget, $sdlev) = @_; |
|
|
58 | } |
|
|
59 | |
123 | |
60 | sub key_up { |
124 | my $focus = $FOCUS; undef $FOCUS; |
61 | my ($widget, $sdlev) = @_; |
125 | $focus->update if $focus; #? |
62 | } |
126 | } |
63 | |
127 | |
|
|
128 | sub mouse_motion { } |
|
|
129 | sub button_up { } |
64 | sub button_down { |
130 | sub button_down { } |
65 | my ($widget, $sdlev) = @_; |
131 | sub key_down { } |
66 | } |
132 | sub key_up { } |
67 | |
133 | |
68 | sub button_up { |
|
|
69 | my ($widget, $sdlev) = @_; |
|
|
70 | } |
|
|
71 | |
|
|
72 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
134 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
73 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
135 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
74 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
136 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
75 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
137 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
76 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
138 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
77 | |
139 | |
78 | sub draw { |
140 | sub draw { |
79 | my ($self) = @_; |
141 | my ($self) = @_; |
80 | |
142 | |
81 | glPushMatrix; |
143 | glPushMatrix; |
82 | glTranslate $self->{x}, $self->{y}, 0; |
144 | glTranslate $self->{x}, $self->{y}, 0; |
83 | $self->_draw; |
145 | $self->_draw; |
84 | if ($self == $::HOVER) { |
146 | if ($self == $HOVER) { |
85 | glColor 1, 1, 1, 0.4; |
147 | glColor 1, 1, 1, 0.4; |
86 | glEnable GL_BLEND; |
148 | glEnable GL_BLEND; |
87 | glBegin GL_QUADS; |
149 | glBegin GL_QUADS; |
88 | glVertex 0, 0; |
150 | glVertex 0 , 0; |
89 | glVertex $self->{w} - 1, 0; |
151 | glVertex $self->{w}, 0; |
90 | glVertex $self->{w} - 1, $self->{h} - 1; |
152 | glVertex $self->{w}, $self->{h}; |
91 | glVertex 0, $self->{h} - 1; |
153 | glVertex 0 , $self->{h}; |
92 | glEnd; |
154 | glEnd; |
93 | glDisable GL_BLEND; |
155 | glDisable GL_BLEND; |
94 | } |
156 | } |
95 | glPopMatrix; |
157 | glPopMatrix; |
96 | } |
158 | } |
… | |
… | |
148 | #$self->deactivate; |
210 | #$self->deactivate; |
149 | } |
211 | } |
150 | |
212 | |
151 | ############################################################################# |
213 | ############################################################################# |
152 | |
214 | |
153 | package Crossfire::Client::Widget::Container; |
215 | package CFClient::Widget::Container; |
154 | |
216 | |
155 | our @ISA = Crossfire::Client::Widget::; |
217 | our @ISA = CFClient::Widget::; |
156 | |
218 | |
157 | sub new { |
219 | sub new { |
158 | my ($class, @widgets) = @_; |
220 | my ($class, %arg) = @_; |
|
|
221 | |
|
|
222 | my $children = delete $arg{children} || []; |
159 | |
223 | |
160 | my $self = $class->SUPER::new (children => []); |
224 | my $self = $class->SUPER::new (children => []); |
161 | $self->add ($_) for @widgets; |
225 | $self->add ($_) for @$children; |
162 | |
226 | |
163 | $self |
227 | $self |
164 | } |
228 | } |
165 | |
229 | |
166 | sub add { |
230 | sub add { |
… | |
… | |
207 | $_->draw for @{$self->{children}}; |
271 | $_->draw for @{$self->{children}}; |
208 | } |
272 | } |
209 | |
273 | |
210 | ############################################################################# |
274 | ############################################################################# |
211 | |
275 | |
212 | package Crossfire::Client::Widget::Bin; |
276 | package CFClient::Widget::Bin; |
213 | |
277 | |
214 | our @ISA = Crossfire::Client::Widget::Container::; |
278 | our @ISA = CFClient::Widget::Container::; |
215 | |
279 | |
216 | sub child { $_[0]->{children}[0] } |
280 | sub child { $_[0]->{children}[0] } |
217 | |
281 | |
218 | sub size_request { |
282 | sub size_request { |
219 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
283 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
227 | if $self->{children}[0] |
291 | if $self->{children}[0] |
228 | } |
292 | } |
229 | |
293 | |
230 | ############################################################################# |
294 | ############################################################################# |
231 | |
295 | |
232 | package Crossfire::Client::Widget::Toplevel; |
296 | package CFClient::Widget::Window; |
233 | |
297 | |
234 | 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 | } |
235 | |
307 | |
236 | sub update { |
308 | sub update { |
237 | my ($self) = @_; |
309 | my ($self) = @_; |
238 | |
310 | |
239 | ::refresh (); |
311 | # we want to do this delayed... |
240 | } |
|
|
241 | |
|
|
242 | sub add { |
|
|
243 | my ($self, $widget) = @_; |
|
|
244 | |
|
|
245 | $self->SUPER::add ($widget); |
|
|
246 | |
|
|
247 | $widget->size_allocate ($widget->size_request); |
|
|
248 | } |
|
|
249 | |
|
|
250 | ############################################################################# |
|
|
251 | |
|
|
252 | package Crossfire::Client::Widget::Window; |
|
|
253 | |
|
|
254 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
255 | |
|
|
256 | use SDL::OpenGL; |
|
|
257 | |
|
|
258 | sub new { |
|
|
259 | my ($class, $x, $y, $z, $w, $h) = @_; |
|
|
260 | |
|
|
261 | my $self = $class->SUPER::new; |
|
|
262 | |
|
|
263 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
264 | } |
|
|
265 | |
|
|
266 | sub update { |
|
|
267 | my ($self) = @_; |
|
|
268 | |
|
|
269 | $self->render_chld; |
312 | $self->render_chld; |
270 | $self->SUPER::update; |
313 | $self->SUPER::update; |
271 | } |
314 | } |
272 | |
315 | |
273 | sub render_chld { |
316 | sub render_chld { |
274 | my ($self) = @_; |
317 | my ($self) = @_; |
275 | |
318 | |
276 | $self->{texture} = |
319 | $self->{texture} = |
277 | Crossfire::Client::Texture->new_from_opengl ( |
320 | CFClient::Texture->new_from_opengl ( |
278 | $self->{w}, $self->{h}, sub { $self->child->draw } |
321 | $self->{w}, $self->{h}, sub { $self->child->draw } |
279 | ); |
322 | ); |
280 | } |
323 | } |
281 | |
324 | |
282 | sub size_allocate { |
325 | sub size_allocate { |
… | |
… | |
299 | or return; |
342 | or return; |
300 | |
343 | |
301 | glEnable GL_BLEND; |
344 | glEnable GL_BLEND; |
302 | glEnable GL_TEXTURE_2D; |
345 | glEnable GL_TEXTURE_2D; |
303 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
346 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
304 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
305 | |
347 | |
306 | glBegin GL_QUADS; |
348 | $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 | |
349 | |
313 | glDisable GL_BLEND; |
350 | glDisable GL_BLEND; |
314 | glDisable GL_TEXTURE_2D; |
351 | glDisable GL_TEXTURE_2D; |
315 | } |
352 | } |
316 | |
353 | |
317 | ############################################################################# |
354 | ############################################################################# |
318 | |
355 | |
319 | package Crossfire::Client::Widget::Frame; |
356 | package CFClient::Widget::Frame; |
320 | |
357 | |
321 | our @ISA = Crossfire::Client::Widget::Bin::; |
358 | our @ISA = CFClient::Widget::Bin::; |
322 | |
359 | |
323 | use SDL::OpenGL; |
360 | use SDL::OpenGL; |
324 | |
361 | |
325 | sub size_request { |
362 | sub size_request { |
326 | my ($self) = @_; |
363 | my ($self) = @_; |
… | |
… | |
349 | |
386 | |
350 | my ($w, $h) = $chld->size_request; |
387 | my ($w, $h) = $chld->size_request; |
351 | |
388 | |
352 | glBegin GL_QUADS; |
389 | glBegin GL_QUADS; |
353 | glColor 0, 0, 0; |
390 | glColor 0, 0, 0; |
354 | glTexCoord 0, 0; glVertex 0 , 0; |
391 | glVertex 0 , 0; |
355 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
392 | glVertex 0 , $h + 4; |
356 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
393 | glVertex $w + 4 , $h + 4; |
357 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
394 | glVertex $w + 4 , 0; |
358 | glEnd; |
395 | glEnd; |
359 | |
396 | |
360 | $chld->draw; |
397 | $chld->draw; |
361 | } |
398 | } |
362 | |
399 | |
363 | ############################################################################# |
400 | ############################################################################# |
364 | |
401 | |
365 | package Crossfire::Client::Widget::FancyFrame; |
402 | package CFClient::Widget::FancyFrame; |
366 | |
403 | |
367 | our @ISA = Crossfire::Client::Widget::Bin::; |
404 | our @ISA = CFClient::Widget::Bin::; |
368 | |
405 | |
369 | use SDL::OpenGL; |
406 | use SDL::OpenGL; |
370 | |
407 | |
371 | my @tex = |
408 | my @tex = |
372 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
409 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
373 | 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); |
374 | |
411 | |
375 | sub size_request { |
412 | sub size_request { |
376 | my ($self) = @_; |
413 | my ($self) = @_; |
377 | |
414 | |
… | |
… | |
412 | glEnable GL_TEXTURE_2D; |
449 | glEnable GL_TEXTURE_2D; |
413 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
450 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
414 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
451 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
415 | |
452 | |
416 | my $top = $tex[1]; |
453 | my $top = $tex[1]; |
417 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
454 | $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 | |
455 | |
426 | my $left = $tex[3]; |
456 | my $left = $tex[3]; |
427 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
457 | $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 | |
458 | |
436 | my $right = $tex[2]; |
459 | my $right = $tex[2]; |
437 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
460 | $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 | |
461 | |
446 | my $bottom = $tex[4]; |
462 | my $bottom = $tex[4]; |
447 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
463 | $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 | |
464 | |
456 | my $bg = $tex[0]; |
465 | my $bg = $tex[0]; |
457 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
466 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
458 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
460 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
469 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
461 | |
470 | |
462 | my $rep_x = $cw / $bg->{width}; |
471 | my $rep_x = $cw / $bg->{width}; |
463 | my $rep_y = $ch / $bg->{height}; |
472 | my $rep_y = $ch / $bg->{height}; |
464 | |
473 | |
465 | glBegin GL_QUADS; |
474 | $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 | |
475 | |
472 | glDisable GL_BLEND; |
476 | glDisable GL_BLEND; |
473 | glDisable GL_TEXTURE_2D; |
477 | glDisable GL_TEXTURE_2D; |
474 | |
478 | |
475 | $self->child->draw; |
479 | $self->child->draw; |
476 | |
480 | |
477 | } |
481 | } |
478 | |
482 | |
479 | ############################################################################# |
483 | ############################################################################# |
480 | |
484 | |
481 | package Crossfire::Client::Widget::Table; |
485 | package CFClient::Widget::Table; |
482 | |
486 | |
483 | our @ISA = Crossfire::Client::Widget::Bin::; |
487 | our @ISA = CFClient::Widget::Bin::; |
484 | |
488 | |
485 | use SDL::OpenGL; |
489 | use SDL::OpenGL; |
486 | |
490 | |
487 | sub add { |
491 | sub add { |
488 | my ($self, $x, $y, $chld) = @_; |
492 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
563 | } |
567 | } |
564 | } |
568 | } |
565 | |
569 | |
566 | ############################################################################# |
570 | ############################################################################# |
567 | |
571 | |
568 | package Crossfire::Client::Widget::VBox; |
572 | package CFClient::Widget::VBox; |
569 | |
573 | |
570 | our @ISA = Crossfire::Client::Widget::Container::; |
574 | our @ISA = CFClient::Widget::Container::; |
571 | |
575 | |
572 | use SDL::OpenGL; |
576 | use SDL::OpenGL; |
573 | |
577 | |
574 | sub size_request { |
578 | sub size_request { |
575 | my ($self) = @_; |
579 | my ($self) = @_; |
… | |
… | |
623 | } |
627 | } |
624 | } |
628 | } |
625 | |
629 | |
626 | ############################################################################# |
630 | ############################################################################# |
627 | |
631 | |
628 | package Crossfire::Client::Widget::Label; |
632 | package CFClient::Widget::Label; |
629 | |
633 | |
630 | our @ISA = Crossfire::Client::Widget::; |
634 | our @ISA = CFClient::Widget::; |
631 | |
635 | |
632 | use SDL::OpenGL; |
636 | use SDL::OpenGL; |
633 | |
637 | |
634 | sub new { |
638 | sub new { |
635 | my ($class, $x, $y, $z, $height, $text) = @_; |
639 | my ($class, %arg) = @_; |
636 | |
640 | |
637 | # TODO: color, and make height, xyz etc. optional |
641 | # TODO: color, and make height, xyz etc. optional |
638 | 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 | ); |
639 | |
649 | |
640 | $self->set_text ($text); |
650 | $self->set_text ($self->{text}); |
641 | |
651 | |
642 | $self |
652 | $self |
643 | } |
653 | } |
644 | |
654 | |
645 | sub set_text { |
655 | sub set_text { |
646 | my ($self, $text) = @_; |
656 | my ($self, $text) = @_; |
647 | |
657 | |
648 | $self->{text} = $text; |
658 | $self->{text} = $text; |
649 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
659 | $self->{layout}->set_markup ($text); |
650 | |
660 | |
651 | $self->update; |
661 | delete $self->{texture}; |
652 | } |
662 | } |
653 | |
663 | |
654 | sub get_text { |
664 | sub get_text { |
655 | my ($self, $text) = @_; |
665 | my ($self, $text) = @_; |
656 | |
666 | |
… | |
… | |
658 | } |
668 | } |
659 | |
669 | |
660 | sub size_request { |
670 | sub size_request { |
661 | my ($self) = @_; |
671 | my ($self) = @_; |
662 | |
672 | |
663 | ( |
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 | # ( |
664 | $self->{texture}{width}, |
678 | # $self->{texture}{width}, |
665 | $self->{texture}{height}, |
679 | # $self->{texture}{height}, |
666 | ) |
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}; |
667 | } |
693 | } |
668 | |
694 | |
669 | sub _draw { |
695 | sub _draw { |
670 | my ($self) = @_; |
696 | my ($self) = @_; |
671 | |
697 | |
672 | 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 | }; |
673 | |
702 | |
674 | glEnable GL_BLEND; |
703 | glEnable GL_BLEND; |
675 | glEnable GL_TEXTURE_2D; |
704 | glEnable GL_TEXTURE_2D; |
676 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
705 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
677 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
706 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
678 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
679 | |
707 | |
680 | glColor 1, 0, 0, 1; # TODO color |
708 | glColor @{$self->{color}}; |
681 | |
709 | |
682 | glBegin GL_QUADS; |
710 | $tex->draw_quad (0, 0); |
683 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
684 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
685 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
686 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
687 | glEnd; |
|
|
688 | |
711 | |
689 | glDisable GL_BLEND; |
712 | glDisable GL_BLEND; |
690 | glDisable GL_TEXTURE_2D; |
713 | glDisable GL_TEXTURE_2D; |
691 | } |
714 | } |
692 | |
715 | |
693 | ############################################################################# |
716 | ############################################################################# |
694 | |
717 | |
695 | package Crossfire::Client::Widget::TextEntry; |
718 | package CFClient::Widget::Entry; |
696 | |
719 | |
697 | our @ISA = Crossfire::Client::Widget::Label::; |
720 | our @ISA = CFClient::Widget::Label::; |
698 | |
721 | |
699 | use SDL; |
722 | use SDL; |
700 | use SDL::OpenGL; |
723 | use SDL::OpenGL; |
701 | |
724 | |
702 | sub key_down { |
725 | sub key_down { |
703 | my ($self, $ev) = @_; |
726 | my ($self, $ev) = @_; |
704 | |
727 | |
705 | my $mod = $ev->key_mod; |
728 | my $mod = $ev->key_mod; |
706 | my $sym = $ev->key_sym; |
729 | my $sym = $ev->key_sym; |
707 | |
730 | |
708 | $ev->set_unicode (1); |
|
|
709 | my $uni = $ev->key_unicode; |
731 | my $uni = $ev->key_unicode; |
710 | |
732 | |
711 | my $text = $self->get_text; |
733 | my $text = $self->get_text; |
712 | |
734 | |
713 | if ($sym == SDLK_BACKSPACE) { |
735 | if ($sym == SDLK_BACKSPACE) { |
714 | substr $text, -1, 1, ''; |
736 | substr $text, -1, 1, ''; |
715 | |
|
|
716 | } elsif ($uni) { |
737 | } elsif ($uni) { |
717 | $text .= chr $uni; |
738 | $text .= chr $uni; |
718 | } |
739 | } |
|
|
740 | |
719 | $self->set_text ($text); |
741 | $self->set_text ($text); |
720 | } |
742 | } |
721 | |
743 | |
722 | ############################################################################# |
744 | sub button_down { |
|
|
745 | my ($self, $ev) = @_; |
723 | |
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 | |
724 | package Crossfire::Client::Widget::MapWidget; |
776 | package CFClient::Widget::MapWidget; |
725 | |
777 | |
726 | use strict; |
778 | use strict; |
727 | |
779 | |
728 | use List::Util qw(min max); |
780 | use List::Util qw(min max); |
729 | |
781 | |
730 | use SDL; |
782 | use SDL; |
731 | use SDL::OpenGL; |
783 | use SDL::OpenGL; |
732 | use SDL::OpenGL::Constants; |
784 | use SDL::OpenGL::Constants; |
733 | |
785 | |
734 | 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 | } |
735 | |
793 | |
736 | sub key_down { |
794 | sub key_down { |
737 | print "MAPKEYDOWN\n"; |
795 | print "MAPKEYDOWN\n"; |
738 | } |
796 | } |
739 | |
797 | |
740 | sub key_up { |
798 | sub key_up { |
741 | } |
799 | } |
742 | |
800 | |
743 | sub size_request { |
801 | sub size_request { |
744 | |
802 | ( |
745 | } |
803 | 1 + int $::WIDTH / 32, |
746 | |
804 | 1 + int $::HEIGHT / 32, |
747 | sub size_allocate { |
805 | ) |
748 | } |
806 | } |
749 | |
807 | |
750 | sub _draw { |
808 | sub _draw { |
751 | my ($self) = @_; |
809 | my ($self) = @_; |
752 | |
810 | |
… | |
… | |
775 | glEnable GL_TEXTURE_2D; |
833 | glEnable GL_TEXTURE_2D; |
776 | glEnable GL_BLEND; |
834 | glEnable GL_BLEND; |
777 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
835 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
778 | |
836 | |
779 | my $sw4 = ($sw + 3) & ~3; |
837 | my $sw4 = ($sw + 3) & ~3; |
780 | my $lighting = "\x00" x ($sw4 * $sh); |
838 | my $darkness = "\x00" x ($sw4 * $sh); |
781 | |
839 | |
782 | for my $x (0 .. $sw - 1) { |
840 | for my $x (0 .. $sw - 1) { |
|
|
841 | my $row = $map->[$x + $xofs]; |
783 | for my $y (0 .. $sh - 1) { |
842 | for my $y (0 .. $sh - 1) { |
784 | |
843 | |
785 | my $cell = $map->[$x + $xofs][$y + $yofs] |
844 | my $cell = $row->[$y + $yofs] |
786 | or next; |
845 | or next; |
787 | |
846 | |
788 | my $darkness = $cell->[0] * (1 / 255); |
847 | my $dark = $cell->[0]; |
789 | if ($darkness < 0) { |
848 | if ($dark < 0) { |
790 | $darkness = 0.15; |
849 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
850 | } else { |
|
|
851 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
791 | } |
852 | } |
792 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
793 | |
853 | |
794 | for my $num (grep $_, @$cell[1,2,3]) { |
854 | for my $num (grep $_, @$cell[1,2,3]) { |
795 | my $tex = $::CONN->{face}[$num]{texture} || next; |
855 | my $tex = $::CONN->{face}[$num]{texture} || next; |
796 | |
856 | |
797 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
798 | |
|
|
799 | my $w = $tex->{width}; |
857 | my $w = $tex->{width}; |
800 | my $h = $tex->{height}; |
858 | my $h = $tex->{height}; |
801 | |
859 | |
802 | my $px = ($x + 1) * 32 - $w; |
860 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
803 | my $py = ($y + 1) * 32 - $h; |
|
|
804 | |
|
|
805 | glBegin GL_QUADS; |
|
|
806 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
807 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
808 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
809 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
810 | glEnd; |
|
|
811 | } |
861 | } |
812 | } |
862 | } |
813 | } |
863 | } |
814 | |
864 | |
815 | # if (1) { # higher quality darkness |
865 | # if (1) { # higher quality darkness |
… | |
… | |
820 | # |
870 | # |
821 | # $lighting = $pb->get_pixels; |
871 | # $lighting = $pb->get_pixels; |
822 | # $lighting =~ s/(.)../$1/gs; |
872 | # $lighting =~ s/(.)../$1/gs; |
823 | # } |
873 | # } |
824 | |
874 | |
825 | $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 |
826 | width => $sw4, |
879 | width => $sw4, |
827 | height => $sh, |
880 | height => $sh, |
828 | data => $lighting, |
881 | data => $darkness, |
829 | internalformat => GL_ALPHA4, |
882 | internalformat => GL_ALPHA, |
830 | format => GL_ALPHA; |
883 | format => GL_ALPHA; |
831 | |
884 | |
832 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
885 | glColor 0.45, 0.45, 0.45, 1; |
833 | glColor 0, 0, 0, 0.75; |
886 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
834 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
835 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
836 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
837 | glBegin GL_QUADS; |
|
|
838 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
839 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
840 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
841 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
842 | glEnd; |
|
|
843 | |
887 | |
844 | glDisable GL_TEXTURE_2D; |
888 | glDisable GL_TEXTURE_2D; |
845 | glDisable GL_BLEND; |
889 | glDisable GL_BLEND; |
846 | } |
890 | } |
847 | |
891 | |
… | |
… | |
896 | } |
940 | } |
897 | } |
941 | } |
898 | |
942 | |
899 | ############################################################################# |
943 | ############################################################################# |
900 | |
944 | |
901 | package Crossfire::Client::Widget::Animator; |
945 | package CFClient::Widget::Animator; |
902 | |
946 | |
903 | use SDL::OpenGL; |
947 | use SDL::OpenGL; |
904 | |
948 | |
905 | our @ISA = Crossfire::Client::Widget::Bin::; |
949 | our @ISA = CFClient::Widget::Bin::; |
906 | |
950 | |
907 | sub moveto { |
951 | sub moveto { |
908 | my ($self, $x, $y) = @_; |
952 | my ($self, $x, $y) = @_; |
909 | |
953 | |
910 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
954 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
911 | $self->{speed} = 0.2; |
955 | $self->{speed} = 0.001; |
912 | $self->{time} = 1; |
956 | $self->{time} = 1; |
913 | |
957 | |
914 | ::animation_start $self; |
958 | ::animation_start $self; |
915 | } |
959 | } |
916 | |
960 | |
… | |
… | |
931 | |
975 | |
932 | sub _draw { |
976 | sub _draw { |
933 | my ($self) = @_; |
977 | my ($self) = @_; |
934 | |
978 | |
935 | glPushMatrix; |
979 | glPushMatrix; |
936 | glRotate $self->{time} * 10000, 0, 1, 0; |
980 | glRotate $self->{time} * 1000, 0, 1, 0; |
937 | $self->{children}[0]->draw; |
981 | $self->{children}[0]->draw; |
938 | glPopMatrix; |
982 | glPopMatrix; |
939 | } |
983 | } |
940 | |
984 | |
941 | 1; |
985 | ############################################################################# |
942 | |
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 | |