From 54c0c4081df55f293bfb5e3a0871aade286608ab Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Tue, 23 Sep 2014 18:17:47 +0200 Subject: [PATCH] Add half-width, half-height, and center accessors to AAAB. Allow anchorPoint to take one AABB. --- src/yuu/core.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/yuu/core.js b/src/yuu/core.js index 1d64e98..a2967b0 100644 --- a/src/yuu/core.js +++ b/src/yuu/core.js @@ -436,25 +436,29 @@ gui.Window.get().showDevTools(); }, "show developer tools"); - yuu.anchorPoint = function (anchor, x0, y0, x1, y1) { + var anchorPoint = yuu.anchorPoint = yf.argcd( /** Calculate the anchor point for a box given extents and anchor mode This function is the inverse of yuu.bottomLeft. */ - switch (anchor) { - case "center": return [(x0 + x1) / 2, (y0 + y1) / 2]; - case "top": return [(x0 + x1) / 2, y1]; - case "bottom": return [(x0 + x1) / 2, y0]; - case "left": return [x0, (y0 + y1) / 2]; - case "right": return [x1, (y0 + y1) / 2]; - - case "topleft": return [x0, y1]; - case "topright": return [x1, y1]; - case "bottomleft": return [x0, y0]; - case "bottomright": return [x0, y0]; - default: return [anchor[0], anchor[1]]; + function (anchor, aabb) { + return anchorPoint(anchor, aabb.x0, aabb.y0, aabb.x1, aabb.y1); + }, + function (anchor, x0, y0, x1, y1) { + switch (anchor) { + case "center": return [(x0 + x1) / 2, (y0 + y1) / 2]; + case "top": return [(x0 + x1) / 2, y1]; + case "bottom": return [(x0 + x1) / 2, y0]; + case "left": return [x0, (y0 + y1) / 2]; + case "right": return [x1, (y0 + y1) / 2]; + case "topleft": return [x0, y1]; + case "topright": return [x1, y1]; + case "bottomleft": return [x0, y0]; + case "bottomright": return [x0, y0]; + default: return [anchor[0], anchor[1]]; + } } - }; + ); yuu.bottomLeft = function (anchor, x, y, w, h) { /** Calculate the bottom-left for a box given size and anchor mode @@ -733,6 +737,13 @@ } ), + xc: { + get: function () { return (this.x0 + this.x1) / 2; } + }, + yc: { + get: function () { return (this.y0 + this.y1) / 2; } + }, + w: { get: function () { return this.x1 - this.x0; }, set: function (w) { this.x1 = this.x0 + w; } @@ -745,6 +756,9 @@ size: { swizzle: "wh" }, + hw: { get: function () { return this.w / 2; } }, + hh: { get: function () { return this.h / 2; } }, + contains: yf.argcd( function (p) { return this.contains(p.x, p.y); }, function (x, y) { -- 2.20.1