… | |
… | |
73 | } |
73 | } |
74 | |
74 | |
75 | sub new { |
75 | sub new { |
76 | my $class = shift; |
76 | my $class = shift; |
77 | |
77 | |
78 | bless { @_ }, $class |
78 | bless { |
|
|
79 | x => 0, |
|
|
80 | y => 0, |
|
|
81 | z => 0, |
|
|
82 | @_ |
|
|
83 | }, $class |
79 | } |
84 | } |
80 | |
85 | |
81 | sub move { |
86 | sub move { |
82 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
83 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
109 | } |
114 | } |
110 | |
115 | |
111 | sub focus_in { |
116 | sub focus_in { |
112 | my ($self) = @_; |
117 | my ($self) = @_; |
113 | |
118 | |
|
|
119 | return if $FOCUS == $self; |
|
|
120 | |
114 | my $focus = $FOCUS; $FOCUS = $self; |
121 | my $focus = $FOCUS; $FOCUS = $self; |
115 | $focus->update if $focus; |
122 | $focus->update if $focus; |
116 | $FOCUS->update; |
123 | $FOCUS->update; |
117 | } |
124 | } |
118 | |
125 | |
… | |
… | |
125 | $focus->update if $focus; #? |
132 | $focus->update if $focus; #? |
126 | } |
133 | } |
127 | |
134 | |
128 | sub mouse_motion { } |
135 | sub mouse_motion { } |
129 | sub button_up { } |
136 | sub button_up { } |
130 | sub button_down { } |
|
|
131 | sub key_down { } |
137 | sub key_down { } |
132 | sub key_up { } |
138 | sub key_up { } |
|
|
139 | |
|
|
140 | sub button_down { |
|
|
141 | my ($self, $ev, $x, $y) = @_; |
|
|
142 | |
|
|
143 | $self->focus_in; |
|
|
144 | } |
133 | |
145 | |
134 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
146 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
135 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
147 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
136 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
148 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
137 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
149 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
138 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
150 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
139 | |
151 | |
140 | sub draw { |
152 | sub draw { |
141 | my ($self) = @_; |
153 | my ($self) = @_; |
|
|
154 | |
|
|
155 | return unless $self->{h} && $self->{w}; |
142 | |
156 | |
143 | glPushMatrix; |
157 | glPushMatrix; |
144 | glTranslate $self->{x}, $self->{y}, 0; |
158 | glTranslate $self->{x}, $self->{y}, 0; |
145 | $self->_draw; |
159 | $self->_draw; |
146 | if ($self == $HOVER) { |
160 | if ($self == $HOVER) { |
… | |
… | |
210 | #$self->deactivate; |
224 | #$self->deactivate; |
211 | } |
225 | } |
212 | |
226 | |
213 | ############################################################################# |
227 | ############################################################################# |
214 | |
228 | |
|
|
229 | package CFClient::Widget::DrawBG; |
|
|
230 | |
|
|
231 | our @ISA = CFClient::Widget::; |
|
|
232 | |
|
|
233 | use strict; |
|
|
234 | use SDL::OpenGL; |
|
|
235 | |
|
|
236 | sub new { |
|
|
237 | my $class = shift; |
|
|
238 | |
|
|
239 | # range [value, low, high, page] |
|
|
240 | |
|
|
241 | $class->SUPER::new ( |
|
|
242 | bg => [0, 0, 0, 0.4], |
|
|
243 | active_bg => [1, 1, 1], |
|
|
244 | @_ |
|
|
245 | ) |
|
|
246 | } |
|
|
247 | |
|
|
248 | sub _draw { |
|
|
249 | my ($self) = @_; |
|
|
250 | |
|
|
251 | my ($w, $h) = @$self{qw(w h)}; |
|
|
252 | |
|
|
253 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
254 | glBegin GL_QUADS; |
|
|
255 | glVertex 0 , 0; |
|
|
256 | glVertex 0 , $h; |
|
|
257 | glVertex $w, $h; |
|
|
258 | glVertex $w, 0; |
|
|
259 | glEnd; |
|
|
260 | } |
|
|
261 | |
|
|
262 | ############################################################################# |
|
|
263 | |
|
|
264 | package CFClient::Widget::Empty; |
|
|
265 | |
|
|
266 | our @ISA = CFClient::Widget::; |
|
|
267 | |
|
|
268 | sub size_request { |
|
|
269 | (0, 0) |
|
|
270 | } |
|
|
271 | |
|
|
272 | sub draw { } |
|
|
273 | |
|
|
274 | ############################################################################# |
|
|
275 | |
215 | package CFClient::Widget::Container; |
276 | package CFClient::Widget::Container; |
216 | |
277 | |
217 | our @ISA = CFClient::Widget::; |
278 | our @ISA = CFClient::Widget::; |
218 | |
279 | |
219 | sub new { |
280 | sub new { |
220 | my ($class, @widgets) = @_; |
281 | my ($class, %arg) = @_; |
221 | |
282 | |
|
|
283 | my $children = delete $arg{children} || []; |
|
|
284 | |
222 | my $self = $class->SUPER::new (children => []); |
285 | my $self = $class->SUPER::new (children => [], %arg); |
223 | $self->add ($_) for @widgets; |
286 | $self->add ($_) for @$children; |
224 | |
287 | |
225 | $self |
288 | $self |
226 | } |
289 | } |
227 | |
290 | |
228 | sub add { |
291 | sub add { |
229 | my ($self, $chld, $expand) = @_; |
292 | my ($self, $chld, $expand) = @_; |
230 | |
293 | |
231 | $chld->{expand} = $expand; |
294 | $chld->{expand} = $expand; |
232 | $chld->set_parent ($self); |
295 | $chld->set_parent ($self); |
233 | |
296 | |
234 | @{$self->{children}} = |
297 | $self->{children} = [ |
235 | sort { $a->{z} <=> $b->{z} } |
298 | sort { $a->{z} <=> $b->{z} } |
236 | @{$self->{children}}, $chld; |
299 | @{$self->{children}}, $chld |
|
|
300 | ]; |
237 | |
301 | |
238 | $self->size_allocate ($self->{w}, $self->{h}) |
302 | $self->size_allocate ($self->{w}, $self->{h}) |
239 | if $self->{w}; #TODO: check for "realised state" |
303 | if $self->{w}; #TODO: check for "realised state" |
240 | } |
304 | } |
241 | |
305 | |
… | |
… | |
273 | |
337 | |
274 | package CFClient::Widget::Bin; |
338 | package CFClient::Widget::Bin; |
275 | |
339 | |
276 | our @ISA = CFClient::Widget::Container::; |
340 | our @ISA = CFClient::Widget::Container::; |
277 | |
341 | |
|
|
342 | sub new { |
|
|
343 | my ($class, %arg) = @_; |
|
|
344 | |
|
|
345 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
|
|
346 | |
|
|
347 | $class->SUPER::new (children => [$child], %arg) |
|
|
348 | } |
|
|
349 | |
|
|
350 | sub add { |
|
|
351 | my ($self, $widget) = @_; |
|
|
352 | |
|
|
353 | $self->{children} = []; |
|
|
354 | |
|
|
355 | $self->SUPER::add ($widget); |
|
|
356 | } |
|
|
357 | |
|
|
358 | sub remove { |
|
|
359 | my ($self, $widget) = @_; |
|
|
360 | |
|
|
361 | $self->SUPER::remove ($widget); |
|
|
362 | |
|
|
363 | $self->{children} = [new CFClient::Widget::Empty] |
|
|
364 | unless @{$self->{children}}; |
|
|
365 | } |
|
|
366 | |
278 | sub child { $_[0]->{children}[0] } |
367 | sub child { $_[0]->{children}[0] } |
279 | |
368 | |
280 | sub size_request { |
369 | sub size_request { |
281 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
370 | $_[0]{children}[0]->size_request |
282 | } |
371 | } |
283 | |
372 | |
284 | sub size_allocate { |
373 | sub size_allocate { |
285 | my ($self, $w, $h) = @_; |
374 | my ($self, $w, $h) = @_; |
286 | |
375 | |
|
|
376 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
377 | |
287 | $self->SUPER::size_allocate ($w, $h); |
378 | $self->SUPER::size_allocate ($w, $h); |
288 | $self->{children}[0]->size_allocate ($w, $h) |
379 | $self->{children}[0]->size_allocate ($w, $h); |
289 | if $self->{children}[0] |
|
|
290 | } |
380 | } |
291 | |
381 | |
292 | ############################################################################# |
382 | ############################################################################# |
293 | |
383 | |
294 | package CFClient::Widget::Window; |
384 | package CFClient::Widget::Window; |
… | |
… | |
296 | our @ISA = CFClient::Widget::Bin::; |
386 | our @ISA = CFClient::Widget::Bin::; |
297 | |
387 | |
298 | use SDL::OpenGL; |
388 | use SDL::OpenGL; |
299 | |
389 | |
300 | sub new { |
390 | sub new { |
301 | my ($class, $x, $y, $z, $w, $h) = @_; |
391 | my ($class, %arg) = @_; |
302 | |
392 | |
303 | my $self = $class->SUPER::new; |
393 | my $self = $class->SUPER::new (%arg); |
304 | |
|
|
305 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
306 | } |
394 | } |
307 | |
395 | |
308 | sub update { |
396 | sub update { |
309 | my ($self) = @_; |
397 | my ($self) = @_; |
310 | |
398 | |
|
|
399 | # we want to do this delayed... |
311 | $self->render_chld; |
400 | $self->render_chld; |
312 | $self->SUPER::update; |
401 | $self->SUPER::update; |
313 | } |
402 | } |
314 | |
403 | |
315 | sub render_chld { |
404 | sub render_chld { |
… | |
… | |
322 | } |
411 | } |
323 | |
412 | |
324 | sub size_allocate { |
413 | sub size_allocate { |
325 | my ($self, $w, $h) = @_; |
414 | my ($self, $w, $h) = @_; |
326 | |
415 | |
|
|
416 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
417 | |
327 | $self->{w} = $w; |
418 | $self->{w} = $w; |
328 | $self->{h} = $h; |
419 | $self->{h} = $h; |
329 | |
420 | |
330 | $self->child->size_allocate ($w, $h); |
421 | $self->child->size_allocate ($w, $h); |
331 | |
422 | |
… | |
… | |
369 | } |
460 | } |
370 | |
461 | |
371 | sub size_allocate { |
462 | sub size_allocate { |
372 | my ($self, $w, $h) = @_; |
463 | my ($self, $w, $h) = @_; |
373 | |
464 | |
|
|
465 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
466 | |
374 | $self->{w} = $w; |
467 | $self->{w} = $w; |
375 | $self->{h} = $h; |
468 | $self->{h} = $h; |
376 | |
469 | |
377 | $self->child->size_allocate ($w - 4, $h - 4); |
470 | $self->child->size_allocate ($w - 4, $h - 4); |
378 | $self->child->move (2, 2); |
471 | $self->child->move (2, 2); |
… | |
… | |
422 | } |
515 | } |
423 | |
516 | |
424 | sub size_allocate { |
517 | sub size_allocate { |
425 | my ($self, $w, $h) = @_; |
518 | my ($self, $w, $h) = @_; |
426 | |
519 | |
|
|
520 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
521 | |
427 | $self->SUPER::size_allocate ($w, $h); |
522 | $self->SUPER::size_allocate ($w, $h); |
428 | |
523 | |
429 | $h -= $tex[1]->{height}; |
524 | $h -= $tex[1]->{height}; |
430 | $h -= $tex[4]->{height}; |
525 | $h -= $tex[4]->{height}; |
431 | $w -= $tex[2]->{width}; |
526 | $w -= $tex[2]->{width}; |
432 | $w -= $tex[3]->{width}; |
527 | $w -= $tex[3]->{width}; |
433 | |
528 | |
434 | $h = $h < 0 ? 0 : $h; |
529 | $h = $h < 0 ? 0 : $h; |
435 | $w = $w < 0 ? 0 : $w; |
530 | $w = $w < 0 ? 0 : $w; |
436 | |
531 | |
|
|
532 | my $child = $self->child; |
|
|
533 | |
437 | $self->child->size_allocate ($w, $h); |
534 | $child->size_allocate ($w, $h); |
438 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
535 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
439 | } |
536 | } |
440 | |
537 | |
441 | sub _draw { |
538 | sub _draw { |
442 | my ($self) = @_; |
539 | my ($self) = @_; |
443 | |
540 | |
… | |
… | |
586 | } |
683 | } |
587 | |
684 | |
588 | sub size_allocate { |
685 | sub size_allocate { |
589 | my ($self, $w, $h) = @_; |
686 | my ($self, $w, $h) = @_; |
590 | |
687 | |
|
|
688 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
689 | |
591 | $self->w ($w); |
690 | $self->{w} = $w; |
592 | $self->h ($h); |
691 | $self->{h} = $h; |
593 | |
692 | |
594 | my $exp; |
693 | return unless $self->{h}; |
595 | my @oth; |
694 | |
596 | # find expand widget |
695 | my $children = $self->{children}; |
597 | for (@{$self->{children}}) { |
696 | |
598 | if ($_->{expand}) { |
697 | my @h = map +($_->size_request)[1], @$children; |
599 | $exp = $_; |
698 | |
600 | last; |
699 | my $req_h = List::Util::sum @h; |
|
|
700 | |
|
|
701 | if ($req_h > $h) { |
|
|
702 | # ah well, not enough space |
|
|
703 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
704 | } else { |
|
|
705 | my @exp = grep $_->{expand}, @$children; |
|
|
706 | @exp = @$children unless @exp; |
|
|
707 | |
|
|
708 | my %exp = map +($_ => 1), @exp; |
|
|
709 | |
|
|
710 | for (0 .. $#$children) { |
|
|
711 | my $child = $children->[$_]; |
|
|
712 | |
|
|
713 | my $alloc_h = $h[$_]; |
|
|
714 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
715 | $h[$_] = $alloc_h; |
601 | } |
716 | } |
602 | push @oth, $_; |
|
|
603 | } |
|
|
604 | |
|
|
605 | my ($ow, $oh); |
|
|
606 | |
|
|
607 | # get sizes of other widgets |
|
|
608 | for (@oth) { |
|
|
609 | my ($w, $h) = $_->size_request; |
|
|
610 | $oh += $h; |
|
|
611 | if ($ow < $w) { $ow = $w } |
|
|
612 | } |
717 | } |
613 | |
718 | |
614 | my $y = 0; |
719 | my $y = 0; |
615 | for (@{$self->{children}}) { |
720 | for (0 .. $#$children) { |
|
|
721 | my $child = $children->[$_]; |
|
|
722 | my $h = $h[$_]; |
616 | $_->move (0, $y); |
723 | $child->move (0, $y); |
617 | |
|
|
618 | if ($_ == $exp) { |
|
|
619 | $_->size_allocate ($w, $h - $oh); |
|
|
620 | $y += $h - $oh; |
|
|
621 | } else { |
|
|
622 | my ($cw, $h) = $_->size_request; |
|
|
623 | $_->size_allocate ($w, $h); |
724 | $child->size_allocate ($w, $h); |
|
|
725 | |
624 | $y += $h; |
726 | $y += $h; |
625 | } |
|
|
626 | } |
727 | } |
627 | } |
728 | } |
628 | |
729 | |
629 | ############################################################################# |
730 | ############################################################################# |
630 | |
731 | |
… | |
… | |
633 | our @ISA = CFClient::Widget::; |
734 | our @ISA = CFClient::Widget::; |
634 | |
735 | |
635 | use SDL::OpenGL; |
736 | use SDL::OpenGL; |
636 | |
737 | |
637 | sub new { |
738 | sub new { |
638 | my ($class, $x, $y, $z, $height, $text) = @_; |
739 | my ($class, %arg) = @_; |
639 | |
740 | |
640 | $height ||= $::FONTSIZE; |
|
|
641 | |
|
|
642 | # TODO: color, and make height, xyz etc. optional |
|
|
643 | my $self = $class->SUPER::new ( |
741 | my $self = $class->SUPER::new ( |
644 | x => $x, |
742 | fg => [1, 1, 1], |
645 | y => $y, |
743 | height => $::FONTSIZE, |
646 | z => $z, |
744 | text => "", |
647 | height => $height, |
|
|
648 | layout => new CFClient::Layout, |
745 | layout => new CFClient::Layout, |
|
|
746 | %arg |
649 | ); |
747 | ); |
650 | |
748 | |
651 | $self->set_text ($text); |
749 | $self->set_text ($self->{text}); |
652 | |
750 | |
653 | $self |
751 | $self |
|
|
752 | } |
|
|
753 | |
|
|
754 | sub escape_text { |
|
|
755 | local $_ = $_[1]; |
|
|
756 | |
|
|
757 | s/&/&/g; |
|
|
758 | s/>/>/g; |
|
|
759 | s/</</g; |
|
|
760 | |
|
|
761 | $_[1] |
654 | } |
762 | } |
655 | |
763 | |
656 | sub set_text { |
764 | sub set_text { |
657 | my ($self, $text) = @_; |
765 | my ($self, $text) = @_; |
658 | |
766 | |
659 | $self->{text} = $text; |
767 | $self->{text} = $text; |
660 | $self->{layout}->set_markup ($text); |
768 | $self->{layout}->set_markup ($text); |
661 | |
769 | |
662 | delete $self->{texture}; |
770 | delete $self->{texture}; |
|
|
771 | $self->update; |
663 | } |
772 | } |
664 | |
773 | |
665 | sub get_text { |
774 | sub get_text { |
666 | my ($self, $text) = @_; |
775 | my ($self, $text) = @_; |
667 | |
776 | |
… | |
… | |
670 | |
779 | |
671 | sub size_request { |
780 | sub size_request { |
672 | my ($self) = @_; |
781 | my ($self) = @_; |
673 | |
782 | |
674 | $self->{layout}->set_width; |
783 | $self->{layout}->set_width; |
|
|
784 | $self->{layout}->set_height ($self->{height}); |
675 | $self->{layout}->size |
785 | $self->{layout}->size |
676 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
786 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
677 | # ( |
787 | # ( |
678 | # $self->{texture}{width}, |
788 | # $self->{texture}{width}, |
679 | # $self->{texture}{height}, |
789 | # $self->{texture}{height}, |
… | |
… | |
686 | } |
796 | } |
687 | |
797 | |
688 | sub size_allocate { |
798 | sub size_allocate { |
689 | my ($self, $w, $h) = @_; |
799 | my ($self, $w, $h) = @_; |
690 | |
800 | |
|
|
801 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
802 | |
691 | $self->SUPER::size_allocate ($w, $h); |
803 | $self->SUPER::size_allocate ($w, $h); |
692 | delete $self->{texture}; |
804 | delete $self->{texture}; |
|
|
805 | } |
|
|
806 | |
|
|
807 | sub update { |
|
|
808 | my ($self) = @_; |
|
|
809 | |
|
|
810 | delete $self->{texture}; |
|
|
811 | $self->SUPER::update; |
693 | } |
812 | } |
694 | |
813 | |
695 | sub _draw { |
814 | sub _draw { |
696 | my ($self) = @_; |
815 | my ($self) = @_; |
697 | |
816 | |
… | |
… | |
703 | glEnable GL_BLEND; |
822 | glEnable GL_BLEND; |
704 | glEnable GL_TEXTURE_2D; |
823 | glEnable GL_TEXTURE_2D; |
705 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
824 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
706 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
825 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
707 | |
826 | |
708 | glColor 1, 0, 0, 1; # TODO color |
827 | glColor @{$self->{fg}}; |
709 | |
828 | |
710 | $tex->draw_quad (0, 0); |
829 | $tex->draw_quad (0, 0); |
711 | |
830 | |
712 | glDisable GL_BLEND; |
831 | glDisable GL_BLEND; |
713 | glDisable GL_TEXTURE_2D; |
832 | glDisable GL_TEXTURE_2D; |
… | |
… | |
720 | our @ISA = CFClient::Widget::Label::; |
839 | our @ISA = CFClient::Widget::Label::; |
721 | |
840 | |
722 | use SDL; |
841 | use SDL; |
723 | use SDL::OpenGL; |
842 | use SDL::OpenGL; |
724 | |
843 | |
|
|
844 | sub new { |
|
|
845 | my $class = shift; |
|
|
846 | |
|
|
847 | $class->SUPER::new ( |
|
|
848 | fg => [1, 1, 1], |
|
|
849 | bg => [0, 0, 0, 0.4], |
|
|
850 | active_bg => [1, 1, 1], |
|
|
851 | active_fg => [0, 0, 0], |
|
|
852 | @_ |
|
|
853 | ) |
|
|
854 | } |
|
|
855 | |
|
|
856 | sub _set_text { |
|
|
857 | my ($self, $text) = @_; |
|
|
858 | |
|
|
859 | $self->{last_activity} = $::NOW; |
|
|
860 | |
|
|
861 | $self->{text} = $text; |
|
|
862 | $self->{layout}->set_width ($self->{w}); |
|
|
863 | $self->{layout}->set_markup ($self->escape_text ($text)); |
|
|
864 | |
|
|
865 | $text = substr $text, 0, $self->{cursor}; |
|
|
866 | utf8::encode $text; |
|
|
867 | |
|
|
868 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
869 | } |
|
|
870 | |
|
|
871 | sub size_request { |
|
|
872 | my ($self) = @_; |
|
|
873 | |
|
|
874 | my ($w, $h) = $self->SUPER::size_request; |
|
|
875 | |
|
|
876 | ($w + 1, $h) # add 1 for cursor |
|
|
877 | } |
|
|
878 | |
|
|
879 | sub size_allocate { |
|
|
880 | my ($self, $w, $h) = @_; |
|
|
881 | |
|
|
882 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
883 | |
|
|
884 | $self->SUPER::size_allocate ($w, $h); |
|
|
885 | |
|
|
886 | $self->_set_text ($self->{text}); |
|
|
887 | } |
|
|
888 | |
|
|
889 | sub set_text { |
|
|
890 | my ($self, $text) = @_; |
|
|
891 | |
|
|
892 | $self->{cursor} = length $text; |
|
|
893 | $self->_set_text ($text); |
|
|
894 | $self->update; |
|
|
895 | } |
|
|
896 | |
725 | sub key_down { |
897 | sub key_down { |
726 | my ($self, $ev) = @_; |
898 | my ($self, $ev) = @_; |
727 | |
899 | |
728 | my $mod = $ev->key_mod; |
900 | my $mod = $ev->key_mod; |
729 | my $sym = $ev->key_sym; |
901 | my $sym = $ev->key_sym; |
… | |
… | |
731 | my $uni = $ev->key_unicode; |
903 | my $uni = $ev->key_unicode; |
732 | |
904 | |
733 | my $text = $self->get_text; |
905 | my $text = $self->get_text; |
734 | |
906 | |
735 | if ($sym == SDLK_BACKSPACE) { |
907 | if ($sym == SDLK_BACKSPACE) { |
736 | substr $text, -1, 1, ''; |
908 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
|
|
909 | } elsif ($sym == SDLK_DELETE) { |
|
|
910 | substr $text, $self->{cursor}, 1, ""; |
|
|
911 | } elsif ($sym == SDLK_LEFT) { |
|
|
912 | --$self->{cursor} if $self->{cursor}; |
|
|
913 | } elsif ($sym == SDLK_RIGHT) { |
|
|
914 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
737 | } elsif ($uni) { |
915 | } elsif ($uni) { |
738 | $text .= chr $uni; |
916 | substr $text, $self->{cursor}++, 0, chr $uni; |
739 | } |
917 | } |
740 | |
918 | |
741 | $self->set_text ($text); |
919 | $self->_set_text ($text); |
|
|
920 | $self->update; |
|
|
921 | } |
|
|
922 | |
|
|
923 | sub focus_in { |
|
|
924 | my ($self) = @_; |
|
|
925 | |
|
|
926 | $self->{last_activity} = $::NOW; |
|
|
927 | |
|
|
928 | $self->SUPER::focus_in; |
742 | } |
929 | } |
743 | |
930 | |
744 | sub button_down { |
931 | sub button_down { |
745 | my ($self, $ev) = @_; |
932 | my ($self, $ev, $x, $y) = @_; |
746 | |
933 | |
747 | $self->focus_in; |
934 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
935 | |
|
|
936 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
|
|
937 | |
|
|
938 | # byte-index to char-index |
|
|
939 | my $text = $self->{layout}; |
|
|
940 | utf8::encode $text; |
|
|
941 | $self->{cursor} = length substr $text, 0, $idx; |
|
|
942 | |
|
|
943 | $self->_set_text ($self->{text}); |
|
|
944 | $self->update; |
748 | } |
945 | } |
749 | |
946 | |
750 | sub mouse_motion { |
947 | sub mouse_motion { |
751 | my ($self, $ev, $x, $y) = @_; |
948 | my ($self, $ev, $x, $y) = @_; |
752 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
949 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
753 | } |
950 | } |
754 | |
951 | |
755 | sub _draw { |
952 | sub _draw { |
756 | my ($self) = @_; |
953 | my ($self) = @_; |
757 | |
954 | |
|
|
955 | local $self->{fg} = $self->{fg}; |
|
|
956 | |
758 | if ($FOCUS == $self) { |
957 | if ($FOCUS == $self) { |
759 | glColor 1, 1, 1; |
958 | glColor @{$self->{active_bg}}; |
|
|
959 | $self->{fg} = $self->{active_fg}; |
760 | } else { |
960 | } else { |
761 | glColor 0.7, 0.7, 0.7; |
961 | glColor @{$self->{bg}}; |
762 | } |
962 | } |
763 | |
963 | |
764 | glBegin GL_QUADS; |
964 | glBegin GL_QUADS; |
765 | glVertex 0 , 0; |
965 | glVertex 0 , 0; |
766 | glVertex 0 , $self->{h} - 1; |
966 | glVertex 0 , $self->{h}; |
767 | glVertex $self->{w} - 1, $self->{h} - 1; |
967 | glVertex $self->{w}, $self->{h}; |
768 | glVertex $self->{w} - 1, 0; |
968 | glVertex $self->{w}, 0; |
769 | glEnd; |
969 | glEnd; |
770 | |
970 | |
771 | $self->SUPER::_draw; |
971 | $self->SUPER::_draw; |
|
|
972 | |
|
|
973 | #TODO: force update every cursor change :( |
|
|
974 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
975 | glColor @{$self->{fg}}; |
|
|
976 | glBegin GL_LINES; |
|
|
977 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
978 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
|
|
979 | glEnd; |
|
|
980 | } |
|
|
981 | } |
|
|
982 | |
|
|
983 | ############################################################################# |
|
|
984 | |
|
|
985 | package CFClient::Widget::Slider; |
|
|
986 | |
|
|
987 | use strict; |
|
|
988 | |
|
|
989 | use SDL::OpenGL; |
|
|
990 | use SDL::OpenGL::Constants; |
|
|
991 | |
|
|
992 | our @ISA = CFClient::Widget::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 | |
|
|
1003 | sub new { |
|
|
1004 | my $class = shift; |
|
|
1005 | |
|
|
1006 | # range [value, low, high, page] |
|
|
1007 | |
|
|
1008 | $class->SUPER::new ( |
|
|
1009 | fg => [1, 1, 1], |
|
|
1010 | active_fg => [0, 0, 0], |
|
|
1011 | range => [0, 0, 100, 10], |
|
|
1012 | vertical => 1, |
|
|
1013 | @_ |
|
|
1014 | ) |
|
|
1015 | } |
|
|
1016 | |
|
|
1017 | sub button_down { |
|
|
1018 | my ($self, $ev, $x, $y) = @_; |
|
|
1019 | |
|
|
1020 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
1021 | $self->mouse_motion ($ev, $x, $y); |
|
|
1022 | } |
|
|
1023 | |
|
|
1024 | sub mouse_motion { |
|
|
1025 | my ($self, $ev, $x, $y) = @_; |
|
|
1026 | |
|
|
1027 | if ($GRAB == $self) { |
|
|
1028 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1029 | |
|
|
1030 | $x = $x * ($hi - $lo) / $self->{w} + $lo; |
|
|
1031 | $x = $lo if $x < $lo; |
|
|
1032 | $x = $hi - $page if $x > $hi - $page; |
|
|
1033 | $self->{range}[0] = $x; |
|
|
1034 | |
|
|
1035 | $self->{changed}($x) if $self->{changed}; |
|
|
1036 | $self->update; |
|
|
1037 | } |
|
|
1038 | } |
|
|
1039 | |
|
|
1040 | sub _draw { |
|
|
1041 | my ($self) = @_; |
|
|
1042 | |
|
|
1043 | $self->SUPER::_draw (); |
|
|
1044 | |
|
|
1045 | glPushMatrix; |
|
|
1046 | |
|
|
1047 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1048 | |
|
|
1049 | if ($self->{vertical}) { |
|
|
1050 | # draw a vertical slider like a rotated horizontal slider |
|
|
1051 | |
|
|
1052 | glTranslate 0, $self->{w}; |
|
|
1053 | glRotate 90, 0, 0, 1; |
|
|
1054 | |
|
|
1055 | ($w, $h) = ($h, $w); |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
|
|
1059 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
|
|
1060 | |
|
|
1061 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1062 | |
|
|
1063 | $page = int $page * $w / ($hi - $lo); |
|
|
1064 | $value = int +($value - $lo) * $w / ($hi - $lo); |
|
|
1065 | |
|
|
1066 | $w -= $page; |
|
|
1067 | $page &= ~1; |
|
|
1068 | glTranslate $page * 0.5, 0, 0; |
|
|
1069 | |
|
|
1070 | glColor @$fg; |
|
|
1071 | glBegin GL_LINES; |
|
|
1072 | glVertex 0, 0; glVertex 0, $h; |
|
|
1073 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1074 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1075 | glEnd; |
|
|
1076 | |
|
|
1077 | my $knob_a = $value - $page * 0.5; |
|
|
1078 | my $knob_b = $value + $page * 0.5; |
|
|
1079 | |
|
|
1080 | glBegin GL_QUADS; |
|
|
1081 | glColor @$fg; |
|
|
1082 | glVertex $knob_a, 0; |
|
|
1083 | glVertex $knob_a, $h; |
|
|
1084 | glVertex $knob_b, $h; |
|
|
1085 | glVertex $knob_b, 0; |
|
|
1086 | |
|
|
1087 | if ($knob_a < $knob_b - 2) { |
|
|
1088 | glColor @$bg; |
|
|
1089 | glVertex $knob_a + 1, 1; |
|
|
1090 | glVertex $knob_a + 1, $h - 1; |
|
|
1091 | glVertex $knob_b - 1, $h - 1; |
|
|
1092 | glVertex $knob_b - 1, 1; |
|
|
1093 | } |
|
|
1094 | glEnd; |
|
|
1095 | |
|
|
1096 | glPopMatrix; |
772 | } |
1097 | } |
773 | |
1098 | |
774 | ############################################################################# |
1099 | ############################################################################# |
775 | |
1100 | |
776 | package CFClient::Widget::MapWidget; |
1101 | package CFClient::Widget::MapWidget; |
… | |
… | |
782 | use SDL; |
1107 | use SDL; |
783 | use SDL::OpenGL; |
1108 | use SDL::OpenGL; |
784 | use SDL::OpenGL::Constants; |
1109 | use SDL::OpenGL::Constants; |
785 | |
1110 | |
786 | our @ISA = CFClient::Widget::; |
1111 | our @ISA = CFClient::Widget::; |
|
|
1112 | |
|
|
1113 | sub new { |
|
|
1114 | my $class = shift; |
|
|
1115 | |
|
|
1116 | $class->SUPER::new ( |
|
|
1117 | z => -1, |
|
|
1118 | list => (glGenLists 1), |
|
|
1119 | @_ |
|
|
1120 | ) |
|
|
1121 | } |
787 | |
1122 | |
788 | sub key_down { |
1123 | sub key_down { |
789 | print "MAPKEYDOWN\n"; |
1124 | print "MAPKEYDOWN\n"; |
790 | } |
1125 | } |
791 | |
1126 | |
… | |
… | |
797 | 1 + int $::WIDTH / 32, |
1132 | 1 + int $::WIDTH / 32, |
798 | 1 + int $::HEIGHT / 32, |
1133 | 1 + int $::HEIGHT / 32, |
799 | ) |
1134 | ) |
800 | } |
1135 | } |
801 | |
1136 | |
|
|
1137 | sub update { |
|
|
1138 | my ($self) = @_; |
|
|
1139 | |
|
|
1140 | $self->{need_update} = 1; |
|
|
1141 | } |
|
|
1142 | |
802 | sub _draw { |
1143 | sub _draw { |
803 | my ($self) = @_; |
1144 | my ($self) = @_; |
804 | |
1145 | |
|
|
1146 | if (delete $self->{need_update}) { |
|
|
1147 | glNewList $self->{list}, GL_COMPILE; |
|
|
1148 | |
805 | my $mx = $::CONN->{mapx}; |
1149 | my $mx = $::CONN->{mapx}; |
806 | my $my = $::CONN->{mapy}; |
1150 | my $my = $::CONN->{mapy}; |
807 | |
1151 | |
808 | my $map = $::CONN->{map}; |
1152 | my $map = $::CONN->{map}; |
809 | |
1153 | |
810 | my ($xofs, $yofs); |
1154 | my ($xofs, $yofs); |
811 | |
1155 | |
812 | my $sw = 1 + int $::WIDTH / 32; |
1156 | my $sw = 1 + int $::WIDTH / 32; |
813 | my $sh = 1 + int $::HEIGHT / 32; |
1157 | my $sh = 1 + int $::HEIGHT / 32; |
814 | |
1158 | |
815 | if ($::CONN->{mapw} > $sw) { |
1159 | if ($::CONN->{mapw} > $sw) { |
816 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
1160 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
817 | } else { |
1161 | } else { |
818 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
1162 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
819 | } |
1163 | } |
820 | |
1164 | |
821 | if ($::CONN->{maph} > $sh) { |
1165 | if ($::CONN->{maph} > $sh) { |
822 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
1166 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
823 | } else { |
1167 | } else { |
824 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1168 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
825 | } |
1169 | } |
826 | |
1170 | |
827 | glEnable GL_TEXTURE_2D; |
1171 | glEnable GL_TEXTURE_2D; |
828 | glEnable GL_BLEND; |
1172 | glEnable GL_BLEND; |
829 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1173 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
830 | |
1174 | |
831 | my $sw4 = ($sw + 3) & ~3; |
1175 | my $sw4 = ($sw + 3) & ~3; |
832 | my $darkness = "\x00" x ($sw4 * $sh); |
1176 | my $darkness = "\x00" x ($sw4 * $sh); |
833 | |
1177 | |
834 | for my $x (0 .. $sw - 1) { |
1178 | for my $x (0 .. $sw - 1) { |
835 | my $row = $map->[$x + $xofs]; |
1179 | my $row = $map->[$x + $xofs]; |
836 | for my $y (0 .. $sh - 1) { |
1180 | for my $y (0 .. $sh - 1) { |
837 | |
1181 | |
838 | my $cell = $row->[$y + $yofs] |
1182 | my $cell = $row->[$y + $yofs] |
839 | or next; |
1183 | or next; |
840 | |
1184 | |
841 | my $dark = $cell->[0]; |
1185 | my $dark = $cell->[0]; |
842 | if ($dark < 0) { |
1186 | if ($dark < 0) { |
843 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
1187 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
844 | } else { |
1188 | } else { |
845 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
1189 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
846 | } |
1190 | } |
847 | |
1191 | |
848 | for my $num (grep $_, @$cell[1,2,3]) { |
1192 | for my $num (grep $_, @$cell[1,2,3]) { |
849 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1193 | my $tex = $::CONN->{face}[$num]{texture} || next; |
850 | |
1194 | |
851 | my $w = $tex->{width}; |
1195 | my $w = $tex->{width}; |
852 | my $h = $tex->{height}; |
1196 | my $h = $tex->{height}; |
853 | |
1197 | |
854 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1198 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
|
|
1199 | } |
855 | } |
1200 | } |
856 | } |
1201 | } |
857 | } |
|
|
858 | |
1202 | |
859 | # if (1) { # higher quality darkness |
1203 | # if (1) { # higher quality darkness |
860 | # $lighting =~ s/(.)/$1$1$1/gs; |
1204 | # $lighting =~ s/(.)/$1$1$1/gs; |
861 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
1205 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
862 | # |
1206 | # |
… | |
… | |
864 | # |
1208 | # |
865 | # $lighting = $pb->get_pixels; |
1209 | # $lighting = $pb->get_pixels; |
866 | # $lighting =~ s/(.)../$1/gs; |
1210 | # $lighting =~ s/(.)../$1/gs; |
867 | # } |
1211 | # } |
868 | |
1212 | |
869 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1213 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
870 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1214 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
871 | |
1215 | |
872 | $darkness = new CFClient::Texture |
1216 | $darkness = new CFClient::Texture |
873 | width => $sw4, |
1217 | width => $sw4, |
874 | height => $sh, |
1218 | height => $sh, |
875 | data => $darkness, |
1219 | data => $darkness, |
876 | internalformat => GL_ALPHA, |
1220 | internalformat => GL_ALPHA, |
877 | format => GL_ALPHA; |
1221 | format => GL_ALPHA; |
878 | |
1222 | |
879 | glColor 0.45, 0.45, 0.45, 1; |
1223 | glColor 0.45, 0.45, 0.45, 1; |
880 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
1224 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
881 | |
1225 | |
882 | glDisable GL_TEXTURE_2D; |
1226 | glDisable GL_TEXTURE_2D; |
883 | glDisable GL_BLEND; |
1227 | glDisable GL_BLEND; |
|
|
1228 | |
|
|
1229 | glEndList; |
|
|
1230 | } |
|
|
1231 | |
|
|
1232 | glCallList $self->{list}; |
884 | } |
1233 | } |
885 | |
1234 | |
886 | my %DIR = ( |
1235 | my %DIR = ( |
887 | SDLK_KP8, [1, "north"], |
1236 | SDLK_KP8, [1, "north"], |
888 | SDLK_KP9, [2, "northeast"], |
1237 | SDLK_KP9, [2, "northeast"], |