File: Vertex.pm

package info (click to toggle)
algotutor 0.8.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 580 kB
  • sloc: perl: 2,563; makefile: 41; php: 24; sh: 1
file content (172 lines) | stat: -rw-r--r-- 4,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Author:  Chao-Kuei Hung
# For more info, including license, please see doc/index.html

package Vertex;
# Vertex of a Graph

use strict;
use Carp;
use vars qw(@ISA);
@ISA = qw(Configurable);

use Configurable;
use Vector2;
use overload
    '""' => 'stringify',
    'fallback' => 1
#    'eq' => '()',
#    'fallback' => undef
;

sub new {
    my ($class, $host, $pos, %opts) = @_;
    $class = ref($class) if ref($class);
    my ($self) = $class->SUPER::new(%opts);
    $self->{"#host"} = $host;
    my ($cv) = $self->host()->cget(-canvas);
    my ($sh) = $self->cget(-shape);
    if ("\L$sh" eq "oval") {
	$self->{shape_id} = $cv->createOval(0,0,2,2);
    } else {
	$self->{shape_id} = $cv->createRectangle(0,0,2,2);
    }
    $self->{text_id} = $cv->createText(0, 0, -justify=>"center");
    $self->set_pos($pos);
    $self->set_size($self->cget(-size));
    $self->configure($self->get_all_opts());
    # the following is needed for easier binding statements
#    $self->{-host}{-canvas}->addtag($self->{-name}, "withtag", $self->{shape_id});
#    $self->{-host}{-canvas}->addtag($self->{-name}, "withtag", $self->{text_id});
    return $self;
}

sub destroy {
    my ($self) = @_;
    $self->host()->cget(-canvas)->delete(@{$self}{"shape_id","text_id"});
}

sub host {
    return $_[0]->{"#host"};
}

sub _get_cv_geom_ {
    my ($self) = @_;
    my (@t) = $self->host()->cget(-canvas)->coords($self->{shape_id});
    return (
	Vector2->new(($t[0] + $t[2]) / 2, ($t[1] + $t[3]) / 2),
	Vector2->new(abs($t[0] - $t[2]), abs($t[1] - $t[3])),
    );
}

sub pos {
    my ($self) = @_;
    croak "you probably wanted to call set_pos()?" if $#_>0;
    my ($lt) = $self->host()->cget(-linear_transform);
    my ($pos, undef) = $self->_get_cv_geom_();
    return ($pos - $lt->{-offset})->pw_div($lt->{-scale});
}

sub size {
    my ($self) = @_;
    croak "you probably wanted to call set_size()?" if $#_>0;
    my ($lt) = $self->host()->cget(-linear_transform);
    my (undef, $size) = $self->_get_cv_geom_();
    return $size->pw_div($lt->{-scale});
}

sub set_pos {
    my ($self, $pos) = @_;
    my ($lt) = $self->host()->cget(-linear_transform);
    my (undef, $size) = $self->_get_cv_geom_();
    $size = $size->pw_div(2);
    $pos = $pos->pw_mul($lt->{-scale}) + $lt->{-offset};
    my ($cv) = $self->host()->cget(-canvas);
    $cv->coords($self->{text_id}, @$pos);
    $cv->coords($self->{shape_id},
	@{ $pos-$size }, @{ $pos+$size }
    );
}

sub set_size {
    my ($self, $size) = @_;
    my ($lt) = $self->host()->cget(-linear_transform);
    my ($pos, undef) = $self->_get_cv_geom_();
    $size = $lt->{-scale}->pw_mul($size)->pw_div(2);
    $self->host()->cget(-canvas)->coords($self->{shape_id},
	@{ $pos-$size }, @{ $pos+$size }
    );
}

sub configure {
    my ($self, %opts) = @_;
    my ($k, %shape_opts, %text_opts);
    my ($opt_map) = {
	-text    => [undef, "-text"],
	-fill    => ["-fill", undef],
	-outline => ["-outline", "-fill"],
	-thick   => ["-width", undef],
	-arrow   => ["-arrow", undef],
	-stipple => ["-stipple", undef],
	-outlinestipple => [undef, undef],
	-state   => ["-state", "-state"],
    };

    if (exists $opts{-name}) {
	$self->{-name} = delete $opts{-name};
	$opts{-text} = $self->cget(-display)->($self)
	    if ref $self->cget(-display) eq "CODE";
    }
    if (exists $opts{-content}) {
	$self->{-content} = delete $opts{-content};
	$opts{-text} = $self->cget(-display)->($self)
	    if ref $self->cget(-display) eq "CODE";
    }
    if (exists $opts{-status}) {
	$self->{-status} = delete $opts{-status};
	my ($ha) = $self->host()->cget(-appearance);
	carp "unknown status $self->{-status} ignored"
	    unless exists $ha->{$self->{-status}};
	%opts = (%{ $ha->{$self->{-status}} }, %opts);
    }
    foreach $k (keys %opts) {
	carp "unknown option $k ignored" unless exists($opt_map->{$k});
	$shape_opts{ $opt_map->{$k}[0] } = $opts{$k}
	    if defined $opt_map->{$k}[0];
	$text_opts{ $opt_map->{$k}[1] } = $opts{$k}
	    if defined $opt_map->{$k}[1];
    }
    my ($cv) = $self->host()->cget(-canvas);
    $cv->itemconfigure($self->{shape_id}, %shape_opts);
    $cv->itemconfigure($self->{text_id}, %text_opts);
}

sub display {
    # serves to print or display a vertex
    my ($self) = @_;
    my ($s) = $self->cget(-display)->($self);
    $s =~ s/\n/ /g;
    return "V[$s]";
}

sub stringify {
    # serves to identify a vertex, such as key for hash
    my ($self) = @_;
    return $self->cget(-name);
}

sub get_all_opts {
    my ($self) = @_;
    my (%opts) = $self->SUPER::get_all_opts();
    delete @opts{qw(-display -shape -size)};
    return %opts;
}

$::Config->{Vertex} = {
    -shape => "oval",
    -size => Vector2->new(50, 30),
    -status => "init",
    -display => sub { return $_[0]->cget(-name); }
};

1;