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