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, 1; |
147 | glColor 1, 1, 1, 0.4; |
|
|
148 | glEnable GL_BLEND; |
86 | glBegin GL_LINES; |
149 | glBegin GL_QUADS; |
87 | glVertex 0, 0; |
150 | glVertex 0 , 0; |
88 | glVertex $self->{w} - 1, 0; |
151 | glVertex $self->{w}, 0; |
89 | glVertex $self->{w} - 1, $self->{h} - 1; |
152 | glVertex $self->{w}, $self->{h}; |
90 | glVertex 0, $self->{h} - 1; |
153 | glVertex 0 , $self->{h}; |
91 | glVertex 0, 0; |
|
|
92 | glEnd; |
154 | glEnd; |
|
|
155 | glDisable GL_BLEND; |
93 | } |
156 | } |
94 | glPopMatrix; |
157 | glPopMatrix; |
95 | } |
158 | } |
96 | |
159 | |
97 | sub _draw { |
160 | sub _draw { |
… | |
… | |
147 | #$self->deactivate; |
210 | #$self->deactivate; |
148 | } |
211 | } |
149 | |
212 | |
150 | ############################################################################# |
213 | ############################################################################# |
151 | |
214 | |
152 | package Crossfire::Client::Widget::Container; |
215 | package CFClient::Widget::Container; |
153 | |
216 | |
154 | our @ISA = Crossfire::Client::Widget::; |
217 | our @ISA = CFClient::Widget::; |
155 | |
218 | |
156 | sub new { |
219 | sub new { |
157 | my ($class, @widgets) = @_; |
220 | my ($class, @widgets) = @_; |
158 | |
221 | |
159 | my $self = $class->SUPER::new (children => []); |
222 | my $self = $class->SUPER::new (children => []); |
… | |
… | |
206 | $_->draw for @{$self->{children}}; |
269 | $_->draw for @{$self->{children}}; |
207 | } |
270 | } |
208 | |
271 | |
209 | ############################################################################# |
272 | ############################################################################# |
210 | |
273 | |
211 | package Crossfire::Client::Widget::Bin; |
274 | package CFClient::Widget::Bin; |
212 | |
275 | |
213 | our @ISA = Crossfire::Client::Widget::Container::; |
276 | our @ISA = CFClient::Widget::Container::; |
214 | |
277 | |
215 | sub child { $_[0]->{children}[0] } |
278 | sub child { $_[0]->{children}[0] } |
216 | |
279 | |
217 | sub size_request { |
280 | sub size_request { |
218 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
281 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
226 | if $self->{children}[0] |
289 | if $self->{children}[0] |
227 | } |
290 | } |
228 | |
291 | |
229 | ############################################################################# |
292 | ############################################################################# |
230 | |
293 | |
231 | package Crossfire::Client::Widget::Toplevel; |
|
|
232 | |
|
|
233 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
234 | |
|
|
235 | sub update { |
|
|
236 | my ($self) = @_; |
|
|
237 | |
|
|
238 | ::refresh (); |
|
|
239 | } |
|
|
240 | |
|
|
241 | sub add { |
|
|
242 | my ($self, $widget) = @_; |
|
|
243 | |
|
|
244 | $self->SUPER::add ($widget); |
|
|
245 | |
|
|
246 | $widget->size_allocate ($widget->size_request); |
|
|
247 | } |
|
|
248 | |
|
|
249 | ############################################################################# |
|
|
250 | |
|
|
251 | package Crossfire::Client::Widget::Window; |
294 | package CFClient::Widget::Window; |
252 | |
295 | |
253 | our @ISA = Crossfire::Client::Widget::Bin::; |
296 | our @ISA = CFClient::Widget::Bin::; |
254 | |
297 | |
255 | use SDL::OpenGL; |
298 | use SDL::OpenGL; |
256 | |
299 | |
257 | sub new { |
300 | sub new { |
258 | my ($class, $x, $y, $z, $w, $h) = @_; |
301 | my ($class, $x, $y, $z, $w, $h) = @_; |
… | |
… | |
271 | |
314 | |
272 | sub render_chld { |
315 | sub render_chld { |
273 | my ($self) = @_; |
316 | my ($self) = @_; |
274 | |
317 | |
275 | $self->{texture} = |
318 | $self->{texture} = |
276 | Crossfire::Client::Texture->new_from_opengl ( |
319 | CFClient::Texture->new_from_opengl ( |
277 | $self->{w}, $self->{h}, sub { $self->child->draw } |
320 | $self->{w}, $self->{h}, sub { $self->child->draw } |
278 | ); |
321 | ); |
279 | } |
322 | } |
280 | |
323 | |
281 | sub size_allocate { |
324 | sub size_allocate { |
… | |
… | |
298 | or return; |
341 | or return; |
299 | |
342 | |
300 | glEnable GL_BLEND; |
343 | glEnable GL_BLEND; |
301 | glEnable GL_TEXTURE_2D; |
344 | glEnable GL_TEXTURE_2D; |
302 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
345 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
303 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
304 | |
346 | |
305 | glBegin GL_QUADS; |
347 | $tex->draw_quad (0, 0, $w, $h); |
306 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
307 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
308 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
309 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
310 | glEnd; |
|
|
311 | |
348 | |
312 | glDisable GL_BLEND; |
349 | glDisable GL_BLEND; |
313 | glDisable GL_TEXTURE_2D; |
350 | glDisable GL_TEXTURE_2D; |
314 | } |
351 | } |
315 | |
352 | |
316 | ############################################################################# |
353 | ############################################################################# |
317 | |
354 | |
318 | package Crossfire::Client::Widget::Frame; |
355 | package CFClient::Widget::Frame; |
319 | |
356 | |
320 | our @ISA = Crossfire::Client::Widget::Bin::; |
357 | our @ISA = CFClient::Widget::Bin::; |
321 | |
358 | |
322 | use SDL::OpenGL; |
359 | use SDL::OpenGL; |
323 | |
360 | |
324 | sub size_request { |
361 | sub size_request { |
325 | my ($self) = @_; |
362 | my ($self) = @_; |
… | |
… | |
348 | |
385 | |
349 | my ($w, $h) = $chld->size_request; |
386 | my ($w, $h) = $chld->size_request; |
350 | |
387 | |
351 | glBegin GL_QUADS; |
388 | glBegin GL_QUADS; |
352 | glColor 0, 0, 0; |
389 | glColor 0, 0, 0; |
353 | glTexCoord 0, 0; glVertex 0 , 0; |
390 | glVertex 0 , 0; |
354 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
391 | glVertex 0 , $h + 4; |
355 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
392 | glVertex $w + 4 , $h + 4; |
356 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
393 | glVertex $w + 4 , 0; |
357 | glEnd; |
394 | glEnd; |
358 | |
395 | |
359 | $chld->draw; |
396 | $chld->draw; |
360 | } |
397 | } |
361 | |
398 | |
362 | ############################################################################# |
399 | ############################################################################# |
363 | |
400 | |
364 | package Crossfire::Client::Widget::FancyFrame; |
401 | package CFClient::Widget::FancyFrame; |
365 | |
402 | |
366 | our @ISA = Crossfire::Client::Widget::Bin::; |
403 | our @ISA = CFClient::Widget::Bin::; |
367 | |
404 | |
368 | use SDL::OpenGL; |
405 | use SDL::OpenGL; |
369 | |
406 | |
370 | my @tex = |
407 | my @tex = |
371 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
408 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
372 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
409 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
373 | |
410 | |
374 | sub size_request { |
411 | sub size_request { |
375 | my ($self) = @_; |
412 | my ($self) = @_; |
376 | |
413 | |
… | |
… | |
411 | glEnable GL_TEXTURE_2D; |
448 | glEnable GL_TEXTURE_2D; |
412 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
449 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
413 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
450 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
414 | |
451 | |
415 | my $top = $tex[1]; |
452 | my $top = $tex[1]; |
416 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
453 | $top->draw_quad (0, 0, $w, $top->{height}); |
417 | |
|
|
418 | glBegin GL_QUADS; |
|
|
419 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
420 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
421 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
422 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
423 | glEnd; |
|
|
424 | |
454 | |
425 | my $left = $tex[3]; |
455 | my $left = $tex[3]; |
426 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
456 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
427 | |
|
|
428 | glBegin GL_QUADS; |
|
|
429 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
430 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
431 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
432 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
433 | glEnd; |
|
|
434 | |
457 | |
435 | my $right = $tex[2]; |
458 | my $right = $tex[2]; |
436 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
459 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
437 | |
|
|
438 | glBegin GL_QUADS; |
|
|
439 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
440 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
441 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
442 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
443 | glEnd; |
|
|
444 | |
460 | |
445 | my $bottom = $tex[4]; |
461 | my $bottom = $tex[4]; |
446 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
462 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
447 | |
|
|
448 | glBegin GL_QUADS; |
|
|
449 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
450 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
451 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
452 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
453 | glEnd; |
|
|
454 | |
463 | |
455 | my $bg = $tex[0]; |
464 | my $bg = $tex[0]; |
456 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
465 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
466 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
467 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
460 | |
469 | |
461 | my $rep_x = $cw / $bg->{width}; |
470 | my $rep_x = $cw / $bg->{width}; |
462 | my $rep_y = $ch / $bg->{height}; |
471 | my $rep_y = $ch / $bg->{height}; |
463 | |
472 | |
464 | glBegin GL_QUADS; |
473 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
465 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
466 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
467 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
468 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
469 | glEnd; |
|
|
470 | |
474 | |
471 | glDisable GL_BLEND; |
475 | glDisable GL_BLEND; |
472 | glDisable GL_TEXTURE_2D; |
476 | glDisable GL_TEXTURE_2D; |
473 | |
477 | |
474 | $self->child->draw; |
478 | $self->child->draw; |
475 | |
479 | |
476 | } |
480 | } |
477 | |
481 | |
478 | ############################################################################# |
482 | ############################################################################# |
479 | |
483 | |
480 | package Crossfire::Client::Widget::Table; |
484 | package CFClient::Widget::Table; |
481 | |
485 | |
482 | our @ISA = Crossfire::Client::Widget::Bin::; |
486 | our @ISA = CFClient::Widget::Bin::; |
483 | |
487 | |
484 | use SDL::OpenGL; |
488 | use SDL::OpenGL; |
485 | |
489 | |
486 | sub add { |
490 | sub add { |
487 | my ($self, $x, $y, $chld) = @_; |
491 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
562 | } |
566 | } |
563 | } |
567 | } |
564 | |
568 | |
565 | ############################################################################# |
569 | ############################################################################# |
566 | |
570 | |
567 | package Crossfire::Client::Widget::VBox; |
571 | package CFClient::Widget::VBox; |
568 | |
572 | |
569 | our @ISA = Crossfire::Client::Widget::Container::; |
573 | our @ISA = CFClient::Widget::Container::; |
570 | |
574 | |
571 | use SDL::OpenGL; |
575 | use SDL::OpenGL; |
572 | |
576 | |
573 | sub size_request { |
577 | sub size_request { |
574 | my ($self) = @_; |
578 | my ($self) = @_; |
… | |
… | |
622 | } |
626 | } |
623 | } |
627 | } |
624 | |
628 | |
625 | ############################################################################# |
629 | ############################################################################# |
626 | |
630 | |
627 | package Crossfire::Client::Widget::Label; |
631 | package CFClient::Widget::Label; |
628 | |
632 | |
629 | our @ISA = Crossfire::Client::Widget::; |
633 | our @ISA = CFClient::Widget::; |
630 | |
634 | |
631 | use SDL::OpenGL; |
635 | use SDL::OpenGL; |
632 | |
636 | |
633 | sub new { |
637 | sub new { |
634 | my ($class, $x, $y, $z, $height, $text) = @_; |
638 | my ($class, $x, $y, $z, $height, $text) = @_; |
635 | |
639 | |
|
|
640 | $height ||= $::FONTSIZE; |
|
|
641 | |
636 | # TODO: color, and make height, xyz etc. optional |
642 | # TODO: color, and make height, xyz etc. optional |
637 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
643 | my $self = $class->SUPER::new ( |
|
|
644 | color => [1, 1, 1], |
|
|
645 | x => $x, |
|
|
646 | y => $y, |
|
|
647 | z => $z, |
|
|
648 | height => $height, |
|
|
649 | layout => new CFClient::Layout $height, |
|
|
650 | ); |
638 | |
651 | |
639 | $self->set_text ($text); |
652 | $self->set_text ($text); |
640 | |
653 | |
641 | $self |
654 | $self |
642 | } |
655 | } |
643 | |
656 | |
644 | sub set_text { |
657 | sub set_text { |
645 | my ($self, $text) = @_; |
658 | my ($self, $text) = @_; |
646 | |
659 | |
647 | $self->{text} = $text; |
660 | $self->{text} = $text; |
648 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
661 | $self->{layout}->set_markup ($text); |
649 | |
662 | |
650 | $self->update; |
663 | delete $self->{texture}; |
651 | } |
664 | } |
652 | |
665 | |
653 | sub get_text { |
666 | sub get_text { |
654 | my ($self, $text) = @_; |
667 | my ($self, $text) = @_; |
655 | |
668 | |
… | |
… | |
657 | } |
670 | } |
658 | |
671 | |
659 | sub size_request { |
672 | sub size_request { |
660 | my ($self) = @_; |
673 | my ($self) = @_; |
661 | |
674 | |
662 | ( |
675 | $self->{layout}->set_width; |
|
|
676 | $self->{layout}->size |
|
|
677 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
678 | # ( |
663 | $self->{texture}{width}, |
679 | # $self->{texture}{width}, |
664 | $self->{texture}{height}, |
680 | # $self->{texture}{height}, |
665 | ) |
681 | # ) |
|
|
682 | # } else { |
|
|
683 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
|
|
684 | # |
|
|
685 | # ($w, $h) |
|
|
686 | # } |
|
|
687 | } |
|
|
688 | |
|
|
689 | sub size_allocate { |
|
|
690 | my ($self, $w, $h) = @_; |
|
|
691 | |
|
|
692 | $self->SUPER::size_allocate ($w, $h); |
|
|
693 | delete $self->{texture}; |
666 | } |
694 | } |
667 | |
695 | |
668 | sub _draw { |
696 | sub _draw { |
669 | my ($self) = @_; |
697 | my ($self) = @_; |
670 | |
698 | |
671 | my $tex = $self->{texture}; |
699 | my $tex = $self->{texture} ||= do { |
|
|
700 | $self->{layout}->set_width ($self->{w}); |
|
|
701 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
702 | }; |
672 | |
703 | |
673 | glEnable GL_BLEND; |
704 | glEnable GL_BLEND; |
674 | glEnable GL_TEXTURE_2D; |
705 | glEnable GL_TEXTURE_2D; |
675 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
706 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
676 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
707 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
677 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
678 | |
708 | |
679 | glColor 1, 0, 0, 1; # TODO color |
709 | glColor @{$self->{color}}; |
680 | |
710 | |
681 | glBegin GL_QUADS; |
711 | $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 | |
712 | |
688 | glDisable GL_BLEND; |
713 | glDisable GL_BLEND; |
689 | glDisable GL_TEXTURE_2D; |
714 | glDisable GL_TEXTURE_2D; |
690 | } |
715 | } |
691 | |
716 | |
692 | ############################################################################# |
717 | ############################################################################# |
693 | |
718 | |
694 | package Crossfire::Client::Widget::TextEntry; |
719 | package CFClient::Widget::Entry; |
695 | |
720 | |
696 | our @ISA = Crossfire::Client::Widget::Label::; |
721 | our @ISA = CFClient::Widget::Label::; |
697 | |
722 | |
698 | use SDL; |
723 | use SDL; |
699 | use SDL::OpenGL; |
724 | use SDL::OpenGL; |
700 | |
725 | |
701 | sub key_down { |
726 | sub key_down { |
702 | my ($self, $ev) = @_; |
727 | my ($self, $ev) = @_; |
703 | |
728 | |
704 | my $mod = $ev->key_mod; |
729 | my $mod = $ev->key_mod; |
705 | my $sym = $ev->key_sym; |
730 | my $sym = $ev->key_sym; |
706 | |
731 | |
707 | $ev->set_unicode (1); |
|
|
708 | my $uni = $ev->key_unicode; |
732 | my $uni = $ev->key_unicode; |
709 | |
733 | |
710 | my $text = $self->get_text; |
734 | my $text = $self->get_text; |
711 | |
735 | |
712 | if ($sym == SDLK_BACKSPACE) { |
736 | if ($sym == SDLK_BACKSPACE) { |
713 | substr $text, -1, 1, ''; |
737 | substr $text, -1, 1, ''; |
714 | |
|
|
715 | } elsif ($uni) { |
738 | } elsif ($uni) { |
716 | $text .= chr $uni; |
739 | $text .= chr $uni; |
717 | } |
740 | } |
|
|
741 | |
718 | $self->set_text ($text); |
742 | $self->set_text ($text); |
719 | } |
743 | } |
720 | |
744 | |
721 | ############################################################################# |
745 | sub button_down { |
|
|
746 | my ($self, $ev) = @_; |
722 | |
747 | |
|
|
748 | $self->focus_in; |
|
|
749 | } |
|
|
750 | |
|
|
751 | sub mouse_motion { |
|
|
752 | my ($self, $ev, $x, $y) = @_; |
|
|
753 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
|
|
754 | } |
|
|
755 | |
|
|
756 | sub _draw { |
|
|
757 | my ($self) = @_; |
|
|
758 | |
|
|
759 | if ($FOCUS == $self) { |
|
|
760 | glColor 1, 1, 1; |
|
|
761 | } else { |
|
|
762 | glColor 0.7, 0.7, 0.7; |
|
|
763 | } |
|
|
764 | |
|
|
765 | glBegin GL_QUADS; |
|
|
766 | glVertex 0 , 0; |
|
|
767 | glVertex 0 , $self->{h} - 1; |
|
|
768 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
769 | glVertex $self->{w} - 1, 0; |
|
|
770 | glEnd; |
|
|
771 | |
|
|
772 | $self->SUPER::_draw; |
|
|
773 | } |
|
|
774 | |
|
|
775 | ############################################################################# |
|
|
776 | |
723 | package Crossfire::Client::Widget::MapWidget; |
777 | package CFClient::Widget::MapWidget; |
724 | |
778 | |
725 | use strict; |
779 | use strict; |
726 | |
780 | |
727 | use List::Util qw(min max); |
781 | use List::Util qw(min max); |
728 | |
782 | |
729 | use SDL; |
783 | use SDL; |
730 | use SDL::OpenGL; |
784 | use SDL::OpenGL; |
731 | use SDL::OpenGL::Constants; |
785 | use SDL::OpenGL::Constants; |
732 | |
786 | |
733 | our @ISA = Crossfire::Client::Widget::; |
787 | our @ISA = CFClient::Widget::; |
734 | |
788 | |
735 | sub key_down { |
789 | sub key_down { |
736 | print "MAPKEYDOWN\n"; |
790 | print "MAPKEYDOWN\n"; |
737 | } |
791 | } |
738 | |
792 | |
739 | sub key_up { |
793 | sub key_up { |
740 | } |
794 | } |
741 | |
795 | |
742 | sub size_request { |
796 | sub size_request { |
743 | |
797 | ( |
744 | } |
798 | 1 + int $::WIDTH / 32, |
745 | |
799 | 1 + int $::HEIGHT / 32, |
746 | sub size_allocate { |
800 | ) |
747 | } |
801 | } |
748 | |
802 | |
749 | sub _draw { |
803 | sub _draw { |
750 | my ($self) = @_; |
804 | my ($self) = @_; |
751 | |
805 | |
… | |
… | |
774 | glEnable GL_TEXTURE_2D; |
828 | glEnable GL_TEXTURE_2D; |
775 | glEnable GL_BLEND; |
829 | glEnable GL_BLEND; |
776 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
830 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
777 | |
831 | |
778 | my $sw4 = ($sw + 3) & ~3; |
832 | my $sw4 = ($sw + 3) & ~3; |
779 | my $lighting = "\x00" x ($sw4 * $sh); |
833 | my $darkness = "\x00" x ($sw4 * $sh); |
780 | |
834 | |
781 | for my $x (0 .. $sw - 1) { |
835 | for my $x (0 .. $sw - 1) { |
|
|
836 | my $row = $map->[$x + $xofs]; |
782 | for my $y (0 .. $sh - 1) { |
837 | for my $y (0 .. $sh - 1) { |
783 | |
838 | |
784 | my $cell = $map->[$x + $xofs][$y + $yofs] |
839 | my $cell = $row->[$y + $yofs] |
785 | or next; |
840 | or next; |
786 | |
841 | |
787 | my $darkness = $cell->[0] * (1 / 255); |
842 | my $dark = $cell->[0]; |
788 | if ($darkness < 0) { |
843 | if ($dark < 0) { |
789 | $darkness = 0.15; |
844 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
845 | } else { |
|
|
846 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
790 | } |
847 | } |
791 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
792 | |
848 | |
793 | for my $num (grep $_, @$cell[1,2,3]) { |
849 | for my $num (grep $_, @$cell[1,2,3]) { |
794 | my $tex = $::CONN->{face}[$num]{texture} || next; |
850 | my $tex = $::CONN->{face}[$num]{texture} || next; |
795 | |
851 | |
796 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
797 | |
|
|
798 | my $w = $tex->{width}; |
852 | my $w = $tex->{width}; |
799 | my $h = $tex->{height}; |
853 | my $h = $tex->{height}; |
800 | |
854 | |
801 | my $px = ($x + 1) * 32 - $w; |
855 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
802 | my $py = ($y + 1) * 32 - $h; |
|
|
803 | |
|
|
804 | glBegin GL_QUADS; |
|
|
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 | } |
856 | } |
811 | } |
857 | } |
812 | } |
858 | } |
813 | |
859 | |
814 | # if (1) { # higher quality darkness |
860 | # if (1) { # higher quality darkness |
… | |
… | |
819 | # |
865 | # |
820 | # $lighting = $pb->get_pixels; |
866 | # $lighting = $pb->get_pixels; |
821 | # $lighting =~ s/(.)../$1/gs; |
867 | # $lighting =~ s/(.)../$1/gs; |
822 | # } |
868 | # } |
823 | |
869 | |
824 | $lighting = new Crossfire::Client::Texture |
870 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
871 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
872 | |
|
|
873 | $darkness = new CFClient::Texture |
825 | width => $sw4, |
874 | width => $sw4, |
826 | height => $sh, |
875 | height => $sh, |
827 | data => $lighting, |
876 | data => $darkness, |
828 | internalformat => GL_ALPHA4, |
877 | internalformat => GL_ALPHA, |
829 | format => GL_ALPHA; |
878 | format => GL_ALPHA; |
830 | |
879 | |
831 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
880 | glColor 0.45, 0.45, 0.45, 1; |
832 | glColor 0, 0, 0, 0.75; |
881 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
833 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
834 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
835 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
836 | glBegin GL_QUADS; |
|
|
837 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
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 | |
882 | |
843 | glDisable GL_TEXTURE_2D; |
883 | glDisable GL_TEXTURE_2D; |
844 | glDisable GL_BLEND; |
884 | glDisable GL_BLEND; |
845 | } |
885 | } |
846 | |
886 | |
… | |
… | |
895 | } |
935 | } |
896 | } |
936 | } |
897 | |
937 | |
898 | ############################################################################# |
938 | ############################################################################# |
899 | |
939 | |
900 | package Crossfire::Client::Widget::Animator; |
940 | package CFClient::Widget::Animator; |
901 | |
941 | |
902 | use SDL::OpenGL; |
942 | use SDL::OpenGL; |
903 | |
943 | |
904 | our @ISA = Crossfire::Client::Widget::Bin::; |
944 | our @ISA = CFClient::Widget::Bin::; |
905 | |
945 | |
906 | sub moveto { |
946 | sub moveto { |
907 | my ($self, $x, $y) = @_; |
947 | my ($self, $x, $y) = @_; |
908 | |
948 | |
909 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
949 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
910 | $self->{speed} = 0.2; |
950 | $self->{speed} = 0.001; |
911 | $self->{time} = 1; |
951 | $self->{time} = 1; |
912 | |
952 | |
913 | ::animation_start $self; |
953 | ::animation_start $self; |
914 | } |
954 | } |
915 | |
955 | |
… | |
… | |
930 | |
970 | |
931 | sub _draw { |
971 | sub _draw { |
932 | my ($self) = @_; |
972 | my ($self) = @_; |
933 | |
973 | |
934 | glPushMatrix; |
974 | glPushMatrix; |
935 | glRotate $self->{time} * 10000, 0, 1, 0; |
975 | glRotate $self->{time} * 1000, 0, 1, 0; |
936 | $self->{children}[0]->draw; |
976 | $self->{children}[0]->draw; |
937 | glPopMatrix; |
977 | glPopMatrix; |
938 | } |
978 | } |
939 | |
979 | |
940 | 1; |
980 | ############################################################################# |
941 | |
981 | |
|
|
982 | package CFClient::Widget::Toplevel; |
|
|
983 | |
|
|
984 | our @ISA = CFClient::Widget::Container::; |
|
|
985 | |
|
|
986 | sub size_request { |
|
|
987 | ($::WIDTH, $::HEIGHT) |
|
|
988 | } |
|
|
989 | |
|
|
990 | sub size_allocate { |
|
|
991 | my ($self, $w, $h) = @_; |
|
|
992 | |
|
|
993 | $self->SUPER::size_allocate ($w, $h); |
|
|
994 | |
|
|
995 | $_->size_allocate ($_->size_request) |
|
|
996 | for @{$self->{children}}; |
|
|
997 | } |
|
|
998 | |
|
|
999 | sub translate { |
|
|
1000 | my ($self, $x, $y) = @_; |
|
|
1001 | |
|
|
1002 | ($x, $y) |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | sub update { |
|
|
1006 | my ($self) = @_; |
|
|
1007 | |
|
|
1008 | $self->size_allocate ($self->size_request); |
|
|
1009 | ::refresh (); |
|
|
1010 | } |
|
|
1011 | |
|
|
1012 | sub add { |
|
|
1013 | my ($self, $widget) = @_; |
|
|
1014 | |
|
|
1015 | $self->SUPER::add ($widget); |
|
|
1016 | |
|
|
1017 | $widget->size_allocate ($widget->size_request); |
|
|
1018 | } |
|
|
1019 | |
|
|
1020 | sub draw { |
|
|
1021 | my ($self) = @_; |
|
|
1022 | |
|
|
1023 | $self->_draw; |
|
|
1024 | } |
|
|
1025 | |
|
|
1026 | ############################################################################# |
|
|
1027 | |
|
|
1028 | package CFClient::Widget; |
|
|
1029 | |
|
|
1030 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
|
|
1031 | |
|
|
1032 | 1 |
|
|
1033 | |