kapstok / NHL-DePa2 (public) (License: Unspecified) (since 2018-11-09) (hash sha1)
Design Patterns school project; a GUI using multiple Design Patterns.
List of commits:
Subject Hash Author Date (UTC)
Implement Visitor pattern for Shapes. e52412c3059c1ee5fa9abdf4a32db0f47981eee1 Jan Allersma 2018-10-16 12:43:17
Use different strats instead of seperate Shapes. a56e1e665a0292d15139eddfd8ebdeb8c95251b4 Jan Allersma 2018-10-01 11:37:45
Fix bug in Rect from commit 6c91140 in other Shapes as well. 4165a5da98dd9a548a222c6160390e4e54026d48 Jan Allersma 2018-09-28 14:53:40
Fully implement File I/O. 45cbb48489a24857cfc76144b38b12ccebfd9e61 Jan Allersma 2018-09-27 16:03:40
Fix GroupMenu bug. be41e51819be1a23e7761ea0662eb188ac641aca Jan Allersma 2018-09-27 14:55:20
Fix TODO's from previous commit. 89d8172b77336a6c18f30521147eb81287961f11 Jan Allersma 2018-09-27 14:06:30
Add groups as preparation for step 3. a91368128e7444f69b3a30ee4d17f9063c8846b8 Jan Allersma 2018-09-26 18:31:49
Fix minor bug. Small cleanup 'ellipse' code. 6c911405eb8e48ffe2d63aaff5255adbd4aec889 Jan Allersma 2018-09-21 18:10:01
Implement file I/O. 6219eca932d454a7dab5fe8679f07865ce3cf49d Jan Allersma 2018-09-21 15:52:45
Add shape 'ellipse'. 99d8b029c769bc291af73df3fd445da3cbf5bef4 Jan Allersma 2018-09-21 10:44:36
Add shape 'circle'. 092cb3c3f788b461192a6e164955c53acaf58720 Jan Allersma 2018-09-21 09:35:27
Add resize option (again). 08c95e97268cf9d74bcb963503eec512e528edfe Jan Allersma 2018-09-20 18:09:03
Fix undo bug. 6ab126be633075923cdcce53044e2468f967db41 Jan Allersma 2018-09-20 10:46:50
Implement 'delete shape' feature. 16d42472d44a9dae988c4745c90c791a8770541b Jan Allersma 2018-09-19 09:04:40
Fix undo/redo for 'MoveCmd'. be51bea021bfe5ff79e16a92d350677aed736ddd Jan Allersma 2018-09-19 08:49:55
Implement shapeOptions without undo/redo. bbe699f41e5dacbfdadfaa879452ac2f4501f5a1 Jan Allersma 2018-09-18 19:44:44
WIP: Restore 'shapeOptions.d'. 888b8ea647c0f25cfbeefcd97ee7ebb59e7f0751 Jan Allersma 2018-09-18 15:46:05
Rebuild project from scratch. df0f2f82a86581ba9fa3a169d63a950229341a9f Jan Allersma 2018-09-18 15:04:32
Add resize option. 961e464090918d83690ff66838f2fc96c6ad213a Jan Allersma 2018-09-13 18:29:44
Make `MoveCmd` Command-oriented. 2970a8d862285752551be2464422375ca958357f Jan Allersma 2018-09-12 15:33:17
Commit e52412c3059c1ee5fa9abdf4a32db0f47981eee1 - Implement Visitor pattern for Shapes.
Author: Jan Allersma
Author date (UTC): 2018-10-16 12:43
Committer name: Jan Allersma
Committer date (UTC): 2018-10-16 13:53
Parent(s): a56e1e665a0292d15139eddfd8ebdeb8c95251b4
Signing key:
Tree: 7ad38d353325d6606f8e61866036366261aaf0b8
File Lines added Lines deleted
README.md 0 4
source/commands/create.d 3 1
source/commands/load.d 3 1
source/commands/move.d 5 2
source/commands/resize.d 5 2
source/entities/shape.d 9 41
source/strategies/circle.d 3 3
source/strategies/ellipse.d 3 3
source/strategies/rect.d 4 4
source/strategies/strategy.d 1 3
source/vistors/moveVisitor.d 19 0
source/vistors/resizeVisitor.d 35 0
source/vistors/saveVisitor.d 16 0
source/vistors/visitor.d 7 0
File README.md changed (mode: 100644) (index cda57e6..ce894fb)
... ... use 'dub' to compile and run the code.
31 31 maar subgroepen niet. maar subgroepen niet.
32 32
33 33 - Groups worden nog niet goed geladen: ze verliezen hun kleurtje. - Groups worden nog niet goed geladen: ze verliezen hun kleurtje.
34
35 # Vragen
36
37 - Is het Visitor pattern niet al geimplementeerd met Command als Visitor?
File source/commands/create.d changed (mode: 100644) (index ab1e0a4..797b0fe)
... ... module dp.command.create;
2 2
3 3 import dp.command.cmd; import dp.command.cmd;
4 4 import dp.ent.shape; import dp.ent.shape;
5 import dp.visitor.save;
5 6
6 7 import Global = dp.global; import Global = dp.global;
7 8
 
... ... public class CreateCmd : Command {
29 30 s.checkBounds(x,y); s.checkBounds(x,y);
30 31 } }
31 32
33 // Should be done by Visitor self.
32 34 public override void save(Savefile file) { public override void save(Savefile file) {
33 35 if(s.saveable) if(s.saveable)
34 file.toFile(s);
36 s.accept(new SaveVisitor(file));
35 37 } }
36 38 } }
File source/commands/load.d changed (mode: 100644) (index b09fa76..e2536b6)
... ... module dp.command.load;
2 2
3 3 import dp.command.cmd; import dp.command.cmd;
4 4 import dp.ent.shape; import dp.ent.shape;
5 import dp.visitor.save;
5 6
6 7 import Global = dp.global; import Global = dp.global;
7 8
 
... ... public class LoadCmd : Command {
33 34 s.checkBounds(x,y); s.checkBounds(x,y);
34 35 } }
35 36
37 // Should be done by Visitor self.
36 38 public override void save(Savefile file) { public override void save(Savefile file) {
37 39 foreach(s; shapes) { foreach(s; shapes) {
38 40 if(s.saveable) if(s.saveable)
39 file.toFile(s);
41 s.accept(new SaveVisitor(file));
40 42 } }
41 43 } }
42 44 } }
File source/commands/move.d changed (mode: 100644) (index 44446c4..6f9a3ae)
... ... module dp.command.move;
2 2
3 3 import dp.command.cmd; import dp.command.cmd;
4 4 import dp.ent.shape; import dp.ent.shape;
5 import dp.visitor.move;
5 6
6 7 import Global = dp.global; import Global = dp.global;
7 8
 
