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) = @_; |
… | |
… | |
155 | return unless $self->{h} && $self->{w}; |
176 | return unless $self->{h} && $self->{w}; |
156 | |
177 | |
157 | glPushMatrix; |
178 | glPushMatrix; |
158 | glTranslate $self->{x}, $self->{y}, 0; |
179 | glTranslate $self->{x}, $self->{y}, 0; |
159 | $self->_draw; |
180 | $self->_draw; |
|
|
181 | glPopMatrix; |
|
|
182 | |
160 | if ($self == $HOVER) { |
183 | if ($self == $HOVER) { |
161 | glColor 1, 1, 1, 0.4; |
184 | my ($x, $y) = @$self{qw(x y)}; |
|
|
185 | |
|
|
186 | glColor 0, 0, 1, 0.2; |
162 | glEnable GL_BLEND; |
187 | glEnable GL_BLEND; |
|
|
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
163 | glBegin GL_QUADS; |
189 | glBegin GL_QUADS; |
164 | glVertex 0 , 0; |
190 | glVertex $x , $y; |
165 | glVertex $self->{w}, 0; |
191 | glVertex $x + $self->{w}, $y; |
166 | glVertex $self->{w}, $self->{h}; |
192 | glVertex $x + $self->{w}, $y + $self->{h}; |
167 | glVertex 0 , $self->{h}; |
193 | glVertex $x , $y + $self->{h}; |
168 | glEnd; |
194 | glEnd; |
169 | glDisable GL_BLEND; |
195 | glDisable GL_BLEND; |
170 | } |
196 | } |
171 | glPopMatrix; |
|
|
172 | } |
197 | } |
173 | |
198 | |
174 | sub _draw { |
199 | sub _draw { |
175 | my ($self) = @_; |
200 | my ($self) = @_; |
176 | |
201 | |
… | |
… | |
216 | |
241 | |
217 | $self->{parent}->update |
242 | $self->{parent}->update |
218 | if $self->{parent}; |
243 | if $self->{parent}; |
219 | } |
244 | } |
220 | |
245 | |
|
|
246 | sub connect { |
|
|
247 | my ($self, $signal, $cb) = @_; |
|
|
248 | |
|
|
249 | push @{ $self->{cb}{$signal} }, $cb; |
|
|
250 | } |
|
|
251 | |
|
|
252 | sub emit { |
|
|
253 | my ($self, $signal, @args) = @_; |
|
|
254 | |
|
|
255 | $_->($self, @args) |
|
|
256 | for @{$self->{cb}{$signal} || []}; |
|
|
257 | } |
|
|
258 | |
221 | sub DESTROY { |
259 | sub DESTROY { |
222 | my ($self) = @_; |
260 | my ($self) = @_; |
223 | |
261 | |
224 | #$self->deactivate; |
262 | #$self->deactivate; |
225 | } |
263 | } |
226 | |
264 | |
227 | ############################################################################# |
265 | ############################################################################# |
228 | |
266 | |
229 | package CFClient::Widget::DrawBG; |
267 | package CFClient::UI::DrawBG; |
230 | |
268 | |
231 | our @ISA = CFClient::Widget::; |
269 | our @ISA = CFClient::UI::Base::; |
232 | |
270 | |
233 | use strict; |
271 | use strict; |
234 | use SDL::OpenGL; |
272 | use SDL::OpenGL; |
235 | |
273 | |
236 | sub new { |
274 | sub new { |
237 | my $class = shift; |
275 | my $class = shift; |
238 | |
276 | |
239 | # range [value, low, high, page] |
277 | # range [value, low, high, page] |
240 | |
278 | |
241 | $class->SUPER::new ( |
279 | $class->SUPER::new ( |
242 | bg => [0, 0, 0, 0.4], |
280 | bg => [0, 0, 0, 0.2], |
243 | active_bg => [1, 1, 1], |
281 | active_bg => [1, 1, 1], |
244 | @_ |
282 | @_ |
245 | ) |
283 | ) |
246 | } |
284 | } |
247 | |
285 | |
248 | sub _draw { |
286 | sub _draw { |
249 | my ($self) = @_; |
287 | my ($self) = @_; |
250 | |
288 | |
251 | my ($w, $h) = @$self{qw(w h)}; |
289 | my ($w, $h) = @$self{qw(w h)}; |
252 | |
290 | |
|
|
291 | glEnable GL_BLEND; |
|
|
292 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
253 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
293 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
294 | |
254 | glBegin GL_QUADS; |
295 | glBegin GL_QUADS; |
255 | glVertex 0 , 0; |
296 | glVertex 0 , 0; |
256 | glVertex 0 , $h; |
297 | glVertex 0 , $h; |
257 | glVertex $w, $h; |
298 | glVertex $w, $h; |
258 | glVertex $w, 0; |
299 | glVertex $w, 0; |
259 | glEnd; |
300 | glEnd; |
260 | } |
|
|
261 | |
301 | |
262 | ############################################################################# |
302 | glDisable GL_BLEND; |
|
|
303 | } |
263 | |
304 | |
|
|
305 | ############################################################################# |
|
|
306 | |
264 | package CFClient::Widget::Empty; |
307 | package CFClient::UI::Empty; |
265 | |
308 | |
266 | our @ISA = CFClient::Widget::; |
309 | our @ISA = CFClient::UI::Base::; |
267 | |
310 | |
268 | sub size_request { |
311 | sub size_request { |
269 | (0, 0) |
312 | (0, 0) |
270 | } |
313 | } |
271 | |
314 | |
272 | sub draw { } |
315 | sub draw { } |
273 | |
316 | |
274 | ############################################################################# |
317 | ############################################################################# |
275 | |
318 | |
276 | package CFClient::Widget::Container; |
319 | package CFClient::UI::Container; |
277 | |
320 | |
278 | our @ISA = CFClient::Widget::; |
321 | our @ISA = CFClient::UI::Base::; |
279 | |
322 | |
280 | sub new { |
323 | sub new { |
281 | my ($class, %arg) = @_; |
324 | my ($class, %arg) = @_; |
282 | |
325 | |
283 | my $children = delete $arg{children} || []; |
326 | my $children = delete $arg{children} || []; |
… | |
… | |
287 | |
330 | |
288 | $self |
331 | $self |
289 | } |
332 | } |
290 | |
333 | |
291 | sub add { |
334 | sub add { |
292 | my ($self, $chld, $expand) = @_; |
335 | my ($self, $chld) = @_; |
293 | |
336 | |
294 | $chld->{expand} = $expand; |
|
|
295 | $chld->set_parent ($self); |
337 | $chld->set_parent ($self); |
296 | |
338 | |
297 | $self->{children} = [ |
339 | $self->{children} = [ |
298 | sort { $a->{z} <=> $b->{z} } |
340 | sort { $a->{z} <=> $b->{z} } |
299 | @{$self->{children}}, $chld |
341 | @{$self->{children}}, $chld |
300 | ]; |
342 | ]; |
301 | |
343 | |
302 | $self->size_allocate ($self->{w}, $self->{h}) |
344 | $self->{w} = $self->{h} = -1; |
303 | if $self->{w}; #TODO: check for "realised state" |
345 | $self->update; |
304 | } |
346 | } |
305 | |
347 | |
306 | sub remove { |
348 | sub remove { |
307 | my ($self, $widget) = @_; |
349 | my ($self, $widget) = @_; |
308 | |
350 | |
309 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
351 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
310 | |
352 | |
311 | $self->size_allocate ($self->{w}, $self->{h}); |
353 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
312 | } |
354 | } |
313 | |
355 | |
314 | sub find_widget { |
356 | sub find_widget { |
315 | my ($self, $x, $y) = @_; |
357 | my ($self, $x, $y) = @_; |
316 | |
358 | |
… | |
… | |
333 | $_->draw for @{$self->{children}}; |
375 | $_->draw for @{$self->{children}}; |
334 | } |
376 | } |
335 | |
377 | |
336 | ############################################################################# |
378 | ############################################################################# |
337 | |
379 | |
338 | package CFClient::Widget::Bin; |
380 | package CFClient::UI::Bin; |
339 | |
381 | |
340 | our @ISA = CFClient::Widget::Container::; |
382 | our @ISA = CFClient::UI::Container::; |
341 | |
383 | |
342 | sub new { |
384 | sub new { |
343 | my ($class, %arg) = @_; |
385 | my ($class, %arg) = @_; |
344 | |
386 | |
345 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
387 | my $child = (delete $arg{child}) || new CFClient::UI::Empty::; |
346 | |
388 | |
347 | $class->SUPER::new (children => [$child], %arg) |
389 | $class->SUPER::new (children => [$child], %arg) |
348 | } |
390 | } |
349 | |
391 | |
350 | sub add { |
392 | sub add { |
… | |
… | |
358 | sub remove { |
400 | sub remove { |
359 | my ($self, $widget) = @_; |
401 | my ($self, $widget) = @_; |
360 | |
402 | |
361 | $self->SUPER::remove ($widget); |
403 | $self->SUPER::remove ($widget); |
362 | |
404 | |
363 | $self->{children} = [new CFClient::Widget::Empty] |
405 | $self->{children} = [new CFClient::UI::Empty] |
364 | unless @{$self->{children}}; |
406 | unless @{$self->{children}}; |
365 | } |
407 | } |
366 | |
408 | |
367 | sub child { $_[0]->{children}[0] } |
409 | sub child { $_[0]->{children}[0] } |
368 | |
410 | |
369 | sub size_request { |
411 | sub size_request { |
370 | $_[0]{children}[0]->size_request |
412 | $_[0]{children}[0]->size_request |
371 | } |
413 | } |
372 | |
414 | |
373 | sub size_allocate { |
415 | sub size_allocate { |
374 | my ($self, $w, $h) = @_; |
416 | my ($self, $x, $y, $w, $h) = @_; |
375 | |
417 | |
376 | return unless $self->{w} != $w || $self->{h} != $h; |
418 | $self->_size_allocate ($x, $y, $w, $h) or return; |
377 | |
419 | |
378 | $self->SUPER::size_allocate ($w, $h); |
|
|
379 | $self->{children}[0]->size_allocate ($w, $h); |
420 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
380 | } |
421 | } |
381 | |
422 | |
382 | ############################################################################# |
423 | ############################################################################# |
383 | |
424 | |
384 | package CFClient::Widget::Window; |
425 | package CFClient::UI::Window; |
385 | |
426 | |
386 | our @ISA = CFClient::Widget::Bin::; |
427 | our @ISA = CFClient::UI::Bin::; |
387 | |
428 | |
388 | use SDL::OpenGL; |
429 | use SDL::OpenGL; |
389 | |
430 | |
390 | sub new { |
431 | sub new { |
391 | my ($class, %arg) = @_; |
432 | my ($class, %arg) = @_; |
… | |
… | |
409 | $self->{w}, $self->{h}, sub { $self->child->draw } |
450 | $self->{w}, $self->{h}, sub { $self->child->draw } |
410 | ); |
451 | ); |
411 | } |
452 | } |
412 | |
453 | |
413 | sub size_allocate { |
454 | sub size_allocate { |
414 | my ($self, $w, $h) = @_; |
455 | my ($self, $x, $y, $w, $h) = @_; |
415 | |
456 | |
416 | return unless $self->{w} != $w || $self->{h} != $h; |
457 | $self->_size_allocate ($x, $y, $w, $h) or return; |
417 | |
458 | |
418 | $self->{w} = $w; |
|
|
419 | $self->{h} = $h; |
|
|
420 | |
|
|
421 | $self->child->size_allocate ($w, $h); |
459 | $self->child->size_allocate (0, 0, $w, $h); |
422 | |
460 | |
423 | $self->render_chld; |
461 | $self->render_chld; |
424 | } |
462 | } |
425 | |
463 | |
426 | sub _draw { |
464 | sub _draw { |
… | |
… | |
430 | |
468 | |
431 | my $tex = $self->{texture} |
469 | my $tex = $self->{texture} |
432 | or return; |
470 | or return; |
433 | |
471 | |
434 | glEnable GL_BLEND; |
472 | glEnable GL_BLEND; |
|
|
473 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
435 | glEnable GL_TEXTURE_2D; |
474 | glEnable GL_TEXTURE_2D; |
436 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
475 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
437 | |
476 | |
438 | $tex->draw_quad (0, 0, $w, $h); |
477 | $tex->draw_quad (0, 0, $w, $h); |
439 | |
478 | |
… | |
… | |
441 | glDisable GL_TEXTURE_2D; |
480 | glDisable GL_TEXTURE_2D; |
442 | } |
481 | } |
443 | |
482 | |
444 | ############################################################################# |
483 | ############################################################################# |
445 | |
484 | |
446 | package CFClient::Widget::Frame; |
485 | package CFClient::UI::Frame; |
447 | |
486 | |
448 | our @ISA = CFClient::Widget::Bin::; |
487 | our @ISA = CFClient::UI::Bin::; |
449 | |
488 | |
450 | use SDL::OpenGL; |
489 | use SDL::OpenGL; |
451 | |
490 | |
452 | sub size_request { |
491 | sub size_request { |
453 | my ($self) = @_; |
492 | my ($self) = @_; |
… | |
… | |
458 | |
497 | |
459 | map { $_ + 4 } $chld->size_request; |
498 | map { $_ + 4 } $chld->size_request; |
460 | } |
499 | } |
461 | |
500 | |
462 | sub size_allocate { |
501 | sub size_allocate { |
463 | my ($self, $w, $h) = @_; |
502 | my ($self, $x, $y, $w, $h) = @_; |
464 | |
503 | |
465 | return unless $self->{w} != $w || $self->{h} != $h; |
504 | $self->_size_allocate ($x, $y, $w, $h) or return; |
466 | |
505 | |
467 | $self->{w} = $w; |
|
|
468 | $self->{h} = $h; |
|
|
469 | |
|
|
470 | $self->child->size_allocate ($w - 4, $h - 4); |
506 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
471 | $self->child->move (2, 2); |
|
|
472 | } |
507 | } |
473 | |
508 | |
474 | sub _draw { |
509 | sub _draw { |
475 | my ($self) = @_; |
510 | my ($self) = @_; |
476 | |
511 | |
… | |
… | |
489 | $chld->draw; |
524 | $chld->draw; |
490 | } |
525 | } |
491 | |
526 | |
492 | ############################################################################# |
527 | ############################################################################# |
493 | |
528 | |
494 | package CFClient::Widget::FancyFrame; |
529 | package CFClient::UI::FancyFrame; |
495 | |
530 | |
496 | our @ISA = CFClient::Widget::Bin::; |
531 | our @ISA = CFClient::UI::Bin::; |
497 | |
532 | |
498 | use SDL::OpenGL; |
533 | use SDL::OpenGL; |
499 | |
534 | |
500 | my @tex = |
535 | my @tex = |
501 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
536 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
502 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
537 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
503 | |
538 | |
504 | sub size_request { |
539 | sub size_request { |
505 | my ($self) = @_; |
540 | my ($self) = @_; |
506 | |
541 | |
|
|
542 | my ($w, $h) = |
|
|
543 | $self->{user_w} && $self->{user_h} |
|
|
544 | ? ($self->{user_w}, $self->{user_h}) |
507 | my ($w, $h) = $self->SUPER::size_request; |
545 | : ($self->SUPER::size_request); |
508 | |
546 | |
509 | $h += $tex[1]->{height}; |
547 | $h += $tex[1]->{h}; |
510 | $h += $tex[4]->{height}; |
548 | $h += $tex[4]->{h}; |
511 | $w += $tex[2]->{width}; |
549 | $w += $tex[2]->{w}; |
512 | $w += $tex[3]->{width}; |
550 | $w += $tex[3]->{w}; |
513 | |
551 | |
514 | ($w, $h) |
552 | ($w, $h) |
515 | } |
553 | } |
516 | |
554 | |
517 | sub size_allocate { |
555 | sub size_allocate { |
518 | my ($self, $w, $h) = @_; |
556 | my ($self, $x, $y, $w, $h) = @_; |
519 | |
557 | |
520 | return unless $self->{w} != $w || $self->{h} != $h; |
558 | $self->_size_allocate ($x, $y, $w, $h) or return; |
521 | |
559 | |
522 | $self->SUPER::size_allocate ($w, $h); |
|
|
523 | |
|
|
524 | $h -= $tex[1]->{height}; |
560 | $h -= $tex[1]{h}; |
525 | $h -= $tex[4]->{height}; |
561 | $h -= $tex[4]{h}; |
526 | $w -= $tex[2]->{width}; |
562 | $w -= $tex[2]{w}; |
527 | $w -= $tex[3]->{width}; |
563 | $w -= $tex[3]{w}; |
528 | |
564 | |
529 | $h = $h < 0 ? 0 : $h; |
565 | $h = $h < 0 ? 0 : $h; |
530 | $w = $w < 0 ? 0 : $w; |
566 | $w = $w < 0 ? 0 : $w; |
531 | |
567 | |
532 | my $child = $self->child; |
568 | my $child = $self->child; |
533 | |
569 | |
534 | $child->size_allocate ($w, $h); |
570 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
535 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
571 | } |
|
|
572 | |
|
|
573 | sub button_down { |
|
|
574 | my ($self, $ev, $x, $y) = @_; |
|
|
575 | |
|
|
576 | if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w} |
|
|
577 | && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) { |
|
|
578 | |
|
|
579 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
580 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
581 | |
|
|
582 | $self->{motion} = sub { |
|
|
583 | my ($ev, $x, $y) = @_; |
|
|
584 | |
|
|
585 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
586 | |
|
|
587 | $self->{user_w} = $bw + $x - $ox; |
|
|
588 | $self->{user_h} = $bh + $y - $oy; |
|
|
589 | $self->update; |
|
|
590 | }; |
|
|
591 | |
|
|
592 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
593 | && $y >= 0 && $y < $tex[1]{h}) { |
|
|
594 | |
|
|
595 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
596 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
597 | |
|
|
598 | $self->{motion} = sub { |
|
|
599 | my ($ev, $x, $y) = @_; |
|
|
600 | |
|
|
601 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
602 | |
|
|
603 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
604 | $self->update; |
|
|
605 | }; |
|
|
606 | } |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub button_up { |
|
|
610 | my ($self, $ev, $x, $y) = @_; |
|
|
611 | |
|
|
612 | delete $self->{motion}; |
|
|
613 | } |
|
|
614 | |
|
|
615 | sub mouse_motion { |
|
|
616 | my ($self, $ev, $x, $y) = @_; |
|
|
617 | |
|
|
618 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
536 | } |
619 | } |
537 | |
620 | |
538 | sub _draw { |
621 | sub _draw { |
539 | my ($self) = @_; |
622 | my ($self) = @_; |
540 | |
623 | |
541 | my ($w, $h) = ($self->{w}, $self->{h}); |
624 | my ($w, $h) = ($self->{w}, $self->{h}); |
542 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
625 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
543 | |
626 | |
544 | glEnable GL_BLEND; |
627 | glEnable GL_BLEND; |
|
|
628 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
545 | glEnable GL_TEXTURE_2D; |
629 | glEnable GL_TEXTURE_2D; |
546 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
547 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
630 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
548 | |
631 | |
549 | my $top = $tex[1]; |
632 | my $top = $tex[1]; |
550 | $top->draw_quad (0, 0, $w, $top->{height}); |
633 | $top->draw_quad (0, 0, $w, $top->{h}); |
551 | |
634 | |
552 | my $left = $tex[3]; |
635 | my $left = $tex[3]; |
553 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
636 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
554 | |
637 | |
555 | my $right = $tex[2]; |
638 | my $right = $tex[2]; |
556 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
639 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
557 | |
640 | |
558 | my $bottom = $tex[4]; |
641 | my $bottom = $tex[4]; |
559 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
642 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
560 | |
643 | |
561 | my $bg = $tex[0]; |
644 | my $bg = $tex[0]; |
|
|
645 | |
562 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
646 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
647 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
564 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
648 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
565 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
649 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
566 | |
650 | |
567 | my $rep_x = $cw / $bg->{width}; |
651 | my $rep_x = $cw / $bg->{w}; |
568 | my $rep_y = $ch / $bg->{height}; |
652 | my $rep_y = $ch / $bg->{h}; |
569 | |
653 | |
570 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
654 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
571 | |
655 | |
|
|
656 | glDisable GL_TEXTURE_2D; |
572 | glDisable GL_BLEND; |
657 | glDisable GL_BLEND; |
573 | glDisable GL_TEXTURE_2D; |
|
|
574 | |
658 | |
575 | $self->child->draw; |
659 | $self->child->draw; |
576 | |
660 | |
577 | } |
661 | } |
578 | |
662 | |
579 | ############################################################################# |
663 | ############################################################################# |
580 | |
664 | |
581 | package CFClient::Widget::Table; |
665 | package CFClient::UI::Table; |
582 | |
666 | |
583 | our @ISA = CFClient::Widget::Bin::; |
667 | our @ISA = CFClient::UI::Base::; |
|
|
668 | |
|
|
669 | use List::Util qw(max sum); |
584 | |
670 | |
585 | use SDL::OpenGL; |
671 | use SDL::OpenGL; |
586 | |
672 | |
587 | sub add { |
673 | sub add { |
588 | my ($self, $x, $y, $chld) = @_; |
674 | my ($self, $x, $y, $chld) = @_; |
589 | my $old_chld = $self->{children}[$y][$x]; |
|
|
590 | |
675 | |
591 | $self->{children}[$y][$x] = $chld; |
676 | $self->{children}[$y][$x] = $chld; |
592 | $chld->set_parent ($self); |
677 | $chld->set_parent ($self); |
|
|
678 | |
|
|
679 | $self->{w} = $self->{h} = -1; |
593 | $self->update; |
680 | $self->update; |
594 | } |
681 | } |
595 | |
682 | |
596 | sub max_row_height { |
683 | sub get_wh { |
597 | my ($self, $row) = @_; |
684 | my ($self) = @_; |
598 | |
685 | |
599 | my $hs = 0; |
686 | my (@w, @h); |
600 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
687 | |
|
|
688 | for my $y (0 .. $#{$self->{children}}) { |
601 | my $c = $self->{children}->[$row]->[$xi]; |
689 | my $row = $self->{children}[$y] |
602 | if ($c) { |
690 | or next; |
|
|
691 | |
|
|
692 | for my $x (0 .. $#$row) { |
|
|
693 | my $widget = $row->[$x] |
|
|
694 | or next; |
603 | my ($w, $h) = $c->size_request; |
695 | my ($w, $h) = $widget->size_request; |
604 | if ($hs < $h) { $hs = $h } |
696 | |
|
|
697 | $w[$x] = max $w[$x], $w; |
|
|
698 | $h[$y] = max $h[$y], $h; |
605 | } |
699 | } |
606 | } |
700 | } |
607 | return $hs; |
|
|
608 | } |
|
|
609 | |
701 | |
610 | sub max_col_width { |
702 | (\@w, \@h) |
|
|
703 | } |
|
|
704 | |
|
|
705 | sub size_request { |
611 | my ($self, $col) = @_; |
706 | my ($self) = @_; |
612 | |
707 | |
|
|
708 | my ($ws, $hs) = $self->get_wh; |
|
|
709 | |
|
|
710 | ( |
|
|
711 | (List::Util::sum @$ws), |
|
|
712 | (List::Util::sum @$hs), |
|
|
713 | ) |
|
|
714 | } |
|
|
715 | |
|
|
716 | sub size_allocate { |
|
|
717 | my ($self, $x, $y, $w, $h) = @_; |
|
|
718 | |
|
|
719 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
720 | |
|
|
721 | my ($ws, $hs) = $self->get_wh; |
|
|
722 | |
|
|
723 | my $req_w = List::Util::sum @$ws; |
|
|
724 | my $req_h = List::Util::sum @$hs; |
|
|
725 | |
|
|
726 | # linearly scale sizes |
|
|
727 | $_ *= $w / $req_w for @$ws; |
|
|
728 | $_ *= $h / $req_h for @$hs; |
|
|
729 | |
|
|
730 | my $y; |
|
|
731 | |
|
|
732 | for my $r (0 .. $#{$self->{children}}) { |
|
|
733 | my $row = $self->{children}[$r] |
|
|
734 | or next; |
|
|
735 | |
613 | my $ws = 0; |
736 | my $x = 0; |
614 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
737 | my $row_h = $hs->[$r]; |
615 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
738 | |
616 | if ($c) { |
739 | for my $c (0 .. $#$row) { |
617 | my ($w, $h) = $c->size_request; |
740 | my $widget = $row->[$c] |
618 | 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; |
619 | } |
748 | } |
|
|
749 | |
|
|
750 | $y += $row_h; |
620 | } |
751 | } |
621 | return $ws; |
752 | |
622 | } |
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::; |
623 | |
786 | |
624 | sub size_request { |
787 | sub size_request { |
625 | my ($self) = @_; |
788 | my ($self) = @_; |
626 | |
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 | |
627 | my ($hs, $ws) = (0, 0); |
805 | ($h, $w) = ($w, $h); |
628 | |
806 | |
629 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
807 | my $children = $self->{children}; |
630 | $hs += $self->max_row_height ($yi); |
|
|
631 | } |
|
|
632 | |
808 | |
633 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
809 | my @h = map +($_->size_request)[0], @$children; |
634 | my $wm = 0; |
810 | |
635 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
811 | my $req_h = List::Util::sum @h; |
636 | $wm += $self->max_col_width ($xi) |
812 | |
|
|
813 | if ($req_h > $h) { |
|
|
814 | # ah well, not enough space |
|
|
815 | $_ = $h[$_] * $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; |
637 | } |
826 | } |
638 | if ($ws < $wm) { $ws = $wm } |
|
|
639 | } |
827 | } |
640 | |
|
|
641 | return ($ws, $hs); |
|
|
642 | } |
|
|
643 | |
|
|
644 | sub _draw { |
|
|
645 | my ($self) = @_; |
|
|
646 | |
828 | |
647 | my $y = 0; |
829 | my $y = 0; |
648 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
830 | for (0 .. $#$children) { |
|
|
831 | my $child = $children->[$_]; |
649 | my $x = 0; |
832 | my $h = $h[$_]; |
|
|
833 | $child->size_allocate ($y, 0, $h, $w); |
650 | |
834 | |
651 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
835 | $y += $h; |
652 | |
|
|
653 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
654 | if ($c) { |
|
|
655 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
656 | $c->draw if $c; |
|
|
657 | } |
|
|
658 | |
|
|
659 | $x += $self->max_col_width ($xi); |
|
|
660 | } |
|
|
661 | |
|
|
662 | $y += $self->max_row_height ($yi); |
|
|
663 | } |
836 | } |
664 | } |
837 | } |
665 | |
838 | |
666 | ############################################################################# |
839 | ############################################################################# |
667 | |
840 | |
668 | package CFClient::Widget::VBox; |
841 | package CFClient::UI::VBox; |
669 | |
842 | |
|
|
843 | # TODO: wrap into common Box base class |
|
|
844 | |
670 | our @ISA = CFClient::Widget::Container::; |
845 | our @ISA = CFClient::UI::Container::; |
671 | |
|
|
672 | use SDL::OpenGL; |
|
|
673 | |
846 | |
674 | sub size_request { |
847 | sub size_request { |
675 | my ($self) = @_; |
848 | my ($self) = @_; |
676 | |
849 | |
677 | my @alloc = map [$_->size_request], @{$self->{children}}; |
850 | my @alloc = map [$_->size_request], @{$self->{children}}; |
… | |
… | |
681 | (List::Util::sum map $_->[1], @alloc), |
854 | (List::Util::sum map $_->[1], @alloc), |
682 | ) |
855 | ) |
683 | } |
856 | } |
684 | |
857 | |
685 | sub size_allocate { |
858 | sub size_allocate { |
686 | my ($self, $w, $h) = @_; |
859 | my ($self, $x, $y, $w, $h) = @_; |
687 | |
860 | |
688 | return unless $self->{w} != $w || $self->{h} != $h; |
861 | $self->_size_allocate ($x, $y, $w, $h) or return; |
689 | |
|
|
690 | $self->{w} = $w; |
|
|
691 | $self->{h} = $h; |
|
|
692 | |
862 | |
693 | return unless $self->{h}; |
863 | return unless $self->{h}; |
694 | |
864 | |
695 | my $children = $self->{children}; |
865 | my $children = $self->{children}; |
696 | |
866 | |
… | |
… | |
700 | |
870 | |
701 | if ($req_h > $h) { |
871 | if ($req_h > $h) { |
702 | # ah well, not enough space |
872 | # ah well, not enough space |
703 | $_ = $h[$_] * $h / $req_h for @h; |
873 | $_ = $h[$_] * $h / $req_h for @h; |
704 | } else { |
874 | } else { |
705 | my @exp = grep $_->{expand}, @$children; |
875 | my $exp = List::Util::sum map $_->{expand}, @$children; |
706 | @exp = @$children unless @exp; |
876 | $exp ||= 1; |
707 | |
877 | |
708 | my %exp = map +($_ => 1), @exp; |
|
|
709 | |
|
|
710 | for (0 .. $#$children) { |
878 | for (0 .. $#$children) { |
711 | my $child = $children->[$_]; |
879 | my $child = $children->[$_]; |
712 | |
880 | |
713 | my $alloc_h = $h[$_]; |
881 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
714 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
715 | $h[$_] = $alloc_h; |
|
|
716 | } |
882 | } |
717 | } |
883 | } |
718 | |
884 | |
719 | my $y = 0; |
885 | my $y = 0; |
720 | for (0 .. $#$children) { |
886 | for (0 .. $#$children) { |
721 | my $child = $children->[$_]; |
887 | my $child = $children->[$_]; |
722 | my $h = $h[$_]; |
888 | my $h = $h[$_]; |
723 | $child->move (0, $y); |
|
|
724 | $child->size_allocate ($w, $h); |
889 | $child->size_allocate (0, $y, $w, $h); |
725 | |
890 | |
726 | $y += $h; |
891 | $y += $h; |
727 | } |
892 | } |
728 | } |
893 | } |
729 | |
894 | |
730 | ############################################################################# |
895 | ############################################################################# |
731 | |
896 | |
732 | package CFClient::Widget::Label; |
897 | package CFClient::UI::Label; |
733 | |
898 | |
734 | our @ISA = CFClient::Widget::; |
899 | our @ISA = CFClient::UI::Base::; |
735 | |
900 | |
736 | use SDL::OpenGL; |
901 | use SDL::OpenGL; |
737 | |
902 | |
738 | sub new { |
903 | sub new { |
739 | my ($class, %arg) = @_; |
904 | my ($class, %arg) = @_; |
740 | |
905 | |
741 | my $self = $class->SUPER::new ( |
906 | my $self = $class->SUPER::new ( |
742 | fg => [1, 1, 1], |
907 | fg => [1, 1, 1], |
743 | height => $::FONTSIZE, |
908 | height => $::FONTSIZE, |
744 | text => "", |
909 | text => "", |
|
|
910 | align => -1, |
|
|
911 | padding => 2, |
745 | layout => new CFClient::Layout, |
912 | layout => new CFClient::Layout, |
746 | %arg |
913 | %arg |
747 | ); |
914 | ); |
748 | |
915 | |
749 | $self->set_text ($self->{text}); |
916 | $self->set_text ($self->{text}); |
750 | |
917 | |
… | |
… | |
780 | sub size_request { |
947 | sub size_request { |
781 | my ($self) = @_; |
948 | my ($self) = @_; |
782 | |
949 | |
783 | $self->{layout}->set_width; |
950 | $self->{layout}->set_width; |
784 | $self->{layout}->set_height ($self->{height}); |
951 | $self->{layout}->set_height ($self->{height}); |
785 | $self->{layout}->size |
952 | my ($w, $h) = $self->{layout}->size; |
786 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
953 | |
787 | # ( |
954 | ( |
788 | # $self->{texture}{width}, |
955 | $w + $self->{padding} * 2, |
789 | # $self->{texture}{height}, |
956 | $h + $self->{padding} * 2, |
790 | # ) |
957 | ) |
791 | # } else { |
|
|
792 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
|
|
793 | # |
|
|
794 | # ($w, $h) |
|
|
795 | # } |
|
|
796 | } |
958 | } |
797 | |
959 | |
798 | sub size_allocate { |
960 | sub size_allocate { |
799 | my ($self, $w, $h) = @_; |
961 | my ($self, $x, $y, $w, $h) = @_; |
800 | |
962 | |
801 | return unless $self->{w} != $w || $self->{h} != $h; |
963 | $self->_size_allocate ($x, $y, $w, $h) or return; |
802 | |
964 | |
803 | $self->SUPER::size_allocate ($w, $h); |
|
|
804 | delete $self->{texture}; |
965 | delete $self->{texture}; |
805 | } |
966 | } |
806 | |
967 | |
807 | sub update { |
968 | sub update { |
808 | my ($self) = @_; |
969 | my ($self) = @_; |
… | |
… | |
814 | sub _draw { |
975 | sub _draw { |
815 | my ($self) = @_; |
976 | my ($self) = @_; |
816 | |
977 | |
817 | my $tex = $self->{texture} ||= do { |
978 | my $tex = $self->{texture} ||= do { |
818 | $self->{layout}->set_width ($self->{w}); |
979 | $self->{layout}->set_width ($self->{w}); |
819 | new_from_layout CFClient::Texture $self->{layout}; |
980 | new_from_layout CFClient::Texture $self->{layout} |
820 | }; |
981 | }; |
821 | |
982 | |
822 | glEnable GL_BLEND; |
983 | glEnable GL_BLEND; |
|
|
984 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
823 | glEnable GL_TEXTURE_2D; |
985 | glEnable GL_TEXTURE_2D; |
824 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
825 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
986 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
826 | |
987 | |
827 | glColor @{$self->{fg}}; |
988 | glColor @{$self->{fg}}; |
828 | |
989 | |
829 | $tex->draw_quad (0, 0); |
990 | my $x = |
|
|
991 | $self->{align} < 0 ? $self->{padding} |
|
|
992 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
993 | : ($self->{w} - $tex->{w}) * 0.5; |
830 | |
994 | |
|
|
995 | $tex->draw_quad ($x, $self->{padding}); |
|
|
996 | |
|
|
997 | glDisable GL_TEXTURE_2D; |
831 | glDisable GL_BLEND; |
998 | glDisable GL_BLEND; |
832 | glDisable GL_TEXTURE_2D; |
|
|
833 | } |
999 | } |
834 | |
1000 | |
835 | ############################################################################# |
1001 | ############################################################################# |
836 | |
1002 | |
837 | package CFClient::Widget::Entry; |
1003 | package CFClient::UI::Entry; |
838 | |
1004 | |
839 | our @ISA = CFClient::Widget::Label::; |
1005 | our @ISA = CFClient::UI::Label::; |
840 | |
1006 | |
841 | use SDL; |
1007 | use SDL; |
842 | use SDL::OpenGL; |
1008 | use SDL::OpenGL; |
843 | |
1009 | |
844 | sub new { |
1010 | sub new { |
845 | my $class = shift; |
1011 | my $class = shift; |
846 | |
1012 | |
847 | $class->SUPER::new ( |
1013 | $class->SUPER::new ( |
848 | fg => [1, 1, 1], |
1014 | fg => [1, 1, 1], |
849 | bg => [0, 0, 0, 0.4], |
1015 | bg => [0, 0, 0, 0.2], |
850 | active_bg => [1, 1, 1], |
1016 | active_bg => [1, 1, 1], |
851 | active_fg => [0, 0, 0], |
1017 | active_fg => [0, 0, 0], |
852 | @_ |
1018 | @_ |
853 | ) |
1019 | ) |
854 | } |
1020 | } |
… | |
… | |
858 | |
1024 | |
859 | $self->{last_activity} = $::NOW; |
1025 | $self->{last_activity} = $::NOW; |
860 | |
1026 | |
861 | $self->{text} = $text; |
1027 | $self->{text} = $text; |
862 | $self->{layout}->set_width ($self->{w}); |
1028 | $self->{layout}->set_width ($self->{w}); |
|
|
1029 | |
|
|
1030 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1031 | |
863 | $self->{layout}->set_markup ($self->escape_text ($text)); |
1032 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
864 | |
1033 | |
865 | $text = substr $text, 0, $self->{cursor}; |
1034 | $text = substr $text, 0, $self->{cursor}; |
866 | utf8::encode $text; |
1035 | utf8::encode $text; |
867 | |
1036 | |
868 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1037 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
… | |
… | |
875 | |
1044 | |
876 | ($w + 1, $h) # add 1 for cursor |
1045 | ($w + 1, $h) # add 1 for cursor |
877 | } |
1046 | } |
878 | |
1047 | |
879 | sub size_allocate { |
1048 | sub size_allocate { |
880 | my ($self, $w, $h) = @_; |
1049 | my ($self, $x, $y, $w, $h) = @_; |
881 | |
1050 | |
882 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
883 | |
|
|
884 | $self->SUPER::size_allocate ($w, $h); |
1051 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
885 | |
1052 | |
886 | $self->_set_text ($self->{text}); |
1053 | $self->_set_text ($self->{text}); |
887 | } |
1054 | } |
888 | |
1055 | |
889 | sub set_text { |
1056 | sub set_text { |
… | |
… | |
910 | substr $text, $self->{cursor}, 1, ""; |
1077 | substr $text, $self->{cursor}, 1, ""; |
911 | } elsif ($sym == SDLK_LEFT) { |
1078 | } elsif ($sym == SDLK_LEFT) { |
912 | --$self->{cursor} if $self->{cursor}; |
1079 | --$self->{cursor} if $self->{cursor}; |
913 | } elsif ($sym == SDLK_RIGHT) { |
1080 | } elsif ($sym == SDLK_RIGHT) { |
914 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1081 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1082 | } elsif ($sym == SDLK_HOME) { |
|
|
1083 | $self->{cursor} = 0; |
|
|
1084 | } elsif ($sym == SDLK_END) { |
|
|
1085 | $self->{cursor} = length $text; |
915 | } elsif ($uni) { |
1086 | } elsif ($uni) { |
916 | substr $text, $self->{cursor}++, 0, chr $uni; |
1087 | substr $text, $self->{cursor}++, 0, chr $uni; |
917 | } |
1088 | } |
918 | |
1089 | |
919 | $self->_set_text ($text); |
1090 | $self->_set_text ($text); |
… | |
… | |
934 | $self->SUPER::button_down ($ev, $x, $y); |
1105 | $self->SUPER::button_down ($ev, $x, $y); |
935 | |
1106 | |
936 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1107 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
937 | |
1108 | |
938 | # byte-index to char-index |
1109 | # byte-index to char-index |
939 | my $text = $self->{layout}; |
1110 | my $text = $self->{text}; |
940 | utf8::encode $text; |
1111 | utf8::encode $text; |
941 | $self->{cursor} = length substr $text, 0, $idx; |
1112 | $self->{cursor} = length substr $text, 0, $idx; |
942 | |
1113 | |
943 | $self->_set_text ($self->{text}); |
1114 | $self->_set_text ($self->{text}); |
944 | $self->update; |
1115 | $self->update; |
… | |
… | |
959 | $self->{fg} = $self->{active_fg}; |
1130 | $self->{fg} = $self->{active_fg}; |
960 | } else { |
1131 | } else { |
961 | glColor @{$self->{bg}}; |
1132 | glColor @{$self->{bg}}; |
962 | } |
1133 | } |
963 | |
1134 | |
|
|
1135 | glEnable GL_BLEND; |
|
|
1136 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
964 | glBegin GL_QUADS; |
1137 | glBegin GL_QUADS; |
965 | glVertex 0 , 0; |
1138 | glVertex 0 , 0; |
966 | glVertex 0 , $self->{h}; |
1139 | glVertex 0 , $self->{h}; |
967 | glVertex $self->{w}, $self->{h}; |
1140 | glVertex $self->{w}, $self->{h}; |
968 | glVertex $self->{w}, 0; |
1141 | glVertex $self->{w}, 0; |
969 | glEnd; |
1142 | glEnd; |
|
|
1143 | glDisable GL_BLEND; |
970 | |
1144 | |
971 | $self->SUPER::_draw; |
1145 | $self->SUPER::_draw; |
972 | |
1146 | |
973 | #TODO: force update every cursor change :( |
1147 | #TODO: force update every cursor change :( |
974 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1148 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
980 | } |
1154 | } |
981 | } |
1155 | } |
982 | |
1156 | |
983 | ############################################################################# |
1157 | ############################################################################# |
984 | |
1158 | |
985 | package CFClient::Widget::Slider; |
1159 | package CFClient::UI::Slider; |
986 | |
1160 | |
987 | use strict; |
1161 | use strict; |
988 | |
1162 | |
989 | use SDL::OpenGL; |
1163 | use SDL::OpenGL; |
990 | use SDL::OpenGL::Constants; |
|
|
991 | |
1164 | |
992 | our @ISA = CFClient::Widget::DrawBG::; |
1165 | our @ISA = CFClient::UI::DrawBG::; |
993 | |
|
|
994 | sub size_request { |
|
|
995 | my ($self) = @_; |
|
|
996 | |
|
|
997 | my $w = 20; |
|
|
998 | my $h = 10; |
|
|
999 | |
|
|
1000 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1001 | } |
|
|
1002 | |
1166 | |
1003 | sub new { |
1167 | sub new { |
1004 | my $class = shift; |
1168 | my $class = shift; |
1005 | |
1169 | |
1006 | # range [value, low, high, page] |
1170 | # range [value, low, high, page] |
1007 | |
1171 | |
1008 | $class->SUPER::new ( |
1172 | my $self = $class->SUPER::new ( |
1009 | fg => [1, 1, 1], |
1173 | fg => [1, 1, 1], |
1010 | active_fg => [0, 0, 0], |
1174 | active_fg => [0, 0, 0], |
1011 | range => [0, 0, 100, 10], |
1175 | range => [0, 0, 100, 10], |
|
|
1176 | req_w => 40, |
|
|
1177 | req_h => 10, |
1012 | vertical => 1, |
1178 | vertical => 0, |
1013 | @_ |
1179 | @_ |
1014 | ) |
1180 | ); |
|
|
1181 | |
|
|
1182 | $self |
|
|
1183 | } |
|
|
1184 | |
|
|
1185 | sub size_request { |
|
|
1186 | my ($self) = @_; |
|
|
1187 | |
|
|
1188 | my $w = $self->{req_w}; |
|
|
1189 | my $h = $self->{req_h}; |
|
|
1190 | |
|
|
1191 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1015 | } |
1192 | } |
1016 | |
1193 | |
1017 | sub button_down { |
1194 | sub button_down { |
1018 | my ($self, $ev, $x, $y) = @_; |
1195 | my ($self, $ev, $x, $y) = @_; |
1019 | |
1196 | |
… | |
… | |
1025 | my ($self, $ev, $x, $y) = @_; |
1202 | my ($self, $ev, $x, $y) = @_; |
1026 | |
1203 | |
1027 | if ($GRAB == $self) { |
1204 | if ($GRAB == $self) { |
1028 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1205 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1029 | |
1206 | |
|
|
1207 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
1208 | |
1030 | $x = $x * ($hi - $lo) / $self->{w} + $lo; |
1209 | $x = $x * ($hi - $lo) / $w + $lo; |
1031 | $x = $lo if $x < $lo; |
1210 | $x = $lo if $x < $lo; |
1032 | $x = $hi - $page if $x > $hi - $page; |
1211 | $x = $hi - $page if $x > $hi - $page; |
1033 | $self->{range}[0] = $x; |
1212 | $self->{range}[0] = $x; |
1034 | |
1213 | |
1035 | $self->{changed}($x) if $self->{changed}; |
1214 | $self->emit (changed => $x); |
1036 | $self->update; |
1215 | $self->update; |
1037 | } |
1216 | } |
1038 | } |
1217 | } |
1039 | |
1218 | |
1040 | sub _draw { |
1219 | sub _draw { |
1041 | my ($self) = @_; |
1220 | my ($self) = @_; |
1042 | |
1221 | |
1043 | $self->SUPER::_draw (); |
1222 | $self->SUPER::_draw (); |
1044 | |
|
|
1045 | glPushMatrix; |
|
|
1046 | |
1223 | |
1047 | my ($w, $h) = @$self{qw(w h)}; |
1224 | my ($w, $h) = @$self{qw(w h)}; |
1048 | |
1225 | |
1049 | if ($self->{vertical}) { |
1226 | if ($self->{vertical}) { |
1050 | # draw a vertical slider like a rotated horizontal slider |
1227 | # draw a vertical slider like a rotated horizontal slider |
1051 | |
1228 | |
1052 | glTranslate 0, $self->{w}; |
|
|
1053 | glRotate 90, 0, 0, 1; |
1229 | glRotate 90, 0, 0, 1; |
|
|
1230 | glTranslate 0, -$self->{w}, 0; |
1054 | |
1231 | |
1055 | ($w, $h) = ($h, $w); |
1232 | ($w, $h) = ($h, $w); |
1056 | } |
1233 | } |
1057 | |
1234 | |
1058 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1235 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
… | |
… | |
1090 | glVertex $knob_a + 1, $h - 1; |
1267 | glVertex $knob_a + 1, $h - 1; |
1091 | glVertex $knob_b - 1, $h - 1; |
1268 | glVertex $knob_b - 1, $h - 1; |
1092 | glVertex $knob_b - 1, 1; |
1269 | glVertex $knob_b - 1, 1; |
1093 | } |
1270 | } |
1094 | glEnd; |
1271 | glEnd; |
1095 | |
|
|
1096 | glPopMatrix; |
|
|
1097 | } |
1272 | } |
1098 | |
1273 | |
1099 | ############################################################################# |
1274 | ############################################################################# |
1100 | |
1275 | |
1101 | package CFClient::Widget::MapWidget; |
1276 | package CFClient::UI::MapWidget; |
1102 | |
1277 | |
1103 | use strict; |
1278 | use strict; |
1104 | |
1279 | |
1105 | use List::Util qw(min max); |
1280 | use List::Util qw(min max); |
1106 | |
1281 | |
1107 | use SDL; |
1282 | use SDL; |
1108 | use SDL::OpenGL; |
1283 | use SDL::OpenGL; |
1109 | use SDL::OpenGL::Constants; |
|
|
1110 | |
1284 | |
1111 | our @ISA = CFClient::Widget::; |
1285 | our @ISA = CFClient::UI::Base::; |
1112 | |
1286 | |
1113 | sub new { |
1287 | sub new { |
1114 | my $class = shift; |
1288 | my $class = shift; |
1115 | |
1289 | |
1116 | $class->SUPER::new ( |
1290 | $class->SUPER::new ( |
… | |
… | |
1127 | sub key_up { |
1301 | sub key_up { |
1128 | } |
1302 | } |
1129 | |
1303 | |
1130 | sub size_request { |
1304 | sub size_request { |
1131 | ( |
1305 | ( |
1132 | 1 + int $::WIDTH / 32, |
1306 | 1 + 32 * int $::WIDTH / 32, |
1133 | 1 + int $::HEIGHT / 32, |
1307 | 1 + 32 * int $::HEIGHT / 32, |
1134 | ) |
1308 | ) |
1135 | } |
1309 | } |
1136 | |
1310 | |
1137 | sub update { |
1311 | sub update { |
1138 | my ($self) = @_; |
1312 | my ($self) = @_; |
1139 | |
1313 | |
1140 | $self->{need_update} = 1; |
1314 | $self->{need_update} = 1; |
|
|
1315 | $self->SUPER::update; |
1141 | } |
1316 | } |
1142 | |
1317 | |
1143 | sub _draw { |
1318 | sub draw { |
1144 | my ($self) = @_; |
1319 | my ($self) = @_; |
1145 | |
1320 | |
1146 | if (delete $self->{need_update}) { |
1321 | if (delete $self->{need_update}) { |
1147 | glNewList $self->{list}, GL_COMPILE; |
1322 | glNewList $self->{list}, GL_COMPILE; |
1148 | |
1323 | |
… | |
… | |
1168 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1343 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1169 | } |
1344 | } |
1170 | |
1345 | |
1171 | glEnable GL_TEXTURE_2D; |
1346 | glEnable GL_TEXTURE_2D; |
1172 | glEnable GL_BLEND; |
1347 | glEnable GL_BLEND; |
|
|
1348 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1173 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1349 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1174 | |
1350 | |
1175 | my $sw4 = ($sw + 3) & ~3; |
1351 | my $sw4 = ($sw + 3) & ~3; |
1176 | my $darkness = "\x00" x ($sw4 * $sh); |
1352 | my $darkness = "\x00" x ($sw4 * $sh); |
1177 | |
1353 | |
… | |
… | |
1190 | } |
1366 | } |
1191 | |
1367 | |
1192 | for my $num (grep $_, @$cell[1,2,3]) { |
1368 | for my $num (grep $_, @$cell[1,2,3]) { |
1193 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1369 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1194 | |
1370 | |
1195 | my $w = $tex->{width}; |
1371 | my ($w, $h) = @$tex{qw(w h)}; |
1196 | my $h = $tex->{height}; |
|
|
1197 | |
1372 | |
1198 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1373 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1199 | } |
1374 | } |
1200 | } |
1375 | } |
1201 | } |
1376 | } |
… | |
… | |
1208 | # |
1383 | # |
1209 | # $lighting = $pb->get_pixels; |
1384 | # $lighting = $pb->get_pixels; |
1210 | # $lighting =~ s/(.)../$1/gs; |
1385 | # $lighting =~ s/(.)../$1/gs; |
1211 | # } |
1386 | # } |
1212 | |
1387 | |
1213 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1214 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1388 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1215 | |
1389 | |
1216 | $darkness = new CFClient::Texture |
1390 | $darkness = new CFClient::Texture |
1217 | width => $sw4, |
1391 | w => $sw4, |
1218 | height => $sh, |
1392 | h => $sh, |
1219 | data => $darkness, |
1393 | data => $darkness, |
1220 | internalformat => GL_ALPHA, |
1394 | internalformat => GL_ALPHA, |
1221 | format => GL_ALPHA; |
1395 | format => GL_ALPHA; |
1222 | |
1396 | |
1223 | glColor 0.45, 0.45, 0.45, 1; |
1397 | glColor 0.45, 0.45, 0.45, 1; |
… | |
… | |
1283 | } |
1457 | } |
1284 | } |
1458 | } |
1285 | |
1459 | |
1286 | ############################################################################# |
1460 | ############################################################################# |
1287 | |
1461 | |
1288 | package CFClient::Widget::Animator; |
1462 | package CFClient::UI::Animator; |
1289 | |
1463 | |
1290 | use SDL::OpenGL; |
1464 | use SDL::OpenGL; |
1291 | |
1465 | |
1292 | our @ISA = CFClient::Widget::Bin::; |
1466 | our @ISA = CFClient::UI::Bin::; |
1293 | |
1467 | |
1294 | sub moveto { |
1468 | sub moveto { |
1295 | my ($self, $x, $y) = @_; |
1469 | my ($self, $x, $y) = @_; |
1296 | |
1470 | |
1297 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1471 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
1325 | glPopMatrix; |
1499 | glPopMatrix; |
1326 | } |
1500 | } |
1327 | |
1501 | |
1328 | ############################################################################# |
1502 | ############################################################################# |
1329 | |
1503 | |
1330 | package CFClient::Widget::Toplevel; |
1504 | package CFClient::UI::Toplevel; |
1331 | |
1505 | |
1332 | our @ISA = CFClient::Widget::Container::; |
1506 | our @ISA = CFClient::UI::Container::; |
1333 | |
1507 | |
1334 | sub size_request { |
1508 | sub size_request { |
1335 | ($::WIDTH, $::HEIGHT) |
1509 | ($::WIDTH, $::HEIGHT) |
1336 | } |
1510 | } |
1337 | |
1511 | |
1338 | sub size_allocate { |
1512 | sub size_allocate { |
1339 | my ($self, $w, $h) = @_; |
1513 | my ($self, $x, $y, $w, $h) = @_; |
1340 | |
1514 | |
1341 | $self->SUPER::size_allocate ($w, $h); |
1515 | $self->_size_allocate ($x, $y, $w, $h); |
1342 | |
1516 | |
1343 | $_->size_allocate ($_->size_request) |
1517 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1344 | for @{$self->{children}}; |
1518 | for @{$self->{children}}; |
1345 | } |
1519 | } |
1346 | |
1520 | |
1347 | sub translate { |
1521 | sub translate { |
1348 | my ($self, $x, $y) = @_; |
1522 | my ($self, $x, $y) = @_; |
… | |
… | |
1351 | } |
1525 | } |
1352 | |
1526 | |
1353 | sub update { |
1527 | sub update { |
1354 | my ($self) = @_; |
1528 | my ($self) = @_; |
1355 | |
1529 | |
1356 | $self->size_allocate ($self->size_request); |
1530 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1357 | ::refresh (); |
1531 | ::refresh (); |
1358 | } |
1532 | } |
1359 | |
1533 | |
1360 | sub add { |
1534 | sub add { |
1361 | my ($self, $widget) = @_; |
1535 | my ($self, $widget) = @_; |
1362 | |
1536 | |
1363 | $self->SUPER::add ($widget); |
1537 | $self->SUPER::add ($widget); |
1364 | |
1538 | |
1365 | $widget->size_allocate ($widget->size_request); |
1539 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1366 | } |
1540 | } |
1367 | |
1541 | |
1368 | sub draw { |
1542 | sub draw { |
1369 | my ($self) = @_; |
1543 | my ($self) = @_; |
1370 | |
1544 | |
1371 | $self->_draw; |
1545 | $self->_draw; |
1372 | } |
1546 | } |
1373 | |
1547 | |
1374 | ############################################################################# |
1548 | ############################################################################# |
1375 | |
1549 | |
1376 | package CFClient::Widget; |
1550 | package CFClient::UI; |
1377 | |
1551 | |
1378 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1552 | $TOPLEVEL = new CFClient::UI::Toplevel; |
1379 | |
1553 | |
1380 | 1 |
1554 | 1 |
1381 | |
1555 | |