mirror of
https://github.com/scratchfoundation/scratch-render.git
synced 2025-06-11 21:10:43 -04:00
Remove setRotationCenter API
This commit is contained in:
parent
1c0928f441
commit
d9c854f1fc
6 changed files with 24 additions and 82 deletions
|
@ -98,7 +98,8 @@ class BitmapSkin extends Skin {
|
|||
this._textureSize = BitmapSkin._getBitmapSize(bitmapData);
|
||||
|
||||
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
|
||||
this.setRotationCenter.apply(this, rotationCenter);
|
||||
this._rotationCenter[0] = rotationCenter[0];
|
||||
this._rotationCenter[1] = rotationCenter[1];
|
||||
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
}
|
||||
|
|
|
@ -1337,32 +1337,6 @@ class RenderWebGL extends EventEmitter {
|
|||
drawable.skin = this._allSkins[skinId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a drawable's skin rotation center.
|
||||
* @param {number} drawableID The drawable's id.
|
||||
* @param {Array.<number>} rotationCenter The rotation center for the skin.
|
||||
*/
|
||||
updateDrawableRotationCenter (drawableID, rotationCenter) {
|
||||
const drawable = this._allDrawables[drawableID];
|
||||
// TODO: https://github.com/LLK/scratch-vm/issues/2288
|
||||
if (!drawable) return;
|
||||
drawable.skin.setRotationCenter(rotationCenter[0], rotationCenter[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a drawable's skin and rotation center together.
|
||||
* @param {number} drawableID The drawable's id.
|
||||
* @param {number} skinId The skin to update to.
|
||||
* @param {Array.<number>} rotationCenter The rotation center for the skin.
|
||||
*/
|
||||
updateDrawableSkinIdRotationCenter (drawableID, skinId, rotationCenter) {
|
||||
const drawable = this._allDrawables[drawableID];
|
||||
// TODO: https://github.com/LLK/scratch-vm/issues/2288
|
||||
if (!drawable) return;
|
||||
drawable.skin = this._allSkins[skinId];
|
||||
drawable.skin.setRotationCenter(rotationCenter[0], rotationCenter[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a drawable's position.
|
||||
* @param {number} drawableID The drawable's id.
|
||||
|
@ -1456,9 +1430,6 @@ class RenderWebGL extends EventEmitter {
|
|||
if ('skinId' in properties) {
|
||||
this.updateDrawableSkinId(drawableID, properties.skinId);
|
||||
}
|
||||
if ('rotationCenter' in properties) {
|
||||
this.updateDrawableRotationCenter(drawableID, properties.rotationCenter);
|
||||
}
|
||||
drawable.updateProperties(properties);
|
||||
}
|
||||
|
||||
|
|
|
@ -58,16 +58,6 @@ class SVGSkin extends Skin {
|
|||
return this._svgRenderer.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the origin, in object space, about which this Skin should rotate.
|
||||
* @param {number} x - The x coordinate of the new rotation center.
|
||||
* @param {number} y - The y coordinate of the new rotation center.
|
||||
*/
|
||||
setRotationCenter (x, y) {
|
||||
const viewOffset = this._svgRenderer.viewOffset;
|
||||
super.setRotationCenter(x - viewOffset[0], y - viewOffset[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a MIP for a given scale.
|
||||
* @param {number} scale - The relative size of the MIP
|
||||
|
@ -174,7 +164,10 @@ class SVGSkin extends Skin {
|
|||
this.resetMIPs();
|
||||
|
||||
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
|
||||
this.setRotationCenter.apply(this, rotationCenter);
|
||||
const viewOffset = this._svgRenderer.viewOffset;
|
||||
this._rotationCenter[0] = rotationCenter[0] - viewOffset[0];
|
||||
this._rotationCenter[1] = rotationCenter[1] - viewOffset[1];
|
||||
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
});
|
||||
}
|
||||
|
|
33
src/Skin.js
33
src/Skin.js
|
@ -5,19 +5,6 @@ const twgl = require('twgl.js');
|
|||
const RenderConstants = require('./RenderConstants');
|
||||
const Silhouette = require('./Silhouette');
|
||||
|
||||
/**
|
||||
* Truncate a number into what could be stored in a 32 bit floating point value.
|
||||
* @param {number} num Number to truncate.
|
||||
* @return {number} Truncated value.
|
||||
*/
|
||||
const toFloat32 = (function () {
|
||||
const memory = new Float32Array(1);
|
||||
return function (num) {
|
||||
memory[0] = num;
|
||||
return memory[0];
|
||||
};
|
||||
}());
|
||||
|
||||
class Skin extends EventEmitter {
|
||||
/**
|
||||
* Create a Skin, which stores and/or generates textures for use in rendering.
|
||||
|
@ -101,26 +88,6 @@ class Skin extends EventEmitter {
|
|||
return [0, 0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the origin, in object space, about which this Skin should rotate.
|
||||
* @param {number} x - The x coordinate of the new rotation center.
|
||||
* @param {number} y - The y coordinate of the new rotation center.
|
||||
* @fires Skin.event:WasAltered
|
||||
*/
|
||||
setRotationCenter (x, y) {
|
||||
const emptySkin = this.size[0] === 0 && this.size[1] === 0;
|
||||
// Compare a 32 bit x and y value against the stored 32 bit center
|
||||
// values.
|
||||
const changed = (
|
||||
toFloat32(x) !== this._rotationCenter[0] ||
|
||||
toFloat32(y) !== this._rotationCenter[1]);
|
||||
if (!emptySkin && changed) {
|
||||
this._rotationCenter[0] = x;
|
||||
this._rotationCenter[1] = y;
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the center of the current bounding box
|
||||
* @return {Array<number>} the center of the current bounding box
|
||||
|
|
10
test/fixtures/MockSkin.js
vendored
10
test/fixtures/MockSkin.js
vendored
|
@ -8,6 +8,16 @@ class MockSkin extends Skin {
|
|||
get size () {
|
||||
return this.dimensions || [0, 0];
|
||||
}
|
||||
|
||||
set rotationCenter (center) {
|
||||
this._rotationCenter[0] = center[0];
|
||||
this._rotationCenter[1] = center[1];
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
}
|
||||
|
||||
get rotationCenter () {
|
||||
return this._rotationCenter;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MockSkin;
|
||||
|
|
|
@ -48,11 +48,11 @@ test('translate by costume center', t => {
|
|||
drawable.skin = new MockSkin();
|
||||
drawable.skin.size = [200, 50];
|
||||
|
||||
drawable.skin.setRotationCenter(1, 0);
|
||||
drawable.skin.rotationCenter = [1, 0];
|
||||
expected.initFromBounds(-1, 199, -50, 0);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
drawable.skin.setRotationCenter(0, -2);
|
||||
drawable.skin.rotationCenter = [0, -2];
|
||||
expected.initFromBounds(0, 200, -52, -2);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
|
@ -73,7 +73,7 @@ test('translate and rotate', t => {
|
|||
expected.initFromBounds(-49, 1, -198, 2);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
drawable.skin.setRotationCenter(100, 25);
|
||||
drawable.skin.rotationCenter = [100, 25];
|
||||
drawable.updateProperties({direction: 270, position: [0, 0]});
|
||||
expected.initFromBounds(-100, 100, -25, 25);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
@ -89,7 +89,7 @@ test('rotate by non-right-angles', t => {
|
|||
const drawable = new Drawable();
|
||||
drawable.skin = new MockSkin();
|
||||
drawable.skin.size = [10, 10];
|
||||
drawable.skin.setRotationCenter(5, 5);
|
||||
drawable.skin.rotationCenter = [5, 5];
|
||||
|
||||
expected.initFromBounds(-5, 5, -5, 5);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
@ -111,11 +111,11 @@ test('scale', t => {
|
|||
expected.initFromBounds(0, 200, -25, 0);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
drawable.skin.setRotationCenter(0, 25);
|
||||
drawable.skin.rotationCenter = [0, 25];
|
||||
expected.initFromBounds(0, 200, -12.5, 12.5);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
drawable.skin.setRotationCenter(150, 50);
|
||||
drawable.skin.rotationCenter = [150, 50];
|
||||
drawable.updateProperties({scale: [50, 50]});
|
||||
expected.initFromBounds(-75, 25, 0, 25);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
@ -129,12 +129,12 @@ test('rotate and scale', t => {
|
|||
drawable.skin = new MockSkin();
|
||||
drawable.skin.size = [100, 1000];
|
||||
|
||||
drawable.skin.setRotationCenter(50, 50);
|
||||
drawable.skin.rotationCenter = [50, 50];
|
||||
expected.initFromBounds(-50, 50, -950, 50);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
drawable.updateProperties({scale: [40, 60]});
|
||||
drawable.skin.setRotationCenter(50, 50);
|
||||
drawable.skin.rotationCenter = [50, 50];
|
||||
expected.initFromBounds(-20, 20, -570, 30);
|
||||
t.same(snapToNearest(drawable.getAABB()), expected);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue