1 | package CFClient::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 | use List::Util (); |
7 | use SDL::OpenGL; |
|
|
8 | use SDL::OpenGL::Constants; |
|
|
9 | |
7 | |
10 | use CFClient; |
8 | use CFClient; |
11 | |
9 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
11 | |
… | |
… | |
45 | |
43 | |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
47 | |
45 | |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
49 | |
47 | |
50 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
51 | |
49 | |
52 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
53 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
54 | $grab->update if $grab; |
52 | $grab->update if $grab; |
55 | $GRAB->update if $GRAB; |
53 | $GRAB->update if $GRAB; |
… | |
… | |
70 | } |
68 | } |
71 | |
69 | |
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
73 | } |
71 | } |
74 | |
72 | |
|
|
73 | ############################################################################# |
|
|
74 | |
|
|
75 | package CFClient::UI::Base; |
|
|
76 | |
|
|
77 | use strict; |
|
|
78 | |
|
|
79 | use SDL::OpenGL; |
|
|
80 | |
75 | sub new { |
81 | sub new { |
76 | my $class = shift; |
82 | my $class = shift; |
77 | |
83 | |
78 | bless { |
84 | bless { |
79 | x => 0, |
85 | x => 0, |
80 | y => 0, |
86 | y => 0, |
81 | z => 0, |
87 | z => 0, |
|
|
88 | w => -1, |
|
|
89 | h => -1, |
82 | @_ |
90 | @_ |
83 | }, $class |
91 | }, $class |
84 | } |
92 | } |
85 | |
93 | |
86 | sub move { |
94 | sub move { |
… | |
… | |
97 | sub size_request { |
105 | sub size_request { |
98 | require Carp; |
106 | require Carp; |
99 | Carp::confess "size_request is abtract"; |
107 | Carp::confess "size_request is abtract"; |
100 | } |
108 | } |
101 | |
109 | |
102 | sub size_allocate { |
110 | sub _size_allocate { |
103 | my ($self, $w, $h) = @_; |
111 | my ($self, $x, $y, $w, $h) = @_; |
|
|
112 | |
|
|
113 | $self->{x} = $x; |
|
|
114 | $self->{y} = $y; |
|
|
115 | |
|
|
116 | return unless $self->{w} != $w || $self->{h} != $h; |
104 | |
117 | |
105 | $self->{w} = $w; |
118 | $self->{w} = $w; |
106 | $self->{h} = $h; |
119 | $self->{h} = $h; |
|
|
120 | |
|
|
121 | 1 |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub size_allocate { |
|
|
125 | my ($self, $x, $y, $w, $h) = @_; |
|
|
126 | |
|
|
127 | $self->_size_allocate ($x, $y, $w, $h); |
107 | } |
128 | } |
108 | |
129 | |
109 | # translate global koordinates to local coordinate system |
130 | # translate global koordinates to local coordinate system |
110 | sub translate { |
131 | sub translate { |
111 | my ($self, $x, $y) = @_; |
132 | my ($self, $x, $y) = @_; |
… | |
… | |
158 | glTranslate $self->{x}, $self->{y}, 0; |
179 | glTranslate $self->{x}, $self->{y}, 0; |
159 | $self->_draw; |
180 | $self->_draw; |
160 | glPopMatrix; |
181 | glPopMatrix; |
161 | |
182 | |
162 | if ($self == $HOVER) { |
183 | if ($self == $HOVER) { |
163 | my ($x, $y) = @$self->{qw(x y)}; |
184 | my ($x, $y) = @$self{qw(x y)}; |
164 | |
185 | |
165 | glColor 1, 1, 1, 0.1; |
186 | glColor 0, 0, 1, 0.2; |
166 | glEnable GL_BLEND; |
187 | glEnable GL_BLEND; |
|
|
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
167 | glBegin GL_QUADS; |
189 | glBegin GL_QUADS; |
168 | glVertex $x , $y; |
190 | glVertex $x , $y; |
169 | glVertex $x + $self->{w}, $y; |
191 | glVertex $x + $self->{w}, $y; |
170 | glVertex $x + $self->{w}, $y + $self->{h}; |
192 | glVertex $x + $self->{w}, $y + $self->{h}; |
171 | glVertex $x , $y + $self->{h}; |
193 | glVertex $x , $y + $self->{h}; |
… | |
… | |
178 | my ($self) = @_; |
200 | my ($self) = @_; |
179 | |
201 | |
180 | warn "no draw defined for $self\n"; |
202 | warn "no draw defined for $self\n"; |
181 | } |
203 | } |
182 | |
204 | |
183 | sub bbox { |
|
|
184 | my ($self) = @_; |
|
|
185 | my ($w, $h) = $self->size_request; |
|
|
186 | ( |
|
|
187 | $self->{x}, |
|
|
188 | $self->{y}, |
|
|
189 | $self->{x} = $w, |
|
|
190 | $self->{y} = $h |
|
|
191 | ) |
|
|
192 | } |
|
|
193 | |
|
|
194 | sub find_widget { |
205 | sub find_widget { |
195 | my ($self, $x, $y) = @_; |
206 | my ($self, $x, $y) = @_; |
196 | |
207 | |
197 | return $self |
208 | return $self |
198 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
209 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
199 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
210 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
200 | |
211 | |
201 | () |
212 | () |
202 | } |
213 | } |
203 | |
214 | |
204 | sub del_parent { $_[0]->{parent} = undef } |
|
|
205 | |
|
|
206 | sub set_parent { |
215 | sub set_parent { |
207 | my ($self, $par) = @_; |
216 | my ($self, $par) = @_; |
208 | |
217 | |
209 | $self->{parent} = $par; |
218 | $self->{parent} = $par; |
210 | Scalar::Util::weaken $self->{parent}; |
219 | Scalar::Util::weaken $self->{parent}; |
… | |
… | |
240 | #$self->deactivate; |
249 | #$self->deactivate; |
241 | } |
250 | } |
242 | |
251 | |
243 | ############################################################################# |
252 | ############################################################################# |
244 | |
253 | |
245 | package CFClient::Widget::DrawBG; |
254 | package CFClient::UI::DrawBG; |
246 | |
255 | |
247 | our @ISA = CFClient::Widget::; |
256 | our @ISA = CFClient::UI::Base::; |
248 | |
257 | |
249 | use strict; |
258 | use strict; |
250 | use SDL::OpenGL; |
259 | use SDL::OpenGL; |
251 | |
260 | |
252 | sub new { |
261 | sub new { |
253 | my $class = shift; |
262 | my $class = shift; |
254 | |
263 | |
255 | # range [value, low, high, page] |
264 | # range [value, low, high, page] |
256 | |
265 | |
257 | $class->SUPER::new ( |
266 | $class->SUPER::new ( |
258 | bg => [0, 0, 0, 0.4], |
267 | bg => [0, 0, 0, 0.2], |
259 | active_bg => [1, 1, 1], |
268 | active_bg => [1, 1, 1], |
260 | @_ |
269 | @_ |
261 | ) |
270 | ) |
262 | } |
271 | } |
263 | |
272 | |
264 | sub _draw { |
273 | sub _draw { |
265 | my ($self) = @_; |
274 | my ($self) = @_; |
266 | |
275 | |
267 | my ($w, $h) = @$self{qw(w h)}; |
276 | my ($w, $h) = @$self{qw(w h)}; |
268 | |
277 | |
|
|
278 | glEnable GL_BLEND; |
|
|
279 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
269 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
280 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
281 | |
270 | glBegin GL_QUADS; |
282 | glBegin GL_QUADS; |
271 | glVertex 0 , 0; |
283 | glVertex 0 , 0; |
272 | glVertex 0 , $h; |
284 | glVertex 0 , $h; |
273 | glVertex $w, $h; |
285 | glVertex $w, $h; |
274 | glVertex $w, 0; |
286 | glVertex $w, 0; |
275 | glEnd; |
287 | glEnd; |
276 | } |
|
|
277 | |
288 | |
278 | ############################################################################# |
289 | glDisable GL_BLEND; |
|
|
290 | } |
279 | |
291 | |
|
|
292 | ############################################################################# |
|
|
293 | |
280 | package CFClient::Widget::Empty; |
294 | package CFClient::UI::Empty; |
281 | |
295 | |
282 | our @ISA = CFClient::Widget::; |
296 | our @ISA = CFClient::UI::Base::; |
283 | |
297 | |
284 | sub size_request { |
298 | sub size_request { |
285 | (0, 0) |
299 | (0, 0) |
286 | } |
300 | } |
287 | |
301 | |
288 | sub draw { } |
302 | sub draw { } |
289 | |
303 | |
290 | ############################################################################# |
304 | ############################################################################# |
291 | |
305 | |
292 | package CFClient::Widget::Container; |
306 | package CFClient::UI::Container; |
293 | |
307 | |
294 | our @ISA = CFClient::Widget::; |
308 | our @ISA = CFClient::UI::Base::; |
295 | |
309 | |
296 | sub new { |
310 | sub new { |
297 | my ($class, %arg) = @_; |
311 | my ($class, %arg) = @_; |
298 | |
312 | |
299 | my $children = delete $arg{children} || []; |
313 | my $children = delete $arg{children} || []; |
… | |
… | |
303 | |
317 | |
304 | $self |
318 | $self |
305 | } |
319 | } |
306 | |
320 | |
307 | sub add { |
321 | sub add { |
308 | my ($self, $chld, $expand) = @_; |
322 | my ($self, $chld) = @_; |
309 | |
323 | |
310 | $chld->{expand} = $expand; |
|
|
311 | $chld->set_parent ($self); |
324 | $chld->set_parent ($self); |
312 | |
325 | |
313 | $self->{children} = [ |
326 | $self->{children} = [ |
314 | sort { $a->{z} <=> $b->{z} } |
327 | sort { $a->{z} <=> $b->{z} } |
315 | @{$self->{children}}, $chld |
328 | @{$self->{children}}, $chld |
316 | ]; |
329 | ]; |
317 | |
330 | |
318 | $self->size_allocate ($self->{w}, $self->{h}) |
331 | $self->{w} = $self->{h} = -1; |
319 | if $self->{w}; #TODO: check for "realised state" |
332 | $self->update; |
320 | } |
333 | } |
321 | |
334 | |
322 | sub remove { |
335 | sub remove { |
323 | my ($self, $widget) = @_; |
336 | my ($self, $widget) = @_; |
324 | |
337 | |
325 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
338 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
326 | |
339 | |
327 | $self->size_allocate ($self->{w}, $self->{h}); |
340 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
328 | } |
341 | } |
329 | |
342 | |
330 | sub find_widget { |
343 | sub find_widget { |
331 | my ($self, $x, $y) = @_; |
344 | my ($self, $x, $y) = @_; |
332 | |
345 | |
… | |
… | |
349 | $_->draw for @{$self->{children}}; |
362 | $_->draw for @{$self->{children}}; |
350 | } |
363 | } |
351 | |
364 | |
352 | ############################################################################# |
365 | ############################################################################# |
353 | |
366 | |
354 | package CFClient::Widget::Bin; |
367 | package CFClient::UI::Bin; |
355 | |
368 | |
356 | our @ISA = CFClient::Widget::Container::; |
369 | our @ISA = CFClient::UI::Container::; |
357 | |
370 | |
358 | sub new { |
371 | sub new { |
359 | my ($class, %arg) = @_; |
372 | my ($class, %arg) = @_; |
360 | |
373 | |
361 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
374 | my $child = (delete $arg{child}) || new CFClient::UI::Empty::; |
362 | |
375 | |
363 | $class->SUPER::new (children => [$child], %arg) |
376 | $class->SUPER::new (children => [$child], %arg) |
364 | } |
377 | } |
365 | |
378 | |
366 | sub add { |
379 | sub add { |
… | |
… | |
374 | sub remove { |
387 | sub remove { |
375 | my ($self, $widget) = @_; |
388 | my ($self, $widget) = @_; |
376 | |
389 | |
377 | $self->SUPER::remove ($widget); |
390 | $self->SUPER::remove ($widget); |
378 | |
391 | |
379 | $self->{children} = [new CFClient::Widget::Empty] |
392 | $self->{children} = [new CFClient::UI::Empty] |
380 | unless @{$self->{children}}; |
393 | unless @{$self->{children}}; |
381 | } |
394 | } |
382 | |
395 | |
383 | sub child { $_[0]->{children}[0] } |
396 | sub child { $_[0]->{children}[0] } |
384 | |
397 | |
385 | sub size_request { |
398 | sub size_request { |
386 | $_[0]{children}[0]->size_request |
399 | $_[0]{children}[0]->size_request |
387 | } |
400 | } |
388 | |
401 | |
389 | sub size_allocate { |
402 | sub size_allocate { |
390 | my ($self, $w, $h) = @_; |
403 | my ($self, $x, $y, $w, $h) = @_; |
391 | |
404 | |
392 | return unless $self->{w} != $w || $self->{h} != $h; |
405 | $self->_size_allocate ($x, $y, $w, $h) or return; |
393 | |
406 | |
394 | $self->SUPER::size_allocate ($w, $h); |
|
|
395 | $self->{children}[0]->size_allocate ($w, $h); |
407 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
396 | } |
408 | } |
397 | |
409 | |
398 | ############################################################################# |
410 | ############################################################################# |
399 | |
411 | |
400 | package CFClient::Widget::Window; |
412 | package CFClient::UI::Window; |
401 | |
413 | |
402 | our @ISA = CFClient::Widget::Bin::; |
414 | our @ISA = CFClient::UI::Bin::; |
403 | |
415 | |
404 | use SDL::OpenGL; |
416 | use SDL::OpenGL; |
405 | |
417 | |
406 | sub new { |
418 | sub new { |
407 | my ($class, %arg) = @_; |
419 | my ($class, %arg) = @_; |
… | |
… | |
425 | $self->{w}, $self->{h}, sub { $self->child->draw } |
437 | $self->{w}, $self->{h}, sub { $self->child->draw } |
426 | ); |
438 | ); |
427 | } |
439 | } |
428 | |
440 | |
429 | sub size_allocate { |
441 | sub size_allocate { |
430 | my ($self, $w, $h) = @_; |
442 | my ($self, $x, $y, $w, $h) = @_; |
431 | |
443 | |
432 | return unless $self->{w} != $w || $self->{h} != $h; |
444 | $self->_size_allocate ($x, $y, $w, $h) or return; |
433 | |
445 | |
434 | $self->{w} = $w; |
|
|
435 | $self->{h} = $h; |
|
|
436 | |
|
|
437 | $self->child->size_allocate ($w, $h); |
446 | $self->child->size_allocate (0, 0, $w, $h); |
438 | |
447 | |
439 | $self->render_chld; |
448 | $self->render_chld; |
440 | } |
449 | } |
441 | |
450 | |
442 | sub _draw { |
451 | sub _draw { |
… | |
… | |
446 | |
455 | |
447 | my $tex = $self->{texture} |
456 | my $tex = $self->{texture} |
448 | or return; |
457 | or return; |
449 | |
458 | |
450 | glEnable GL_BLEND; |
459 | glEnable GL_BLEND; |
|
|
460 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
451 | glEnable GL_TEXTURE_2D; |
461 | glEnable GL_TEXTURE_2D; |
452 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
462 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
453 | |
463 | |
454 | $tex->draw_quad (0, 0, $w, $h); |
464 | $tex->draw_quad (0, 0, $w, $h); |
455 | |
465 | |
… | |
… | |
457 | glDisable GL_TEXTURE_2D; |
467 | glDisable GL_TEXTURE_2D; |
458 | } |
468 | } |
459 | |
469 | |
460 | ############################################################################# |
470 | ############################################################################# |
461 | |
471 | |
462 | package CFClient::Widget::Frame; |
472 | package CFClient::UI::Frame; |
463 | |
473 | |
464 | our @ISA = CFClient::Widget::Bin::; |
474 | our @ISA = CFClient::UI::Bin::; |
465 | |
475 | |
466 | use SDL::OpenGL; |
476 | use SDL::OpenGL; |
467 | |
477 | |
468 | sub size_request { |
478 | sub size_request { |
469 | my ($self) = @_; |
479 | my ($self) = @_; |
… | |
… | |
474 | |
484 | |
475 | map { $_ + 4 } $chld->size_request; |
485 | map { $_ + 4 } $chld->size_request; |
476 | } |
486 | } |
477 | |
487 | |
478 | sub size_allocate { |
488 | sub size_allocate { |
479 | my ($self, $w, $h) = @_; |
489 | my ($self, $x, $y, $w, $h) = @_; |
480 | |
490 | |
481 | return unless $self->{w} != $w || $self->{h} != $h; |
491 | $self->_size_allocate ($x, $y, $w, $h) or return; |
482 | |
492 | |
483 | $self->{w} = $w; |
|
|
484 | $self->{h} = $h; |
|
|
485 | |
|
|
486 | $self->child->size_allocate ($w - 4, $h - 4); |
493 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
487 | $self->child->move (2, 2); |
|
|
488 | } |
494 | } |
489 | |
495 | |
490 | sub _draw { |
496 | sub _draw { |
491 | my ($self) = @_; |
497 | my ($self) = @_; |
492 | |
498 | |
… | |
… | |
505 | $chld->draw; |
511 | $chld->draw; |
506 | } |
512 | } |
507 | |
513 | |
508 | ############################################################################# |
514 | ############################################################################# |
509 | |
515 | |
510 | package CFClient::Widget::FancyFrame; |
516 | package CFClient::UI::FancyFrame; |
511 | |
517 | |
512 | our @ISA = CFClient::Widget::Bin::; |
518 | our @ISA = CFClient::UI::Bin::; |
513 | |
519 | |
514 | use SDL::OpenGL; |
520 | use SDL::OpenGL; |
515 | |
521 | |
516 | my @tex = |
522 | my @tex = |
517 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
523 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
518 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
524 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
519 | |
525 | |
520 | sub size_request { |
526 | sub size_request { |
521 | my ($self) = @_; |
527 | my ($self) = @_; |
|
|
528 | |
|
|
529 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
522 | |
530 | |
523 | my ($w, $h) = $self->SUPER::size_request; |
531 | my ($w, $h) = $self->SUPER::size_request; |
524 | |
532 | |
525 | $h += $tex[1]->{h}; |
533 | $h += $tex[1]->{h}; |
526 | $h += $tex[4]->{h}; |
534 | $h += $tex[4]->{h}; |
… | |
… | |
529 | |
537 | |
530 | ($w, $h) |
538 | ($w, $h) |
531 | } |
539 | } |
532 | |
540 | |
533 | sub size_allocate { |
541 | sub size_allocate { |
534 | my ($self, $w, $h) = @_; |
542 | my ($self, $x, $y, $w, $h) = @_; |
535 | |
543 | |
536 | return unless $self->{w} != $w || $self->{h} != $h; |
544 | $self->_size_allocate ($x, $y, $w, $h) or return; |
537 | |
545 | |
538 | $self->SUPER::size_allocate ($w, $h); |
|
|
539 | |
|
|
540 | $h -= $tex[1]->{h}; |
546 | $h -= $tex[1]{h}; |
541 | $h -= $tex[4]->{h}; |
547 | $h -= $tex[4]{h}; |
542 | $w -= $tex[2]->{w}; |
548 | $w -= $tex[2]{w}; |
543 | $w -= $tex[3]->{w}; |
549 | $w -= $tex[3]{w}; |
544 | |
550 | |
545 | $h = $h < 0 ? 0 : $h; |
551 | $h = $h < 0 ? 0 : $h; |
546 | $w = $w < 0 ? 0 : $w; |
552 | $w = $w < 0 ? 0 : $w; |
547 | |
553 | |
548 | my $child = $self->child; |
554 | my $child = $self->child; |
549 | |
555 | |
550 | $child->size_allocate ($w, $h); |
|
|
551 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
556 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
|
|
557 | } |
|
|
558 | |
|
|
559 | sub button_down { |
|
|
560 | my ($self, $ev, $x, $y) = @_; |
|
|
561 | |
|
|
562 | if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w} |
|
|
563 | && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) { |
|
|
564 | |
|
|
565 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
566 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
567 | |
|
|
568 | $self->{motion} = sub { |
|
|
569 | my ($ev, $x, $y) = @_; |
|
|
570 | |
|
|
571 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
572 | |
|
|
573 | $self->{user_w} = $bw + $x - $ox; |
|
|
574 | $self->{user_h} = $bh + $y - $oy; |
|
|
575 | $self->update; |
|
|
576 | }; |
|
|
577 | |
|
|
578 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
579 | && $y >= 0 && $y < $tex[1]{h}) { |
|
|
580 | |
|
|
581 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
582 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
583 | |
|
|
584 | $self->{motion} = sub { |
|
|
585 | my ($ev, $x, $y) = @_; |
|
|
586 | |
|
|
587 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
588 | |
|
|
589 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
590 | $self->update; |
|
|
591 | }; |
|
|
592 | } |
|
|
593 | } |
|
|
594 | |
|
|
595 | sub button_up { |
|
|
596 | my ($self, $ev, $x, $y) = @_; |
|
|
597 | |
|
|
598 | delete $self->{motion}; |
|
|
599 | } |
|
|
600 | |
|
|
601 | sub mouse_motion { |
|
|
602 | my ($self, $ev, $x, $y) = @_; |
|
|
603 | |
|
|
604 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
552 | } |
605 | } |
553 | |
606 | |
554 | sub _draw { |
607 | sub _draw { |
555 | my ($self) = @_; |
608 | my ($self) = @_; |
556 | |
609 | |
557 | my ($w, $h) = ($self->{w}, $self->{h}); |
610 | my ($w, $h) = ($self->{w}, $self->{h}); |
558 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
611 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
559 | |
612 | |
560 | glEnable GL_BLEND; |
613 | glEnable GL_BLEND; |
|
|
614 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
561 | glEnable GL_TEXTURE_2D; |
615 | glEnable GL_TEXTURE_2D; |
562 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
616 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
564 | |
617 | |
565 | my $top = $tex[1]; |
618 | my $top = $tex[1]; |
566 | $top->draw_quad (0, 0, $w, $top->{h}); |
619 | $top->draw_quad (0, 0, $w, $top->{h}); |
567 | |
620 | |
… | |
… | |
573 | |
626 | |
574 | my $bottom = $tex[4]; |
627 | my $bottom = $tex[4]; |
575 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
628 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
576 | |
629 | |
577 | my $bg = $tex[0]; |
630 | my $bg = $tex[0]; |
|
|
631 | |
578 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
632 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
579 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
633 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
580 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
634 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
581 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
635 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
582 | |
636 | |
583 | my $rep_x = $cw / $bg->{w}; |
637 | my $rep_x = $cw / $bg->{w}; |
584 | my $rep_y = $ch / $bg->{h}; |
638 | my $rep_y = $ch / $bg->{h}; |
585 | |
639 | |
586 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
640 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
587 | |
641 | |
|
|
642 | glDisable GL_TEXTURE_2D; |
588 | glDisable GL_BLEND; |
643 | glDisable GL_BLEND; |
589 | glDisable GL_TEXTURE_2D; |
|
|
590 | |
644 | |
591 | $self->child->draw; |
645 | $self->child->draw; |
592 | |
646 | |
593 | } |
647 | } |
594 | |
648 | |
595 | ############################################################################# |
649 | ############################################################################# |
596 | |
650 | |
597 | package CFClient::Widget::Table; |
651 | package CFClient::UI::Table; |
598 | |
652 | |
599 | our @ISA = CFClient::Widget::; |
653 | our @ISA = CFClient::UI::Base::; |
|
|
654 | |
|
|
655 | use List::Util qw(max sum); |
600 | |
656 | |
601 | use SDL::OpenGL; |
657 | use SDL::OpenGL; |
|
|
658 | |
|
|
659 | sub new { |
|
|
660 | my $class = shift; |
|
|
661 | |
|
|
662 | $class->SUPER::new ( |
|
|
663 | col_expand => [], |
|
|
664 | @_ |
|
|
665 | ) |
|
|
666 | } |
602 | |
667 | |
603 | sub add { |
668 | sub add { |
604 | my ($self, $x, $y, $chld) = @_; |
669 | my ($self, $x, $y, $chld) = @_; |
605 | my $old_chld = $self->{children}[$y][$x]; |
|
|
606 | |
670 | |
607 | $self->{children}[$y][$x] = $chld; |
671 | $self->{children}[$y][$x] = $chld; |
608 | $chld->set_parent ($self); |
672 | $chld->set_parent ($self); |
|
|
673 | |
|
|
674 | $self->{w} = $self->{h} = -1; |
609 | $self->update; |
675 | $self->update; |
610 | } |
676 | } |
611 | |
677 | |
612 | sub max_row_height { |
678 | sub get_wh { |
613 | my ($self, $row) = @_; |
679 | my ($self) = @_; |
614 | |
680 | |
615 | my $hs = 0; |
681 | my (@w, @h); |
616 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
682 | |
|
|
683 | for my $y (0 .. $#{$self->{children}}) { |
617 | my $c = $self->{children}->[$row]->[$xi]; |
684 | my $row = $self->{children}[$y] |
618 | if ($c) { |
685 | or next; |
|
|
686 | |
|
|
687 | for my $x (0 .. $#$row) { |
|
|
688 | my $widget = $row->[$x] |
|
|
689 | or next; |
619 | my ($w, $h) = $c->size_request; |
690 | my ($w, $h) = $widget->size_request; |
620 | if ($hs < $h) { $hs = $h } |
691 | |
|
|
692 | $w[$x] = max $w[$x], $w; |
|
|
693 | $h[$y] = max $h[$y], $h; |
621 | } |
694 | } |
622 | } |
695 | } |
623 | return $hs; |
|
|
624 | } |
|
|
625 | |
696 | |
626 | sub max_col_width { |
697 | (\@w, \@h) |
|
|
698 | } |
|
|
699 | |
|
|
700 | sub size_request { |
627 | my ($self, $col) = @_; |
701 | my ($self) = @_; |
628 | |
702 | |
|
|
703 | my ($ws, $hs) = $self->get_wh; |
|
|
704 | |
|
|
705 | ( |
|
|
706 | (sum @$ws), |
|
|
707 | (sum @$hs), |
|
|
708 | ) |
|
|
709 | } |
|
|
710 | |
|
|
711 | sub size_allocate { |
|
|
712 | my ($self, $x, $y, $w, $h) = @_; |
|
|
713 | |
|
|
714 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
715 | |
|
|
716 | my ($ws, $hs) = $self->get_wh; |
|
|
717 | |
|
|
718 | my $req_w = sum @$ws; |
|
|
719 | my $req_h = sum @$hs; |
|
|
720 | |
|
|
721 | # TODO: nicer code && do row_expand |
|
|
722 | my @col_expand = @{$self->{col_expand}}; |
|
|
723 | @col_expand = (1) x @$ws unless @col_expand; |
|
|
724 | my $col_expand = (sum @col_expand) || 1; |
|
|
725 | |
|
|
726 | # linearly scale sizes |
|
|
727 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
|
|
728 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
729 | |
|
|
730 | my $y; |
|
|
731 | |
|
|
732 | for my $r (0 .. $#{$self->{children}}) { |
|
|
733 | my $row = $self->{children}[$r] |
|
|
734 | or next; |
|
|
735 | |
629 | my $ws = 0; |
736 | my $x = 0; |
630 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
737 | my $row_h = $hs->[$r]; |
631 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
738 | |
632 | if ($c) { |
739 | for my $c (0 .. $#$row) { |
633 | my ($w, $h) = $c->size_request; |
740 | my $widget = $row->[$c] |
634 | if ($ws < $w) { $ws = $w } |
741 | or next; |
|
|
742 | |
|
|
743 | my $col_w = $ws->[$c]; |
|
|
744 | |
|
|
745 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
746 | |
|
|
747 | $x += $col_w; |
635 | } |
748 | } |
|
|
749 | |
|
|
750 | $y += $row_h; |
636 | } |
751 | } |
637 | return $ws; |
752 | |
638 | } |
753 | } |
|
|
754 | |
|
|
755 | sub find_widget { |
|
|
756 | my ($self, $x, $y) = @_; |
|
|
757 | |
|
|
758 | $x -= $self->{x}; |
|
|
759 | $y -= $self->{y}; |
|
|
760 | |
|
|
761 | my $res; |
|
|
762 | |
|
|
763 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
764 | $res = $_->find_widget ($x, $y) |
|
|
765 | and return $res; |
|
|
766 | } |
|
|
767 | |
|
|
768 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
769 | } |
|
|
770 | |
|
|
771 | sub _draw { |
|
|
772 | my ($self) = @_; |
|
|
773 | |
|
|
774 | for (grep $_, @{$self->{children}}) { |
|
|
775 | $_->draw for grep $_, @$_; |
|
|
776 | } |
|
|
777 | } |
|
|
778 | |
|
|
779 | ############################################################################# |
|
|
780 | |
|
|
781 | package CFClient::UI::HBox; |
|
|
782 | |
|
|
783 | # TODO: wrap into common Box base class |
|
|
784 | |
|
|
785 | our @ISA = CFClient::UI::Container::; |
639 | |
786 | |
640 | sub size_request { |
787 | sub size_request { |
641 | my ($self) = @_; |
788 | my ($self) = @_; |
642 | |
789 | |
|
|
790 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
791 | |
|
|
792 | ( |
|
|
793 | (List::Util::sum map $_->[0], @alloc), |
|
|
794 | (List::Util::max map $_->[1], @alloc), |
|
|
795 | ) |
|
|
796 | } |
|
|
797 | |
|
|
798 | sub size_allocate { |
|
|
799 | my ($self, $x, $y, $w, $h) = @_; |
|
|
800 | |
|
|
801 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
802 | |
|
|
803 | return unless $self->{w}; |
|
|
804 | |
643 | my ($hs, $ws) = (0, 0); |
805 | ($h, $w) = ($w, $h); |
644 | |
806 | |
645 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
807 | my $children = $self->{children}; |
646 | $hs += $self->max_row_height ($yi); |
|
|
647 | } |
|
|
648 | |
808 | |
649 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
809 | my @h = map +($_->size_request)[0], @$children; |
650 | my $wm = 0; |
810 | |
651 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
811 | my $req_h = List::Util::sum @h; |
652 | $wm += $self->max_col_width ($xi) |
812 | |
|
|
813 | if ($req_h > $h) { |
|
|
814 | # ah well, not enough space |
|
|
815 | $_ *= $h / $req_h for @h; |
|
|
816 | } else { |
|
|
817 | my $exp = List::Util::sum map $_->{expand}, @$children; |
|
|
818 | $exp ||= 1; |
|
|
819 | |
|
|
820 | for (0 .. $#$children) { |
|
|
821 | my $child = $children->[$_]; |
|
|
822 | |
|
|
823 | my $alloc_h = $h[$_]; |
|
|
824 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
|
|
825 | $h[$_] = $alloc_h; |
653 | } |
826 | } |
654 | if ($ws < $wm) { $ws = $wm } |
|
|
655 | } |
827 | } |
656 | |
|
|
657 | return ($ws, $hs); |
|
|
658 | } |
|
|
659 | |
|
|
660 | sub _draw { |
|
|
661 | my ($self) = @_; |
|
|
662 | |
828 | |
663 | my $y = 0; |
829 | my $y = 0; |
664 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
830 | for (0 .. $#$children) { |
|
|
831 | my $child = $children->[$_]; |
665 | my $x = 0; |
832 | my $h = $h[$_]; |
|
|
833 | $child->size_allocate ($y, 0, $h, $w); |
666 | |
834 | |
667 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
835 | $y += $h; |
668 | |
|
|
669 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
670 | if ($c) { |
|
|
671 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
672 | $c->draw if $c; |
|
|
673 | } |
|
|
674 | |
|
|
675 | $x += $self->max_col_width ($xi); |
|
|
676 | } |
|
|
677 | |
|
|
678 | $y += $self->max_row_height ($yi); |
|
|
679 | } |
836 | } |
680 | } |
837 | } |
681 | |
838 | |
682 | ############################################################################# |
839 | ############################################################################# |
683 | |
840 | |
684 | package CFClient::Widget::VBox; |
841 | package CFClient::UI::VBox; |
685 | |
842 | |
|
|
843 | # TODO: wrap into common Box base class |
|
|
844 | |
686 | our @ISA = CFClient::Widget::Container::; |
845 | our @ISA = CFClient::UI::Container::; |
687 | |
|
|
688 | use SDL::OpenGL; |
|
|
689 | |
846 | |
690 | sub size_request { |
847 | sub size_request { |
691 | my ($self) = @_; |
848 | my ($self) = @_; |
692 | |
849 | |
693 | my @alloc = map [$_->size_request], @{$self->{children}}; |
850 | my @alloc = map [$_->size_request], @{$self->{children}}; |
… | |
… | |
697 | (List::Util::sum map $_->[1], @alloc), |
854 | (List::Util::sum map $_->[1], @alloc), |
698 | ) |
855 | ) |
699 | } |
856 | } |
700 | |
857 | |
701 | sub size_allocate { |
858 | sub size_allocate { |
702 | my ($self, $w, $h) = @_; |
859 | my ($self, $x, $y, $w, $h) = @_; |
703 | |
860 | |
704 | return unless $self->{w} != $w || $self->{h} != $h; |
861 | $self->_size_allocate ($x, $y, $w, $h) or return; |
705 | |
|
|
706 | $self->{w} = $w; |
|
|
707 | $self->{h} = $h; |
|
|
708 | |
862 | |
709 | return unless $self->{h}; |
863 | return unless $self->{h}; |
710 | |
864 | |
711 | my $children = $self->{children}; |
865 | my $children = $self->{children}; |
712 | |
866 | |
… | |
… | |
714 | |
868 | |
715 | my $req_h = List::Util::sum @h; |
869 | my $req_h = List::Util::sum @h; |
716 | |
870 | |
717 | if ($req_h > $h) { |
871 | if ($req_h > $h) { |
718 | # ah well, not enough space |
872 | # ah well, not enough space |
719 | $_ = $h[$_] * $h / $req_h for @h; |
873 | $_ *= $h / $req_h for @h; |
720 | } else { |
874 | } else { |
721 | my @exp = grep $_->{expand}, @$children; |
875 | my $exp = List::Util::sum map $_->{expand}, @$children; |
722 | @exp = @$children unless @exp; |
876 | $exp ||= 1; |
723 | |
877 | |
724 | my %exp = map +($_ => 1), @exp; |
|
|
725 | |
|
|
726 | for (0 .. $#$children) { |
878 | for (0 .. $#$children) { |
727 | my $child = $children->[$_]; |
879 | my $child = $children->[$_]; |
728 | |
880 | |
729 | my $alloc_h = $h[$_]; |
881 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
730 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
731 | $h[$_] = $alloc_h; |
|
|
732 | } |
882 | } |
733 | } |
883 | } |
734 | |
884 | |
735 | my $y = 0; |
885 | my $y = 0; |
736 | for (0 .. $#$children) { |
886 | for (0 .. $#$children) { |
737 | my $child = $children->[$_]; |
887 | my $child = $children->[$_]; |
738 | my $h = $h[$_]; |
888 | my $h = $h[$_]; |
739 | $child->move (0, $y); |
|
|
740 | $child->size_allocate ($w, $h); |
889 | $child->size_allocate (0, $y, $w, $h); |
741 | |
890 | |
742 | $y += $h; |
891 | $y += $h; |
743 | } |
892 | } |
744 | } |
893 | } |
745 | |
894 | |
746 | ############################################################################# |
895 | ############################################################################# |
747 | |
896 | |
748 | package CFClient::Widget::Label; |
897 | package CFClient::UI::Label; |
749 | |
898 | |
750 | our @ISA = CFClient::Widget::; |
899 | our @ISA = CFClient::UI::Base::; |
751 | |
900 | |
752 | use SDL::OpenGL; |
901 | use SDL::OpenGL; |
753 | |
902 | |
754 | sub new { |
903 | sub new { |
755 | my ($class, %arg) = @_; |
904 | my ($class, %arg) = @_; |
756 | |
905 | |
757 | my $self = $class->SUPER::new ( |
906 | my $self = $class->SUPER::new ( |
758 | fg => [1, 1, 1], |
907 | fg => [1, 1, 1], |
759 | height => $::FONTSIZE, |
908 | height => $::FONTSIZE, |
760 | text => "", |
909 | text => "", |
761 | align => -1, |
910 | align => -1, |
|
|
911 | padding => 2, |
762 | layout => new CFClient::Layout, |
912 | layout => new CFClient::Layout, |
763 | %arg |
913 | %arg |
764 | ); |
914 | ); |
765 | |
915 | |
766 | $self->set_text ($self->{text}); |
916 | $self->set_text ($self->{text}); |
767 | |
917 | |
… | |
… | |
797 | sub size_request { |
947 | sub size_request { |
798 | my ($self) = @_; |
948 | my ($self) = @_; |
799 | |
949 | |
800 | $self->{layout}->set_width; |
950 | $self->{layout}->set_width; |
801 | $self->{layout}->set_height ($self->{height}); |
951 | $self->{layout}->set_height ($self->{height}); |
802 | $self->{layout}->size |
952 | my ($w, $h) = $self->{layout}->size; |
803 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
953 | |
804 | # ( |
954 | ( |
805 | # $self->{texture}{w}, |
955 | $w + $self->{padding} * 2, |
806 | # $self->{texture}{h}, |
956 | $h + $self->{padding} * 2, |
807 | # ) |
957 | ) |
808 | # } else { |
|
|
809 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
810 | # |
|
|
811 | # ($w, $h) |
|
|
812 | # } |
|
|
813 | } |
958 | } |
814 | |
959 | |
815 | sub size_allocate { |
960 | sub size_allocate { |
816 | my ($self, $w, $h) = @_; |
961 | my ($self, $x, $y, $w, $h) = @_; |
817 | |
962 | |
818 | return unless $self->{w} != $w || $self->{h} != $h; |
963 | $self->_size_allocate ($x, $y, $w, $h) or return; |
819 | |
964 | |
820 | $self->SUPER::size_allocate ($w, $h); |
|
|
821 | delete $self->{texture}; |
965 | delete $self->{texture}; |
822 | } |
966 | } |
823 | |
967 | |
824 | sub update { |
968 | sub update { |
825 | my ($self) = @_; |
969 | my ($self) = @_; |
… | |
… | |
831 | sub _draw { |
975 | sub _draw { |
832 | my ($self) = @_; |
976 | my ($self) = @_; |
833 | |
977 | |
834 | my $tex = $self->{texture} ||= do { |
978 | my $tex = $self->{texture} ||= do { |
835 | $self->{layout}->set_width ($self->{w}); |
979 | $self->{layout}->set_width ($self->{w}); |
|
|
980 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{height}); |
836 | new_from_layout CFClient::Texture $self->{layout}; |
981 | new_from_layout CFClient::Texture $self->{layout} |
837 | }; |
982 | }; |
838 | |
983 | |
839 | glEnable GL_BLEND; |
984 | glEnable GL_BLEND; |
|
|
985 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
840 | glEnable GL_TEXTURE_2D; |
986 | glEnable GL_TEXTURE_2D; |
841 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
842 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
987 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
843 | |
988 | |
844 | glColor @{$self->{fg}}; |
989 | glColor @{$self->{fg}}; |
845 | |
990 | |
846 | my $x = |
991 | my $x = |
847 | $self->{align} < 0 ? 0 |
992 | $self->{align} < 0 ? $self->{padding} |
848 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
993 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
849 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
994 | : ($self->{w} - $tex->{w}) * 0.5; |
850 | |
995 | |
851 | $tex->draw_quad ($x, 0); |
996 | $tex->draw_quad ($x, $self->{padding}); |
852 | |
997 | |
|
|
998 | glDisable GL_TEXTURE_2D; |
853 | glDisable GL_BLEND; |
999 | glDisable GL_BLEND; |
854 | glDisable GL_TEXTURE_2D; |
|
|
855 | } |
1000 | } |
856 | |
1001 | |
857 | ############################################################################# |
1002 | ############################################################################# |
858 | |
1003 | |
859 | package CFClient::Widget::Entry; |
1004 | package CFClient::UI::Entry; |
860 | |
1005 | |
861 | our @ISA = CFClient::Widget::Label::; |
1006 | our @ISA = CFClient::UI::Label::; |
862 | |
1007 | |
863 | use SDL; |
1008 | use SDL; |
864 | use SDL::OpenGL; |
1009 | use SDL::OpenGL; |
865 | |
1010 | |
866 | sub new { |
1011 | sub new { |
867 | my $class = shift; |
1012 | my $class = shift; |
868 | |
1013 | |
869 | $class->SUPER::new ( |
1014 | $class->SUPER::new ( |
870 | fg => [1, 1, 1], |
1015 | fg => [1, 1, 1], |
871 | bg => [0, 0, 0, 0.4], |
1016 | bg => [0, 0, 0, 0.2], |
872 | active_bg => [1, 1, 1], |
1017 | active_bg => [1, 1, 1], |
873 | active_fg => [0, 0, 0], |
1018 | active_fg => [0, 0, 0], |
874 | @_ |
1019 | @_ |
875 | ) |
1020 | ) |
876 | } |
1021 | } |
… | |
… | |
883 | $self->{text} = $text; |
1028 | $self->{text} = $text; |
884 | $self->{layout}->set_width ($self->{w}); |
1029 | $self->{layout}->set_width ($self->{w}); |
885 | |
1030 | |
886 | $text =~ s/./*/g if $self->{hidden}; |
1031 | $text =~ s/./*/g if $self->{hidden}; |
887 | |
1032 | |
888 | |
|
|
889 | $self->{layout}->set_markup ($self->escape_text ($text)); |
1033 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
890 | |
1034 | |
891 | $text = substr $text, 0, $self->{cursor}; |
1035 | $text = substr $text, 0, $self->{cursor}; |
892 | utf8::encode $text; |
1036 | utf8::encode $text; |
893 | |
1037 | |
894 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1038 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
… | |
… | |
901 | |
1045 | |
902 | ($w + 1, $h) # add 1 for cursor |
1046 | ($w + 1, $h) # add 1 for cursor |
903 | } |
1047 | } |
904 | |
1048 | |
905 | sub size_allocate { |
1049 | sub size_allocate { |
906 | my ($self, $w, $h) = @_; |
1050 | my ($self, $x, $y, $w, $h) = @_; |
907 | |
1051 | |
908 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
909 | |
|
|
910 | $self->SUPER::size_allocate ($w, $h); |
1052 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
911 | |
1053 | |
912 | $self->_set_text ($self->{text}); |
1054 | $self->_set_text ($self->{text}); |
913 | } |
1055 | } |
914 | |
1056 | |
915 | sub set_text { |
1057 | sub set_text { |
… | |
… | |
936 | substr $text, $self->{cursor}, 1, ""; |
1078 | substr $text, $self->{cursor}, 1, ""; |
937 | } elsif ($sym == SDLK_LEFT) { |
1079 | } elsif ($sym == SDLK_LEFT) { |
938 | --$self->{cursor} if $self->{cursor}; |
1080 | --$self->{cursor} if $self->{cursor}; |
939 | } elsif ($sym == SDLK_RIGHT) { |
1081 | } elsif ($sym == SDLK_RIGHT) { |
940 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1082 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1083 | } elsif ($sym == SDLK_HOME) { |
|
|
1084 | $self->{cursor} = 0; |
|
|
1085 | } elsif ($sym == SDLK_END) { |
|
|
1086 | $self->{cursor} = length $text; |
941 | } elsif ($uni) { |
1087 | } elsif ($uni) { |
942 | substr $text, $self->{cursor}++, 0, chr $uni; |
1088 | substr $text, $self->{cursor}++, 0, chr $uni; |
943 | } |
1089 | } |
944 | |
1090 | |
945 | $self->_set_text ($text); |
1091 | $self->_set_text ($text); |
… | |
… | |
960 | $self->SUPER::button_down ($ev, $x, $y); |
1106 | $self->SUPER::button_down ($ev, $x, $y); |
961 | |
1107 | |
962 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1108 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
963 | |
1109 | |
964 | # byte-index to char-index |
1110 | # byte-index to char-index |
965 | my $text = $self->{layout}; |
1111 | my $text = $self->{text}; |
966 | utf8::encode $text; |
1112 | utf8::encode $text; |
967 | $self->{cursor} = length substr $text, 0, $idx; |
1113 | $self->{cursor} = length substr $text, 0, $idx; |
968 | |
1114 | |
969 | $self->_set_text ($self->{text}); |
1115 | $self->_set_text ($self->{text}); |
970 | $self->update; |
1116 | $self->update; |
… | |
… | |
985 | $self->{fg} = $self->{active_fg}; |
1131 | $self->{fg} = $self->{active_fg}; |
986 | } else { |
1132 | } else { |
987 | glColor @{$self->{bg}}; |
1133 | glColor @{$self->{bg}}; |
988 | } |
1134 | } |
989 | |
1135 | |
|
|
1136 | glEnable GL_BLEND; |
|
|
1137 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
990 | glBegin GL_QUADS; |
1138 | glBegin GL_QUADS; |
991 | glVertex 0 , 0; |
1139 | glVertex 0 , 0; |
992 | glVertex 0 , $self->{h}; |
1140 | glVertex 0 , $self->{h}; |
993 | glVertex $self->{w}, $self->{h}; |
1141 | glVertex $self->{w}, $self->{h}; |
994 | glVertex $self->{w}, 0; |
1142 | glVertex $self->{w}, 0; |
995 | glEnd; |
1143 | glEnd; |
|
|
1144 | glDisable GL_BLEND; |
996 | |
1145 | |
997 | $self->SUPER::_draw; |
1146 | $self->SUPER::_draw; |
998 | |
1147 | |
999 | #TODO: force update every cursor change :( |
1148 | #TODO: force update every cursor change :( |
1000 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1149 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1006 | } |
1155 | } |
1007 | } |
1156 | } |
1008 | |
1157 | |
1009 | ############################################################################# |
1158 | ############################################################################# |
1010 | |
1159 | |
1011 | package CFClient::Widget::Slider; |
1160 | package CFClient::UI::Slider; |
1012 | |
1161 | |
1013 | use strict; |
1162 | use strict; |
1014 | |
1163 | |
1015 | use SDL::OpenGL; |
1164 | use SDL::OpenGL; |
1016 | use SDL::OpenGL::Constants; |
|
|
1017 | |
1165 | |
1018 | our @ISA = CFClient::Widget::DrawBG::; |
1166 | our @ISA = CFClient::UI::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 | |
1167 | |
1029 | sub new { |
1168 | sub new { |
1030 | my $class = shift; |
1169 | my $class = shift; |
1031 | |
1170 | |
1032 | # range [value, low, high, page] |
1171 | # range [value, low, high, page] |
1033 | |
1172 | |
1034 | $class->SUPER::new ( |
1173 | my $self = $class->SUPER::new ( |
1035 | fg => [1, 1, 1], |
1174 | fg => [1, 1, 1], |
1036 | active_fg => [0, 0, 0], |
1175 | active_fg => [0, 0, 0], |
1037 | range => [0, 0, 100, 10], |
1176 | range => [0, 0, 100, 10], |
|
|
1177 | req_w => 40, |
|
|
1178 | req_h => 10, |
1038 | vertical => 1, |
1179 | vertical => 0, |
1039 | @_ |
1180 | @_ |
1040 | ) |
1181 | ); |
|
|
1182 | |
|
|
1183 | $self |
|
|
1184 | } |
|
|
1185 | |
|
|
1186 | sub size_request { |
|
|
1187 | my ($self) = @_; |
|
|
1188 | |
|
|
1189 | my $w = $self->{req_w}; |
|
|
1190 | my $h = $self->{req_h}; |
|
|
1191 | |
|
|
1192 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1041 | } |
1193 | } |
1042 | |
1194 | |
1043 | sub button_down { |
1195 | sub button_down { |
1044 | my ($self, $ev, $x, $y) = @_; |
1196 | my ($self, $ev, $x, $y) = @_; |
1045 | |
1197 | |
… | |
… | |
1120 | glEnd; |
1272 | glEnd; |
1121 | } |
1273 | } |
1122 | |
1274 | |
1123 | ############################################################################# |
1275 | ############################################################################# |
1124 | |
1276 | |
1125 | package CFClient::Widget::MapWidget; |
1277 | package CFClient::UI::MapWidget; |
1126 | |
1278 | |
1127 | use strict; |
1279 | use strict; |
1128 | |
1280 | |
1129 | use List::Util qw(min max); |
1281 | use List::Util qw(min max); |
1130 | |
1282 | |
1131 | use SDL; |
1283 | use SDL; |
1132 | use SDL::OpenGL; |
1284 | use SDL::OpenGL; |
1133 | use SDL::OpenGL::Constants; |
|
|
1134 | |
1285 | |
1135 | our @ISA = CFClient::Widget::; |
1286 | our @ISA = CFClient::UI::Base::; |
1136 | |
1287 | |
1137 | sub new { |
1288 | sub new { |
1138 | my $class = shift; |
1289 | my $class = shift; |
1139 | |
1290 | |
1140 | $class->SUPER::new ( |
1291 | $class->SUPER::new ( |
… | |
… | |
1151 | sub key_up { |
1302 | sub key_up { |
1152 | } |
1303 | } |
1153 | |
1304 | |
1154 | sub size_request { |
1305 | sub size_request { |
1155 | ( |
1306 | ( |
1156 | 1 + int $::WIDTH / 32, |
1307 | 1 + 32 * int $::WIDTH / 32, |
1157 | 1 + int $::HEIGHT / 32, |
1308 | 1 + 32 * int $::HEIGHT / 32, |
1158 | ) |
1309 | ) |
1159 | } |
1310 | } |
1160 | |
1311 | |
1161 | sub update { |
1312 | sub update { |
1162 | my ($self) = @_; |
1313 | my ($self) = @_; |
1163 | |
1314 | |
1164 | $self->{need_update} = 1; |
1315 | $self->{need_update} = 1; |
|
|
1316 | $self->SUPER::update; |
1165 | } |
1317 | } |
1166 | |
1318 | |
1167 | sub _draw { |
1319 | sub draw { |
1168 | my ($self) = @_; |
1320 | my ($self) = @_; |
1169 | |
1321 | |
1170 | if (delete $self->{need_update}) { |
1322 | if (delete $self->{need_update}) { |
1171 | glNewList $self->{list}, GL_COMPILE; |
1323 | glNewList $self->{list}, GL_COMPILE; |
1172 | |
1324 | |
… | |
… | |
1192 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1344 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1193 | } |
1345 | } |
1194 | |
1346 | |
1195 | glEnable GL_TEXTURE_2D; |
1347 | glEnable GL_TEXTURE_2D; |
1196 | glEnable GL_BLEND; |
1348 | glEnable GL_BLEND; |
|
|
1349 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1197 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1350 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1198 | |
1351 | |
1199 | my $sw4 = ($sw + 3) & ~3; |
1352 | my $sw4 = ($sw + 3) & ~3; |
1200 | my $darkness = "\x00" x ($sw4 * $sh); |
1353 | my $darkness = "\x00" x ($sw4 * $sh); |
1201 | |
1354 | |
… | |
… | |
1231 | # |
1384 | # |
1232 | # $lighting = $pb->get_pixels; |
1385 | # $lighting = $pb->get_pixels; |
1233 | # $lighting =~ s/(.)../$1/gs; |
1386 | # $lighting =~ s/(.)../$1/gs; |
1234 | # } |
1387 | # } |
1235 | |
1388 | |
1236 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1237 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1389 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1238 | |
1390 | |
1239 | $darkness = new CFClient::Texture |
1391 | $darkness = new CFClient::Texture |
1240 | width => $sw4, |
1392 | w => $sw4, |
1241 | height => $sh, |
1393 | h => $sh, |
1242 | data => $darkness, |
1394 | data => $darkness, |
1243 | internalformat => GL_ALPHA, |
1395 | internalformat => GL_ALPHA, |
1244 | format => GL_ALPHA; |
1396 | format => GL_ALPHA; |
1245 | |
1397 | |
1246 | glColor 0.45, 0.45, 0.45, 1; |
1398 | glColor 0.45, 0.45, 0.45, 1; |
… | |
… | |
1306 | } |
1458 | } |
1307 | } |
1459 | } |
1308 | |
1460 | |
1309 | ############################################################################# |
1461 | ############################################################################# |
1310 | |
1462 | |
1311 | package CFClient::Widget::Animator; |
1463 | package CFClient::UI::Animator; |
1312 | |
1464 | |
1313 | use SDL::OpenGL; |
1465 | use SDL::OpenGL; |
1314 | |
1466 | |
1315 | our @ISA = CFClient::Widget::Bin::; |
1467 | our @ISA = CFClient::UI::Bin::; |
1316 | |
1468 | |
1317 | sub moveto { |
1469 | sub moveto { |
1318 | my ($self, $x, $y) = @_; |
1470 | my ($self, $x, $y) = @_; |
1319 | |
1471 | |
1320 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1472 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
1348 | glPopMatrix; |
1500 | glPopMatrix; |
1349 | } |
1501 | } |
1350 | |
1502 | |
1351 | ############################################################################# |
1503 | ############################################################################# |
1352 | |
1504 | |
1353 | package CFClient::Widget::Toplevel; |
1505 | package CFClient::UI::Toplevel; |
1354 | |
1506 | |
1355 | our @ISA = CFClient::Widget::Container::; |
1507 | our @ISA = CFClient::UI::Container::; |
1356 | |
1508 | |
1357 | sub size_request { |
1509 | sub size_request { |
1358 | ($::WIDTH, $::HEIGHT) |
1510 | ($::WIDTH, $::HEIGHT) |
1359 | } |
1511 | } |
1360 | |
1512 | |
1361 | sub size_allocate { |
1513 | sub size_allocate { |
1362 | my ($self, $w, $h) = @_; |
1514 | my ($self, $x, $y, $w, $h) = @_; |
1363 | |
1515 | |
1364 | $self->SUPER::size_allocate ($w, $h); |
1516 | $self->_size_allocate ($x, $y, $w, $h); |
1365 | |
1517 | |
1366 | $_->size_allocate ($_->size_request) |
1518 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1367 | for @{$self->{children}}; |
1519 | for @{$self->{children}}; |
1368 | } |
1520 | } |
1369 | |
1521 | |
1370 | sub translate { |
1522 | sub translate { |
1371 | my ($self, $x, $y) = @_; |
1523 | my ($self, $x, $y) = @_; |
… | |
… | |
1374 | } |
1526 | } |
1375 | |
1527 | |
1376 | sub update { |
1528 | sub update { |
1377 | my ($self) = @_; |
1529 | my ($self) = @_; |
1378 | |
1530 | |
1379 | $self->size_allocate ($self->size_request); |
1531 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1380 | ::refresh (); |
1532 | ::refresh (); |
1381 | } |
1533 | } |
1382 | |
1534 | |
1383 | sub add { |
1535 | sub add { |
1384 | my ($self, $widget) = @_; |
1536 | my ($self, $widget) = @_; |
1385 | |
1537 | |
1386 | $self->SUPER::add ($widget); |
1538 | $self->SUPER::add ($widget); |
1387 | |
1539 | |
1388 | $widget->size_allocate ($widget->size_request); |
1540 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1389 | } |
1541 | } |
1390 | |
1542 | |
1391 | sub draw { |
1543 | sub draw { |
1392 | my ($self) = @_; |
1544 | my ($self) = @_; |
1393 | |
1545 | |
1394 | $self->_draw; |
1546 | $self->_draw; |
1395 | } |
1547 | } |
1396 | |
1548 | |
1397 | ############################################################################# |
1549 | ############################################################################# |
1398 | |
1550 | |
1399 | package CFClient::Widget; |
1551 | package CFClient::UI; |
1400 | |
1552 | |
1401 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1553 | $TOPLEVEL = new CFClient::UI::Toplevel; |
1402 | |
1554 | |
1403 | 1 |
1555 | 1 |
1404 | |
1556 | |