… | |
… | |
114 | } |
114 | } |
115 | |
115 | |
116 | sub focus_in { |
116 | sub focus_in { |
117 | my ($self) = @_; |
117 | my ($self) = @_; |
118 | |
118 | |
|
|
119 | return if $FOCUS == $self; |
|
|
120 | |
119 | my $focus = $FOCUS; $FOCUS = $self; |
121 | my $focus = $FOCUS; $FOCUS = $self; |
120 | $focus->update if $focus; |
122 | $focus->update if $focus; |
121 | $FOCUS->update; |
123 | $FOCUS->update; |
122 | } |
124 | } |
123 | |
125 | |
… | |
… | |
130 | $focus->update if $focus; #? |
132 | $focus->update if $focus; #? |
131 | } |
133 | } |
132 | |
134 | |
133 | sub mouse_motion { } |
135 | sub mouse_motion { } |
134 | sub button_up { } |
136 | sub button_up { } |
135 | sub button_down { } |
|
|
136 | sub key_down { } |
137 | sub key_down { } |
137 | sub key_up { } |
138 | sub key_up { } |
|
|
139 | |
|
|
140 | sub button_down { |
|
|
141 | my ($self, $ev, $x, $y) = @_; |
|
|
142 | |
|
|
143 | $self->focus_in; |
|
|
144 | } |
138 | |
145 | |
139 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
146 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
140 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
147 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
141 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
148 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
142 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
149 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
143 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
150 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
144 | |
151 | |
145 | sub draw { |
152 | sub draw { |
146 | my ($self) = @_; |
153 | my ($self) = @_; |
147 | |
154 | |
|
|
155 | return unless $self->{h} && $self->{w}; |
|
|
156 | |
148 | glPushMatrix; |
157 | glPushMatrix; |
149 | glTranslate $self->{x}, $self->{y}, 0; |
158 | glTranslate $self->{x}, $self->{y}, 0; |
150 | $self->_draw; |
159 | $self->_draw; |
|
|
160 | glPopMatrix; |
|
|
161 | |
151 | if ($self == $HOVER) { |
162 | if ($self == $HOVER) { |
|
|
163 | my ($x, $y) = @$self->{qw(x y)}; |
|
|
164 | |
152 | glColor 1, 1, 1, 0.4; |
165 | glColor 1, 1, 1, 0.1; |
153 | glEnable GL_BLEND; |
166 | glEnable GL_BLEND; |
154 | glBegin GL_QUADS; |
167 | glBegin GL_QUADS; |
155 | glVertex 0 , 0; |
168 | glVertex $x , $y; |
156 | glVertex $self->{w}, 0; |
169 | glVertex $x + $self->{w}, $y; |
157 | glVertex $self->{w}, $self->{h}; |
170 | glVertex $x + $self->{w}, $y + $self->{h}; |
158 | glVertex 0 , $self->{h}; |
171 | glVertex $x , $y + $self->{h}; |
159 | glEnd; |
172 | glEnd; |
160 | glDisable GL_BLEND; |
173 | glDisable GL_BLEND; |
161 | } |
174 | } |
162 | glPopMatrix; |
|
|
163 | } |
175 | } |
164 | |
176 | |
165 | sub _draw { |
177 | sub _draw { |
166 | my ($self) = @_; |
178 | my ($self) = @_; |
167 | |
179 | |
… | |
… | |
207 | |
219 | |
208 | $self->{parent}->update |
220 | $self->{parent}->update |
209 | if $self->{parent}; |
221 | if $self->{parent}; |
210 | } |
222 | } |
211 | |
223 | |
|
|
224 | sub connect { |
|
|
225 | my ($self, $signal, $cb) = @_; |
|
|
226 | |
|
|
227 | push @{ $self->{cb}{$signal} }, $cb; |
|
|
228 | } |
|
|
229 | |
|
|
230 | sub emit { |
|
|
231 | my ($self, $signal, @args) = @_; |
|
|
232 | |
|
|
233 | $_->($self, @args) |
|
|
234 | for @{$self->{cb}{$signal} || []}; |
|
|
235 | } |
|
|
236 | |
212 | sub DESTROY { |
237 | sub DESTROY { |
213 | my ($self) = @_; |
238 | my ($self) = @_; |
214 | |
239 | |
215 | #$self->deactivate; |
240 | #$self->deactivate; |
|
|
241 | } |
|
|
242 | |
|
|
243 | ############################################################################# |
|
|
244 | |
|
|
245 | package CFClient::Widget::DrawBG; |
|
|
246 | |
|
|
247 | our @ISA = CFClient::Widget::; |
|
|
248 | |
|
|
249 | use strict; |
|
|
250 | use SDL::OpenGL; |
|
|
251 | |
|
|
252 | sub new { |
|
|
253 | my $class = shift; |
|
|
254 | |
|
|
255 | # range [value, low, high, page] |
|
|
256 | |
|
|
257 | $class->SUPER::new ( |
|
|
258 | bg => [0, 0, 0, 0.4], |
|
|
259 | active_bg => [1, 1, 1], |
|
|
260 | @_ |
|
|
261 | ) |
|
|
262 | } |
|
|
263 | |
|
|
264 | sub _draw { |
|
|
265 | my ($self) = @_; |
|
|
266 | |
|
|
267 | my ($w, $h) = @$self{qw(w h)}; |
|
|
268 | |
|
|
269 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
270 | glBegin GL_QUADS; |
|
|
271 | glVertex 0 , 0; |
|
|
272 | glVertex 0 , $h; |
|
|
273 | glVertex $w, $h; |
|
|
274 | glVertex $w, 0; |
|
|
275 | glEnd; |
216 | } |
276 | } |
217 | |
277 | |
218 | ############################################################################# |
278 | ############################################################################# |
219 | |
279 | |
220 | package CFClient::Widget::Empty; |
280 | package CFClient::Widget::Empty; |
… | |
… | |
223 | |
283 | |
224 | sub size_request { |
284 | sub size_request { |
225 | (0, 0) |
285 | (0, 0) |
226 | } |
286 | } |
227 | |
287 | |
228 | sub draw { |
288 | sub draw { } |
229 | } |
|
|
230 | |
289 | |
231 | ############################################################################# |
290 | ############################################################################# |
232 | |
291 | |
233 | package CFClient::Widget::Container; |
292 | package CFClient::Widget::Container; |
234 | |
293 | |
… | |
… | |
322 | } |
381 | } |
323 | |
382 | |
324 | sub child { $_[0]->{children}[0] } |
383 | sub child { $_[0]->{children}[0] } |
325 | |
384 | |
326 | sub size_request { |
385 | sub size_request { |
327 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
386 | $_[0]{children}[0]->size_request |
328 | } |
387 | } |
329 | |
388 | |
330 | sub size_allocate { |
389 | sub size_allocate { |
331 | my ($self, $w, $h) = @_; |
390 | my ($self, $w, $h) = @_; |
332 | |
391 | |
|
|
392 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
393 | |
333 | $self->SUPER::size_allocate ($w, $h); |
394 | $self->SUPER::size_allocate ($w, $h); |
334 | $self->{children}[0]->size_allocate ($w, $h) |
395 | $self->{children}[0]->size_allocate ($w, $h); |
335 | if $self->{children}[0] |
|
|
336 | } |
396 | } |
337 | |
397 | |
338 | ############################################################################# |
398 | ############################################################################# |
339 | |
399 | |
340 | package CFClient::Widget::Window; |
400 | package CFClient::Widget::Window; |
… | |
… | |
367 | } |
427 | } |
368 | |
428 | |
369 | sub size_allocate { |
429 | sub size_allocate { |
370 | my ($self, $w, $h) = @_; |
430 | my ($self, $w, $h) = @_; |
371 | |
431 | |
|
|
432 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
433 | |
372 | $self->{w} = $w; |
434 | $self->{w} = $w; |
373 | $self->{h} = $h; |
435 | $self->{h} = $h; |
374 | |
436 | |
375 | $self->child->size_allocate ($w, $h); |
437 | $self->child->size_allocate ($w, $h); |
376 | |
438 | |
… | |
… | |
414 | } |
476 | } |
415 | |
477 | |
416 | sub size_allocate { |
478 | sub size_allocate { |
417 | my ($self, $w, $h) = @_; |
479 | my ($self, $w, $h) = @_; |
418 | |
480 | |
|
|
481 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
482 | |
419 | $self->{w} = $w; |
483 | $self->{w} = $w; |
420 | $self->{h} = $h; |
484 | $self->{h} = $h; |
421 | |
485 | |
422 | $self->child->size_allocate ($w - 4, $h - 4); |
486 | $self->child->size_allocate ($w - 4, $h - 4); |
423 | $self->child->move (2, 2); |
487 | $self->child->move (2, 2); |
… | |
… | |
456 | sub size_request { |
520 | sub size_request { |
457 | my ($self) = @_; |
521 | my ($self) = @_; |
458 | |
522 | |
459 | my ($w, $h) = $self->SUPER::size_request; |
523 | my ($w, $h) = $self->SUPER::size_request; |
460 | |
524 | |
461 | $h += $tex[1]->{height}; |
525 | $h += $tex[1]->{h}; |
462 | $h += $tex[4]->{height}; |
526 | $h += $tex[4]->{h}; |
463 | $w += $tex[2]->{width}; |
527 | $w += $tex[2]->{w}; |
464 | $w += $tex[3]->{width}; |
528 | $w += $tex[3]->{w}; |
465 | |
529 | |
466 | ($w, $h) |
530 | ($w, $h) |
467 | } |
531 | } |
468 | |
532 | |
469 | sub size_allocate { |
533 | sub size_allocate { |
470 | my ($self, $w, $h) = @_; |
534 | my ($self, $w, $h) = @_; |
471 | |
535 | |
|
|
536 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
537 | |
472 | $self->SUPER::size_allocate ($w, $h); |
538 | $self->SUPER::size_allocate ($w, $h); |
473 | |
539 | |
474 | $h -= $tex[1]->{height}; |
540 | $h -= $tex[1]->{h}; |
475 | $h -= $tex[4]->{height}; |
541 | $h -= $tex[4]->{h}; |
476 | $w -= $tex[2]->{width}; |
542 | $w -= $tex[2]->{w}; |
477 | $w -= $tex[3]->{width}; |
543 | $w -= $tex[3]->{w}; |
478 | |
544 | |
479 | $h = $h < 0 ? 0 : $h; |
545 | $h = $h < 0 ? 0 : $h; |
480 | $w = $w < 0 ? 0 : $w; |
546 | $w = $w < 0 ? 0 : $w; |
481 | |
547 | |
482 | my $child = $self->child; |
548 | my $child = $self->child; |
483 | |
549 | |
484 | $child->size_allocate ($w, $h); |
550 | $child->size_allocate ($w, $h); |
485 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
551 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
486 | } |
552 | } |
487 | |
553 | |
488 | sub _draw { |
554 | sub _draw { |
489 | my ($self) = @_; |
555 | my ($self) = @_; |
490 | |
556 | |
… | |
… | |
495 | glEnable GL_TEXTURE_2D; |
561 | glEnable GL_TEXTURE_2D; |
496 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
562 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
497 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
498 | |
564 | |
499 | my $top = $tex[1]; |
565 | my $top = $tex[1]; |
500 | $top->draw_quad (0, 0, $w, $top->{height}); |
566 | $top->draw_quad (0, 0, $w, $top->{h}); |
501 | |
567 | |
502 | my $left = $tex[3]; |
568 | my $left = $tex[3]; |
503 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
569 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
504 | |
570 | |
505 | my $right = $tex[2]; |
571 | my $right = $tex[2]; |
506 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
572 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
507 | |
573 | |
508 | my $bottom = $tex[4]; |
574 | my $bottom = $tex[4]; |
509 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
575 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
510 | |
576 | |
511 | my $bg = $tex[0]; |
577 | my $bg = $tex[0]; |
512 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
578 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
513 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
579 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
514 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
580 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
515 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
581 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
516 | |
582 | |
517 | my $rep_x = $cw / $bg->{width}; |
583 | my $rep_x = $cw / $bg->{w}; |
518 | my $rep_y = $ch / $bg->{height}; |
584 | my $rep_y = $ch / $bg->{h}; |
519 | |
585 | |
520 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
586 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
521 | |
587 | |
522 | glDisable GL_BLEND; |
588 | glDisable GL_BLEND; |
523 | glDisable GL_TEXTURE_2D; |
589 | glDisable GL_TEXTURE_2D; |
524 | |
590 | |
525 | $self->child->draw; |
591 | $self->child->draw; |
… | |
… | |
528 | |
594 | |
529 | ############################################################################# |
595 | ############################################################################# |
530 | |
596 | |
531 | package CFClient::Widget::Table; |
597 | package CFClient::Widget::Table; |
532 | |
598 | |
533 | our @ISA = CFClient::Widget::Bin::; |
599 | our @ISA = CFClient::Widget::; |
534 | |
600 | |
535 | use SDL::OpenGL; |
601 | use SDL::OpenGL; |
536 | |
602 | |
537 | sub add { |
603 | sub add { |
538 | my ($self, $x, $y, $chld) = @_; |
604 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
633 | } |
699 | } |
634 | |
700 | |
635 | sub size_allocate { |
701 | sub size_allocate { |
636 | my ($self, $w, $h) = @_; |
702 | my ($self, $w, $h) = @_; |
637 | |
703 | |
|
|
704 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
705 | |
638 | $self->w ($w); |
706 | $self->{w} = $w; |
639 | $self->h ($h); |
707 | $self->{h} = $h; |
640 | |
708 | |
641 | my $exp; |
709 | return unless $self->{h}; |
642 | my @oth; |
710 | |
643 | # find expand widget |
711 | my $children = $self->{children}; |
644 | for (@{$self->{children}}) { |
712 | |
645 | if ($_->{expand}) { |
713 | my @h = map +($_->size_request)[1], @$children; |
646 | $exp = $_; |
714 | |
647 | last; |
715 | my $req_h = List::Util::sum @h; |
|
|
716 | |
|
|
717 | if ($req_h > $h) { |
|
|
718 | # ah well, not enough space |
|
|
719 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
720 | } else { |
|
|
721 | my @exp = grep $_->{expand}, @$children; |
|
|
722 | @exp = @$children unless @exp; |
|
|
723 | |
|
|
724 | my %exp = map +($_ => 1), @exp; |
|
|
725 | |
|
|
726 | for (0 .. $#$children) { |
|
|
727 | my $child = $children->[$_]; |
|
|
728 | |
|
|
729 | my $alloc_h = $h[$_]; |
|
|
730 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
731 | $h[$_] = $alloc_h; |
648 | } |
732 | } |
649 | push @oth, $_; |
|
|
650 | } |
|
|
651 | |
|
|
652 | my ($ow, $oh); |
|
|
653 | |
|
|
654 | # get sizes of other widgets |
|
|
655 | for (@oth) { |
|
|
656 | my ($w, $h) = $_->size_request; |
|
|
657 | $oh += $h; |
|
|
658 | if ($ow < $w) { $ow = $w } |
|
|
659 | } |
733 | } |
660 | |
734 | |
661 | my $y = 0; |
735 | my $y = 0; |
662 | for (@{$self->{children}}) { |
736 | for (0 .. $#$children) { |
|
|
737 | my $child = $children->[$_]; |
|
|
738 | my $h = $h[$_]; |
663 | $_->move (0, $y); |
739 | $child->move (0, $y); |
664 | |
|
|
665 | if ($_ == $exp) { |
|
|
666 | $_->size_allocate ($w, $h - $oh); |
|
|
667 | $y += $h - $oh; |
|
|
668 | } else { |
|
|
669 | my ($cw, $h) = $_->size_request; |
|
|
670 | $_->size_allocate ($w, $h); |
740 | $child->size_allocate ($w, $h); |
|
|
741 | |
671 | $y += $h; |
742 | $y += $h; |
672 | } |
|
|
673 | } |
743 | } |
674 | } |
744 | } |
675 | |
745 | |
676 | ############################################################################# |
746 | ############################################################################# |
677 | |
747 | |
… | |
… | |
682 | use SDL::OpenGL; |
752 | use SDL::OpenGL; |
683 | |
753 | |
684 | sub new { |
754 | sub new { |
685 | my ($class, %arg) = @_; |
755 | my ($class, %arg) = @_; |
686 | |
756 | |
687 | # TODO: color, and make height, xyz etc. optional |
|
|
688 | my $self = $class->SUPER::new ( |
757 | my $self = $class->SUPER::new ( |
689 | color => [1, 1, 1], |
758 | fg => [1, 1, 1], |
690 | height => $::FONTSIZE, |
759 | height => $::FONTSIZE, |
691 | text => "", |
760 | text => "", |
|
|
761 | align => -1, |
692 | layout => new CFClient::Layout, |
762 | layout => new CFClient::Layout, |
693 | %arg |
763 | %arg |
694 | ); |
764 | ); |
695 | |
765 | |
696 | $self->set_text ($self->{text}); |
766 | $self->set_text ($self->{text}); |
697 | |
767 | |
698 | $self |
768 | $self |
699 | } |
769 | } |
700 | |
770 | |
|
|
771 | sub escape_text { |
|
|
772 | local $_ = $_[1]; |
|
|
773 | |
|
|
774 | s/&/&/g; |
|
|
775 | s/>/>/g; |
|
|
776 | s/</</g; |
|
|
777 | |
|
|
778 | $_[1] |
|
|
779 | } |
|
|
780 | |
701 | sub set_text { |
781 | sub set_text { |
702 | my ($self, $text) = @_; |
782 | my ($self, $text) = @_; |
703 | |
783 | |
704 | $self->{text} = $text; |
784 | $self->{text} = $text; |
705 | $self->{layout}->set_markup ($text); |
785 | $self->{layout}->set_markup ($text); |
706 | |
786 | |
707 | delete $self->{texture}; |
787 | delete $self->{texture}; |
|
|
788 | $self->update; |
708 | } |
789 | } |
709 | |
790 | |
710 | sub get_text { |
791 | sub get_text { |
711 | my ($self, $text) = @_; |
792 | my ($self, $text) = @_; |
712 | |
793 | |
… | |
… | |
717 | my ($self) = @_; |
798 | my ($self) = @_; |
718 | |
799 | |
719 | $self->{layout}->set_width; |
800 | $self->{layout}->set_width; |
720 | $self->{layout}->set_height ($self->{height}); |
801 | $self->{layout}->set_height ($self->{height}); |
721 | $self->{layout}->size |
802 | $self->{layout}->size |
722 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
803 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
723 | # ( |
804 | # ( |
724 | # $self->{texture}{width}, |
805 | # $self->{texture}{w}, |
725 | # $self->{texture}{height}, |
806 | # $self->{texture}{h}, |
726 | # ) |
807 | # ) |
727 | # } else { |
808 | # } else { |
728 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
809 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
729 | # |
810 | # |
730 | # ($w, $h) |
811 | # ($w, $h) |
731 | # } |
812 | # } |
732 | } |
813 | } |
733 | |
814 | |
734 | sub size_allocate { |
815 | sub size_allocate { |
735 | my ($self, $w, $h) = @_; |
816 | my ($self, $w, $h) = @_; |
736 | |
817 | |
|
|
818 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
819 | |
737 | $self->SUPER::size_allocate ($w, $h); |
820 | $self->SUPER::size_allocate ($w, $h); |
738 | delete $self->{texture}; |
821 | delete $self->{texture}; |
|
|
822 | } |
|
|
823 | |
|
|
824 | sub update { |
|
|
825 | my ($self) = @_; |
|
|
826 | |
|
|
827 | delete $self->{texture}; |
|
|
828 | $self->SUPER::update; |
739 | } |
829 | } |
740 | |
830 | |
741 | sub _draw { |
831 | sub _draw { |
742 | my ($self) = @_; |
832 | my ($self) = @_; |
743 | |
833 | |
… | |
… | |
749 | glEnable GL_BLEND; |
839 | glEnable GL_BLEND; |
750 | glEnable GL_TEXTURE_2D; |
840 | glEnable GL_TEXTURE_2D; |
751 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
841 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
752 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
842 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
753 | |
843 | |
754 | glColor @{$self->{color}}; |
844 | glColor @{$self->{fg}}; |
755 | |
845 | |
|
|
846 | my $x = |
|
|
847 | $self->{align} < 0 ? 0 |
|
|
848 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
|
|
849 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
|
|
850 | |
756 | $tex->draw_quad (0, 0); |
851 | $tex->draw_quad ($x, 0); |
757 | |
852 | |
758 | glDisable GL_BLEND; |
853 | glDisable GL_BLEND; |
759 | glDisable GL_TEXTURE_2D; |
854 | glDisable GL_TEXTURE_2D; |
760 | } |
855 | } |
761 | |
856 | |
… | |
… | |
766 | our @ISA = CFClient::Widget::Label::; |
861 | our @ISA = CFClient::Widget::Label::; |
767 | |
862 | |
768 | use SDL; |
863 | use SDL; |
769 | use SDL::OpenGL; |
864 | use SDL::OpenGL; |
770 | |
865 | |
|
|
866 | sub new { |
|
|
867 | my $class = shift; |
|
|
868 | |
|
|
869 | $class->SUPER::new ( |
|
|
870 | fg => [1, 1, 1], |
|
|
871 | bg => [0, 0, 0, 0.4], |
|
|
872 | active_bg => [1, 1, 1], |
|
|
873 | active_fg => [0, 0, 0], |
|
|
874 | @_ |
|
|
875 | ) |
|
|
876 | } |
|
|
877 | |
|
|
878 | sub _set_text { |
|
|
879 | my ($self, $text) = @_; |
|
|
880 | |
|
|
881 | $self->{last_activity} = $::NOW; |
|
|
882 | |
|
|
883 | $self->{text} = $text; |
|
|
884 | $self->{layout}->set_width ($self->{w}); |
|
|
885 | |
|
|
886 | $text =~ s/./*/g if $self->{hidden}; |
|
|
887 | |
|
|
888 | |
|
|
889 | $self->{layout}->set_markup ($self->escape_text ($text)); |
|
|
890 | |
|
|
891 | $text = substr $text, 0, $self->{cursor}; |
|
|
892 | utf8::encode $text; |
|
|
893 | |
|
|
894 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
895 | } |
|
|
896 | |
|
|
897 | sub size_request { |
|
|
898 | my ($self) = @_; |
|
|
899 | |
|
|
900 | my ($w, $h) = $self->SUPER::size_request; |
|
|
901 | |
|
|
902 | ($w + 1, $h) # add 1 for cursor |
|
|
903 | } |
|
|
904 | |
|
|
905 | sub size_allocate { |
|
|
906 | my ($self, $w, $h) = @_; |
|
|
907 | |
|
|
908 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
909 | |
|
|
910 | $self->SUPER::size_allocate ($w, $h); |
|
|
911 | |
|
|
912 | $self->_set_text ($self->{text}); |
|
|
913 | } |
|
|
914 | |
|
|
915 | sub set_text { |
|
|
916 | my ($self, $text) = @_; |
|
|
917 | |
|
|
918 | $self->{cursor} = length $text; |
|
|
919 | $self->_set_text ($text); |
|
|
920 | $self->update; |
|
|
921 | } |
|
|
922 | |
771 | sub key_down { |
923 | sub key_down { |
772 | my ($self, $ev) = @_; |
924 | my ($self, $ev) = @_; |
773 | |
925 | |
774 | my $mod = $ev->key_mod; |
926 | my $mod = $ev->key_mod; |
775 | my $sym = $ev->key_sym; |
927 | my $sym = $ev->key_sym; |
… | |
… | |
777 | my $uni = $ev->key_unicode; |
929 | my $uni = $ev->key_unicode; |
778 | |
930 | |
779 | my $text = $self->get_text; |
931 | my $text = $self->get_text; |
780 | |
932 | |
781 | if ($sym == SDLK_BACKSPACE) { |
933 | if ($sym == SDLK_BACKSPACE) { |
782 | substr $text, -1, 1, ''; |
934 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
|
|
935 | } elsif ($sym == SDLK_DELETE) { |
|
|
936 | substr $text, $self->{cursor}, 1, ""; |
|
|
937 | } elsif ($sym == SDLK_LEFT) { |
|
|
938 | --$self->{cursor} if $self->{cursor}; |
|
|
939 | } elsif ($sym == SDLK_RIGHT) { |
|
|
940 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
783 | } elsif ($uni) { |
941 | } elsif ($uni) { |
784 | $text .= chr $uni; |
942 | substr $text, $self->{cursor}++, 0, chr $uni; |
785 | } |
943 | } |
786 | |
944 | |
787 | $self->set_text ($text); |
945 | $self->_set_text ($text); |
|
|
946 | $self->update; |
|
|
947 | } |
|
|
948 | |
|
|
949 | sub focus_in { |
|
|
950 | my ($self) = @_; |
|
|
951 | |
|
|
952 | $self->{last_activity} = $::NOW; |
|
|
953 | |
|
|
954 | $self->SUPER::focus_in; |
788 | } |
955 | } |
789 | |
956 | |
790 | sub button_down { |
957 | sub button_down { |
791 | my ($self, $ev) = @_; |
958 | my ($self, $ev, $x, $y) = @_; |
792 | |
959 | |
793 | $self->focus_in; |
960 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
961 | |
|
|
962 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
|
|
963 | |
|
|
964 | # byte-index to char-index |
|
|
965 | my $text = $self->{layout}; |
|
|
966 | utf8::encode $text; |
|
|
967 | $self->{cursor} = length substr $text, 0, $idx; |
|
|
968 | |
|
|
969 | $self->_set_text ($self->{text}); |
|
|
970 | $self->update; |
794 | } |
971 | } |
795 | |
972 | |
796 | sub mouse_motion { |
973 | sub mouse_motion { |
797 | my ($self, $ev, $x, $y) = @_; |
974 | my ($self, $ev, $x, $y) = @_; |
798 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
975 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
799 | } |
976 | } |
800 | |
977 | |
801 | sub _draw { |
978 | sub _draw { |
802 | my ($self) = @_; |
979 | my ($self) = @_; |
803 | |
980 | |
|
|
981 | local $self->{fg} = $self->{fg}; |
|
|
982 | |
804 | if ($FOCUS == $self) { |
983 | if ($FOCUS == $self) { |
805 | glColor 1, 1, 1; |
984 | glColor @{$self->{active_bg}}; |
|
|
985 | $self->{fg} = $self->{active_fg}; |
806 | } else { |
986 | } else { |
807 | glColor 0.7, 0.7, 0.7; |
987 | glColor @{$self->{bg}}; |
808 | } |
988 | } |
809 | |
989 | |
810 | glBegin GL_QUADS; |
990 | glBegin GL_QUADS; |
811 | glVertex 0 , 0; |
991 | glVertex 0 , 0; |
812 | glVertex 0 , $self->{h} - 1; |
992 | glVertex 0 , $self->{h}; |
813 | glVertex $self->{w} - 1, $self->{h} - 1; |
993 | glVertex $self->{w}, $self->{h}; |
814 | glVertex $self->{w} - 1, 0; |
994 | glVertex $self->{w}, 0; |
815 | glEnd; |
995 | glEnd; |
816 | |
996 | |
817 | $self->SUPER::_draw; |
997 | $self->SUPER::_draw; |
|
|
998 | |
|
|
999 | #TODO: force update every cursor change :( |
|
|
1000 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1001 | glColor @{$self->{fg}}; |
|
|
1002 | glBegin GL_LINES; |
|
|
1003 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1004 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
|
|
1005 | glEnd; |
|
|
1006 | } |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | ############################################################################# |
|
|
1010 | |
|
|
1011 | package CFClient::Widget::Slider; |
|
|
1012 | |
|
|
1013 | use strict; |
|
|
1014 | |
|
|
1015 | use SDL::OpenGL; |
|
|
1016 | use SDL::OpenGL::Constants; |
|
|
1017 | |
|
|
1018 | our @ISA = CFClient::Widget::DrawBG::; |
|
|
1019 | |
|
|
1020 | sub size_request { |
|
|
1021 | my ($self) = @_; |
|
|
1022 | |
|
|
1023 | my $w = 50; |
|
|
1024 | my $h = 10; |
|
|
1025 | |
|
|
1026 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | sub new { |
|
|
1030 | my $class = shift; |
|
|
1031 | |
|
|
1032 | # range [value, low, high, page] |
|
|
1033 | |
|
|
1034 | $class->SUPER::new ( |
|
|
1035 | fg => [1, 1, 1], |
|
|
1036 | active_fg => [0, 0, 0], |
|
|
1037 | range => [0, 0, 100, 10], |
|
|
1038 | vertical => 1, |
|
|
1039 | @_ |
|
|
1040 | ) |
|
|
1041 | } |
|
|
1042 | |
|
|
1043 | sub button_down { |
|
|
1044 | my ($self, $ev, $x, $y) = @_; |
|
|
1045 | |
|
|
1046 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
1047 | $self->mouse_motion ($ev, $x, $y); |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | sub mouse_motion { |
|
|
1051 | my ($self, $ev, $x, $y) = @_; |
|
|
1052 | |
|
|
1053 | if ($GRAB == $self) { |
|
|
1054 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1055 | |
|
|
1056 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
1057 | |
|
|
1058 | $x = $x * ($hi - $lo) / $w + $lo; |
|
|
1059 | $x = $lo if $x < $lo; |
|
|
1060 | $x = $hi - $page if $x > $hi - $page; |
|
|
1061 | $self->{range}[0] = $x; |
|
|
1062 | |
|
|
1063 | $self->emit (changed => $x); |
|
|
1064 | $self->update; |
|
|
1065 | } |
|
|
1066 | } |
|
|
1067 | |
|
|
1068 | sub _draw { |
|
|
1069 | my ($self) = @_; |
|
|
1070 | |
|
|
1071 | $self->SUPER::_draw (); |
|
|
1072 | |
|
|
1073 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1074 | |
|
|
1075 | if ($self->{vertical}) { |
|
|
1076 | # draw a vertical slider like a rotated horizontal slider |
|
|
1077 | |
|
|
1078 | glRotate 90, 0, 0, 1; |
|
|
1079 | glTranslate 0, -$self->{w}, 0; |
|
|
1080 | |
|
|
1081 | ($w, $h) = ($h, $w); |
|
|
1082 | } |
|
|
1083 | |
|
|
1084 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
|
|
1085 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
|
|
1086 | |
|
|
1087 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1088 | |
|
|
1089 | $page = int $page * $w / ($hi - $lo); |
|
|
1090 | $value = int +($value - $lo) * $w / ($hi - $lo); |
|
|
1091 | |
|
|
1092 | $w -= $page; |
|
|
1093 | $page &= ~1; |
|
|
1094 | glTranslate $page * 0.5, 0, 0; |
|
|
1095 | |
|
|
1096 | glColor @$fg; |
|
|
1097 | glBegin GL_LINES; |
|
|
1098 | glVertex 0, 0; glVertex 0, $h; |
|
|
1099 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1100 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1101 | glEnd; |
|
|
1102 | |
|
|
1103 | my $knob_a = $value - $page * 0.5; |
|
|
1104 | my $knob_b = $value + $page * 0.5; |
|
|
1105 | |
|
|
1106 | glBegin GL_QUADS; |
|
|
1107 | glColor @$fg; |
|
|
1108 | glVertex $knob_a, 0; |
|
|
1109 | glVertex $knob_a, $h; |
|
|
1110 | glVertex $knob_b, $h; |
|
|
1111 | glVertex $knob_b, 0; |
|
|
1112 | |
|
|
1113 | if ($knob_a < $knob_b - 2) { |
|
|
1114 | glColor @$bg; |
|
|
1115 | glVertex $knob_a + 1, 1; |
|
|
1116 | glVertex $knob_a + 1, $h - 1; |
|
|
1117 | glVertex $knob_b - 1, $h - 1; |
|
|
1118 | glVertex $knob_b - 1, 1; |
|
|
1119 | } |
|
|
1120 | glEnd; |
818 | } |
1121 | } |
819 | |
1122 | |
820 | ############################################################################# |
1123 | ############################################################################# |
821 | |
1124 | |
822 | package CFClient::Widget::MapWidget; |
1125 | package CFClient::Widget::MapWidget; |
… | |
… | |
911 | } |
1214 | } |
912 | |
1215 | |
913 | for my $num (grep $_, @$cell[1,2,3]) { |
1216 | for my $num (grep $_, @$cell[1,2,3]) { |
914 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1217 | my $tex = $::CONN->{face}[$num]{texture} || next; |
915 | |
1218 | |
916 | my $w = $tex->{width}; |
1219 | my ($w, $h) = @$tex{qw(w h)}; |
917 | my $h = $tex->{height}; |
|
|
918 | |
1220 | |
919 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1221 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
920 | } |
1222 | } |
921 | } |
1223 | } |
922 | } |
1224 | } |