Skip to content

Commit 64b804a

Browse files
committed
Introduce Move tool
1 parent 1c3e3c3 commit 64b804a

15 files changed

+333
-74
lines changed

src/core/ResourceTool.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export class ResourceTool {
4949

5050
if (type === 'image-template') {
5151
const scenes = await this.parseImageTemplate(json, zip);
52-
return Tools.insertElementAreaTool(info, scenes, undefined);
52+
return Tools.insertScenesTool(info, scenes, undefined);
5353

5454
} else if (type === 'random-template') {
5555
const scenes = await this.parseRandomTemplate(json, zip);
56-
return Tools.insertElementAreaTool(info, scenes, undefined);
56+
return Tools.insertScenesTool(info, scenes, undefined);
5757

5858
} else {
5959
throw 'Tool action not supported: ' + type;

src/core/SandGame.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ export class SandGame {
146146
}
147147

148148
graphics() {
149-
return new SandGameGraphics(this.#elementArea, this.#random, (x, y) => {
149+
const defaults = this.#processor.getDefaults();
150+
return new SandGameGraphics(this.#elementArea, this.#random, defaults, (x, y) => {
150151
this.#processor.trigger(x, y);
151152
this.#renderer.trigger(x, y);
152153
});

src/core/SandGameGraphics.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {CircleIterator} from "./CircleIterator.js";
77
/**
88
*
99
* @author Patrik Harag
10-
* @version 2023-12-10
10+
* @version 2023-12-28
1111
*/
1212
export class SandGameGraphics {
1313

@@ -17,45 +17,34 @@ export class SandGameGraphics {
1717
/** @type DeterministicRandom */
1818
#random;
1919

20-
/** @type function(number, number) */
20+
/** @type ProcessorDefaults */
21+
#defaults;
22+
23+
/** @type {function(number,number)} */
2124
#triggerFunction;
2225

23-
constructor(elementArea, random, triggerFunction) {
26+
constructor(elementArea, random, defaults, triggerFunction) {
2427
this.#elementArea = elementArea;
2528
this.#random = random;
29+
this.#defaults = defaults;
2630
this.#triggerFunction = triggerFunction;
2731
}
2832

29-
/**
30-
*
31-
* @param aX {number}
32-
* @param aY {number}
33-
* @param bX {number}
34-
* @param bY {number}
35-
*/
36-
swap(aX, aY, bX, bY) {
37-
aX = Math.trunc(aX);
38-
aY = Math.trunc(aY);
39-
bX = Math.trunc(bX);
40-
bY = Math.trunc(bY);
41-
42-
if (this.#elementArea.isValidPosition(aX, aY) && this.#elementArea.isValidPosition(bX, bY)) {
43-
this.#elementArea.swap(aX, aY, bX, bY);
44-
}
45-
}
46-
4733
/**
4834
*
4935
* @param x {number}
5036
* @param y {number}
51-
* @param brushOrElement {Brush|Element}
37+
* @param brushOrElement {Brush|Element|null}
5238
*/
5339
draw(x, y, brushOrElement) {
5440
x = Math.trunc(x);
5541
y = Math.trunc(y);
5642

5743
if (this.#elementArea.isValidPosition(x, y)) {
58-
if (brushOrElement instanceof Element) {
44+
if (brushOrElement === null) {
45+
this.#elementArea.setElement(x, y, this.#defaults.getDefaultElement());
46+
this.#triggerFunction(x, y);
47+
} else if (brushOrElement instanceof Element) {
5948
this.#elementArea.setElement(x, y, brushOrElement);
6049
this.#triggerFunction(x, y);
6150
} else if (brushOrElement instanceof Brush) {
@@ -108,16 +97,28 @@ export class SandGameGraphics {
10897
x2 = Math.trunc(x2);
10998
y2 = Math.trunc(y2);
11099

111-
const d = Math.ceil(size / 2);
112-
113100
let consumer;
114101
if (round) {
102+
let maxLevel = Math.trunc(size / 2);
103+
104+
let blueprint;
105+
if (maxLevel <= 3) {
106+
blueprint = CircleIterator.BLUEPRINT_3;
107+
} else if (maxLevel === 4) {
108+
blueprint = CircleIterator.BLUEPRINT_4;
109+
} else {
110+
blueprint = CircleIterator.BLUEPRINT_9;
111+
}
112+
115113
consumer = (x, y) => {
116-
CircleIterator.iterate(CircleIterator.BLUEPRINT_3, (dx, dy, level) => {
117-
this.draw(x + dx, y + dy, brush);
114+
CircleIterator.iterate(blueprint, (dx, dy, level) => {
115+
if (level <= maxLevel) {
116+
this.draw(x + dx, y + dy, brush);
117+
}
118118
});
119119
};
120120
} else {
121+
const d = Math.ceil(size / 2);
121122
consumer = (x, y) => {
122123
this.drawRectangle(x - d, y - d, x + d, y + d, brush);
123124
};
@@ -185,4 +186,8 @@ export class SandGameGraphics {
185186
getHeight() {
186187
return this.#elementArea.getHeight();
187188
}
189+
190+
getDefaults() {
191+
return this.#defaults;
192+
}
188193
}

src/core/Tool.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {CursorDefinition} from "./CursorDefinition.js";
44
* @interface
55
*
66
* @author Patrik Harag
7-
* @version 2023-12-23
7+
* @version 2023-12-28
88
*/
99
export class Tool {
1010

@@ -23,15 +23,7 @@ export class Tool {
2323
return this.#info;
2424
}
2525

26-
isStrokeEnabled() {
27-
return false;
28-
}
29-
30-
isSelectionEnabled() {
31-
return false;
32-
}
33-
34-
isRepeatingEnabled() {
26+
hasCursor() {
3527
return false;
3628
}
3729

@@ -43,6 +35,10 @@ export class Tool {
4335
return null;
4436
}
4537

38+
isRepeatingEnabled() {
39+
return false;
40+
}
41+
4642
/**
4743
*
4844
* @param x {number}
@@ -52,7 +48,19 @@ export class Tool {
5248
* @return {void}
5349
*/
5450
applyPoint(x, y, graphics, altModifier) {
55-
throw 'Not implemented';
51+
// no action by default
52+
}
53+
54+
isLineModeEnabled() {
55+
return false;
56+
}
57+
58+
onDragStart(x, y, graphics, altModifier) {
59+
// no action by default
60+
}
61+
62+
onDragEnd(x, y, graphics, altModifier) {
63+
// no action by default
5664
}
5765

5866
/**
@@ -69,6 +77,10 @@ export class Tool {
6977
// no action by default
7078
}
7179

80+
isAreaModeEnabled() {
81+
return false;
82+
}
83+
7284
/**
7385
*
7486
* @param x1 {number}
@@ -94,4 +106,20 @@ export class Tool {
94106
applySpecial(x, y, graphics, altModifier) {
95107
// no action by default
96108
}
109+
110+
isSecondaryActionEnabled() {
111+
return false;
112+
}
113+
114+
/**
115+
*
116+
* @param x {number}
117+
* @param y {number}
118+
* @param graphics {SandGameGraphics}
119+
* @param altModifier {boolean}
120+
* @return {void}
121+
*/
122+
applySecondaryAction(x, y, graphics, altModifier) {
123+
// no action by default
124+
}
97125
}

src/core/Tools.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import {RoundBrushTool} from "./tool/RoundBrushTool";
22
import {PointBrushTool} from "./tool/PointBrushTool";
33
import {Point2BrushTool} from "./tool/Point2BrushTool";
44
import {MeteorTool} from "./tool/MeteorTool";
5-
import {InsertSceneTool} from "./tool/InsertSceneTool";
5+
import {InsertElementAreaTool} from "./tool/InsertElementAreaTool";
66
import {InsertRandomSceneTool} from "./tool/InsertRandomSceneTool";
77
import {ActionTool} from "./tool/ActionTool";
8+
import {MoveTool} from "./tool/MoveTool";
89

910
/**
1011
*
1112
* @author Patrik Harag
12-
* @version 2023-12-25
13+
* @version 2023-12-28
1314
*/
1415
export class Tools {
1516

@@ -29,9 +30,10 @@ export class Tools {
2930
return new MeteorTool(info);
3031
}
3132

32-
static insertElementAreaTool(info, scenes, handler) {
33+
static insertScenesTool(info, scenes, handler) {
3334
if (scenes.length === 1) {
34-
return new InsertSceneTool(info, scenes[0], handler);
35+
const elementArea = InsertElementAreaTool.asElementArea(scenes[0]);
36+
return new InsertElementAreaTool(info, elementArea, handler);
3537
} else {
3638
return new InsertRandomSceneTool(info, scenes, handler);
3739
}
@@ -40,4 +42,8 @@ export class Tools {
4042
static actionTool(info, handler) {
4143
return new ActionTool(info, handler);
4244
}
45+
46+
static moveTool(info, size) {
47+
return new MoveTool(info, size);
48+
}
4349
}

src/core/tool/InsertSceneTool.js renamed to src/core/tool/InsertElementAreaTool.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import {Tool} from "../Tool";
66
/**
77
*
88
* @author Patrik Harag
9-
* @version 2023-12-25
9+
* @version 2023-12-28
1010
*/
11-
export class InsertSceneTool extends Tool {
11+
export class InsertElementAreaTool extends Tool {
12+
13+
static asElementArea(scene) {
14+
return scene.createElementArea(InsertElementAreaTool.DEFAULT_W, InsertElementAreaTool.DEFAULT_H,
15+
ElementArea.TRANSPARENT_ELEMENT);
16+
}
17+
1218

1319
static DEFAULT_W = 30;
1420
static DEFAULT_H = 30;
@@ -18,12 +24,9 @@ export class InsertSceneTool extends Tool {
1824
/** @type function */
1925
#onInsertHandler;
2026

21-
constructor(info, scene, onInsertHandler) {
27+
constructor(info, elementArea, onInsertHandler) {
2228
super(info);
23-
24-
this.#elementArea = scene.createElementArea(
25-
InsertSceneTool.DEFAULT_W, InsertSceneTool.DEFAULT_H, ElementArea.TRANSPARENT_ELEMENT);
26-
29+
this.#elementArea = elementArea;
2730
this.#onInsertHandler = onInsertHandler;
2831
}
2932

@@ -66,6 +69,10 @@ export class InsertSceneTool extends Tool {
6669
}
6770
}
6871

72+
hasCursor() {
73+
return true;
74+
}
75+
6976
createCursor() {
7077
return new CursorDefinitionElementArea(this.#elementArea);
7178
}

src/core/tool/InsertRandomSceneTool.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {DeterministicRandom} from "../DeterministicRandom";
2-
import {InsertSceneTool} from "./InsertSceneTool";
2+
import {InsertElementAreaTool} from "./InsertElementAreaTool";
33
import {Tool} from "../Tool";
44

55
/**
66
*
77
* @author Patrik Harag
8-
* @version 2023-12-25
8+
* @version 2023-12-29
99
*/
1010
export class InsertRandomSceneTool extends Tool {
1111

@@ -31,14 +31,19 @@ export class InsertRandomSceneTool extends Tool {
3131

3232
const i = DeterministicRandom.DEFAULT.nextInt(this.#scenes.length);
3333
const scene = this.#scenes[i];
34-
this.#currentTool = new InsertSceneTool(this.getInfo(), scene, this.#onInsertHandler);
34+
const elementArea = InsertElementAreaTool.asElementArea(scene);
35+
this.#currentTool = new InsertElementAreaTool(this.getInfo(), elementArea, this.#onInsertHandler);
3536
}
3637

3738
applyPoint(x, y, graphics, aldModifier) {
3839
this.#currentTool.applyPoint(x, y, graphics, aldModifier);
3940
this.#initRandomTool();
4041
}
4142

43+
hasCursor() {
44+
return true;
45+
}
46+
4247
createCursor() {
4348
return this.#currentTool.createCursor();
4449
}

0 commit comments

Comments
 (0)