Graphics: drawRect,fillRect,clearRect are now able to take an object as the first argument

This commit is contained in:
Gordon Williams 2021-10-27 09:52:49 +01:00
parent ab92dc8f4f
commit 05435fca26
4 changed files with 36 additions and 10 deletions

View File

@ -65,6 +65,7 @@
Bangle.js2: Fix 'stuck' bottom row of pixels if scrolling downwards with g.scroll(0,1)
Bangle.js: Fix issue where minified E.showMenu was creating a global var called 'c'
Fix string formatting for "Field or method X does not already exist" error
Graphics: drawRect,fillRect,clearRect are now able to take an object as the first argument
2v10 : Bangle.js: Improved HRM calculations - swapped autocorrelation for bandpass filter
Bangle.js: Significantly improved step counting algorithm using bandpass filter (fix #1846)

View File

@ -153,6 +153,7 @@ void graphicsStructInit(JsGraphics *gfx, int width, int height, int bpp) {
}
bool graphicsGetFromVar(JsGraphics *gfx, JsVar *parent) {
if (!parent) return false;
gfx->graphicsVar = parent;
JsVar *data = jsvObjectGetChild(parent, JS_HIDDEN_CHAR_STR"gfx", 0);
assert(data);

View File

@ -901,7 +901,25 @@ JsVar *jswrap_graphics_clear(JsVar *parent, bool resetState) {
return jsvLockAgain(parent);
}
void _jswrap_graphics_getRect(JsVar *opt, int *x1, int *y1, int *x2, int *y2) {
if (jsvIsObject(opt)) {
int w = -1,h = -1;
jsvConfigObject configs[] = {
{"x", JSV_INTEGER, x1},
{"y", JSV_INTEGER, y1},
{"x1", JSV_INTEGER, x1},
{"y1", JSV_INTEGER, y1},
{"x2", JSV_INTEGER, x2},
{"y2", JSV_INTEGER, y2},
{"w", JSV_INTEGER, &w},
{"h", JSV_INTEGER, &h},
};
jsvReadConfigObject(opt, configs, sizeof(configs) / sizeof(jsvConfigObject));
if (w>=0) *x2 = *x1 + w;
if (h>=0) *y2 = *y1 + w;
} else
*x1 = jsvGetInteger(opt);
}
/*JSON{
"type" : "method",
@ -909,7 +927,7 @@ JsVar *jswrap_graphics_clear(JsVar *parent, bool resetState) {
"name" : "fillRect",
"generate" : "jswrap_graphics_fillRect",
"params" : [
["x1","int32","The left X coordinate"],
["x1","JsVar","The left X coordinate OR an object containing `{x,y,x2,y2}` or `{x,y,w,h}`"],
["y1","int32","The top Y coordinate"],
["x2","int32","The right X coordinate"],
["y2","int32","The bottom Y coordinate"]
@ -919,7 +937,9 @@ JsVar *jswrap_graphics_clear(JsVar *parent, bool resetState) {
}
Fill a rectangular area in the Foreground Color
*/
JsVar *jswrap_graphics_fillRect(JsVar *parent, int x1, int y1, int x2, int y2) {
JsVar *jswrap_graphics_fillRect(JsVar *parent, JsVar *opt, int y1, int x2, int y2) {
int x1;
_jswrap_graphics_getRect(opt, &x1, &y1, &x2, &y2);
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
graphicsFillRect(&gfx, x1,y1,x2,y2,gfx.data.fgColor);
graphicsSetVar(&gfx); // gfx data changed because modified area
@ -933,7 +953,7 @@ JsVar *jswrap_graphics_fillRect(JsVar *parent, int x1, int y1, int x2, int y2) {
"ifndef" : "SAVE_ON_FLASH",
"generate" : "jswrap_graphics_clearRect",
"params" : [
["x1","int32","The left X coordinate"],
["x1","JsVar","The left X coordinate OR an object containing `{x,y,x2,y2}` or `{x,y,w,h}`"],
["y1","int32","The top Y coordinate"],
["x2","int32","The right X coordinate"],
["y2","int32","The bottom Y coordinate"]
@ -943,7 +963,9 @@ JsVar *jswrap_graphics_fillRect(JsVar *parent, int x1, int y1, int x2, int y2) {
}
Fill a rectangular area in the Background Color
*/
JsVar *jswrap_graphics_clearRect(JsVar *parent, int x1, int y1, int x2, int y2) {
JsVar *jswrap_graphics_clearRect(JsVar *parent, JsVar *opt, int y1, int x2, int y2) {
int x1;
_jswrap_graphics_getRect(opt, &x1, &y1, &x2, &y2);
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
graphicsFillRect(&gfx, x1,y1,x2,y2,gfx.data.bgColor);
graphicsSetVar(&gfx); // gfx data changed because modified area
@ -956,7 +978,7 @@ JsVar *jswrap_graphics_clearRect(JsVar *parent, int x1, int y1, int x2, int y2)
"name" : "drawRect",
"generate" : "jswrap_graphics_drawRect",
"params" : [
["x1","int32","The left X coordinate"],
["x1","JsVar","The left X coordinate OR an object containing `{x,y,x2,y2}` or `{x,y,w,h}`"],
["y1","int32","The top Y coordinate"],
["x2","int32","The right X coordinate"],
["y2","int32","The bottom Y coordinate"]
@ -966,7 +988,9 @@ JsVar *jswrap_graphics_clearRect(JsVar *parent, int x1, int y1, int x2, int y2)
}
Draw an unfilled rectangle 1px wide in the Foreground Color
*/
JsVar *jswrap_graphics_drawRect(JsVar *parent, int x1, int y1, int x2, int y2) {
JsVar *jswrap_graphics_drawRect(JsVar *parent, JsVar *opt, int y1, int x2, int y2) {
int x1;
_jswrap_graphics_getRect(opt, &x1, &y1, &x2, &y2);
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
graphicsDrawRect(&gfx, x1,y1,x2,y2);
graphicsSetVar(&gfx); // gfx data changed because modified area

View File

@ -39,9 +39,9 @@ int jswrap_graphics_getWidthOrHeight(JsVar *parent, bool height);
int jswrap_graphics_getBPP(JsVar *parent);
JsVar *jswrap_graphics_reset(JsVar *parent);
JsVar *jswrap_graphics_clear(JsVar *parent, bool resetState);
JsVar *jswrap_graphics_fillRect(JsVar *parent, int x1, int y1, int x2, int y2);
JsVar *jswrap_graphics_clearRect(JsVar *parent, int x1, int y1, int x2, int y2);
JsVar *jswrap_graphics_drawRect(JsVar *parent, int x1, int y1, int x2, int y2);
JsVar *jswrap_graphics_fillRect(JsVar *parent, JsVar *opt, int y1, int x2, int y2);
JsVar *jswrap_graphics_clearRect(JsVar *parent, JsVar *opt, int y1, int x2, int y2);
JsVar *jswrap_graphics_drawRect(JsVar *parent, JsVar *opt, int y1, int x2, int y2);
JsVar *jswrap_graphics_drawCircle(JsVar *parent, int x, int y, int rad);
JsVar *jswrap_graphics_drawCircleAA(JsVar *parent, int x, int y, int r);
JsVar *jswrap_graphics_fillCircle(JsVar *parent, int x, int y, int rad);