Skip to content

Instantly share code, notes, and snippets.

@bjorn
Created March 26, 2019 22:32
Show Gist options
  • Save bjorn/092064e6a13619993d24fcccd596dd5b to your computer and use it in GitHub Desktop.
Save bjorn/092064e6a13619993d24fcccd596dd5b to your computer and use it in GitHub Desktop.
Tiled Custom Tool
var tool = tiled.registerTool('MyCustomTool', {
name: "My Custom Tool",
icon: "foo.png", // todo: this would be looked up and loaded somehow
shortcut: undefined,
statusInfo: undefined,
cursor: undefined,
activated: function() { tiled.log("tool.activated") },
deactivated: function() { tiled.log("tool.deactivated") },
keyPressed: function(key, modifiers) { tiled.log("tool.keyPressed") },
mouseEntered: function() { tiled.log("tool.mouseEntered") },
mouseLeft: function() { tiled.log("tool.mouseLeft") },
mouseMoved: function(x, y, modifiers) {
tiled.log("tool.mouseMoved " + this.pressed)
if (this.pressed) {
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
tiled.log("drag distance: " + this.distance)
if (this.distance > 32) {
var objectLayer = tiled.activeAsset.currentLayer
if (objectLayer && objectLayer.isObjectLayer) {
var object = new MapObject()
object.name = ++this.counter
object.x = x
object.y = y
objectLayer.addObject(object)
}
this.distance = 0
}
}
},
mousePressed: function(button, x, y, modifiers) {
tiled.log("tool.mousePressed")
this.pressed = true
this.x = x
this.y = y
this.distance = 0
this.counter = 0
},
mouseReleased: function(button, x, y, modifiers) {
tiled.log("tool.mouseReleased")
this.pressed = false
},
mouseDoubleClicked: function(button, x, y, modifiers) { tiled.log("tool.mouseDoubleClicked") },
modifiersChanged: function(modifiers) { tiled.log("tool.modifiersChanged") },
mapChanged: function(oldMap, newMap) { tiled.log("tool.mapChanged(" + oldMap + ", " + newMap + ")") },
// todo: how to hook into the tool bar? list of actions?
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment