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)
Change of strategy: Make project command-oriented. ce40dffb1675661922411eeb07f148f61a176c22 Jan Allersma 2018-09-12 11:35:45
Initial commit. 38c8818349679937ae25dde38949202ec45ea7b2 Jan Allersma 2018-09-11 20:21:57
Commit ce40dffb1675661922411eeb07f148f61a176c22 - Change of strategy: Make project command-oriented.
- Renamed variables with more sensible names.

- WIP: The commands should contain the shape information. `canvas.shapes` has refs to it.

- TODO: Don't append shapes to file(buffer). Keep information contained in an object (e.g. Command or Shape) until file is being saved.

- TODO: Implement MoveCommand in a command-oriented way.
Author: Jan Allersma
Author date (UTC): 2018-09-12 11:35
Committer name: Jan Allersma
Committer date (UTC): 2018-09-12 11:35
Parent(s): 38c8818349679937ae25dde38949202ec45ea7b2
Signing key:
Tree: 59df76c548a15da283e1e4193bbedda107f73a5b
File Lines added Lines deleted
.gitignore 1 0
README.md 2 8
source/commands/shapeCommands.d 12 11
source/drawing/shapeOptions.d 2 2
source/entities/canvas.d 7 7
source/entities/entity.d 2 2
source/entities/shape.d 31 35
File .gitignore changed (mode: 100644) (index f5374fc..e3a4253)
... ... source/test.sav
18 18 dpainter.dlangidews dpainter.dlangidews
19 19 dpainter.dlangidews.wssettings dpainter.dlangidews.wssettings
20 20 dpainter.settings dpainter.settings
21 vgcore*
File README.md changed (mode: 100644) (index 0be689b..7263ec2)
... ... This repo is a school project for Design Patterns.
3 3
4 4 use 'dub' to compile and run the code. use 'dub' to compile and run the code.
5 5
6 Each iteration of the application can be found under 'Releases'.
6 # NOTES
7 7
8 # TODO:
9
10 * How could 'isRemoved' not showing up everywhere in the code?
11
12 * Refractor 'shape.d'.
13
14 * Refractor 'shapeCommands.d'.
8 * CheckBounds werkt retrograde (laatst toegevoegde item eerst checken) en stopt na een collision.
File source/commands/shapeCommands.d changed (mode: 100644) (index f0bb7e2..dcd2941)
... ... import dp.shape;
9 9 import std.stdio; // For debug. import std.stdio; // For debug.
10 10
11 11 public class CreateCmd : Command { public class CreateCmd : Command {
12 //size_t shapePtr;
12 public Shape s;
13 13
14 14 this() { this() {
15 shapePtr = Global.canvas.childrenAmount;
16 Global.canvas.addShape();
17 File.Append(Global.canvas.getChild(shapePtr).toString());
15 s = new Shape();
16 Global.canvas.addShape(s);
17 File.Append(s.toString());
18 18 } }
19 19
20 20 public override void execute() { public override void execute() {
21 //Global.canvas.getChild(shapePtr).isRemoved = false;
21 s.enabled = s.visible = true;
22 22 Global.canvas.repaint(); Global.canvas.repaint();
23 23 } }
24 24
25 25 public override void undo() { public override void undo() {
26 //Global.canvas.removeChild(shapePtr);
26 s.enabled = s.visible = false;
27 27 Global.canvas.repaint(); Global.canvas.repaint();
28 28 } }
29 29 } }
30 30
31 31 public class MoveCmd : Command { public class MoveCmd : Command {
32 // size_t shapePtr;
32 size_t shapeIndex;
33 33 // bool executed; // bool executed;
34 34
35 35 this () { this () {
36 36 //executed = false; //executed = false;
37 shapeIndex = Global.canvas.childrenAmount;
37 38 execute(); execute();
38 39 } }
39 40
 
... ... public class MoveCmd : Command {
45 46
46 47 //Global.canvas.getShape(shapePtr).lastPosition = Global.canvas.getShape(shapePtr).position; //Global.canvas.getShape(shapePtr).lastPosition = Global.canvas.getShape(shapePtr).position;
47 48 //Global.canvas.getShape(shapePtr).position = tmp; //Global.canvas.getShape(shapePtr).position = tmp;
48 Global.canvas.getShape(shapePtr).remove();
49 // Global.canvas.getShape(shapeIndex).remove();
49 50
50 Global.canvas.getShape(shapePtr).revive(
51 /+ Global.canvas.getShape(shapeIndex).revive(
51 52 12,//Global.canvas.getShape(shapePtr).position[0], 12,//Global.canvas.getShape(shapePtr).position[0],
52 53 177//Global.canvas.getShape(shapePtr).position[1] 177//Global.canvas.getShape(shapePtr).position[1]
53 54 ); );
54 //}
55 +/ //}
55 56 } }
56 57
57 58 public override void undo() { public override void undo() {
 
... ... public class MoveCmd : Command {
62 63
63 64 //Global.canvas.getShape(shapePtr).position = Global.canvas.getShape(shapePtr).lastPosition; //Global.canvas.getShape(shapePtr).position = Global.canvas.getShape(shapePtr).lastPosition;
64 65 //Global.canvas.getShape(shapePtr).lastPosition = tmp; //Global.canvas.getShape(shapePtr).lastPosition = tmp;
65 Global.canvas.getShape(shapePtr).remove();
66 // Global.canvas.getShape(shapeIndex).remove();
66 67
67 68 //Global.canvas.getShape(shapePtr).revive( //Global.canvas.getShape(shapePtr).revive(
68 69 //Global.canvas.getShape(shapePtr).position[0], //Global.canvas.getShape(shapePtr).position[0],
File source/drawing/shapeOptions.d changed (mode: 100644) (index bd1d840..f4e4d94)
... ... public class ShapeOptions : Menu {
18 18 this.append(new MoveShape()); this.append(new MoveShape());
19 19 } }
20 20
21 // Will look like: sOptions.OfShape(s); Should be differently named.
21 // Will look like: sOptions.OfShape(s); Should be differently named.
22 22 public void OfShape (Shape shape) { // Shows options of param shape. public void OfShape (Shape shape) { // Shows options of param shape.
23 23 s = shape; s = shape;
24 24 this.showAll(); this.showAll();
 
... ... protected class MoveShape : MenuItem {
51 51 s.remove(); s.remove();
52 52 //s.queueRevival = true; //s.queueRevival = true;
53 53 Global.canvas.repaint(); Global.canvas.repaint();
54 s.select();
54 // Create ghost shape.
55 55 return false; // Hide ShapeOptions when button is released. return false; // Hide ShapeOptions when button is released.
56 56 } }
57 57 } }
File source/entities/canvas.d changed (mode: 100644) (index fd0ffc5..d23b99b)
... ... public class Canvas : Entity {
50 50 return cast(Entity[]) shapes; return cast(Entity[]) shapes;
51 51 } }
52 52
53 public Shape getShape(ulong n) {
53 public ref Shape getShape(ulong n) {
54 54 return shapes[n]; return shapes[n];
55 55 } }
56 56
57 57 public void addChild(ulong n) { public void addChild(ulong n) {
58 getChild(n).isRemoved = false;
58 getChild(n).enabled = true;
59 59 } }
60 60
61 61 public void removeChild(ulong n) { public void removeChild(ulong n) {
62 getChild(n).isRemoved = true;
62 getChild(n).enabled = false;
63 63 } }
64 64
65 65 private bool clickCallback(Event event, Widget widget) { private bool clickCallback(Event event, Widget widget) {
 
... ... public class Canvas : Entity {
130 130 area.queueDraw(); area.queueDraw();
131 131 } }
132 132
133 public Shape addShape() {
133 public void addShape(ref Shape shape) {
134 134 size_t i = shapes.length; size_t i = shapes.length;
135 135
136 shape.move(mouseX, mouseY, Context.create(surface));
137
136 138 shapes.length++; shapes.length++;
137 shapes[i] = new Shape(Context.create(surface), mouseX, mouseY);
139 shapes[i] = shape;//new Shape(Context.create(surface), mouseX, mouseY);
138 140
139 141 area.queueDraw(); area.queueDraw();
140
141 return shapes[i];
142 142 } }
143 143 } }
File source/entities/entity.d changed (mode: 100644) (index 6d889c6..55a9560)
... ... import std.stdio;
4 4
5 5 abstract class Entity { abstract class Entity {
6 6 protected Entity[] children; protected Entity[] children;
7 public bool isRemoved = false;
7 public bool enabled = true;
8 8
9 9 this() {} this() {}
10 10
 
... ... abstract class Entity {
37 37 } }
38 38 /+ /+
39 39 public void removeChild(int n) { public void removeChild(int n) {
40 getChild(n).isRemoved = true;
40 getChild(n).enabled = false;
41 41 } }
42 42 +/ +/
43 43 public string treeToString(int depth = 1) { // Laagste depth = 1. public string treeToString(int depth = 1) { // Laagste depth = 1.
File source/entities/shape.d changed (mode: 100644) (index 49c986d..d524870)
... ... import dp.command.shape;
13 13
14 14 class Shape : Global.Entity { class Shape : Global.Entity {
15 15 private Context c; private Context c;
16 private bool revivable;
16 /+private+/public bool visible;
17 17
18 18 /+ /+
19 19 bounds[0][0] = minX bounds[0][0] = minX
 
... ... class Shape : Global.Entity {
22 22 bounds[1][1] = maxY bounds[1][1] = maxY
23 23 +/ +/
24 24 private double[2][2] bounds; private double[2][2] bounds;
25 private double[2] lastPos;
25 //private double[2] lastPos;
26 26
27 27 private string shapeType; private string shapeType;
28 28
29 29
30 this(Context context, int x, int y) {
31 isRemoved = revivable = false;
32 c = context;
33 draw(x,y);
30 // this(Context context, int x, int y) {
31 this() {
32 enabled = visible = true;
34 33 } }
35 34
36 35 @property @property
 
... ... class Shape : Global.Entity {
38 37 return bounds[0]; return bounds[0];
39 38 } }
40 39
41 @property
40 /* @property
42 41 public double[2] lastPosition() { public double[2] lastPosition() {
43 42 return lastPos; return lastPos;
44 }
43 } */
45 44
46 //TMP?
45 /* //TMP?
47 46 @property @property
48 47 public double[2] lastPosition(double[2] newPos) { public double[2] lastPosition(double[2] newPos) {
49 48 return lastPos = newPos; return lastPos = newPos;
50 }
49 } */
51 50
52 51 @property @property
53 52 public double[2] position(double[2] newPos) { public double[2] position(double[2] newPos) {
 
... ... class Shape : Global.Entity {
75 74 return shapeType; return shapeType;
76 75 } }
77 76
78 /+
79 Wait for revival of shape until first left-click.
80 Just to prevent instant movement from
81 dp.shapeOptions.MoveShape.
82 +/
83 @property
84 public bool queueRevival (bool revive) {
85 return revivable = revive;
86 }
87
88 77 public override string toString() { public override string toString() {
89 78 return text(type, " ", bounds[0][0], " ", bounds[0][1], " ", sizeX, " ", sizeY); return text(type, " ", bounds[0][0], " ", bounds[0][1], " ", sizeX, " ", sizeY);
90 79 } }
91 80
81 public void move(int x, int y, Context context=null) {
82 if(context !is null)
83 c = context;
84
85 draw(x,y);
86 }
87
92 88 public void select () { public void select () {
93 89 c.setSourceRgba(1-Global.Brush.Red, 1-Global.Brush.Green, 1-Global.Brush.Blue, 1); c.setSourceRgba(1-Global.Brush.Red, 1-Global.Brush.Green, 1-Global.Brush.Blue, 1);
94 90 c.stroke(); c.stroke();
 
... ... class Shape : Global.Entity {
97 93
98 94 public void remove () { public void remove () {
99 95 writeln("Shape removed. Please repaint canvas to see changes."); writeln("Shape removed. Please repaint canvas to see changes.");
100 isRemoved = true;
101 revivable = false;
96 visible = enabled = false;
102 97 } }
103 98
104 public void revive (int x, int y) {
99 /* public void revive (int x, int y) {
105 100 writeln("Im alive! Redraw me!"); writeln("Im alive! Redraw me!");
106 isRemoved = false;
101 enabled = true;
107 102 c.newPath(); c.newPath();
108 103
109 104 final switch (type) { final switch (type) {
 
... ... class Shape : Global.Entity {
185 180 } }
186 181 applyToSource(); applyToSource();
187 182 Global.canvas.repaint(); Global.canvas.repaint();
188 }
183 } */
189 184
190 /+private+/public void revive(double x, double y) { revive(cast(int)x, cast(int)y); }
185 /* /+private+/public void revive(double x, double y) { revive(cast(int)x, cast(int)y); } */
191 186
192 187 private void draw(int x, int y) { private void draw(int x, int y) {
193 if(!isRemoved) {
188 if(visible) {
194 189 shapeType = Global.Brush.Shape; shapeType = Global.Brush.Shape;
195 190 final switch (Global.Brush.Shape) { final switch (Global.Brush.Shape) {
196 191 case "rectangle": case "rectangle":
 
... ... class Shape : Global.Entity {
233 228 } }
234 229
235 230 public bool CheckBounds (int x, int y) { public bool CheckBounds (int x, int y) {
236 if(!isRemoved) {
231 if(enabled) {
232 writeln(shapeType ~ ":");
237 233 write(bounds[0][0]);write(" <- ");write(x);write(" -> ");writeln(bounds[1][0]); write(bounds[0][0]);write(" <- ");write(x);write(" -> ");writeln(bounds[1][0]);
238 234 write(bounds[0][1]);write(" <- ");write(y);write(" -> ");writeln(bounds[1][1]); write(bounds[0][1]);write(" <- ");write(y);write(" -> ");writeln(bounds[1][1]);
239 235
 
... ... class Shape : Global.Entity {
247 243 return true; return true;
248 244 } }
249 245 } }
250 else if(revivable) {
251 lastPos = [x - sizeX / 2, y - sizeY / 2];
252 History.addCommand(new MoveCmd());
246 else if(visible) {
247 /* lastPos = [x - sizeX / 2, y - sizeY / 2];
248 History.addCommand(new MoveCmd()); */
253 249 } }
254 250
255 251 return false; return false;
 
... ... class Shape : Global.Entity {
257 253
258 254 // This will be used when the user scrolls on the canvas. // This will be used when the user scrolls on the canvas.
259 255 public bool CheckBounds (int x, int y, int growContext) { public bool CheckBounds (int x, int y, int growContext) {
260 if(!isRemoved) {
256 if(enabled) {
261 257 //write(bounds[0][0]);write(" <- ");write(x);write(" -> ");writeln(bounds[1][0]); //write(bounds[0][0]);write(" <- ");write(x);write(" -> ");writeln(bounds[1][0]);
262 258 //write(bounds[0][1]);write(" <- ");write(y);write(" -> ");writeln(bounds[1][1]); //write(bounds[0][1]);write(" <- ");write(y);write(" -> ");writeln(bounds[1][1]);
263 259
 
... ... class Shape : Global.Entity {
278 274 bounds[0][1] += 5; bounds[0][1] += 5;
279 275 bounds[1][1] -= 5; bounds[1][1] -= 5;
280 276 } }
281
277 /+
282 278 revive( revive(
283 279 (bounds[0][0] + bounds[1][0]) / 2, (bounds[0][0] + bounds[1][0]) / 2,
284 280 (bounds[0][1] + bounds[1][1]) / 2 (bounds[0][1] + bounds[1][1]) / 2
285 281 ); );
286
282 +/
287 283 return true; return true;
288 284 } }
289 285
 
... ... class Shape : Global.Entity {
292 288 } }
293 289
294 290 public void applyToSource() { public void applyToSource() {
295 if(!isRemoved)
291 if(visible)
296 292 c.fillPreserve(); c.fillPreserve();
297 293 } }
298 294 } }
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