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)
Make `MoveCmd` Command-oriented. 2970a8d862285752551be2464422375ca958357f Jan Allersma 2018-09-12 15:33:17
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 2970a8d862285752551be2464422375ca958357f - Make `MoveCmd` Command-oriented.
Remove all old commented code.
Author: Jan Allersma
Author date (UTC): 2018-09-12 15:33
Committer name: Jan Allersma
Committer date (UTC): 2018-09-12 16:00
Parent(s): ce40dffb1675661922411eeb07f148f61a176c22
Signing key:
Tree: 7ca9e4150dfdcf295ba39f664e489ca6523fbf81
File Lines added Lines deleted
README.md 4 0
source/commands/shapeCommands.d 12 34
source/drawing/shapeOptions.d 2 1
source/entities/canvas.d 2 2
source/entities/entity.d 2 11
source/entities/shape.d 5 121
source/globals.d 0 1
File README.md changed (mode: 100644) (index 7263ec2..17074fb)
... ... use 'dub' to compile and run the code.
6 6 # NOTES # NOTES
7 7
8 8 * CheckBounds werkt retrograde (laatst toegevoegde item eerst checken) en stopt na een collision. * CheckBounds werkt retrograde (laatst toegevoegde item eerst checken) en stopt na een collision.
9
10 * [DLang issue](https://forum.dlang.org/post/emixkjutxnnrplaziwkj@forum.dlang.org)
11
12 * Moet nog checken op ongebruikte functies.
File source/commands/shapeCommands.d changed (mode: 100644) (index dcd2941..06b10d2)
... ... public class CreateCmd : Command {
29 29 } }
30 30
31 31 public class MoveCmd : Command { public class MoveCmd : Command {
32 size_t shapeIndex;
33 // bool executed;
34
35 this () {
36 //executed = false;
37 shapeIndex = Global.canvas.childrenAmount;
32 Shape original;
33 Shape newShape;
34
35 this (Shape original) {
36 this.original = original;
37 newShape = new Shape();
38 Global.Brush.Shape = original.type;
39 Global.canvas.addShape(newShape);
38 40 execute(); execute();
39 41 } }
40 42
41 43 public override void execute() { public override void execute() {
42 //if(!executed) {
43 //double[2] tmp = Global.canvas.getShape(shapePtr).lastPosition;
44
45 //executed = true;
46
47 //Global.canvas.getShape(shapePtr).lastPosition = Global.canvas.getShape(shapePtr).position;
48 //Global.canvas.getShape(shapePtr).position = tmp;
49 // Global.canvas.getShape(shapeIndex).remove();
50
51 /+ Global.canvas.getShape(shapeIndex).revive(
52 12,//Global.canvas.getShape(shapePtr).position[0],
53 177//Global.canvas.getShape(shapePtr).position[1]
54 );
55 +/ //}
44 original.visible = original.enabled = false;
45 newShape.visible = newShape.enabled = true;
56 46 } }
57 47
58 48 public override void undo() { public override void undo() {
59 //if(executed) {
60 //double[2] tmp = Global.canvas.getShape(shapePtr).position;
61
62 //executed = false;
63
64 //Global.canvas.getShape(shapePtr).position = Global.canvas.getShape(shapePtr).lastPosition;
65 //Global.canvas.getShape(shapePtr).lastPosition = tmp;
66 // Global.canvas.getShape(shapeIndex).remove();
67
68 //Global.canvas.getShape(shapePtr).revive(
69 //Global.canvas.getShape(shapePtr).position[0],
70 //Global.canvas.getShape(shapePtr).position[1]
71 //);
72 //}
49 original.visible = original.enabled = true;
50 newShape.visible = newShape.enabled = false;
73 51 } }
74 52 } }
File source/drawing/shapeOptions.d changed (mode: 100644) (index f4e4d94..657ec24)
... ... protected class MoveShape : MenuItem {
48 48
49 49 private bool relCallback (Event event, Widget widget) { private bool relCallback (Event event, Widget widget) {
50 50 writeln("Moved shape"); writeln("Moved shape");
51 s.remove();
51 s.select();
52 s.enabled = false;
52 53 //s.queueRevival = true; //s.queueRevival = true;
53 54 Global.canvas.repaint(); Global.canvas.repaint();
54 55 // Create ghost shape. // Create ghost shape.
File source/entities/canvas.d changed (mode: 100644) (index d23b99b..ac3c76c)
... ... public class Canvas : Entity {
111 111 most at front is selected first. most at front is selected first.
112 112 +/ +/
113 113 for(int i = cast(int)shapes.length - 1; i >= 0; i -= 1) for(int i = cast(int)shapes.length - 1; i >= 0; i -= 1)
114 if(shapes[i].CheckBounds(mouseX, mouseY, dir))
114 if(shapes[i].CheckBounds(mouseX, mouseY, dir))
115 115 break; break;
116 116
117 117 return true; return true;
 
... ... public class Canvas : Entity {
136 136 shape.move(mouseX, mouseY, Context.create(surface)); shape.move(mouseX, mouseY, Context.create(surface));
137 137
138 138 shapes.length++; shapes.length++;
139 shapes[i] = shape;//new Shape(Context.create(surface), mouseX, mouseY);
139 shapes[i] = shape;
140 140
141 141 area.queueDraw(); area.queueDraw();
142 142 } }
File source/entities/entity.d changed (mode: 100644) (index 55a9560..47c02d2)
... ... abstract class Entity {
17 17 public size_t childrenAmount() { public size_t childrenAmount() {
18 18 return children.length; return children.length;
19 19 } }
20 /+
21 public Entity getChild() { // Debug
22 writeln(children[0]);
23 return children[0];
24 }
25 +/
20
26 21 public Entity getChild(ulong n) { public Entity getChild(ulong n) {
27 22 return getChildren()[n]; return getChildren()[n];
28 23 } }
 
... ... abstract class Entity {
35 30 children.length++; children.length++;
36 31 children[children.length - 1] = child; children[children.length - 1] = child;
37 32 } }
38 /+
39 public void removeChild(int n) {
40 getChild(n).enabled = false;
41 }
42 +/
33
43 34 public string treeToString(int depth = 1) { // Laagste depth = 1. public string treeToString(int depth = 1) { // Laagste depth = 1.
44 35 string result = this.toString() ~ "\n"; string result = this.toString() ~ "\n";
45 36
File source/entities/shape.d changed (mode: 100644) (index d524870..bb2a901)
... ... import cairo.Context;
6 6 import cairo.ImageSurface; import cairo.ImageSurface;
7 7
8 8 import std.stdio; // For debug import std.stdio; // For debug
9 import std.conv;
9 import std.conv; // Redundant?
10 10
11 11 import dp.history; import dp.history;
12 12 import dp.command.shape; 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+/public bool visible;
16 public bool visible; // Should be encapsulated.
17 private string shapeType;
17 18
18 19 /+ /+
19 20 bounds[0][0] = minX bounds[0][0] = minX
 
... ... class Shape : Global.Entity {
22 23 bounds[1][1] = maxY bounds[1][1] = maxY
23 24 +/ +/
24 25 private double[2][2] bounds; private double[2][2] bounds;
25 //private double[2] lastPos;
26
27 private string shapeType;
28
29 26
30 // this(Context context, int x, int y) {
31 27 this() { this() {
32 28 enabled = visible = true; enabled = visible = true;
33 29 } }
 
... ... class Shape : Global.Entity {
37 33 return bounds[0]; return bounds[0];
38 34 } }
39 35
40 /* @property
41 public double[2] lastPosition() {
42 return lastPos;
43 } */
44
45 /* //TMP?
46 @property
47 public double[2] lastPosition(double[2] newPos) {
48 return lastPos = newPos;
49 } */
50
51 36 @property @property
52 37 public double[2] position(double[2] newPos) { public double[2] position(double[2] newPos) {
53 38 double sX = sizeX; double sX = sizeX;
 
... ... class Shape : Global.Entity {
92 77 } }
93 78
94 79 public void remove () { public void remove () {
95 writeln("Shape removed. Please repaint canvas to see changes.");
96 80 visible = enabled = false; visible = enabled = false;
97 81 } }
98 82
99 /* public void revive (int x, int y) {
100 writeln("Im alive! Redraw me!");
101 enabled = true;
102 c.newPath();
103
104 final switch (type) {
105 case "rectangle":
106 c.rectangle(x, y, sizeX, sizeY);
107 c.fillPreserve();
108
109 /+
110 Recalculate the bounds, since they changed due to
111 moving the shape.
112
113 Since sizeX and sizeY change to different values
114 during recalculation of bounds, due to sizeX and
115 sizeY are based off the bounds, sizeX and sizeY
116 have to be captured before recalculation.
117 +/
118 double sX = sizeX;
119 double sY = sizeY;
120 bounds[0][0] = x;
121 bounds[1][0] = x + sX;
122 bounds[0][1] = y;
123 bounds[1][1] = y + sY;
124
125 break;
126 case "ellipse":
127 /+
128 3.141 = Pi.
129 sizeY is in diameters. Parameter arc() requires radius.
130 +/
131 c.arc(x * 2 + sizeX / 2, y + sizeY / 2, sizeY / 2 , 0 , 2 * 3.141);
132 c.fillPreserve();
133
134 /+
135 No need to scale c again. That has been preserved
136 from the first time it was drawn.
137
138 Recalculate the bounds, since they changed due to
139 moving the shape.
140
141 Since sizeX and sizeY change to different values
142 during recalculation of bounds, due to sizeX and
143 sizeY are based off the bounds, sizeX and sizeY
144 have to be captured before recalculation.
145 +/
146 double sX = sizeX;
147 double sY = sizeY;
148 bounds[0][0] = x;
149 bounds[1][0] = x + sX;
150 bounds[0][1] = y;
151 bounds[1][1] = y + sY;
152
153 break;
154 case "circle":
155 /+
156 3.141 = Pi.
157 sizeY is in diameters. Parameter arc() requires radius.
158 +/
159 c.arc(x + sizeX / 2, y + sizeY / 2, sizeY / 2 , 0 , 2 * 3.141);
160 c.fillPreserve();
161
162 /+
163 Recalculate the bounds, since they changed due to
164 moving the shape.
165
166 Since sizeX and sizeY change to different values
167 during recalculation of bounds, due to sizeX and
168 sizeY are based off the bounds, sizeX and sizeY
169 have to be captured before recalculation.
170 +/
171 double sX = sizeX;
172 double sY = sizeY;
173 bounds[0][0] = x;
174 bounds[1][0] = x + sX;
175 bounds[0][1] = y;
176 bounds[1][1] = y + sY;
177
178 break;
179
180 }
181 applyToSource();
182 Global.canvas.repaint();
183 } */
184
185 /* /+private+/public void revive(double x, double y) { revive(cast(int)x, cast(int)y); } */
186
187 83 private void draw(int x, int y) { private void draw(int x, int y) {
188 84 if(visible) { if(visible) {
189 85 shapeType = Global.Brush.Shape; shapeType = Global.Brush.Shape;
 
... ... class Shape : Global.Entity {
243 139 return true; return true;
244 140 } }
245 141 } }
246 else if(visible) {
247 /* lastPos = [x - sizeX / 2, y - sizeY / 2];
248 History.addCommand(new MoveCmd()); */
249 }
142 else if(visible)
143 History.addCommand(new MoveCmd(this));
250 144
251 145 return false; return false;
252 146 } }
 
... ... class Shape : Global.Entity {
254 148 // This will be used when the user scrolls on the canvas. // This will be used when the user scrolls on the canvas.
255 149 public bool CheckBounds (int x, int y, int growContext) { public bool CheckBounds (int x, int y, int growContext) {
256 150 if(enabled) { if(enabled) {
257 //write(bounds[0][0]);write(" <- ");write(x);write(" -> ");writeln(bounds[1][0]);
258 //write(bounds[0][1]);write(" <- ");write(y);write(" -> ");writeln(bounds[1][1]);
259
260 151 if( if(
261 152 x > bounds[0][0] && // minX x > bounds[0][0] && // minX
262 153 x < bounds[1][0] && // maxX x < bounds[1][0] && // maxX
 
... ... class Shape : Global.Entity {
274 165 bounds[0][1] += 5; bounds[0][1] += 5;
275 166 bounds[1][1] -= 5; bounds[1][1] -= 5;
276 167 } }
277 /+
278 revive(
279 (bounds[0][0] + bounds[1][0]) / 2,
280 (bounds[0][1] + bounds[1][1]) / 2
281 );
282 +/
283 168 return true; return true;
284 169 } }
285
286 170 } }
287 171 return false; return false;
288 172 } }
File source/globals.d changed (mode: 100644) (index d66b5dc..84c27dd)
1 1 module dp.globals; module dp.globals;
2 2
3 //public import dp.canvas;
4 3 public import dp.history; public import dp.history;
5 4 public import dp.entity; public import dp.entity;
6 5 public import Brush = dp.brush; public import Brush = dp.brush;
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