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