Skip to content

Instantly share code, notes, and snippets.

@nmtitov
Created September 2, 2012 17:44
Show Gist options
  • Select an option

  • Save nmtitov/3602146 to your computer and use it in GitHub Desktop.

Select an option

Save nmtitov/3602146 to your computer and use it in GitHub Desktop.
var BrushToolDelegate = (function() {
function BrushToolDelegate (tool) {
var self = this;
self.tool = tool;
self.picked = null;
}
/* abstract - override this! */
BrushToolDelegate.prototype.outcome = function () {}
BrushToolDelegate.prototype.didApplyTo = function (destPt) {}
/* */
BrushToolDelegate.prototype.isAlreadyApplied = function (destPt) {
var self = this;
validatePoint(destPt);
var found = self.tool.em.findEntities(function (entity) {
if (entity.type) {
var type = entity.type.type;
var isSameType = type === self.outcome();
if (isSameType) {
if (entity.position) {
var existingPt = entity.position.initial;
var isSamePt = pointCmp(existingPt, destPt);
if (isSamePt) {
return true;
}
}
}
}
});
var empty = found.length == 0;
return !empty
}
BrushToolDelegate.prototype.shouldApplyTo = function (destPt) {
var self = this;
validatePoint(destPt);
var isApplied = self.isAlreadyApplied(destPt);
return !isApplied;
}
BrushToolDelegate.prototype.shouldCleanAt = function (destPt) {
var self = this;
validatePoint(destPt);
var isApplied = self.isAlreadyApplied(destPt);
return isApplied;
}
// 'singleton' brush - remove everything from cell, not just one entity
BrushToolDelegate.prototype.didCleanAt = function (destPt) {
var self = this;
validatePoint(destPt);
self.tool.em.removeEntities(function (entity) {
if (entity.type) {
var type = entity.type.type;
var isSameType = type === self.outcome();
if (isSameType) {
if (entity.position) {
var existingPt = entity.position.initial;
var isSamePt = pointCmp(existingPt, destPt);
if (isSamePt) {
return true;
}
}
}
}
});
}
BrushToolDelegate.prototype.didCutAt = function(destPt) {
var self = this;
var found = self.tool.em.findEntities(function (entity) {
if (entity.type) {
var type = entity.type.type;
var isSameType = type === self.outcome();
if (isSameType) {
if (entity.position) {
var existingPt = entity.position.initial;
var isSamePt = pointCmp(existingPt, destPt);
if (isSamePt) {
return true;
}
}
}
}
});
var empty = found.length == 0;
if (!empty) {
// FIXME we assume that we have only one entity of each type in one cell
var desiredEntity = found[0];
self.picked = desiredEntity;
}
}
return BrushToolDelegate;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment