-
Notifications
You must be signed in to change notification settings - Fork 25
/
rectangle-chain.js
59 lines (49 loc) · 1.25 KB
/
rectangle-chain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// <reference types="@mapeditor/tiled-api" />
/*
* rectangle-chain.js
*
* Example tool that places rectangle objects when the mouse is dragged over
* the map.
*/
tiled.registerTool("RectangleChain", {
name: "Draw Rectangle Chain",
icon: "rectangle-chain.svg",
mouseMoved(x, y /*, modifiers */) {
if (!this.pressed) {
return;
}
var dx = Math.abs(this.x - x);
var dy = Math.abs(this.y - y);
this.distance += Math.sqrt(dx*dx + dy*dy);
this.x = x;
this.y = y;
if (this.distance > 32) {
/** @type ObjectGroup */
var objectLayer = this.map.currentLayer;
if (objectLayer && objectLayer.isObjectLayer) {
var object = new MapObject(MapObject.Rectangle, ++this.counter);
object.x = Math.min(this.lastX, x);
object.y = Math.min(this.lastY, y);
object.width = Math.abs(this.lastX - x);
object.height = Math.abs(this.lastY - y);
objectLayer.addObject(object);
object.selected = true;
}
this.distance = 0;
this.lastX = x;
this.lastY = y;
}
},
mousePressed(button, x, y /*, modifiers */) {
this.pressed = true;
this.x = x;
this.y = y;
this.distance = 0;
this.counter = 0;
this.lastX = x;
this.lastY = y;
},
mouseReleased(/* button, x, y, modifiers */) {
this.pressed = false;
},
});