... ... public class MoveCmd : Command {
18 19 } }
19 20
20 21 public override void execute() { public override void execute() {
21 s.move(newPos[0], newPos[1]);
22 //s.move(newPos[0], newPos[1]);
23 s.accept(new MoveVisitor(newPos));
22 24 } }
23 25
24 26 public override void undo() { public override void undo() {
25 s.move(oldPos[0], oldPos[1]);
27 //move(oldPos[0], oldPos[1]);
28 s.accept(new MoveVisitor(oldPos));
26 29 } }
27 30
28 31 public override void render() { public override void render() {
File source/commands/resize.d changed (mode: 100644) (index dfb73ac..34a8eb6)
... ... module dp.command.resize;
2 2
3 3 import dp.command.cmd; import dp.command.cmd;
4 4 import dp.ent.shape; import dp.ent.shape;
5 import dp.visitor.resize;
5 6
6 7 import Global = dp.global; import Global = dp.global;
7 8
 
... ... public class ResizeCmd : Command {
16 17 } }
17 18
18 19 public override void execute() { public override void execute() {
19 s.resize(amount);
20 //s.resize(amount);
21 s.accept(new ResizeVisitor(amount));
20 22 } }
21 23
22 24 public override void undo() { public override void undo() {
23 s.resize(-amount);
25 //s.resize(-amount);
26 s.accept(new ResizeVisitor(-amount));
24 27 } }
25 28
26 29 public override void render() { public override void render() {
File source/entities/shape.d changed (mode: 100644) (index cd6c655..9b25d6f)
... ... module dp.ent.shape;
2 2
3 3 import dp.ent.entity; import dp.ent.entity;
4 4 import dp.strat.strat; import dp.strat.strat;
5 import dp.visitor.visitor;
5 6 import cairo.Context; import cairo.Context;
6 7 import std.conv; import std.conv;
7 8 import std.stdio; // Debug import std.stdio; // Debug
 
... ... class Shape : Entity {
17 18 +/ +/
18 19
19 20 public Context c; public Context c;
20 private double size;
21 private double[2][2] bounds;
21 public double[2][2] bounds;
22 public double size;
22 23 private Strategy strat; private Strategy strat;
23 24
24 25 this(int x, int y, Context context, Strategy strategy, double size = 0) { this(int x, int y, Context context, Strategy strategy, double size = 0) {
 
... ... class Shape : Entity {
28 29 this.strat = strategy; this.strat = strategy;
29 30 this.size = size != 0 ? size : strat.initialSize; this.size = size != 0 ? size : strat.initialSize;
30 31 this.bounds = calcBounds(x,y); this.bounds = calcBounds(x,y);
31 writeln("Done! " ~ to!string(getBounds) ~ to!string(getSize));
32 writeln("Done! " ~ to!string(bounds) ~ to!string(this.size));
32 33 } }
33 34
34 35 @property @property
 
... ... class Shape : Entity {
51 52 } }
52 53
53 54 @property @property
54 public double[2][2] getBounds() {
55 return bounds;
56 }
57
58 @property
59 public double getSize() {
60 return size;
61 }
62
63 @property
64 private double sizeX() {
55 public double sizeX() {
65 56 return size * strat.sizeXScale; return size * strat.sizeXScale;
66 57 } }
67 58
68 59 @property @property
69 private double sizeY() {
60 public double sizeY() {
70 61 return size * strat.sizeYScale; return size * strat.sizeYScale;
71 62 } }
72 63
 
... ... class Shape : Entity {
96 87 } }
97 88 } }
98 89
99 public void move(int x, int y) {
100 this.bounds = calcBounds(x,y);
101 }
102
103 public void resize(int amount) {
104 int[2] newPos;
105
106 /+
107 'size' can become negative number due to 'amount' being a
108 bigger negative number than 'size' being a positive number.
109
110 Size could be divided by 2 in some cases, depending on it's
111 shape. Therefore, the size will be checked in it's smallest
112 possible size: this.size divided by 2.
113 +/
114 if(size / 2 + amount <= 0)
115 return;
116
117 newPos = [
118 to!int(bounds[0][0] + sizeX),
119 to!int(bounds[0][1] + sizeY)
120 ];
121
122 size += amount;
123 this.bounds = calcBounds(newPos[0], newPos[1]);
90 public void accept(Visitor v) {
91 v.visit(this);
124 92 } }
125 93
126 protected double[2][2] calcBounds(int x, int y) {
94 public double[2][2] calcBounds(int x, int y) {
127 95 double[2][2] result; double[2][2] result;
128 96
129 97 result[0][0] = x - sizeX; // minX result[0][0] = x - sizeX; // minX
File source/strategies/circle.d changed (mode: 100644) (index 19ff8f3..9db38cd)
... ... private static class CircleDrawer : Strategy {
38 38
39 39 public override void draw(Shape shape) { public override void draw(Shape shape) {
40 40 shape.c.arc ( shape.c.arc (
41 shape.getBounds[0][0] + shape.getSize,
42 shape.getBounds[0][1] + shape.getSize,
43 shape.getSize,
41 shape.bounds[0][0] + shape.size,
42 shape.bounds[0][1] + shape.size,
43 shape.size,
44 44 0, 0,
45 45 2 * pi 2 * pi
46 46 ); );
File source/strategies/ellipse.d changed (mode: 100644) (index 1f0ae3f..2c2043e)
... ... private static class EllipseDrawer : Strategy {
40 40 shape.c.scale(0.5, 1); shape.c.scale(0.5, 1);
41 41
42 42 shape.c.arc ( shape.c.arc (
43 shape.getBounds[0][0] * 2 + shape.getSize,
44 shape.getBounds[0][1] + shape.getSize,
45 shape.getSize,
43 shape.bounds[0][0] * 2 + shape.size,
44 shape.bounds[0][1] + shape.size,
45 shape.size,
46 46 0, 0,
47 47 2 * pi 2 * pi
48 48 ); );
File source/strategies/rect.d changed (mode: 100644) (index d84c9b0..37d2d5d)
... ... private static class RectDrawer : Strategy {
38 38 public override void draw(Shape shape) { public override void draw(Shape shape) {
39 39 writeln("Drawing.."); writeln("Drawing..");
40 40 shape.c.rectangle ( shape.c.rectangle (
41 shape.getBounds[0][0],
42 shape.getBounds[0][1],
43 shape.getSize,
44 shape.getSize
41 shape.bounds[0][0],
42 shape.bounds[0][1],
43 shape.size,
44 shape.size
45 45 ); );
46 46 } }
47 47 } }
File source/strategies/strategy.d changed (mode: 100644) (index ce753a5..5b8db29)
... ... public import dp.ent.shape;
4 4
5 5 import std.conv; import std.conv;
6 6
7 // Used to implement strategy pattern.
8
9 7 abstract class Strategy { abstract class Strategy {
10 8 @property @property
11 9 public abstract string type(); public abstract string type();
 
... ... abstract class Strategy {
25 23 string result = type ~ " "; string result = type ~ " ";
26 24 result ~= to!string(shape.position[0]) ~ " "; result ~= to!string(shape.position[0]) ~ " ";
27 25 result ~= to!string(shape.position[1]) ~ " "; result ~= to!string(shape.position[1]) ~ " ";
28 result ~= to!string(shape.getSize) ~ "\n";
26 result ~= to!string(shape.size) ~ "\n";
29 27
30 28 return result; return result;
31 29 } }
File source/vistors/moveVisitor.d added (mode: 100644) (index 0000000..55b7417)
1 module dp.visitor.move;
2
3 import dp.visitor.visitor;
4
5 public class MoveVisitor : Visitor {
6 int[2] position;
7
8 this(int x, int y) {
9 position = [x,y];
10 }
11
12 this(int[2] position) {
13 this.position = position;
14 }
15
16 public override void visit(Shape s) {
17 s.bounds = s.calcBounds(position[0], position[1]);
18 }
19 }
File source/vistors/resizeVisitor.d added (mode: 100644) (index 0000000..7f7f191)
1 module dp.visitor.resize;
2
3 import dp.visitor.visitor;
4 import std.conv;
5
6 public class ResizeVisitor : Visitor {
7 int amount;
8
9 this(int amount) {
10 this.amount = amount;
11 }
12
13 public override void visit(Shape s) {
14 int[2] newPos;
15
16 /+
17 'size' can become negative number due to 'amount' being a
18 bigger negative number than 'size' being a positive number.
19
20 Size could be divided by 2 in some cases, depending on it's
21 shape. Therefore, the size will be checked in it's smallest
22 possible size: this.size divided by 2.
23 +/
24 if(s.size / 2 + amount <= 0)
25 return;
26
27 newPos = [
28 to!int(s.bounds[0][0] + s.sizeX),
29 to!int(s.bounds[0][1] + s.sizeY)
30 ];
31
32 s.size += amount;
33 s.bounds = s.calcBounds(newPos[0], newPos[1]);
34 }
35 }
File source/vistors/saveVisitor.d added (mode: 100644) (index 0000000..007d08a)
1 module dp.visitor.save;
2
3 import dp.visitor.visitor;
4 import dp.file;
5
6 public class SaveVisitor : Visitor {
7 Savefile f;
8
9 this(Savefile file) {
10 f = file;
11 }
12
13 public override void visit(Shape s) {
14 f.toFile(s);
15 }
16 }
File source/vistors/visitor.d added (mode: 100644) (index 0000000..f16b472)
1 module dp.visitor.visitor;
2
3 public import dp.ent.shape;
4
5 abstract class Visitor {
6 public abstract void visit(Shape s);
7 }
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/kapstok/NHL-DePa2

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/kapstok/NHL-DePa2

Clone this repository using git:
git clone git://git.rocketgit.com/user/kapstok/NHL-DePa2

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main