File source/commands/load.d changed (mode: 100644) (index 454a67f..395d82e) |
1 |
1 |
module dp.command.load; |
module dp.command.load; |
2 |
2 |
|
|
3 |
3 |
import dp.command.cmd; |
import dp.command.cmd; |
4 |
|
import dp.ent.shape; |
|
|
4 |
|
import dp.ent.entity; |
5 |
5 |
import dp.visitor.save; |
import dp.visitor.save; |
6 |
6 |
|
|
7 |
7 |
import Global = dp.global; |
import Global = dp.global; |
8 |
8 |
|
|
9 |
9 |
public class LoadCmd : Command { |
public class LoadCmd : Command { |
10 |
|
private Shape[] shapes; |
|
|
10 |
|
private Entity[] entities; |
11 |
11 |
|
|
12 |
|
this(Shape[] shapes) { |
|
13 |
|
this.shapes = shapes; |
|
|
12 |
|
this(Entity[] entities) { |
|
13 |
|
this.entities = entities; |
14 |
14 |
execute(); // Obsolete, but remained for consistency between 'Command' classes. |
execute(); // Obsolete, but remained for consistency between 'Command' classes. |
15 |
15 |
} |
} |
16 |
16 |
|
|
17 |
17 |
public override void execute() { |
public override void execute() { |
18 |
|
foreach(s; shapes) |
|
19 |
|
s.active = true; |
|
|
18 |
|
foreach(e; entities) |
|
19 |
|
e.active = true; |
20 |
20 |
} |
} |
21 |
21 |
|
|
22 |
22 |
public override void undo() { |
public override void undo() { |
23 |
|
foreach(s; shapes) |
|
24 |
|
s.active = false; |
|
|
23 |
|
foreach(e; entities) |
|
24 |
|
e.active = false; |
25 |
25 |
} |
} |
26 |
26 |
|
|
27 |
27 |
public override void render() { |
public override void render() { |
28 |
|
foreach(s; shapes) |
|
29 |
|
s.render(); |
|
|
28 |
|
foreach(e; entities) |
|
29 |
|
e.render(); |
30 |
30 |
} |
} |
31 |
31 |
|
|
32 |
32 |
public override void check(int x, int y) { |
public override void check(int x, int y) { |
33 |
|
foreach(s; shapes) |
|
34 |
|
s.checkBounds(x,y); |
|
|
33 |
|
foreach(e; entities) |
|
34 |
|
e.checkBounds(x,y); |
35 |
35 |
} |
} |
36 |
36 |
|
|
37 |
37 |
// Should be done by Visitor self. |
// Should be done by Visitor self. |
38 |
38 |
public override void save(Savefile file) { |
public override void save(Savefile file) { |
39 |
|
foreach(s; shapes) { |
|
40 |
|
if(s.group is null) |
|
41 |
|
s.accept(new SaveVisitor(file)); |
|
|
39 |
|
foreach(e; entities) { |
|
40 |
|
if(e.group is null) |
|
41 |
|
e.accept(new SaveVisitor(file)); |
42 |
42 |
} |
} |
43 |
43 |
} |
} |
44 |
44 |
} |
} |
File source/file.d changed (mode: 100644) (index 6994ac5..84876e4) |
... |
... |
import std.conv; |
6 |
6 |
import std.array; |
import std.array; |
7 |
7 |
import std.string; |
import std.string; |
8 |
8 |
|
|
9 |
|
import dp.ent.entity, dp.ent.group, dp.ent.shape; |
|
|
9 |
|
import dp.ent.entity, dp.ent.group, dp.ent.shape, dp.ent.ornament; |
|
10 |
|
import dp.dec.entornament; |
10 |
11 |
import dp.strat.rect; |
import dp.strat.rect; |
11 |
12 |
import dp.strat.circle; |
import dp.strat.circle; |
12 |
13 |
import dp.strat.ellipse; |
import dp.strat.ellipse; |
|
... |
... |
public class Savefile { |
29 |
30 |
} |
} |
30 |
31 |
|
|
31 |
32 |
public void load() { |
public void load() { |
32 |
|
Entity e; |
|
33 |
|
Shape[] entities = []; |
|
|
33 |
|
Entity[] entities = []; |
34 |
34 |
|
|
35 |
35 |
Global.Groups.clear(); |
Global.Groups.clear(); |
36 |
36 |
File f = File(filename, "r"); |
File f = File(filename, "r"); |
37 |
|
e = fromFile(f, entities); |
|
38 |
|
|
|
39 |
|
while(e !is null) |
|
40 |
|
e = fromFile(f, entities); |
|
|
37 |
|
fromFile(f, entities); |
41 |
38 |
|
|
42 |
39 |
f.close(); |
f.close(); |
43 |
40 |
|
|
|
... |
... |
public class Savefile { |
54 |
51 |
content = ""; |
content = ""; |
55 |
52 |
} |
} |
56 |
53 |
|
|
57 |
|
private Shape parseLine(string line) { |
|
58 |
|
auto words = line.split(); |
|
59 |
|
|
|
60 |
|
writeln(words); |
|
|
54 |
|
private EntityOrnament parseShape(string shape, string x, string y, string size) { |
|
55 |
|
Shape s; |
61 |
56 |
|
|
62 |
|
final switch(words[0]) { |
|
|
57 |
|
final switch(shape) { |
63 |
58 |
case "rectangle": |
case "rectangle": |
64 |
|
return new Shape ( |
|
65 |
|
to!int(words[1]), |
|
66 |
|
to!int(words[2]), |
|
67 |
|
Global.canvas.newContext, |
|
68 |
|
rect, |
|
69 |
|
to!double(words[3]) |
|
70 |
|
); |
|
|
59 |
|
s = new Shape ( |
|
60 |
|
to!int(x), |
|
61 |
|
to!int(y), |
|
62 |
|
Global.canvas.newContext, |
|
63 |
|
rect, |
|
64 |
|
to!double(size) |
|
65 |
|
); |
|
66 |
|
break; |
71 |
67 |
case "circle": |
case "circle": |
72 |
|
return new Shape ( |
|
73 |
|
to!int(words[1]), |
|
74 |
|
to!int(words[2]), |
|
75 |
|
Global.canvas.newContext, |
|
76 |
|
circle, |
|
77 |
|
to!double(words[3]) |
|
78 |
|
); |
|
|
68 |
|
s = new Shape ( |
|
69 |
|
to!int(x), |
|
70 |
|
to!int(y), |
|
71 |
|
Global.canvas.newContext, |
|
72 |
|
circle, |
|
73 |
|
to!double(size) |
|
74 |
|
); |
|
75 |
|
break; |
79 |
76 |
case "ellipse": |
case "ellipse": |
80 |
|
return new Shape ( |
|
81 |
|
to!int(words[1]), |
|
82 |
|
to!int(words[2]), |
|
83 |
|
Global.canvas.newContext, |
|
84 |
|
ellipse, |
|
85 |
|
to!double(words[3]) |
|
86 |
|
); |
|
|
77 |
|
s = new Shape ( |
|
78 |
|
to!int(x), |
|
79 |
|
to!int(y), |
|
80 |
|
Global.canvas.newContext, |
|
81 |
|
ellipse, |
|
82 |
|
to!double(size) |
|
83 |
|
); |
|
84 |
|
break; |
87 |
85 |
} |
} |
|
86 |
|
return new EntityOrnament(s); |
88 |
87 |
} |
} |
89 |
88 |
|
|
90 |
|
private Entity fromFile(ref File f, ref Shape[] shapes) { |
|
91 |
|
int depth = 0; |
|
92 |
|
Group group; |
|
|
89 |
|
private void fromFile(ref File f, ref Entity[] entities) { |
|
90 |
|
string type; |
93 |
91 |
string line; |
string line; |
94 |
92 |
|
|
95 |
|
if(f.eof) return null; |
|
96 |
|
line = strip(f.readln()); |
|
97 |
|
if(line == "") return null; |
|
98 |
|
|
|
99 |
|
depth = groupsize(line); |
|
100 |
|
|
|
101 |
|
if(depth > 0) { |
|
102 |
|
group = Global.Groups.newGroup; |
|
103 |
|
|
|
104 |
|
for(int i = 0; i < depth; i++) { |
|
105 |
|
Entity e = fromFile(f,shapes); |
|
106 |
|
if(e is null) return group; |
|
107 |
|
else group.add(e); |
|
|
93 |
|
while(!f.eof) { |
|
94 |
|
line = strip(f.readln()); |
|
95 |
|
if(line == "") return; |
|
96 |
|
|
|
97 |
|
entities.length++; |
|
98 |
|
auto words = line.split(); |
|
99 |
|
|
|
100 |
|
switch(words[0]) { |
|
101 |
|
case "group": |
|
102 |
|
Entity[] e; |
|
103 |
|
entities[entities.length - 1] = Global.Groups.newGroup; |
|
104 |
|
fromFile(f,e); |
|
105 |
|
foreach(entity; e) |
|
106 |
|
(cast(Group)entities[entities.length - 1]).add(entity); |
|
107 |
|
writeln("F: group"); |
|
108 |
|
break; |
|
109 |
|
case "rectangle": |
|
110 |
|
case "circle": |
|
111 |
|
case "ellipse": |
|
112 |
|
entities[entities.length - 1] = parseShape( |
|
113 |
|
words[0], words[1], words[2], words[3] |
|
114 |
|
); |
|
115 |
|
writeln("F: shape"); |
|
116 |
|
break; |
|
117 |
|
case "ornament": |
|
118 |
|
EntityOrnament eo = new EntityOrnament(entities[entities.length - 2]); |
|
119 |
|
eo.setOrnament(new Ornament(eo, words[1], words[2], Global.canvas.newContext)); |
|
120 |
|
entities[entities.length - 1] = eo; |
|
121 |
|
writeln("F: ornament"); |
|
122 |
|
break; |
|
123 |
|
default: |
|
124 |
|
writeln("ERROR: unsupported type in 'dp.file.fromFile'!"); |
|
125 |
|
writeln("Loading aborted."); |
|
126 |
|
return; |
108 |
127 |
} |
} |
109 |
|
|
|
110 |
|
return group; |
|
111 |
|
} else { |
|
112 |
|
shapes.length++; |
|
113 |
|
shapes[shapes.length - 1] = parseLine(line); |
|
114 |
|
write("SHAPES: "); |
|
115 |
|
writeln(shapes); |
|
116 |
|
return shapes[shapes.length - 1]; |
|
117 |
128 |
} |
} |
118 |
129 |
} |
} |
119 |
|
|
|
120 |
|
private int groupsize(string line) { |
|
121 |
|
auto words = line.split(); |
|
122 |
|
|
|
123 |
|
if(words[0] != "group") |
|
124 |
|
return 0; |
|
125 |
|
|
|
126 |
|
return to!int(words[1]); |
|
127 |
|
} |
|
128 |
130 |
} |
} |