1 | package Crossfire::Client::Widget; |
1 | package CFClient::UI; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util (); |
|
|
6 | use List::Util (); |
|
|
7 | |
|
|
8 | use CFClient; |
|
|
9 | |
|
|
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
11 | |
|
|
12 | our $TOPLEVEL; |
|
|
13 | our $BUTTON_STATE; |
|
|
14 | |
|
|
15 | # class methods for events |
|
|
16 | sub feed_sdl_key_down_event { |
|
|
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
|
|
18 | } |
|
|
19 | |
|
|
20 | sub feed_sdl_key_up_event { |
|
|
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
|
|
22 | } |
|
|
23 | |
|
|
24 | sub feed_sdl_button_down_event { |
|
|
25 | my ($ev) = @_; |
|
|
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
27 | |
|
|
28 | if (!$BUTTON_STATE) { |
|
|
29 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
|
|
30 | |
|
|
31 | $GRAB = $widget; |
|
|
32 | $GRAB->update if $GRAB; |
|
|
33 | } |
|
|
34 | |
|
|
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
|
|
36 | |
|
|
37 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
|
|
38 | } |
|
|
39 | |
|
|
40 | sub feed_sdl_button_up_event { |
|
|
41 | my ($ev) = @_; |
|
|
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
43 | |
|
|
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
45 | |
|
|
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
47 | |
|
|
48 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
|
|
49 | |
|
|
50 | if (!$BUTTON_STATE) { |
|
|
51 | my $grab = $GRAB; undef $GRAB; |
|
|
52 | $grab->update if $grab; |
|
|
53 | $GRAB->update if $GRAB; |
|
|
54 | } |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub feed_sdl_motion_event { |
|
|
58 | my ($ev) = @_; |
|
|
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
60 | |
|
|
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
62 | |
|
|
63 | if ($widget != $HOVER) { |
|
|
64 | my $hover = $HOVER; $HOVER = $widget; |
|
|
65 | |
|
|
66 | $hover->update if $hover; |
|
|
67 | $HOVER->update if $HOVER; |
|
|
68 | } |
|
|
69 | |
|
|
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
|
|
71 | } |
|
|
72 | |
|
|
73 | ############################################################################# |
|
|
74 | |
|
|
75 | package CFClient::UI::Base; |
|
|
76 | |
|
|
77 | use strict; |
6 | |
78 | |
7 | use SDL::OpenGL; |
79 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
|
|
9 | |
|
|
10 | use Crossfire::Client; |
|
|
11 | |
|
|
12 | our $FOCUS; # the widget with current focus |
|
|
13 | |
|
|
14 | # class methods for events |
|
|
15 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
|
|
16 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
|
|
17 | sub feed_sdl_button_down_event { } |
|
|
18 | sub feed_sdl_button_up_event { } |
|
|
19 | |
80 | |
20 | sub new { |
81 | sub new { |
21 | my $class = shift; |
82 | my $class = shift; |
22 | |
83 | |
23 | bless { @_ }, $class |
84 | bless { |
|
|
85 | x => 0, |
|
|
86 | y => 0, |
|
|
87 | z => 0, |
|
|
88 | w => -1, |
|
|
89 | h => -1, |
|
|
90 | @_ |
|
|
91 | }, $class |
24 | } |
92 | } |
25 | |
93 | |
26 | sub move { |
94 | sub move { |
27 | my ($self, $x, $y, $z) = @_; |
95 | my ($self, $x, $y, $z) = @_; |
28 | $self->{x} = $x; |
96 | $self->{x} = $x; |
… | |
… | |
37 | sub size_request { |
105 | sub size_request { |
38 | require Carp; |
106 | require Carp; |
39 | Carp::confess "size_request is abtract"; |
107 | Carp::confess "size_request is abtract"; |
40 | } |
108 | } |
41 | |
109 | |
42 | sub size_allocate { |
110 | sub _size_allocate { |
43 | my ($self, $w, $h) = @_; |
111 | my ($self, $x, $y, $w, $h) = @_; |
|
|
112 | |
|
|
113 | $self->{x} = $x; |
|
|
114 | $self->{y} = $y; |
|
|
115 | |
|
|
116 | return unless $self->{w} != $w || $self->{h} != $h; |
44 | |
117 | |
45 | $self->{w} = $w; |
118 | $self->{w} = $w; |
46 | $self->{h} = $h; |
119 | $self->{h} = $h; |
|
|
120 | |
|
|
121 | 1 |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub size_allocate { |
|
|
125 | my ($self, $x, $y, $w, $h) = @_; |
|
|
126 | |
|
|
127 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
128 | } |
|
|
129 | |
|
|
130 | # translate global koordinates to local coordinate system |
|
|
131 | sub translate { |
|
|
132 | my ($self, $x, $y) = @_; |
|
|
133 | |
|
|
134 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
47 | } |
135 | } |
48 | |
136 | |
49 | sub focus_in { |
137 | sub focus_in { |
50 | my ($widget) = @_; |
138 | my ($self) = @_; |
51 | $FOCUS = $widget; |
139 | |
|
|
140 | return if $FOCUS == $self; |
|
|
141 | |
|
|
142 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
143 | $focus->update if $focus; |
|
|
144 | $FOCUS->update; |
52 | } |
145 | } |
53 | |
146 | |
54 | sub focus_out { |
147 | sub focus_out { |
55 | my ($widget) = @_; |
148 | my ($self) = @_; |
56 | } |
|
|
57 | |
149 | |
|
|
150 | return unless $FOCUS == $self; |
|
|
151 | |
|
|
152 | my $focus = $FOCUS; undef $FOCUS; |
|
|
153 | $focus->update if $focus; #? |
|
|
154 | } |
|
|
155 | |
|
|
156 | sub mouse_motion { } |
|
|
157 | sub button_up { } |
58 | sub key_down { |
158 | sub key_down { } |
59 | my ($widget, $sdlev) = @_; |
|
|
60 | } |
|
|
61 | |
|
|
62 | sub key_up { |
159 | sub key_up { } |
63 | my ($widget, $sdlev) = @_; |
|
|
64 | } |
|
|
65 | |
160 | |
66 | sub button_down { |
161 | sub button_down { |
67 | my ($widget, $sdlev) = @_; |
162 | my ($self, $ev, $x, $y) = @_; |
68 | } |
|
|
69 | |
163 | |
70 | sub button_up { |
164 | $self->focus_in; |
71 | my ($widget, $sdlev) = @_; |
|
|
72 | } |
165 | } |
73 | |
166 | |
74 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
167 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
75 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
168 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
76 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
169 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
77 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
170 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
78 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
171 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
79 | |
172 | |
80 | sub draw { |
173 | sub draw { |
81 | my ($self) = @_; |
174 | my ($self) = @_; |
|
|
175 | |
|
|
176 | return unless $self->{h} && $self->{w}; |
82 | |
177 | |
83 | glPushMatrix; |
178 | glPushMatrix; |
84 | glTranslate $self->{x}, $self->{y}, 0; |
179 | glTranslate $self->{x}, $self->{y}, 0; |
85 | $self->_draw; |
180 | $self->_draw; |
86 | glPopMatrix; |
181 | glPopMatrix; |
|
|
182 | |
|
|
183 | if ($self == $HOVER) { |
|
|
184 | my ($x, $y) = @$self{qw(x y)}; |
|
|
185 | |
|
|
186 | glColor 1, 1, 1, 0.1; |
|
|
187 | glEnable GL_BLEND; |
|
|
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
189 | glBegin GL_QUADS; |
|
|
190 | glVertex $x , $y; |
|
|
191 | glVertex $x + $self->{w}, $y; |
|
|
192 | glVertex $x + $self->{w}, $y + $self->{h}; |
|
|
193 | glVertex $x , $y + $self->{h}; |
|
|
194 | glEnd; |
|
|
195 | glDisable GL_BLEND; |
|
|
196 | } |
87 | } |
197 | } |
88 | |
198 | |
89 | sub _draw { |
199 | sub _draw { |
90 | my ($self) = @_; |
200 | my ($self) = @_; |
91 | |
201 | |
… | |
… | |
131 | |
241 | |
132 | $self->{parent}->update |
242 | $self->{parent}->update |
133 | if $self->{parent}; |
243 | if $self->{parent}; |
134 | } |
244 | } |
135 | |
245 | |
|
|
246 | sub connect { |
|
|
247 | my ($self, $signal, $cb) = @_; |
|
|
248 | |
|
|
249 | push @{ $self->{cb}{$signal} }, $cb; |
|
|
250 | } |
|
|
251 | |
|
|
252 | sub emit { |
|
|
253 | my ($self, $signal, @args) = @_; |
|
|
254 | |
|
|
255 | $_->($self, @args) |
|
|
256 | for @{$self->{cb}{$signal} || []}; |
|
|
257 | } |
|
|
258 | |
136 | sub DESTROY { |
259 | sub DESTROY { |
137 | my ($self) = @_; |
260 | my ($self) = @_; |
138 | |
261 | |
139 | #$self->deactivate; |
262 | #$self->deactivate; |
140 | } |
263 | } |
141 | |
264 | |
142 | ############################################################################# |
265 | ############################################################################# |
143 | |
266 | |
144 | package Crossfire::Client::Widget::Container; |
267 | package CFClient::UI::DrawBG; |
145 | |
268 | |
146 | our @ISA = Crossfire::Client::Widget::; |
269 | our @ISA = CFClient::UI::Base::; |
|
|
270 | |
|
|
271 | use strict; |
|
|
272 | use SDL::OpenGL; |
147 | |
273 | |
148 | sub new { |
274 | sub new { |
|
|
275 | my $class = shift; |
|
|
276 | |
|
|
277 | # range [value, low, high, page] |
|
|
278 | |
|
|
279 | $class->SUPER::new ( |
|
|
280 | bg => [0, 0, 0, 0.4], |
|
|
281 | active_bg => [1, 1, 1], |
|
|
282 | @_ |
|
|
283 | ) |
|
|
284 | } |
|
|
285 | |
|
|
286 | sub _draw { |
|
|
287 | my ($self) = @_; |
|
|
288 | |
|
|
289 | my ($w, $h) = @$self{qw(w h)}; |
|
|
290 | |
|
|
291 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
292 | glBegin GL_QUADS; |
|
|
293 | glVertex 0 , 0; |
|
|
294 | glVertex 0 , $h; |
|
|
295 | glVertex $w, $h; |
|
|
296 | glVertex $w, 0; |
|
|
297 | glEnd; |
|
|
298 | } |
|
|
299 | |
|
|
300 | ############################################################################# |
|
|
301 | |
|
|
302 | package CFClient::UI::Empty; |
|
|
303 | |
|
|
304 | our @ISA = CFClient::UI::Base::; |
|
|
305 | |
|
|
306 | sub size_request { |
|
|
307 | (0, 0) |
|
|
308 | } |
|
|
309 | |
|
|
310 | sub draw { } |
|
|
311 | |
|
|
312 | ############################################################################# |
|
|
313 | |
|
|
314 | package CFClient::UI::Container; |
|
|
315 | |
|
|
316 | our @ISA = CFClient::UI::Base::; |
|
|
317 | |
|
|
318 | sub new { |
149 | my ($class, @widgets) = @_; |
319 | my ($class, %arg) = @_; |
150 | |
320 | |
|
|
321 | my $children = delete $arg{children} || []; |
|
|
322 | |
151 | my $self = $class->SUPER::new (children => []); |
323 | my $self = $class->SUPER::new (children => [], %arg); |
152 | $self->add ($_) for @widgets; |
324 | $self->add ($_) for @$children; |
153 | |
325 | |
154 | $self |
326 | $self |
155 | } |
327 | } |
156 | |
328 | |
157 | sub add { |
329 | sub add { |
158 | my ($self, $chld, $expand) = @_; |
330 | my ($self, $chld, $expand) = @_; |
159 | |
331 | |
160 | $chld->{expand} = $expand; |
332 | $chld->{expand} = $expand; |
161 | $chld->set_parent ($self); |
333 | $chld->set_parent ($self); |
162 | |
334 | |
163 | @{$self->{children}} = |
335 | $self->{children} = [ |
164 | sort { $a->{z} <=> $b->{z} } |
336 | sort { $a->{z} <=> $b->{z} } |
165 | @{$self->{children}}, $chld; |
337 | @{$self->{children}}, $chld |
|
|
338 | ]; |
166 | |
339 | |
167 | $self->size_allocate ($self->{w}, $self->{h}) |
340 | $self->{w} = $self->{h} = -1; |
168 | if $self->{w}; #TODO: check for "realised state" |
341 | $self->update; |
169 | } |
342 | } |
170 | |
343 | |
171 | sub remove { |
344 | sub remove { |
172 | my ($self, $widget) = @_; |
345 | my ($self, $widget) = @_; |
173 | |
346 | |
174 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
175 | |
348 | |
176 | $self->size_allocate ($self->{w}, $self->{h}); |
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
177 | } |
350 | } |
178 | |
351 | |
179 | sub find_widget { |
352 | sub find_widget { |
180 | my ($self, $x, $y) = @_; |
353 | my ($self, $x, $y) = @_; |
181 | |
354 | |
|
|
355 | $x -= $self->{x}; |
|
|
356 | $y -= $self->{y}; |
|
|
357 | |
182 | my $res; |
358 | my $res; |
183 | |
359 | |
184 | for (@{ $self->{children} }) { |
360 | for (reverse @{ $self->{children} }) { |
185 | $res = $_->find_widget ($x, $y) |
361 | $res = $_->find_widget ($x, $y) |
186 | and return $res; |
362 | and return $res; |
187 | } |
363 | } |
188 | |
364 | |
189 | () |
365 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
190 | } |
366 | } |
191 | |
367 | |
192 | sub _draw { |
368 | sub _draw { |
193 | my ($self) = @_; |
369 | my ($self) = @_; |
194 | |
370 | |
195 | $_->draw for @{$self->{children}}; |
371 | $_->draw for @{$self->{children}}; |
196 | } |
372 | } |
197 | |
373 | |
198 | ############################################################################# |
374 | ############################################################################# |
199 | |
375 | |
200 | package Crossfire::Client::Widget::Bin; |
376 | package CFClient::UI::Bin; |
201 | |
377 | |
202 | our @ISA = Crossfire::Client::Widget::Container::; |
378 | our @ISA = CFClient::UI::Container::; |
203 | |
379 | |
204 | sub child { $_[0]->{children}[0] } |
380 | sub new { |
|
|
381 | my ($class, %arg) = @_; |
205 | |
382 | |
206 | sub size_request { |
383 | my $child = (delete $arg{child}) || new CFClient::UI::Empty::; |
207 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
|
|
208 | } |
|
|
209 | |
384 | |
210 | sub size_allocate { |
385 | $class->SUPER::new (children => [$child], %arg) |
211 | my ($self, $w, $h) = @_; |
|
|
212 | |
|
|
213 | $self->SUPER::size_allocate ($w, $h); |
|
|
214 | $self->{children}[0]->size_allocate ($w, $h) |
|
|
215 | if $self->{children}[0] |
|
|
216 | } |
|
|
217 | |
|
|
218 | ############################################################################# |
|
|
219 | |
|
|
220 | package Crossfire::Client::Widget::Toplevel; |
|
|
221 | |
|
|
222 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
223 | |
|
|
224 | sub update { |
|
|
225 | my ($self) = @_; |
|
|
226 | |
|
|
227 | ::refresh (); |
|
|
228 | } |
386 | } |
229 | |
387 | |
230 | sub add { |
388 | sub add { |
231 | my ($self, $widget) = @_; |
389 | my ($self, $widget) = @_; |
232 | |
390 | |
|
|
391 | $self->{children} = []; |
|
|
392 | |
233 | $self->SUPER::add ($widget); |
393 | $self->SUPER::add ($widget); |
234 | |
|
|
235 | $widget->size_allocate ($widget->size_request); |
|
|
236 | } |
394 | } |
237 | |
395 | |
238 | ############################################################################# |
396 | sub remove { |
|
|
397 | my ($self, $widget) = @_; |
239 | |
398 | |
240 | package Crossfire::Client::Widget::Window; |
399 | $self->SUPER::remove ($widget); |
241 | |
400 | |
242 | our @ISA = Crossfire::Client::Widget::Bin::; |
401 | $self->{children} = [new CFClient::UI::Empty] |
|
|
402 | unless @{$self->{children}}; |
|
|
403 | } |
|
|
404 | |
|
|
405 | sub child { $_[0]->{children}[0] } |
|
|
406 | |
|
|
407 | sub size_request { |
|
|
408 | $_[0]{children}[0]->size_request |
|
|
409 | } |
|
|
410 | |
|
|
411 | sub size_allocate { |
|
|
412 | my ($self, $x, $y, $w, $h) = @_; |
|
|
413 | |
|
|
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
415 | |
|
|
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
|
|
417 | } |
|
|
418 | |
|
|
419 | ############################################################################# |
|
|
420 | |
|
|
421 | package CFClient::UI::Window; |
|
|
422 | |
|
|
423 | our @ISA = CFClient::UI::Bin::; |
243 | |
424 | |
244 | use SDL::OpenGL; |
425 | use SDL::OpenGL; |
245 | |
426 | |
246 | sub new { |
427 | sub new { |
247 | my ($class, $x, $y, $z, $w, $h) = @_; |
428 | my ($class, %arg) = @_; |
248 | |
429 | |
249 | my $self = $class->SUPER::new; |
430 | my $self = $class->SUPER::new (%arg); |
250 | |
|
|
251 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
252 | } |
431 | } |
253 | |
432 | |
254 | sub update { |
433 | sub update { |
255 | my ($self) = @_; |
434 | my ($self) = @_; |
256 | |
435 | |
|
|
436 | # we want to do this delayed... |
257 | $self->render_chld; |
437 | $self->render_chld; |
258 | $self->SUPER::update; |
438 | $self->SUPER::update; |
259 | } |
439 | } |
260 | |
440 | |
261 | sub render_chld { |
441 | sub render_chld { |
262 | my ($self) = @_; |
442 | my ($self) = @_; |
263 | |
443 | |
264 | $self->{texture} = |
444 | $self->{texture} = |
265 | Crossfire::Client::Texture->new_from_opengl ( |
445 | CFClient::Texture->new_from_opengl ( |
266 | $self->{w}, $self->{h}, sub { $self->child->draw } |
446 | $self->{w}, $self->{h}, sub { $self->child->draw } |
267 | ); |
447 | ); |
268 | } |
448 | } |
269 | |
449 | |
270 | sub size_allocate { |
450 | sub size_allocate { |
271 | my ($self, $w, $h) = @_; |
451 | my ($self, $x, $y, $w, $h) = @_; |
272 | |
452 | |
273 | $self->{w} = $w; |
453 | $self->_size_allocate ($x, $y, $w, $h) or return; |
274 | $self->{h} = $h; |
|
|
275 | |
454 | |
276 | $self->child->size_allocate ($w, $h); |
455 | $self->child->size_allocate (0, 0, $w, $h); |
277 | |
456 | |
278 | $self->render_chld; |
457 | $self->render_chld; |
279 | } |
458 | } |
280 | |
459 | |
281 | sub _draw { |
460 | sub _draw { |
… | |
… | |
285 | |
464 | |
286 | my $tex = $self->{texture} |
465 | my $tex = $self->{texture} |
287 | or return; |
466 | or return; |
288 | |
467 | |
289 | glEnable GL_BLEND; |
468 | glEnable GL_BLEND; |
|
|
469 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
290 | glEnable GL_TEXTURE_2D; |
470 | glEnable GL_TEXTURE_2D; |
291 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
471 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
292 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
293 | |
472 | |
294 | glBegin GL_QUADS; |
473 | $tex->draw_quad (0, 0, $w, $h); |
295 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
296 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
297 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
298 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
299 | glEnd; |
|
|
300 | |
474 | |
301 | glDisable GL_BLEND; |
475 | glDisable GL_BLEND; |
302 | glDisable GL_TEXTURE_2D; |
476 | glDisable GL_TEXTURE_2D; |
303 | } |
477 | } |
304 | |
478 | |
305 | ############################################################################# |
479 | ############################################################################# |
306 | |
480 | |
307 | package Crossfire::Client::Widget::Frame; |
481 | package CFClient::UI::Frame; |
308 | |
482 | |
309 | our @ISA = Crossfire::Client::Widget::Bin::; |
483 | our @ISA = CFClient::UI::Bin::; |
310 | |
484 | |
311 | use SDL::OpenGL; |
485 | use SDL::OpenGL; |
312 | |
486 | |
313 | sub size_request { |
487 | sub size_request { |
314 | my ($self) = @_; |
488 | my ($self) = @_; |
… | |
… | |
319 | |
493 | |
320 | map { $_ + 4 } $chld->size_request; |
494 | map { $_ + 4 } $chld->size_request; |
321 | } |
495 | } |
322 | |
496 | |
323 | sub size_allocate { |
497 | sub size_allocate { |
324 | my ($self, $w, $h) = @_; |
498 | my ($self, $x, $y, $w, $h) = @_; |
325 | |
499 | |
326 | $self->{w} = $w; |
500 | $self->_size_allocate ($x, $y, $w, $h) or return; |
327 | $self->{h} = $h; |
|
|
328 | |
501 | |
329 | $self->child->size_allocate ($w - 4, $h - 4); |
502 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
330 | $self->child->move (2, 2); |
|
|
331 | } |
503 | } |
332 | |
504 | |
333 | sub _draw { |
505 | sub _draw { |
334 | my ($self) = @_; |
506 | my ($self) = @_; |
335 | |
507 | |
… | |
… | |
337 | |
509 | |
338 | my ($w, $h) = $chld->size_request; |
510 | my ($w, $h) = $chld->size_request; |
339 | |
511 | |
340 | glBegin GL_QUADS; |
512 | glBegin GL_QUADS; |
341 | glColor 0, 0, 0; |
513 | glColor 0, 0, 0; |
342 | glTexCoord 0, 0; glVertex 0 , 0; |
514 | glVertex 0 , 0; |
343 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
515 | glVertex 0 , $h + 4; |
344 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
516 | glVertex $w + 4 , $h + 4; |
345 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
517 | glVertex $w + 4 , 0; |
346 | glEnd; |
518 | glEnd; |
347 | |
519 | |
348 | $chld->draw; |
520 | $chld->draw; |
349 | } |
521 | } |
350 | |
522 | |
351 | ############################################################################# |
523 | ############################################################################# |
352 | |
524 | |
353 | package Crossfire::Client::Widget::FancyFrame; |
525 | package CFClient::UI::FancyFrame; |
354 | |
526 | |
355 | our @ISA = Crossfire::Client::Widget::Bin::; |
527 | our @ISA = CFClient::UI::Bin::; |
356 | |
528 | |
357 | use SDL::OpenGL; |
529 | use SDL::OpenGL; |
358 | |
530 | |
359 | my @tex = |
531 | my @tex = |
360 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
361 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
533 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
362 | |
534 | |
363 | sub size_request { |
535 | sub size_request { |
364 | my ($self) = @_; |
536 | my ($self) = @_; |
365 | |
537 | |
366 | my ($w, $h) = $self->SUPER::size_request; |
538 | my ($w, $h) = $self->SUPER::size_request; |
367 | |
539 | |
368 | $h += $tex[1]->{height}; |
540 | $h += $tex[1]->{h}; |
369 | $h += $tex[4]->{height}; |
541 | $h += $tex[4]->{h}; |
370 | $w += $tex[2]->{width}; |
542 | $w += $tex[2]->{w}; |
371 | $w += $tex[3]->{width}; |
543 | $w += $tex[3]->{w}; |
372 | |
544 | |
373 | ($w, $h) |
545 | ($w, $h) |
374 | } |
546 | } |
375 | |
547 | |
376 | sub size_allocate { |
548 | sub size_allocate { |
377 | my ($self, $w, $h) = @_; |
549 | my ($self, $x, $y, $w, $h) = @_; |
378 | |
550 | |
379 | $self->SUPER::size_allocate ($w, $h); |
551 | $self->_size_allocate ($x, $y, $w, $h) or return; |
380 | |
552 | |
381 | $h -= $tex[1]->{height}; |
553 | $h -= $tex[1]->{h}; |
382 | $h -= $tex[4]->{height}; |
554 | $h -= $tex[4]->{h}; |
383 | $w -= $tex[2]->{width}; |
555 | $w -= $tex[2]->{w}; |
384 | $w -= $tex[3]->{width}; |
556 | $w -= $tex[3]->{w}; |
385 | |
557 | |
386 | $h = $h < 0 ? 0 : $h; |
558 | $h = $h < 0 ? 0 : $h; |
387 | $w = $w < 0 ? 0 : $w; |
559 | $w = $w < 0 ? 0 : $w; |
388 | |
560 | |
389 | $self->child->size_allocate ($w, $h); |
561 | my $child = $self->child; |
390 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
562 | |
|
|
563 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
391 | } |
564 | } |
392 | |
565 | |
393 | sub _draw { |
566 | sub _draw { |
394 | my ($self) = @_; |
567 | my ($self) = @_; |
395 | |
568 | |
396 | my ($w, $h) = ($self->{w}, $self->{h}); |
569 | my ($w, $h) = ($self->{w}, $self->{h}); |
397 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
570 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
398 | |
571 | |
399 | glEnable GL_BLEND; |
572 | glEnable GL_BLEND; |
|
|
573 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
400 | glEnable GL_TEXTURE_2D; |
574 | glEnable GL_TEXTURE_2D; |
401 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
402 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
575 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
403 | |
576 | |
404 | my $top = $tex[1]; |
577 | my $top = $tex[1]; |
405 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
578 | $top->draw_quad (0, 0, $w, $top->{h}); |
406 | |
|
|
407 | glBegin GL_QUADS; |
|
|
408 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
409 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
410 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
411 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
412 | glEnd; |
|
|
413 | |
579 | |
414 | my $left = $tex[3]; |
580 | my $left = $tex[3]; |
415 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
581 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
416 | |
|
|
417 | glBegin GL_QUADS; |
|
|
418 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
419 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
420 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
421 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
422 | glEnd; |
|
|
423 | |
582 | |
424 | my $right = $tex[2]; |
583 | my $right = $tex[2]; |
425 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
584 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
426 | |
|
|
427 | glBegin GL_QUADS; |
|
|
428 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
429 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
430 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
431 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
432 | glEnd; |
|
|
433 | |
585 | |
434 | my $bottom = $tex[4]; |
586 | my $bottom = $tex[4]; |
435 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
587 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
436 | |
|
|
437 | glBegin GL_QUADS; |
|
|
438 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
439 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
440 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
441 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
442 | glEnd; |
|
|
443 | |
588 | |
444 | my $bg = $tex[0]; |
589 | my $bg = $tex[0]; |
445 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
590 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
446 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
591 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
447 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
592 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
448 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
593 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
449 | |
594 | |
450 | my $rep_x = $cw / $bg->{width}; |
595 | my $rep_x = $cw / $bg->{w}; |
451 | my $rep_y = $ch / $bg->{height}; |
596 | my $rep_y = $ch / $bg->{h}; |
452 | |
597 | |
453 | glBegin GL_QUADS; |
598 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
454 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
455 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
456 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
457 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
458 | glEnd; |
|
|
459 | |
599 | |
460 | glDisable GL_BLEND; |
600 | glDisable GL_BLEND; |
461 | glDisable GL_TEXTURE_2D; |
601 | glDisable GL_TEXTURE_2D; |
462 | |
602 | |
463 | $self->child->draw; |
603 | $self->child->draw; |
464 | |
604 | |
465 | } |
605 | } |
466 | |
606 | |
467 | ############################################################################# |
607 | ############################################################################# |
468 | |
608 | |
469 | package Crossfire::Client::Widget::Table; |
609 | package CFClient::UI::Table; |
470 | |
610 | |
471 | our @ISA = Crossfire::Client::Widget::Bin::; |
611 | our @ISA = CFClient::UI::Base::; |
|
|
612 | |
|
|
613 | use List::Util qw(max sum); |
472 | |
614 | |
473 | use SDL::OpenGL; |
615 | use SDL::OpenGL; |
474 | |
616 | |
475 | sub add { |
617 | sub add { |
476 | my ($self, $x, $y, $chld) = @_; |
618 | my ($self, $x, $y, $chld) = @_; |
477 | my $old_chld = $self->{children}[$y][$x]; |
|
|
478 | |
619 | |
479 | $self->{children}[$y][$x] = $chld; |
620 | $self->{children}[$y][$x] = $chld; |
480 | $chld->set_parent ($self); |
621 | $chld->set_parent ($self); |
|
|
622 | |
|
|
623 | $self->{w} = $self->{h} = -1; |
481 | $self->update; |
624 | $self->update; |
482 | } |
625 | } |
483 | |
626 | |
484 | sub max_row_height { |
627 | sub get_wh { |
485 | my ($self, $row) = @_; |
628 | my ($self) = @_; |
486 | |
629 | |
487 | my $hs = 0; |
630 | my (@w, @h); |
488 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
631 | |
|
|
632 | for my $y (0 .. $#{$self->{children}}) { |
489 | my $c = $self->{children}->[$row]->[$xi]; |
633 | my $row = $self->{children}[$y] |
490 | if ($c) { |
634 | or next; |
|
|
635 | |
|
|
636 | for my $x (0 .. $#$row) { |
|
|
637 | my $widget = $row->[$x] |
|
|
638 | or next; |
491 | my ($w, $h) = $c->size_request; |
639 | my ($w, $h) = $widget->size_request; |
492 | if ($hs < $h) { $hs = $h } |
640 | |
|
|
641 | $w[$x] = max $w[$x], $w; |
|
|
642 | $h[$y] = max $h[$y], $h; |
493 | } |
643 | } |
494 | } |
644 | } |
495 | return $hs; |
|
|
496 | } |
|
|
497 | |
645 | |
498 | sub max_col_width { |
646 | (\@w, \@h) |
|
|
647 | } |
|
|
648 | |
|
|
649 | sub size_request { |
499 | my ($self, $col) = @_; |
650 | my ($self) = @_; |
500 | |
651 | |
|
|
652 | my ($ws, $hs) = $self->get_wh; |
|
|
653 | |
|
|
654 | ( |
|
|
655 | (List::Util::sum @$ws), |
|
|
656 | (List::Util::sum @$hs), |
|
|
657 | ) |
|
|
658 | } |
|
|
659 | |
|
|
660 | sub size_allocate { |
|
|
661 | my ($self, $x, $y, $w, $h) = @_; |
|
|
662 | |
|
|
663 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
664 | |
|
|
665 | my ($ws, $hs) = $self->get_wh; |
|
|
666 | |
|
|
667 | my $req_w = List::Util::sum @$ws; |
|
|
668 | my $req_h = List::Util::sum @$hs; |
|
|
669 | |
|
|
670 | # linearly scale sizes |
|
|
671 | $_ *= $req_w / $w for @$ws; |
|
|
672 | $_ *= $req_h / $h for @$hs; |
|
|
673 | |
|
|
674 | my $y; |
|
|
675 | |
|
|
676 | for my $r (0 .. $#{$self->{children}}) { |
|
|
677 | my $row = $self->{children}[$r] |
|
|
678 | or next; |
|
|
679 | |
501 | my $ws = 0; |
680 | my $x = 0; |
502 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
681 | my $row_h = $hs->[$r]; |
503 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
682 | |
504 | if ($c) { |
683 | for my $c (0 .. $#$row) { |
505 | my ($w, $h) = $c->size_request; |
684 | my $widget = $row->[$c] |
506 | if ($ws < $w) { $ws = $w } |
685 | or next; |
|
|
686 | |
|
|
687 | my $col_w = $ws->[$c]; |
|
|
688 | |
|
|
689 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
690 | |
|
|
691 | $x += $col_w; |
507 | } |
692 | } |
508 | } |
|
|
509 | return $ws; |
|
|
510 | } |
|
|
511 | |
693 | |
512 | sub size_request { |
694 | $y += $row_h; |
513 | my ($self) = @_; |
|
|
514 | |
|
|
515 | my ($hs, $ws) = (0, 0); |
|
|
516 | |
|
|
517 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
518 | $hs += $self->max_row_height ($yi); |
|
|
519 | } |
695 | } |
520 | |
696 | |
521 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
522 | my $wm = 0; |
|
|
523 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
524 | $wm += $self->max_col_width ($xi) |
|
|
525 | } |
|
|
526 | if ($ws < $wm) { $ws = $wm } |
|
|
527 | } |
|
|
528 | |
|
|
529 | return ($ws, $hs); |
|
|
530 | } |
697 | } |
531 | |
698 | |
532 | sub _draw { |
699 | sub _draw { |
533 | my ($self) = @_; |
700 | my ($self) = @_; |
534 | |
701 | |
535 | my $y = 0; |
702 | for (grep $_, @{$self->{children}}) { |
536 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
703 | $_->draw for grep $_, @$_; |
537 | my $x = 0; |
|
|
538 | |
|
|
539 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
540 | |
|
|
541 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
542 | if ($c) { |
|
|
543 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
544 | $c->draw if $c; |
|
|
545 | } |
|
|
546 | |
|
|
547 | $x += $self->max_col_width ($xi); |
|
|
548 | } |
|
|
549 | |
|
|
550 | $y += $self->max_row_height ($yi); |
|
|
551 | } |
704 | } |
552 | } |
705 | } |
553 | |
706 | |
554 | ############################################################################# |
707 | ############################################################################# |
555 | |
708 | |
556 | package Crossfire::Client::Widget::VBox; |
709 | package CFClient::UI::VBox; |
557 | |
710 | |
558 | our @ISA = Crossfire::Client::Widget::Container::; |
711 | our @ISA = CFClient::UI::Container::; |
559 | |
712 | |
560 | use SDL::OpenGL; |
713 | use SDL::OpenGL; |
561 | |
714 | |
562 | sub size_request { |
715 | sub size_request { |
563 | my ($self) = @_; |
716 | my ($self) = @_; |
… | |
… | |
569 | (List::Util::sum map $_->[1], @alloc), |
722 | (List::Util::sum map $_->[1], @alloc), |
570 | ) |
723 | ) |
571 | } |
724 | } |
572 | |
725 | |
573 | sub size_allocate { |
726 | sub size_allocate { |
574 | my ($self, $w, $h) = @_; |
727 | my ($self, $x, $y, $w, $h) = @_; |
575 | |
728 | |
576 | $self->w ($w); |
729 | $self->_size_allocate ($x, $y, $w, $h) or return; |
577 | $self->h ($h); |
|
|
578 | |
730 | |
579 | my $exp; |
731 | return unless $self->{h}; |
580 | my @oth; |
732 | |
581 | # find expand widget |
733 | my $children = $self->{children}; |
582 | for (@{$self->{children}}) { |
734 | |
583 | if ($_->{expand}) { |
735 | my @h = map +($_->size_request)[1], @$children; |
584 | $exp = $_; |
736 | |
585 | last; |
737 | my $req_h = List::Util::sum @h; |
|
|
738 | |
|
|
739 | if ($req_h > $h) { |
|
|
740 | # ah well, not enough space |
|
|
741 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
742 | } else { |
|
|
743 | my @exp = grep $_->{expand}, @$children; |
|
|
744 | @exp = @$children unless @exp; |
|
|
745 | |
|
|
746 | my %exp = map +($_ => 1), @exp; |
|
|
747 | |
|
|
748 | for (0 .. $#$children) { |
|
|
749 | my $child = $children->[$_]; |
|
|
750 | |
|
|
751 | my $alloc_h = $h[$_]; |
|
|
752 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
753 | $h[$_] = $alloc_h; |
586 | } |
754 | } |
587 | push @oth, $_; |
|
|
588 | } |
|
|
589 | |
|
|
590 | my ($ow, $oh); |
|
|
591 | |
|
|
592 | # get sizes of other widgets |
|
|
593 | for (@oth) { |
|
|
594 | my ($w, $h) = $_->size_request; |
|
|
595 | $oh += $h; |
|
|
596 | if ($ow < $w) { $ow = $w } |
|
|
597 | } |
755 | } |
598 | |
756 | |
599 | my $y = 0; |
757 | my $y = 0; |
600 | for (@{$self->{children}}) { |
758 | for (0 .. $#$children) { |
601 | $_->move (0, $y); |
759 | my $child = $children->[$_]; |
602 | |
760 | my $h = $h[$_]; |
603 | if ($_ == $exp) { |
|
|
604 | $_->size_allocate ($w, $h - $oh); |
|
|
605 | $y += $h - $oh; |
|
|
606 | } else { |
|
|
607 | my ($cw, $h) = $_->size_request; |
|
|
608 | $_->size_allocate ($w, $h); |
761 | $child->size_allocate (0, $y, $w, $h); |
|
|
762 | |
609 | $y += $h; |
763 | $y += $h; |
610 | } |
|
|
611 | } |
764 | } |
612 | } |
765 | } |
613 | |
766 | |
614 | ############################################################################# |
767 | ############################################################################# |
615 | |
768 | |
616 | package Crossfire::Client::Widget::Label; |
769 | package CFClient::UI::Label; |
617 | |
770 | |
618 | our @ISA = Crossfire::Client::Widget::; |
771 | our @ISA = CFClient::UI::Base::; |
619 | |
772 | |
620 | use SDL::OpenGL; |
773 | use SDL::OpenGL; |
621 | |
774 | |
622 | sub new { |
775 | sub new { |
623 | my ($class, $x, $y, $z, $height, $text) = @_; |
776 | my ($class, %arg) = @_; |
624 | |
777 | |
625 | # TODO: color, and make height, xyz etc. optional |
778 | my $self = $class->SUPER::new ( |
626 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
779 | fg => [1, 1, 1], |
|
|
780 | height => $::FONTSIZE, |
|
|
781 | text => "", |
|
|
782 | align => -1, |
|
|
783 | layout => new CFClient::Layout, |
|
|
784 | %arg |
|
|
785 | ); |
627 | |
786 | |
628 | $self->set_text ($text); |
787 | $self->set_text ($self->{text}); |
629 | |
788 | |
630 | $self |
789 | $self |
|
|
790 | } |
|
|
791 | |
|
|
792 | sub escape_text { |
|
|
793 | local $_ = $_[1]; |
|
|
794 | |
|
|
795 | s/&/&/g; |
|
|
796 | s/>/>/g; |
|
|
797 | s/</</g; |
|
|
798 | |
|
|
799 | $_[1] |
631 | } |
800 | } |
632 | |
801 | |
633 | sub set_text { |
802 | sub set_text { |
634 | my ($self, $text) = @_; |
803 | my ($self, $text) = @_; |
635 | |
804 | |
636 | $self->{text} = $text; |
805 | $self->{text} = $text; |
637 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
806 | $self->{layout}->set_markup ($text); |
638 | |
807 | |
|
|
808 | delete $self->{texture}; |
639 | $self->update; |
809 | $self->update; |
640 | } |
810 | } |
641 | |
811 | |
642 | sub get_text { |
812 | sub get_text { |
643 | my ($self, $text) = @_; |
813 | my ($self, $text) = @_; |
… | |
… | |
646 | } |
816 | } |
647 | |
817 | |
648 | sub size_request { |
818 | sub size_request { |
649 | my ($self) = @_; |
819 | my ($self) = @_; |
650 | |
820 | |
651 | ( |
821 | $self->{layout}->set_width; |
|
|
822 | $self->{layout}->set_height ($self->{height}); |
|
|
823 | $self->{layout}->size |
|
|
824 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
825 | # ( |
652 | $self->{texture}{width}, |
826 | # $self->{texture}{w}, |
653 | $self->{texture}{height}, |
827 | # $self->{texture}{h}, |
654 | ) |
828 | # ) |
|
|
829 | # } else { |
|
|
830 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
831 | # |
|
|
832 | # ($w, $h) |
|
|
833 | # } |
|
|
834 | } |
|
|
835 | |
|
|
836 | sub size_allocate { |
|
|
837 | my ($self, $x, $y, $w, $h) = @_; |
|
|
838 | |
|
|
839 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
840 | |
|
|
841 | delete $self->{texture}; |
|
|
842 | } |
|
|
843 | |
|
|
844 | sub update { |
|
|
845 | my ($self) = @_; |
|
|
846 | |
|
|
847 | delete $self->{texture}; |
|
|
848 | $self->SUPER::update; |
655 | } |
849 | } |
656 | |
850 | |
657 | sub _draw { |
851 | sub _draw { |
658 | my ($self) = @_; |
852 | my ($self) = @_; |
659 | |
853 | |
660 | my $tex = $self->{texture}; |
854 | my $tex = $self->{texture} ||= do { |
|
|
855 | $self->{layout}->set_width ($self->{w}); |
|
|
856 | new_from_layout CFClient::Texture $self->{layout} |
|
|
857 | }; |
661 | |
858 | |
662 | glEnable GL_BLEND; |
859 | glEnable GL_BLEND; |
|
|
860 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
663 | glEnable GL_TEXTURE_2D; |
861 | glEnable GL_TEXTURE_2D; |
664 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
665 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
862 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
666 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
667 | |
863 | |
668 | glColor 1, 0, 0, 1; # TODO color |
864 | glColor @{$self->{fg}}; |
669 | |
865 | |
670 | glBegin GL_QUADS; |
866 | my $x = |
671 | glTexCoord 0, 0; glVertex 0 , 0; |
867 | $self->{align} < 0 ? 0 |
672 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
868 | : $self->{align} > 0 ? $self->{w} - $tex->{w} |
673 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
869 | : ($self->{w} - $tex->{w}) * 0.5; |
674 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
675 | glEnd; |
|
|
676 | |
870 | |
|
|
871 | $tex->draw_quad ($x, 0); |
|
|
872 | |
|
|
873 | glDisable GL_TEXTURE_2D; |
677 | glDisable GL_BLEND; |
874 | glDisable GL_BLEND; |
678 | glDisable GL_TEXTURE_2D; |
|
|
679 | } |
875 | } |
680 | |
876 | |
681 | ############################################################################# |
877 | ############################################################################# |
682 | |
878 | |
683 | package Crossfire::Client::Widget::TextEntry; |
879 | package CFClient::UI::Entry; |
684 | |
880 | |
685 | our @ISA = Crossfire::Client::Widget::Label::; |
881 | our @ISA = CFClient::UI::Label::; |
686 | |
882 | |
687 | use SDL; |
883 | use SDL; |
688 | use SDL::OpenGL; |
884 | use SDL::OpenGL; |
689 | |
885 | |
|
|
886 | sub new { |
|
|
887 | my $class = shift; |
|
|
888 | |
|
|
889 | $class->SUPER::new ( |
|
|
890 | fg => [1, 1, 1], |
|
|
891 | bg => [0, 0, 0, 0.4], |
|
|
892 | active_bg => [1, 1, 1], |
|
|
893 | active_fg => [0, 0, 0], |
|
|
894 | @_ |
|
|
895 | ) |
|
|
896 | } |
|
|
897 | |
|
|
898 | sub _set_text { |
|
|
899 | my ($self, $text) = @_; |
|
|
900 | |
|
|
901 | $self->{last_activity} = $::NOW; |
|
|
902 | |
|
|
903 | $self->{text} = $text; |
|
|
904 | $self->{layout}->set_width ($self->{w}); |
|
|
905 | |
|
|
906 | $text =~ s/./*/g if $self->{hidden}; |
|
|
907 | |
|
|
908 | |
|
|
909 | $self->{layout}->set_markup ($self->escape_text ($text)); |
|
|
910 | |
|
|
911 | $text = substr $text, 0, $self->{cursor}; |
|
|
912 | utf8::encode $text; |
|
|
913 | |
|
|
914 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
915 | } |
|
|
916 | |
|
|
917 | sub size_request { |
|
|
918 | my ($self) = @_; |
|
|
919 | |
|
|
920 | my ($w, $h) = $self->SUPER::size_request; |
|
|
921 | |
|
|
922 | ($w + 1, $h) # add 1 for cursor |
|
|
923 | } |
|
|
924 | |
|
|
925 | sub size_allocate { |
|
|
926 | my ($self, $x, $y, $w, $h) = @_; |
|
|
927 | |
|
|
928 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
929 | |
|
|
930 | $self->_set_text ($self->{text}); |
|
|
931 | } |
|
|
932 | |
|
|
933 | sub set_text { |
|
|
934 | my ($self, $text) = @_; |
|
|
935 | |
|
|
936 | $self->{cursor} = length $text; |
|
|
937 | $self->_set_text ($text); |
|
|
938 | $self->update; |
|
|
939 | } |
|
|
940 | |
690 | sub key_down { |
941 | sub key_down { |
691 | my ($self, $ev) = @_; |
942 | my ($self, $ev) = @_; |
692 | |
943 | |
693 | my $mod = $ev->key_mod; |
944 | my $mod = $ev->key_mod; |
694 | my $sym = $ev->key_sym; |
945 | my $sym = $ev->key_sym; |
695 | |
946 | |
696 | $ev->set_unicode (1); |
|
|
697 | my $uni = $ev->key_unicode; |
947 | my $uni = $ev->key_unicode; |
698 | |
948 | |
699 | my $text = $self->get_text; |
949 | my $text = $self->get_text; |
700 | |
950 | |
701 | if ($sym == SDLK_BACKSPACE) { |
951 | if ($sym == SDLK_BACKSPACE) { |
702 | substr $text, -1, 1, ''; |
952 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
703 | |
953 | } elsif ($sym == SDLK_DELETE) { |
|
|
954 | substr $text, $self->{cursor}, 1, ""; |
|
|
955 | } elsif ($sym == SDLK_LEFT) { |
|
|
956 | --$self->{cursor} if $self->{cursor}; |
|
|
957 | } elsif ($sym == SDLK_RIGHT) { |
|
|
958 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
704 | } elsif ($uni) { |
959 | } elsif ($uni) { |
705 | $text .= chr $uni; |
960 | substr $text, $self->{cursor}++, 0, chr $uni; |
706 | } |
961 | } |
|
|
962 | |
707 | $self->set_text ($text); |
963 | $self->_set_text ($text); |
|
|
964 | $self->update; |
708 | } |
965 | } |
709 | |
966 | |
710 | ############################################################################# |
967 | sub focus_in { |
|
|
968 | my ($self) = @_; |
711 | |
969 | |
|
|
970 | $self->{last_activity} = $::NOW; |
|
|
971 | |
|
|
972 | $self->SUPER::focus_in; |
|
|
973 | } |
|
|
974 | |
|
|
975 | sub button_down { |
|
|
976 | my ($self, $ev, $x, $y) = @_; |
|
|
977 | |
|
|
978 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
979 | |
|
|
980 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
|
|
981 | |
|
|
982 | # byte-index to char-index |
|
|
983 | my $text = $self->{layout}; |
|
|
984 | utf8::encode $text; |
|
|
985 | $self->{cursor} = length substr $text, 0, $idx; |
|
|
986 | |
|
|
987 | $self->_set_text ($self->{text}); |
|
|
988 | $self->update; |
|
|
989 | } |
|
|
990 | |
|
|
991 | sub mouse_motion { |
|
|
992 | my ($self, $ev, $x, $y) = @_; |
|
|
993 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
|
|
994 | } |
|
|
995 | |
|
|
996 | sub _draw { |
|
|
997 | my ($self) = @_; |
|
|
998 | |
|
|
999 | local $self->{fg} = $self->{fg}; |
|
|
1000 | |
|
|
1001 | if ($FOCUS == $self) { |
|
|
1002 | glColor @{$self->{active_bg}}; |
|
|
1003 | $self->{fg} = $self->{active_fg}; |
|
|
1004 | } else { |
|
|
1005 | glColor @{$self->{bg}}; |
|
|
1006 | } |
|
|
1007 | |
|
|
1008 | glBegin GL_QUADS; |
|
|
1009 | glVertex 0 , 0; |
|
|
1010 | glVertex 0 , $self->{h}; |
|
|
1011 | glVertex $self->{w}, $self->{h}; |
|
|
1012 | glVertex $self->{w}, 0; |
|
|
1013 | glEnd; |
|
|
1014 | |
|
|
1015 | $self->SUPER::_draw; |
|
|
1016 | |
|
|
1017 | #TODO: force update every cursor change :( |
|
|
1018 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1019 | glColor @{$self->{fg}}; |
|
|
1020 | glBegin GL_LINES; |
|
|
1021 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1022 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
|
|
1023 | glEnd; |
|
|
1024 | } |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | ############################################################################# |
|
|
1028 | |
|
|
1029 | package CFClient::UI::Slider; |
|
|
1030 | |
|
|
1031 | use strict; |
|
|
1032 | |
|
|
1033 | use SDL::OpenGL; |
|
|
1034 | |
|
|
1035 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1036 | |
|
|
1037 | sub size_request { |
|
|
1038 | my ($self) = @_; |
|
|
1039 | |
|
|
1040 | my $w = 50; |
|
|
1041 | my $h = 10; |
|
|
1042 | |
|
|
1043 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1044 | } |
|
|
1045 | |
|
|
1046 | sub new { |
|
|
1047 | my $class = shift; |
|
|
1048 | |
|
|
1049 | # range [value, low, high, page] |
|
|
1050 | |
|
|
1051 | $class->SUPER::new ( |
|
|
1052 | fg => [1, 1, 1], |
|
|
1053 | active_fg => [0, 0, 0], |
|
|
1054 | range => [0, 0, 100, 10], |
|
|
1055 | vertical => 1, |
|
|
1056 | @_ |
|
|
1057 | ) |
|
|
1058 | } |
|
|
1059 | |
|
|
1060 | sub button_down { |
|
|
1061 | my ($self, $ev, $x, $y) = @_; |
|
|
1062 | |
|
|
1063 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
1064 | $self->mouse_motion ($ev, $x, $y); |
|
|
1065 | } |
|
|
1066 | |
|
|
1067 | sub mouse_motion { |
|
|
1068 | my ($self, $ev, $x, $y) = @_; |
|
|
1069 | |
|
|
1070 | if ($GRAB == $self) { |
|
|
1071 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1072 | |
|
|
1073 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
1074 | |
|
|
1075 | $x = $x * ($hi - $lo) / $w + $lo; |
|
|
1076 | $x = $lo if $x < $lo; |
|
|
1077 | $x = $hi - $page if $x > $hi - $page; |
|
|
1078 | $self->{range}[0] = $x; |
|
|
1079 | |
|
|
1080 | $self->emit (changed => $x); |
|
|
1081 | $self->update; |
|
|
1082 | } |
|
|
1083 | } |
|
|
1084 | |
|
|
1085 | sub _draw { |
|
|
1086 | my ($self) = @_; |
|
|
1087 | |
|
|
1088 | $self->SUPER::_draw (); |
|
|
1089 | |
|
|
1090 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1091 | |
|
|
1092 | if ($self->{vertical}) { |
|
|
1093 | # draw a vertical slider like a rotated horizontal slider |
|
|
1094 | |
|
|
1095 | glRotate 90, 0, 0, 1; |
|
|
1096 | glTranslate 0, -$self->{w}, 0; |
|
|
1097 | |
|
|
1098 | ($w, $h) = ($h, $w); |
|
|
1099 | } |
|
|
1100 | |
|
|
1101 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
|
|
1102 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
|
|
1103 | |
|
|
1104 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1105 | |
|
|
1106 | $page = int $page * $w / ($hi - $lo); |
|
|
1107 | $value = int +($value - $lo) * $w / ($hi - $lo); |
|
|
1108 | |
|
|
1109 | $w -= $page; |
|
|
1110 | $page &= ~1; |
|
|
1111 | glTranslate $page * 0.5, 0, 0; |
|
|
1112 | |
|
|
1113 | glColor @$fg; |
|
|
1114 | glBegin GL_LINES; |
|
|
1115 | glVertex 0, 0; glVertex 0, $h; |
|
|
1116 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1117 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1118 | glEnd; |
|
|
1119 | |
|
|
1120 | my $knob_a = $value - $page * 0.5; |
|
|
1121 | my $knob_b = $value + $page * 0.5; |
|
|
1122 | |
|
|
1123 | glBegin GL_QUADS; |
|
|
1124 | glColor @$fg; |
|
|
1125 | glVertex $knob_a, 0; |
|
|
1126 | glVertex $knob_a, $h; |
|
|
1127 | glVertex $knob_b, $h; |
|
|
1128 | glVertex $knob_b, 0; |
|
|
1129 | |
|
|
1130 | if ($knob_a < $knob_b - 2) { |
|
|
1131 | glColor @$bg; |
|
|
1132 | glVertex $knob_a + 1, 1; |
|
|
1133 | glVertex $knob_a + 1, $h - 1; |
|
|
1134 | glVertex $knob_b - 1, $h - 1; |
|
|
1135 | glVertex $knob_b - 1, 1; |
|
|
1136 | } |
|
|
1137 | glEnd; |
|
|
1138 | } |
|
|
1139 | |
|
|
1140 | ############################################################################# |
|
|
1141 | |
712 | package Crossfire::Client::Widget::MapWidget; |
1142 | package CFClient::UI::MapWidget; |
713 | |
1143 | |
714 | use strict; |
1144 | use strict; |
715 | |
1145 | |
716 | use List::Util qw(min max); |
1146 | use List::Util qw(min max); |
717 | |
1147 | |
718 | use SDL; |
1148 | use SDL; |
719 | use SDL::OpenGL; |
1149 | use SDL::OpenGL; |
720 | use SDL::OpenGL::Constants; |
|
|
721 | |
1150 | |
722 | our @ISA = Crossfire::Client::Widget::; |
1151 | our @ISA = CFClient::UI::Base::; |
|
|
1152 | |
|
|
1153 | sub new { |
|
|
1154 | my $class = shift; |
|
|
1155 | |
|
|
1156 | $class->SUPER::new ( |
|
|
1157 | z => -1, |
|
|
1158 | list => (glGenLists 1), |
|
|
1159 | @_ |
|
|
1160 | ) |
|
|
1161 | } |
723 | |
1162 | |
724 | sub key_down { |
1163 | sub key_down { |
725 | print "MAPKEYDOWN\n"; |
1164 | print "MAPKEYDOWN\n"; |
726 | } |
1165 | } |
727 | |
1166 | |
728 | sub key_up { |
1167 | sub key_up { |
729 | } |
1168 | } |
730 | |
1169 | |
731 | sub size_request { |
1170 | sub size_request { |
732 | |
1171 | ( |
|
|
1172 | 1 + int $::WIDTH / 32, |
|
|
1173 | 1 + int $::HEIGHT / 32, |
|
|
1174 | ) |
733 | } |
1175 | } |
734 | |
1176 | |
735 | sub size_allocate { |
1177 | sub update { |
|
|
1178 | my ($self) = @_; |
|
|
1179 | |
|
|
1180 | $self->{need_update} = 1; |
|
|
1181 | $self->SUPER::update; |
736 | } |
1182 | } |
737 | |
1183 | |
738 | sub _draw { |
1184 | sub _draw { |
739 | my ($self) = @_; |
1185 | my ($self) = @_; |
740 | |
1186 | |
|
|
1187 | if (delete $self->{need_update}) { |
|
|
1188 | glNewList $self->{list}, GL_COMPILE; |
|
|
1189 | |
741 | my $mx = $::CONN->{mapx}; |
1190 | my $mx = $::CONN->{mapx}; |
742 | my $my = $::CONN->{mapy}; |
1191 | my $my = $::CONN->{mapy}; |
743 | |
1192 | |
744 | my $map = $::CONN->{map}; |
1193 | my $map = $::CONN->{map}; |
745 | |
1194 | |
746 | my ($xofs, $yofs); |
1195 | my ($xofs, $yofs); |
747 | |
1196 | |
748 | my $sw = 1 + int $::WIDTH / 32; |
1197 | my $sw = 1 + int $::WIDTH / 32; |
749 | my $sh = 1 + int $::HEIGHT / 32; |
1198 | my $sh = 1 + int $::HEIGHT / 32; |
750 | |
1199 | |
751 | if ($::CONN->{mapw} > $sw) { |
1200 | if ($::CONN->{mapw} > $sw) { |
752 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
1201 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
753 | } else { |
1202 | } else { |
754 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
1203 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
755 | } |
1204 | } |
756 | |
1205 | |
757 | if ($::CONN->{maph} > $sh) { |
1206 | if ($::CONN->{maph} > $sh) { |
758 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
1207 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
759 | } else { |
1208 | } else { |
760 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1209 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
761 | } |
1210 | } |
762 | |
1211 | |
763 | glEnable GL_TEXTURE_2D; |
1212 | glEnable GL_TEXTURE_2D; |
764 | glEnable GL_BLEND; |
1213 | glEnable GL_BLEND; |
|
|
1214 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
765 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1215 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
766 | |
1216 | |
767 | my $sw4 = ($sw + 3) & ~3; |
1217 | my $sw4 = ($sw + 3) & ~3; |
768 | my $lighting = "\x00" x ($sw4 * $sh); |
1218 | my $darkness = "\x00" x ($sw4 * $sh); |
769 | |
1219 | |
770 | for my $x (0 .. $sw - 1) { |
1220 | for my $x (0 .. $sw - 1) { |
|
|
1221 | my $row = $map->[$x + $xofs]; |
771 | for my $y (0 .. $sh - 1) { |
1222 | for my $y (0 .. $sh - 1) { |
772 | |
1223 | |
773 | my $cell = $map->[$x + $xofs][$y + $yofs] |
1224 | my $cell = $row->[$y + $yofs] |
774 | or next; |
1225 | or next; |
775 | |
1226 | |
776 | my $darkness = $cell->[0] * (1 / 255); |
1227 | my $dark = $cell->[0]; |
777 | if ($darkness < 0) { |
1228 | if ($dark < 0) { |
778 | $darkness = 0.15; |
1229 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
1230 | } else { |
|
|
1231 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
779 | } |
1232 | } |
780 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
781 | |
1233 | |
782 | for my $num (grep $_, @$cell[1,2,3]) { |
1234 | for my $num (grep $_, @$cell[1,2,3]) { |
783 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1235 | my $tex = $::CONN->{face}[$num]{texture} || next; |
|
|
1236 | |
|
|
1237 | my ($w, $h) = @$tex{qw(w h)}; |
784 | |
1238 | |
785 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1239 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
786 | |
|
|
787 | my $w = $tex->{width}; |
|
|
788 | my $h = $tex->{height}; |
|
|
789 | |
|
|
790 | my $px = ($x + 1) * 32 - $w; |
|
|
791 | my $py = ($y + 1) * 32 - $h; |
|
|
792 | |
|
|
793 | glBegin GL_QUADS; |
|
|
794 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
795 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
796 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
797 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
798 | glEnd; |
1240 | } |
799 | } |
1241 | } |
800 | } |
1242 | } |
801 | } |
|
|
802 | |
1243 | |
803 | # if (1) { # higher quality darkness |
1244 | # if (1) { # higher quality darkness |
804 | # $lighting =~ s/(.)/$1$1$1/gs; |
1245 | # $lighting =~ s/(.)/$1$1$1/gs; |
805 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
1246 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
806 | # |
1247 | # |
… | |
… | |
808 | # |
1249 | # |
809 | # $lighting = $pb->get_pixels; |
1250 | # $lighting = $pb->get_pixels; |
810 | # $lighting =~ s/(.)../$1/gs; |
1251 | # $lighting =~ s/(.)../$1/gs; |
811 | # } |
1252 | # } |
812 | |
1253 | |
813 | $lighting = new Crossfire::Client::Texture |
|
|
814 | width => $sw4, |
|
|
815 | height => $sh, |
|
|
816 | data => $lighting, |
|
|
817 | internalformat => GL_ALPHA4, |
|
|
818 | format => GL_ALPHA; |
|
|
819 | |
|
|
820 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
821 | glColor 0, 0, 0, 0.75; |
|
|
822 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1254 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
823 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
824 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
825 | glBegin GL_QUADS; |
|
|
826 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
827 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
828 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
829 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
830 | glEnd; |
|
|
831 | |
1255 | |
|
|
1256 | $darkness = new CFClient::Texture |
|
|
1257 | w => $sw4, |
|
|
1258 | h => $sh, |
|
|
1259 | data => $darkness, |
|
|
1260 | internalformat => GL_ALPHA, |
|
|
1261 | format => GL_ALPHA; |
|
|
1262 | |
|
|
1263 | glColor 0.45, 0.45, 0.45, 1; |
|
|
1264 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
1265 | |
832 | glDisable GL_TEXTURE_2D; |
1266 | glDisable GL_TEXTURE_2D; |
833 | glDisable GL_BLEND; |
1267 | glDisable GL_BLEND; |
|
|
1268 | |
|
|
1269 | glEndList; |
|
|
1270 | } |
|
|
1271 | |
|
|
1272 | glCallList $self->{list}; |
834 | } |
1273 | } |
835 | |
1274 | |
836 | my %DIR = ( |
1275 | my %DIR = ( |
837 | SDLK_KP8, [1, "north"], |
1276 | SDLK_KP8, [1, "north"], |
838 | SDLK_KP9, [2, "northeast"], |
1277 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
884 | } |
1323 | } |
885 | } |
1324 | } |
886 | |
1325 | |
887 | ############################################################################# |
1326 | ############################################################################# |
888 | |
1327 | |
889 | package Crossfire::Client::Widget::Animator; |
1328 | package CFClient::UI::Animator; |
890 | |
1329 | |
891 | use SDL::OpenGL; |
1330 | use SDL::OpenGL; |
892 | |
1331 | |
893 | our @ISA = Crossfire::Client::Widget::Bin::; |
1332 | our @ISA = CFClient::UI::Bin::; |
894 | |
1333 | |
895 | sub moveto { |
1334 | sub moveto { |
896 | my ($self, $x, $y) = @_; |
1335 | my ($self, $x, $y) = @_; |
897 | |
1336 | |
898 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1337 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
899 | $self->{speed} = 0.2; |
1338 | $self->{speed} = 0.001; |
900 | $self->{time} = 1; |
1339 | $self->{time} = 1; |
901 | |
1340 | |
902 | ::animation_start $self; |
1341 | ::animation_start $self; |
903 | } |
1342 | } |
904 | |
1343 | |
… | |
… | |
919 | |
1358 | |
920 | sub _draw { |
1359 | sub _draw { |
921 | my ($self) = @_; |
1360 | my ($self) = @_; |
922 | |
1361 | |
923 | glPushMatrix; |
1362 | glPushMatrix; |
924 | glRotate $self->{time} * 10000, 0, 1, 0; |
1363 | glRotate $self->{time} * 1000, 0, 1, 0; |
925 | $self->{children}[0]->draw; |
1364 | $self->{children}[0]->draw; |
926 | glPopMatrix; |
1365 | glPopMatrix; |
927 | } |
1366 | } |
928 | |
1367 | |
929 | 1; |
1368 | ############################################################################# |
930 | |
1369 | |
|
|
1370 | package CFClient::UI::Toplevel; |
|
|
1371 | |
|
|
1372 | our @ISA = CFClient::UI::Container::; |
|
|
1373 | |
|
|
1374 | sub size_request { |
|
|
1375 | ($::WIDTH, $::HEIGHT) |
|
|
1376 | } |
|
|
1377 | |
|
|
1378 | sub size_allocate { |
|
|
1379 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1380 | |
|
|
1381 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1382 | |
|
|
1383 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
|
|
1384 | for @{$self->{children}}; |
|
|
1385 | } |
|
|
1386 | |
|
|
1387 | sub translate { |
|
|
1388 | my ($self, $x, $y) = @_; |
|
|
1389 | |
|
|
1390 | ($x, $y) |
|
|
1391 | } |
|
|
1392 | |
|
|
1393 | sub update { |
|
|
1394 | my ($self) = @_; |
|
|
1395 | |
|
|
1396 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
|
|
1397 | ::refresh (); |
|
|
1398 | } |
|
|
1399 | |
|
|
1400 | sub add { |
|
|
1401 | my ($self, $widget) = @_; |
|
|
1402 | |
|
|
1403 | $self->SUPER::add ($widget); |
|
|
1404 | |
|
|
1405 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
1406 | } |
|
|
1407 | |
|
|
1408 | sub draw { |
|
|
1409 | my ($self) = @_; |
|
|
1410 | |
|
|
1411 | $self->_draw; |
|
|
1412 | } |
|
|
1413 | |
|
|
1414 | ############################################################################# |
|
|
1415 | |
|
|
1416 | package CFClient::UI; |
|
|
1417 | |
|
|
1418 | $TOPLEVEL = new CFClient::UI::Toplevel; |
|
|
1419 | |
|
|
1420 | 1 |
|
|
1421 | |