Skip to content

Instantly share code, notes, and snippets.

@talentless
Created April 10, 2012 18:30
Show Gist options
  • Save talentless/2353518 to your computer and use it in GitHub Desktop.
Save talentless/2353518 to your computer and use it in GitHub Desktop.
Crafty.c("AnchorPoint", {init: function() { this.requires("2D"); this._anchorPoint = [0.5,0.5]; this._defineGetterSetter_setter(); this._defineGetterSetter_defineProperty(); },
_anchorPoint: [0.5,0.5],
_defineGetterSetter_setter: function() {
//create getters and setters using __defineSetter__ and __defineGetter__
this.__defineSetter__('x', function (v) { this._attr('_x', v - this._anchorPoint[0] * this._w); });
this.__defineSetter__('y', function (v) { this._attr('_y', v - this._anchorPoint[1] * this._h); });
this.__defineSetter__('w', function (v) { this._attr('_w', v); this._origin.x=this._anchorPoint[0]*this._w; this.x = this.x; });
this.__defineSetter__('h', function (v) { this._attr('_h', v); this._origin.y=this._anchorPoint[1]*this._h; this.y = this.y;});
this.__defineSetter__('anchorPoint', function (v) { var xd = (v[0] - this._anchorPoint[0]) * this._w; this._x = this._x + xd; this._attr('_anchorPoint', v); this.origin(this._anchorPoint[0]*this._w,this._anchorPoint[1]*this._h); this._origin.x=this._anchorPoint[0]*this._w; this._origin.y=this._anchorPoint[1]*this._h; }); // holy messification
this.__defineGetter__('x', function () { return this._x + this._anchorPoint[0] * this._w; });
this.__defineGetter__('y', function () { return this._y + this._anchorPoint[1] * this._h; });
this.__defineGetter__('anchorPoint', function () { return this._anchorPoint; });
},
_defineGetterSetter_defineProperty: function() {
Object.defineProperty(this, 'x', {
set: function (v) { this._attr('_x', v - this._anchorPoint[0] * this._w); }
, get: function () { return this._x + this._anchorPoint[0] * this._w; }
, configurable: true
});
Object.defineProperty(this, 'y', {
set: function (v) { this._attr('_y', v - this._anchorPoint[1] * this._h); }
, get: function () { return this._y + this._anchorPoint[1] * this._h; }
, configurable: true
});
Object.defineProperty(this, 'w', {
set: function (v) { var x = this.x; this._attr('_w', v); this._origin.x=this._anchorPoint[0]*this._w; this.x = x; }
, get: function () { return this._w; }
, configurable: true
});
Object.defineProperty(this, 'h', {
set: function (v) { var y = this.y; this._attr('_h', v); this._origin.y=this._anchorPoint[1]*this._h; this.y = y; }
, get: function () { return this._h; }
, configurable: true
});
Object.defineProperty(this, 'anchorPoint', {
set: function (v) { var xd = (v[0] - this._anchorPoint[0]) * this._w; this._x = this._x - xd; var yd = (v[1] - this._anchorPoint[1]) * this._h; this._y = this._y - yd; this._attr('_anchorPoint', v); this.origin(this._anchorPoint[0]*this._w,this._anchorPoint[1]*this._h); this._origin.x=this._anchorPoint[0]*this._w; this._origin.y=this._anchorPoint[1]*this._h; }
, get: function () { return this._anchorPoint; }
, configurable: true
});
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment