1 | package Crossfire::Client::Widget; |
1 | package Crossfire::Client::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
|
|
4 | |
|
|
5 | use Scalar::Util; |
|
|
6 | |
4 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
5 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
6 | |
9 | |
7 | our $FOCUS; # the widget with current focus |
10 | our $FOCUS; # the widget with current focus |
8 | our @ACTIVE_WIDGETS; |
|
|
9 | |
11 | |
10 | # class methods for events |
12 | # class methods for events |
11 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
13 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
12 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
14 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
13 | sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } |
15 | sub feed_sdl_button_down_event { } |
14 | sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } |
16 | sub feed_sdl_button_up_event { } |
15 | |
17 | |
16 | sub new { |
18 | sub new { |
17 | my $class = shift; |
19 | my $class = shift; |
18 | |
20 | |
19 | bless { @_ }, $class |
21 | bless { @_ }, $class |
20 | } |
22 | } |
21 | |
23 | |
22 | sub activate { |
24 | sub move { |
23 | push @ACTIVE_WIDGETS, $_[0]; |
25 | my ($self, $x, $y, $z) = @_; |
|
|
26 | $self->{x} = $x; |
|
|
27 | $self->{y} = $y; |
|
|
28 | $self->{z} = $z if defined $z; |
24 | } |
29 | } |
25 | |
30 | |
26 | sub deactivate { |
31 | sub needs_redraw { |
27 | @ACTIVE_WIDGETS = |
32 | 0 |
28 | sort { $a->{z} <=> $b->{z} } |
|
|
29 | grep { $_ != $_[0] } |
|
|
30 | @ACTIVE_WIDGETS; |
|
|
31 | } |
33 | } |
32 | |
34 | |
33 | sub size_request { |
35 | sub size_request { |
|
|
36 | require Carp; |
34 | die "size_request is abtract"; |
37 | Carp::confess "size_request is abtract"; |
|
|
38 | } |
|
|
39 | |
|
|
40 | sub size_allocate { |
|
|
41 | my ($self, $w, $h) = @_; |
|
|
42 | $self->w ($w); |
|
|
43 | $self->h ($h); |
35 | } |
44 | } |
36 | |
45 | |
37 | sub focus_in { |
46 | sub focus_in { |
38 | my ($widget) = @_; |
47 | my ($widget) = @_; |
39 | $FOCUS = $widget; |
48 | $FOCUS = $widget; |
… | |
… | |
57 | |
66 | |
58 | sub button_up { |
67 | sub button_up { |
59 | my ($widget, $sdlev) = @_; |
68 | my ($widget, $sdlev) = @_; |
60 | } |
69 | } |
61 | |
70 | |
62 | sub w { $_[0]->{w} } |
71 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
63 | sub h { $_[0]->{h} } |
72 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
64 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
73 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
65 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
74 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
66 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
75 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
67 | |
76 | |
68 | sub draw { |
77 | sub draw { |
… | |
… | |
77 | sub _draw { |
86 | sub _draw { |
78 | my ($widget) = @_; |
87 | my ($widget) = @_; |
79 | } |
88 | } |
80 | |
89 | |
81 | sub bbox { |
90 | sub bbox { |
82 | my ($widget) = @_; |
91 | my ($self) = @_; |
|
|
92 | my ($w, $h) = $self->size_request; |
|
|
93 | ( |
|
|
94 | $self->{x}, |
|
|
95 | $self->{y}, |
|
|
96 | $self->{x} = $w, |
|
|
97 | $self->{y} = $h |
|
|
98 | ) |
83 | } |
99 | } |
84 | |
100 | |
|
|
101 | sub del_parent { $_[0]->{parent} = undef } |
|
|
102 | |
|
|
103 | sub set_parent { |
|
|
104 | my ($self, $par) = @_; |
|
|
105 | |
|
|
106 | $self->{parent} = $par; |
|
|
107 | Scalar::Util::weaken $self->{parent}; |
|
|
108 | } |
|
|
109 | |
|
|
110 | sub get_parent { |
|
|
111 | $_[0]->{parent} |
|
|
112 | } |
|
|
113 | |
|
|
114 | sub update { |
|
|
115 | my ($self) = @_; |
|
|
116 | |
|
|
117 | $self->{parent}->update |
|
|
118 | if $self->{parent}; |
|
|
119 | } |
|
|
120 | |
|
|
121 | sub DESTROY { |
|
|
122 | my ($self) = @_; |
|
|
123 | |
|
|
124 | #$self->deactivate; |
|
|
125 | } |
|
|
126 | |
85 | package Crossfire::Client::Widget::Container; |
127 | package Crossfire::Client::Widget::Bin; |
86 | |
128 | |
87 | our @ISA = Crossfire::Client::Widget::; |
129 | our @ISA = Crossfire::Client::Widget::; |
88 | |
130 | |
89 | use SDL::OpenGL; |
131 | sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->{expand} = $_[2] } |
90 | |
|
|
91 | sub add { $_[0]->{child} = $_[1] } |
|
|
92 | sub get { $_[0]->{child} } |
132 | sub get { $_[0]->{child} } |
93 | |
133 | |
|
|
134 | sub remove { |
|
|
135 | my ($self, $chld) = @_; |
|
|
136 | delete $self->{child} |
|
|
137 | if $self->{child} == $chld; |
|
|
138 | } |
|
|
139 | |
|
|
140 | sub size_request { |
94 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
141 | $_[0]->{child}->size_request if $_[0]->{child} |
|
|
142 | } |
|
|
143 | sub size_allocate { |
|
|
144 | my ($self, $w, $h) = @_; |
|
|
145 | $self->SUPER::size_allocate ($w, $h); |
|
|
146 | $self->{child}->size_allocate ($w, $h) |
|
|
147 | if $self->{child} |
|
|
148 | } |
95 | |
149 | |
96 | sub _draw { die "Containers can't be drawn!" } |
150 | sub _draw { |
|
|
151 | my ($self) = @_; |
|
|
152 | |
|
|
153 | $self->{child}->draw; |
|
|
154 | } |
|
|
155 | |
|
|
156 | package Crossfire::Client::Widget::Toplevel; |
|
|
157 | |
|
|
158 | our @ISA = Crossfire::Client::Widget::; |
|
|
159 | |
|
|
160 | use SDL::OpenGL; |
|
|
161 | |
|
|
162 | sub add { |
|
|
163 | my ($self, $chld) = @_; |
|
|
164 | |
|
|
165 | push @{$self->{childs}}, $chld; |
|
|
166 | @{$self->{childs}} = |
|
|
167 | sort { $a->{z} <=> $b->{z} } |
|
|
168 | @{$self->{childs}}; |
|
|
169 | |
|
|
170 | $chld->set_parent ($self); |
|
|
171 | $chld->size_allocate ($chld->size_request); |
|
|
172 | } |
|
|
173 | |
|
|
174 | sub remove { |
|
|
175 | my ($self, $chld) = @_; |
|
|
176 | @{$self->{childs}} = |
|
|
177 | sort { $a->{z} <=> $b->{z} } |
|
|
178 | grep { $_ && $_ != $_[0] } |
|
|
179 | @{$self->{childs}} |
|
|
180 | } |
|
|
181 | |
|
|
182 | sub update { |
|
|
183 | my ($self) = @_; |
|
|
184 | ::refresh (); |
|
|
185 | } |
|
|
186 | |
|
|
187 | sub _draw { |
|
|
188 | my ($self) = @_; |
|
|
189 | |
|
|
190 | $_->draw for @{$self->{childs}}; |
|
|
191 | } |
|
|
192 | |
|
|
193 | package Crossfire::Client::Widget::Window; |
|
|
194 | |
|
|
195 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
196 | |
|
|
197 | use SDL::OpenGL; |
|
|
198 | |
|
|
199 | sub add { |
|
|
200 | my ($self, $chld) = @_; |
|
|
201 | warn "ADD $chld\n"; |
|
|
202 | $self->SUPER::add ($chld); |
|
|
203 | $chld->set_parent ($self); |
|
|
204 | } |
|
|
205 | |
|
|
206 | sub remove { |
|
|
207 | my ($self) = @_; |
|
|
208 | # TODO FIXME: removing a child from a window will crash, see render_chld |
|
|
209 | # $self->update; |
|
|
210 | } |
|
|
211 | |
|
|
212 | sub update { |
|
|
213 | my ($self) = @_; |
|
|
214 | $self->render_chld; |
|
|
215 | } |
|
|
216 | |
|
|
217 | sub render_chld { |
|
|
218 | my ($self) = @_; |
|
|
219 | my $chld = $self->get; |
|
|
220 | my ($w, $h) = $self->size_request; |
|
|
221 | |
|
|
222 | require Carp; |
|
|
223 | Carp::cluck "RENDERCHI $w $h"; |
|
|
224 | warn "RENDERCHI $w $h\n"; |
|
|
225 | $self->{texture} = |
|
|
226 | Crossfire::Client::Texture->new_from_opengl ( |
|
|
227 | $w, $h, sub { $chld->draw } |
|
|
228 | ); |
|
|
229 | $self->{texture}->upload; |
|
|
230 | } |
|
|
231 | |
|
|
232 | sub size_request { |
|
|
233 | my ($self) = @_; |
|
|
234 | ($self->w, $self->h) |
|
|
235 | } |
|
|
236 | |
|
|
237 | sub size_allocate { |
|
|
238 | my ($self, $w, $h) = @_; |
|
|
239 | |
|
|
240 | $self->w ($w); |
|
|
241 | $self->h ($h); |
|
|
242 | $self->get->size_allocate ($w, $h); |
|
|
243 | |
|
|
244 | $self->update; #TODO: Move this to the size_request event propably? |
|
|
245 | } |
|
|
246 | |
|
|
247 | sub _draw { |
|
|
248 | my ($self) = @_; |
|
|
249 | |
|
|
250 | my ($w, $h) = ($self->w, $self->h); |
|
|
251 | |
|
|
252 | my $tex = $self->{texture} |
|
|
253 | or return; |
|
|
254 | |
|
|
255 | glEnable GL_BLEND; |
|
|
256 | glEnable GL_TEXTURE_2D; |
|
|
257 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
258 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
259 | |
|
|
260 | glBegin GL_QUADS; |
|
|
261 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
262 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
263 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
264 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
265 | glEnd; |
|
|
266 | |
|
|
267 | glDisable GL_BLEND; |
|
|
268 | glDisable GL_TEXTURE_2D; |
|
|
269 | } |
97 | |
270 | |
98 | package Crossfire::Client::Widget::Frame; |
271 | package Crossfire::Client::Widget::Frame; |
99 | |
272 | |
100 | our @ISA = Crossfire::Client::Widget::Container::; |
273 | our @ISA = Crossfire::Client::Widget::Bin::; |
101 | |
274 | |
102 | use SDL::OpenGL; |
275 | use SDL::OpenGL; |
103 | |
276 | |
104 | sub size_request { |
277 | sub size_request { |
105 | my ($self) = @_; |
278 | my ($self) = @_; |
106 | my $chld = $self->get |
279 | my $chld = $self->get |
107 | or return (0, 0); |
280 | or return (0, 0); |
|
|
281 | |
|
|
282 | $chld->move (2, 2); |
|
|
283 | |
108 | map { $_ + 4 } $chld->size_request; |
284 | map { $_ + 4 } $chld->size_request; |
109 | } |
285 | } |
110 | |
286 | |
|
|
287 | sub size_allocate { |
|
|
288 | my ($self, $w, $h) = @_; |
|
|
289 | |
|
|
290 | $self->w ($w); |
|
|
291 | $self->h ($h); |
|
|
292 | |
|
|
293 | $self->get->size_allocate ($w - 4, $h - 4); |
|
|
294 | $self->get->move (2, 2); |
|
|
295 | } |
|
|
296 | |
111 | sub _draw { |
297 | sub _draw { |
112 | my ($self) = @_; |
298 | my ($self) = @_; |
113 | |
299 | |
114 | my $chld = $self->get; |
300 | my $chld = $self->get; |
115 | |
301 | |
116 | my ($w, $h) = $chld->size_request; |
302 | my ($w, $h) = $chld->size_request; |
117 | |
303 | |
118 | glColor 1, 0, 0; |
|
|
119 | glBegin GL_QUADS; |
304 | glBegin GL_QUADS; |
|
|
305 | glColor 0, 0, 0; |
120 | glTexCoord 0, 0; glVertex 0 , 0; |
306 | glTexCoord 0, 0; glVertex 0 , 0; |
121 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
307 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
122 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
308 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
123 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
309 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
124 | glEnd; |
310 | glEnd; |
125 | |
311 | |
126 | glPushMatrix; |
|
|
127 | glTranslate (2, 2, 0); |
|
|
128 | $chld->_draw; |
312 | $chld->draw; |
129 | glPopMatrix; |
313 | } |
|
|
314 | |
|
|
315 | package Crossfire::Client::Widget::FancyFrame; |
|
|
316 | |
|
|
317 | our @ISA = Crossfire::Client::Widget::Frame::; |
|
|
318 | |
|
|
319 | use SDL::OpenGL; |
|
|
320 | |
|
|
321 | sub new { |
|
|
322 | my ($self, $theme) = @_; |
|
|
323 | $self = $self->SUPER::new; |
|
|
324 | |
|
|
325 | $self->{txts} = [ |
|
|
326 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
|
|
327 | qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ |
|
|
328 | ]; |
|
|
329 | $self |
|
|
330 | } |
|
|
331 | |
|
|
332 | sub size_request { |
|
|
333 | my ($self) = @_; |
|
|
334 | my ($w, $h) = $self->get->size_request; |
|
|
335 | |
|
|
336 | $h += $self->{txts}->[1]->{height}; |
|
|
337 | $h += $self->{txts}->[4]->{height}; |
|
|
338 | $w += $self->{txts}->[2]->{width}; |
|
|
339 | $w += $self->{txts}->[3]->{width}; |
|
|
340 | |
|
|
341 | ($w, $h) |
|
|
342 | } |
|
|
343 | |
|
|
344 | sub size_allocate { |
|
|
345 | my ($self, $w, $h) = @_; |
|
|
346 | |
|
|
347 | $self->w ($w); |
|
|
348 | $self->h ($h); |
|
|
349 | $h -= $self->{txts}->[1]->{height}; |
|
|
350 | $h -= $self->{txts}->[4]->{height}; |
|
|
351 | $w -= $self->{txts}->[2]->{width}; |
|
|
352 | $w -= $self->{txts}->[3]->{width}; |
|
|
353 | |
|
|
354 | $h = $h < 0 ? 0 : $h; |
|
|
355 | $w = $w < 0 ? 0 : $w; |
|
|
356 | warn "CHILD:$w $h\n"; |
|
|
357 | $self->get->size_allocate ($w, $h); |
|
|
358 | $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
|
|
359 | } |
|
|
360 | |
|
|
361 | sub _draw { |
|
|
362 | my ($self) = @_; |
|
|
363 | |
|
|
364 | my ($w, $h) = ($self->w, $self->h); |
|
|
365 | my ($cw, $ch) = ($self->get->w, $self->get->h); |
|
|
366 | |
|
|
367 | glEnable GL_BLEND; |
|
|
368 | glEnable GL_TEXTURE_2D; |
|
|
369 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
370 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
371 | |
|
|
372 | my $top = $self->{txts}->[1]; |
|
|
373 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
|
|
374 | |
|
|
375 | glBegin GL_QUADS; |
|
|
376 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
377 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
378 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
379 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
380 | glEnd; |
|
|
381 | |
|
|
382 | my $left = $self->{txts}->[3]; |
|
|
383 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
|
|
384 | |
|
|
385 | glBegin GL_QUADS; |
|
|
386 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
387 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
388 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
389 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
390 | glEnd; |
|
|
391 | |
|
|
392 | my $right = $self->{txts}->[2]; |
|
|
393 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
|
|
394 | |
|
|
395 | glBegin GL_QUADS; |
|
|
396 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
397 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
398 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
399 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
400 | glEnd; |
|
|
401 | |
|
|
402 | my $bottom = $self->{txts}->[4]; |
|
|
403 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
|
|
404 | |
|
|
405 | glBegin GL_QUADS; |
|
|
406 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
407 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
408 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
409 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
410 | glEnd; |
|
|
411 | |
|
|
412 | my $bg = $self->{txts}->[0]; |
|
|
413 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
414 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
415 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
416 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
417 | |
|
|
418 | my $rep_x = $cw / $bg->{width}; |
|
|
419 | my $rep_y = $ch / $bg->{height}; |
|
|
420 | |
|
|
421 | glBegin GL_QUADS; |
|
|
422 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
423 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
424 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
425 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
426 | glEnd; |
|
|
427 | |
|
|
428 | glDisable GL_BLEND; |
|
|
429 | glDisable GL_TEXTURE_2D; |
|
|
430 | |
|
|
431 | $self->get->draw; |
|
|
432 | |
130 | } |
433 | } |
131 | |
434 | |
132 | package Crossfire::Client::Widget::Table; |
435 | package Crossfire::Client::Widget::Table; |
133 | |
436 | |
134 | our @ISA = Crossfire::Client::Widget::Container::; |
437 | our @ISA = Crossfire::Client::Widget::Bin::; |
135 | |
438 | |
136 | use SDL::OpenGL; |
439 | use SDL::OpenGL; |
137 | |
440 | |
138 | sub add { |
441 | sub add { |
139 | my ($self, $x, $y, $chld) = @_; |
442 | my ($self, $x, $y, $chld) = @_; |
|
|
443 | my $old_chld = $self->{childs}[$y][$x]; |
|
|
444 | |
140 | $self->{childs}[$y][$x] = $chld; |
445 | $self->{childs}[$y][$x] = $chld; |
|
|
446 | $chld->set_parent ($self); |
|
|
447 | $self->update; |
141 | } |
448 | } |
142 | |
449 | |
143 | sub max_row_height { |
450 | sub max_row_height { |
144 | my ($self, $row) = @_; |
451 | my ($self, $row) = @_; |
145 | |
452 | |
146 | my $hs = 0; |
453 | my $hs = 0; |
147 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
454 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
148 | my $c = $self->{childs}->[$row]->[$xi]; |
455 | my $c = $self->{childs}->[$row]->[$xi]; |
|
|
456 | if ($c) { |
149 | my ($w, $h) = $c->size_request if $c; |
457 | my ($w, $h) = $c->size_request; |
150 | if ($hs < $h) { $hs = $h } |
458 | if ($hs < $h) { $hs = $h } |
|
|
459 | } |
151 | } |
460 | } |
152 | return $hs; |
461 | return $hs; |
153 | } |
462 | } |
154 | |
463 | |
155 | sub max_col_width { |
464 | sub max_col_width { |
156 | my ($self, $col) = @_; |
465 | my ($self, $col) = @_; |
157 | |
466 | |
158 | my $ws = 0; |
467 | my $ws = 0; |
159 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
468 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
160 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
469 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
|
|
470 | if ($c) { |
161 | my ($w, $h) = $c->size_request if $c; |
471 | my ($w, $h) = $c->size_request; |
162 | if ($ws < $w) { $ws = $w } |
472 | if ($ws < $w) { $ws = $w } |
|
|
473 | } |
163 | } |
474 | } |
164 | return $ws; |
475 | return $ws; |
165 | } |
476 | } |
166 | |
477 | |
167 | sub size_request { |
478 | sub size_request { |
… | |
… | |
191 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
502 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
192 | my $x = 0; |
503 | my $x = 0; |
193 | |
504 | |
194 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
505 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
195 | |
506 | |
196 | glPushMatrix; |
|
|
197 | glTranslate ($x, $y, 0); |
|
|
198 | my $c = $self->{childs}->[$yi]->[$xi]; |
507 | my $c = $self->{childs}->[$yi]->[$xi]; |
|
|
508 | if ($c) { |
|
|
509 | $c->move ($x, $y, 0); #TODO: Move to size_request |
199 | $c->_draw if $c; |
510 | $c->draw if $c; |
200 | glPopMatrix; |
511 | } |
201 | |
512 | |
202 | $x += $self->max_col_width ($xi); |
513 | $x += $self->max_col_width ($xi); |
203 | } |
514 | } |
204 | |
515 | |
205 | $y += $self->max_row_height ($yi); |
516 | $y += $self->max_row_height ($yi); |
206 | } |
517 | } |
207 | } |
518 | } |
208 | |
519 | |
209 | package Crossfire::Client::Widget::VBox; |
520 | package Crossfire::Client::Widget::VBox; |
210 | |
521 | |
211 | our @ISA = Crossfire::Client::Widget::Container::; |
522 | our @ISA = Crossfire::Client::Widget::Bin::; |
212 | |
523 | |
213 | use SDL::OpenGL; |
524 | use SDL::OpenGL; |
214 | |
525 | |
215 | sub add { |
526 | sub add { |
216 | my ($self, $chld) = @_; |
527 | my ($self, $chld, $expand) = @_; |
217 | push @{$self->{childs}}, $chld; |
528 | push @{$self->{childs}}, $chld; |
|
|
529 | $chld->{expand} = $expand; |
|
|
530 | $chld->set_parent ($self); |
|
|
531 | $self->update; |
218 | } |
532 | } |
219 | |
533 | |
220 | sub size_request { |
534 | sub size_request { |
221 | my ($self) = @_; |
535 | my ($self) = @_; |
222 | |
536 | |
… | |
… | |
228 | } |
542 | } |
229 | |
543 | |
230 | return ($ws, $hs); |
544 | return ($ws, $hs); |
231 | } |
545 | } |
232 | |
546 | |
|
|
547 | sub size_allocate { |
|
|
548 | my ($self, $w, $h) = @_; |
|
|
549 | |
|
|
550 | $self->w ($w); |
|
|
551 | $self->h ($h); |
|
|
552 | |
|
|
553 | my $exp; |
|
|
554 | my @oth; |
|
|
555 | # find expand widget |
|
|
556 | for (@{$self->{childs}}) { |
|
|
557 | if ($_->{expand}) { |
|
|
558 | $exp = $_; |
|
|
559 | last; |
|
|
560 | } |
|
|
561 | push @oth, $_; |
|
|
562 | } |
|
|
563 | |
|
|
564 | my ($ow, $oh); |
|
|
565 | |
|
|
566 | # get sizes of other widgets |
|
|
567 | for (@oth) { |
|
|
568 | my ($w, $h) = $_->size_request; |
|
|
569 | $oh += $h; |
|
|
570 | if ($ow < $w) { $ow = $w } |
|
|
571 | } |
|
|
572 | |
|
|
573 | my $y = 0; |
|
|
574 | for (@{$self->{childs}}) { |
|
|
575 | $_->move (0, $y); |
|
|
576 | |
|
|
577 | if ($_ == $exp) { |
|
|
578 | $_->size_allocate ($w, $h - $oh); |
|
|
579 | $y += $h - $oh; |
|
|
580 | } else { |
|
|
581 | my ($cw, $h) = $_->size_request; |
|
|
582 | $_->size_allocate ($w, $h); |
|
|
583 | $y += $h; |
|
|
584 | } |
|
|
585 | } |
|
|
586 | } |
|
|
587 | |
233 | sub _draw { |
588 | sub _draw { |
234 | my ($self) = @_; |
589 | my ($self) = @_; |
235 | |
590 | |
236 | my ($x, $y); |
591 | my ($x, $y); |
237 | for (@{$self->{childs} || []}) { |
592 | for (@{$self->{childs} || []}) { |
238 | glPushMatrix; |
|
|
239 | glTranslate (0, $y, 0); |
|
|
240 | $_->_draw; |
593 | $_->draw; |
241 | glPopMatrix; |
|
|
242 | my ($w, $h) = $_->size_request; |
|
|
243 | $y += $h; |
594 | $y += $_->h; |
244 | } |
595 | } |
245 | } |
596 | } |
246 | |
597 | |
247 | package Crossfire::Client::Widget::Label; |
598 | package Crossfire::Client::Widget::Label; |
248 | |
599 | |
249 | our @ISA = Crossfire::Client::Widget::; |
600 | our @ISA = Crossfire::Client::Widget::; |
250 | |
601 | |
251 | use SDL::OpenGL; |
602 | use SDL::OpenGL; |
252 | |
603 | |
253 | sub new { |
604 | sub new { |
254 | my ($class, $x, $y, $z, $ttf, $text) = @_; |
605 | my ($class, $x, $y, $z, $height, $text) = @_; |
255 | |
606 | |
|
|
607 | # TODO: color, and make height, xyz etc. optional |
256 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); |
608 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
257 | |
609 | |
258 | $self->set_text ($text); |
610 | $self->set_text ($text); |
259 | |
611 | |
260 | $self |
612 | $self |
261 | } |
613 | } |
262 | |
614 | |
263 | sub set_text { |
615 | sub set_text { |
264 | my ($self, $text) = @_; |
616 | my ($self, $text) = @_; |
|
|
617 | |
|
|
618 | $self->{text} = $text; |
265 | $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; |
619 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
|
|
620 | |
|
|
621 | $self->update; |
266 | } |
622 | } |
267 | |
623 | |
268 | sub get_text { |
624 | sub get_text { |
269 | my ($self, $text) = @_; |
625 | my ($self, $text) = @_; |
|
|
626 | |
270 | $self->{text} |
627 | $self->{text} |
271 | } |
628 | } |
272 | |
629 | |
273 | sub size_request { |
630 | sub size_request { |
274 | my ($self) = @_; |
631 | my ($self) = @_; |
… | |
… | |
282 | sub _draw { |
639 | sub _draw { |
283 | my ($self) = @_; |
640 | my ($self) = @_; |
284 | |
641 | |
285 | my $tex = $self->{texture}; |
642 | my $tex = $self->{texture}; |
286 | |
643 | |
287 | $self->{x}--; |
|
|
288 | |
|
|
289 | glEnable GL_BLEND; |
644 | glEnable GL_BLEND; |
290 | glEnable GL_TEXTURE_2D; |
645 | glEnable GL_TEXTURE_2D; |
|
|
646 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
291 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
647 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
292 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
648 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
293 | |
649 | |
294 | glColor 1, 1, 1; |
650 | glColor 1, 0, 0, 1; # TODO color |
295 | |
651 | |
296 | glBegin GL_QUADS; |
652 | glBegin GL_QUADS; |
297 | glTexCoord 0, 0; glVertex 0 , 0; |
653 | glTexCoord 0, 0; glVertex 0 , 0; |
298 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
654 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
299 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
655 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
… | |
… | |
302 | |
658 | |
303 | glDisable GL_BLEND; |
659 | glDisable GL_BLEND; |
304 | glDisable GL_TEXTURE_2D; |
660 | glDisable GL_TEXTURE_2D; |
305 | } |
661 | } |
306 | |
662 | |
307 | package Crossfire::Client::Widget::TextView; |
663 | package Crossfire::Client::Widget::TextEntry; |
|
|
664 | |
|
|
665 | our @ISA = Crossfire::Client::Widget::Label::; |
|
|
666 | |
|
|
667 | use SDL; |
|
|
668 | use SDL::OpenGL; |
|
|
669 | |
|
|
670 | sub key_down { |
|
|
671 | my ($self, $ev) = @_; |
|
|
672 | |
|
|
673 | my $mod = $ev->key_mod; |
|
|
674 | my $sym = $ev->key_sym; |
|
|
675 | |
|
|
676 | $ev->set_unicode (1); |
|
|
677 | my $uni = $ev->key_unicode; |
|
|
678 | |
|
|
679 | my $text = $self->get_text; |
|
|
680 | |
|
|
681 | if ($sym == SDLK_BACKSPACE) { |
|
|
682 | substr $text, -1, 1, ''; |
|
|
683 | |
|
|
684 | } elsif ($uni) { |
|
|
685 | $text .= chr $uni; |
|
|
686 | } |
|
|
687 | $self->set_text ($text); |
|
|
688 | } |
|
|
689 | |
|
|
690 | package Crossfire::Client::Widget::MapWidget; |
308 | |
691 | |
309 | use strict; |
692 | use strict; |
310 | |
693 | |
311 | our @ISA = qw/Crossfire::Client::Widget/; |
694 | use List::Util qw(min max); |
312 | |
695 | |
|
|
696 | use SDL; |
313 | use SDL::OpenGL; |
697 | use SDL::OpenGL; |
314 | use SDL::OpenGL::Constants; |
698 | use SDL::OpenGL::Constants; |
315 | |
699 | |
316 | sub add_line { |
|
|
317 | my ($self, $line) = @_; |
|
|
318 | push @{$self->{lines}}, $line; |
|
|
319 | } |
|
|
320 | |
|
|
321 | sub _draw { |
|
|
322 | my ($self) = @_; |
|
|
323 | |
|
|
324 | } |
|
|
325 | |
|
|
326 | package Crossfire::Client::Widget::MapWidget; |
|
|
327 | |
|
|
328 | use strict; |
|
|
329 | |
|
|
330 | our @ISA = qw/Crossfire::Client::Widget/; |
700 | our @ISA = Crossfire::Client::Widget::; |
331 | |
|
|
332 | use SDL; |
|
|
333 | use SDL::OpenGL; |
|
|
334 | use SDL::OpenGL::Constants; |
|
|
335 | |
701 | |
336 | sub key_down { |
702 | sub key_down { |
337 | print "MAPKEYDOWN\n"; |
703 | print "MAPKEYDOWN\n"; |
338 | } |
704 | } |
339 | |
705 | |
340 | sub key_up { |
706 | sub key_up { |
341 | } |
707 | } |
342 | |
708 | |
|
|
709 | sub size_request { |
|
|
710 | |
|
|
711 | } |
|
|
712 | |
|
|
713 | sub size_allocate { |
|
|
714 | } |
|
|
715 | |
343 | sub _draw { |
716 | sub _draw { |
|
|
717 | my ($self) = @_; |
|
|
718 | |
|
|
719 | my $mx = $::CONN->{mapx}; |
|
|
720 | my $my = $::CONN->{mapy}; |
|
|
721 | |
|
|
722 | my $map = $::CONN->{map}; |
|
|
723 | |
|
|
724 | my ($xofs, $yofs); |
|
|
725 | |
|
|
726 | my $sw = 1 + int $::WIDTH / 32; |
|
|
727 | my $sh = 1 + int $::HEIGHT / 32; |
|
|
728 | |
|
|
729 | if ($::CONN->{mapw} > $sw) { |
|
|
730 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
|
|
731 | } else { |
|
|
732 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
|
|
733 | } |
|
|
734 | |
|
|
735 | if ($::CONN->{maph} > $sh) { |
|
|
736 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
|
|
737 | } else { |
|
|
738 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
|
|
739 | } |
|
|
740 | |
344 | glEnable GL_TEXTURE_2D; |
741 | glEnable GL_TEXTURE_2D; |
345 | glEnable GL_BLEND; |
742 | glEnable GL_BLEND; |
346 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
743 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
347 | |
744 | |
348 | my $map = $::CONN->{map}; |
745 | my $sw4 = ($sw + 3) & ~3; |
|
|
746 | my $lighting = "\x00" x ($sw4 * $sh); |
349 | |
747 | |
350 | for my $x (0 .. $::CONN->{mapw} - 1) { |
748 | for my $x (0 .. $sw - 1) { |
351 | for my $y (0 .. $::CONN->{maph} - 1) { |
749 | for my $y (0 .. $sh - 1) { |
352 | |
750 | |
353 | my $cell = $map->[$x][$y] |
751 | my $cell = $map->[$x + $xofs][$y + $yofs] |
354 | or next; |
752 | or next; |
355 | |
753 | |
356 | my $darkness = $cell->[3] * (1 / 255); |
754 | my $darkness = $cell->[0] * (1 / 255); |
357 | glColor $darkness, $darkness, $darkness; |
755 | if ($darkness < 0) { |
|
|
756 | $darkness = 0.15; |
|
|
757 | } |
|
|
758 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
358 | |
759 | |
359 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
760 | for my $num (grep $_, @$cell[1,2,3]) { |
360 | my $tex = $::CONN->{face}[$num]{texture} || next; |
761 | my $tex = $::CONN->{face}[$num]{texture} || next; |
361 | |
762 | |
362 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
763 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
363 | |
764 | |
|
|
765 | my $w = $tex->{width}; |
|
|
766 | my $h = $tex->{height}; |
|
|
767 | |
|
|
768 | my $px = ($x + 1) * 32 - $w; |
|
|
769 | my $py = ($y + 1) * 32 - $h; |
|
|
770 | |
364 | glBegin GL_QUADS; |
771 | glBegin GL_QUADS; |
365 | glTexCoord 0, 0; glVertex $x * 32 , $y * 32; |
772 | glTexCoord 0, 0; glVertex $px , $py; |
366 | glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; |
773 | glTexCoord 0, 1; glVertex $px , $py + $h; |
367 | glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; |
774 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
368 | glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; |
775 | glTexCoord 1, 0; glVertex $px + $w, $py; |
369 | glEnd; |
776 | glEnd; |
370 | } |
777 | } |
371 | } |
778 | } |
372 | } |
779 | } |
373 | |
780 | |
|
|
781 | # if (1) { # higher quality darkness |
|
|
782 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
783 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
784 | # |
|
|
785 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
786 | # |
|
|
787 | # $lighting = $pb->get_pixels; |
|
|
788 | # $lighting =~ s/(.)../$1/gs; |
|
|
789 | # } |
|
|
790 | |
|
|
791 | $lighting = new Crossfire::Client::Texture |
|
|
792 | width => $sw4, |
|
|
793 | height => $sh, |
|
|
794 | data => $lighting, |
|
|
795 | internalformat => GL_ALPHA4, |
|
|
796 | format => GL_ALPHA; |
|
|
797 | |
|
|
798 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
799 | glColor 0, 0, 0, 0.75; |
|
|
800 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
801 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
802 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
803 | glBegin GL_QUADS; |
|
|
804 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
805 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
806 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
807 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
808 | glEnd; |
|
|
809 | |
374 | glDisable GL_TEXTURE_2D; |
810 | glDisable GL_TEXTURE_2D; |
375 | glDisable GL_BLEND; |
811 | glDisable GL_BLEND; |
376 | } |
812 | } |
377 | |
813 | |
378 | my %DIR = ( |
814 | my %DIR = ( |
379 | SDLK_KP8, [1, "north"], |
815 | SDLK_KP8, [1, "north"], |
380 | SDLK_KP9, [2, "northest"], |
816 | SDLK_KP9, [2, "northeast"], |
381 | SDLK_KP6, [3, "east"], |
817 | SDLK_KP6, [3, "east"], |
382 | SDLK_KP3, [4, "southeast"], |
818 | SDLK_KP3, [4, "southeast"], |
383 | SDLK_KP2, [5, "south"], |
819 | SDLK_KP2, [5, "south"], |
384 | SDLK_KP1, [6, "southwest"], |
820 | SDLK_KP1, [6, "southwest"], |
385 | SDLK_KP4, [7, "west"], |
821 | SDLK_KP4, [7, "west"], |
386 | SDLK_KP7, [8, "northwest"], |
822 | SDLK_KP7, [8, "northwest"], |
|
|
823 | |
|
|
824 | SDLK_UP, [1, "north"], |
|
|
825 | SDLK_RIGHT, [3, "east"], |
|
|
826 | SDLK_DOWN, [5, "south"], |
|
|
827 | SDLK_LEFT, [7, "west"], |
387 | ); |
828 | ); |
388 | |
829 | |
389 | sub key_down { |
830 | sub key_down { |
390 | my ($self, $ev) = @_; |
831 | my ($self, $ev) = @_; |
391 | |
832 | |
… | |
… | |
394 | |
835 | |
395 | if ($sym == SDLK_KP5) { |
836 | if ($sym == SDLK_KP5) { |
396 | $::CONN->send ("command stay fire"); |
837 | $::CONN->send ("command stay fire"); |
397 | } elsif (exists $DIR{$sym}) { |
838 | } elsif (exists $DIR{$sym}) { |
398 | if ($mod & KMOD_SHIFT) { |
839 | if ($mod & KMOD_SHIFT) { |
|
|
840 | $self->{shft}++; |
399 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
841 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
400 | } elsif ($mod & KMOD_CTRL) { |
842 | } elsif ($mod & KMOD_CTRL) { |
|
|
843 | $self->{ctrl}++; |
401 | $::CONN->send ("command run $DIR{$sym}[0]"); |
844 | $::CONN->send ("command run $DIR{$sym}[0]"); |
402 | } else { |
845 | } else { |
|
|
846 | $::CONN->send ("command $DIR{$sym}[1]"); |
403 | } |
847 | } |
404 | } |
848 | } |
405 | } |
849 | } |
406 | |
850 | |
407 | sub key_up { |
851 | sub key_up { |
408 | my ($self, $ev) = @_; |
852 | my ($self, $ev) = @_; |
409 | |
853 | |
410 | my $mod = $ev->key_mod; |
854 | my $mod = $ev->key_mod; |
411 | my $sym = $ev->key_sym; |
855 | my $sym = $ev->key_sym; |
412 | |
856 | |
413 | if (exists $DIR{$sym}) { |
857 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
414 | if ($mod & KMOD_SHIFT) { |
|
|
415 | $::CONN->send ("command fire_stop"); |
858 | $::CONN->send ("command fire_stop"); |
416 | } else { |
859 | } |
|
|
860 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
417 | $::CONN->send ("command run_stop"); |
861 | $::CONN->send ("command run_stop"); |
|
|
862 | } |
|
|
863 | } |
|
|
864 | |
|
|
865 | package Crossfire::Client::Widget::Animator; |
|
|
866 | |
|
|
867 | use SDL::OpenGL; |
|
|
868 | |
|
|
869 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
870 | |
|
|
871 | sub moveto { |
|
|
872 | my ($self, $x, $y) = @_; |
|
|
873 | |
|
|
874 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
|
|
875 | $self->{speed} = 0.01; |
|
|
876 | $self->{time} = 1; |
|
|
877 | |
|
|
878 | ::animation_start $self; |
|
|
879 | } |
|
|
880 | |
|
|
881 | sub animate { |
|
|
882 | my ($self, $interval) = @_; |
|
|
883 | |
|
|
884 | $self->{time} -= $interval * $self->{speed}; |
|
|
885 | if ($self->{time} <= 0) { |
|
|
886 | $self->{time} = 0; |
|
|
887 | ::animation_stop $self; |
|
|
888 | } |
|
|
889 | |
|
|
890 | my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; |
418 | } |
891 | |
419 | } |
892 | $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); |
|
|
893 | $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); |
|
|
894 | } |
|
|
895 | |
|
|
896 | sub _draw { |
|
|
897 | my ($self) = @_; |
|
|
898 | |
|
|
899 | glPushMatrix; |
|
|
900 | glRotate $self->{time} * 10000, 0, 1, 0; |
|
|
901 | $self->{child}->draw; |
|
|
902 | glPopMatrix; |
420 | } |
903 | } |
421 | |
904 | |
422 | 1; |
905 | 1; |
423 | |
906 | |