kineticscroll - Fix warnings and whitespace

master
Martin Boonk 2024-03-25 16:16:12 +01:00
parent ecbac6e017
commit 98325fa102
1 changed files with 21 additions and 20 deletions

View File

@ -9,17 +9,17 @@
remove = function() remove = function()
select = function(idx, touch) select = function(idx, touch)
} }
returns { returns {
scroll: int // current scroll amount scroll: int // current scroll amount
draw: function() // draw all draw: function() // draw all
drawItem : function(idx) // draw specific item drawItem : function(idx) // draw specific item
isActive : function() // is this scroller still active? isActive : function() // is this scroller still active?
} }
*/ */
if (!options) return Bangle.setUI(); // remove existing handlers if (!options) return Bangle.setUI(); // remove existing handlers
const MAX_VELOCITY=100; const MAX_VELOCITY=100;
let scheduledDraw; let scheduledDraw;
let velocity = 0; let velocity = 0;
@ -30,7 +30,7 @@
let menuScrollMin = 0|options.scrollMin; let menuScrollMin = 0|options.scrollMin;
let menuScrollMax = options.h*options.c - R.h; let menuScrollMax = options.h*options.c - R.h;
if (menuScrollMax<menuScrollMin) menuScrollMax=menuScrollMin; if (menuScrollMax<menuScrollMin) menuScrollMax=menuScrollMin;
const touchHandler = (_,e)=>{ const touchHandler = (_,e)=>{
if (e.y<R.y-4) return; if (e.y<R.y-4) return;
velocity = 0; velocity = 0;
@ -51,7 +51,7 @@
for (var i=a;i<=b;i++) for (var i=a;i<=b;i++)
options.draw(i, {x:R.x,y:idxToY(i),w:R.w,h:options.h}); options.draw(i, {x:R.x,y:idxToY(i),w:R.w,h:options.h});
g.setClipRect(0,0,g.getWidth()-1,g.getHeight()-1); g.setClipRect(0,0,g.getWidth()-1,g.getHeight()-1);
} };
const draw = () => { const draw = () => {
let dy = velocity; let dy = velocity;
@ -63,18 +63,18 @@
dy = s.scroll - menuScrollMin; dy = s.scroll - menuScrollMin;
velocity = 0; velocity = 0;
} }
s.scroll -= dy; s.scroll -= dy;
let oldScroll = rScroll; let oldScroll = rScroll;
rScroll = s.scroll &~1; rScroll = s.scroll &~1;
let d = oldScroll-rScroll; let d = oldScroll-rScroll;
if (Math.abs(velocity) > 0.01) if (Math.abs(velocity) > 0.01)
scheduledDraw = setTimeout(draw,0); scheduledDraw = setTimeout(draw,0);
else else
scheduledDraw = undefined; scheduledDraw = undefined;
if (!d) { if (!d) {
return; return;
} }
@ -83,7 +83,7 @@
let y = Math.max(R.y2-(1-d), R.y); let y = Math.max(R.y2-(1-d), R.y);
g.setClipRect(R.x,y,R.x2,R.y2); g.setClipRect(R.x,y,R.x2,R.y2);
let i = YtoIdx(y); let i = YtoIdx(y);
for (y = idxToY(i);y < R.y2;y+=options.h) { for (y = idxToY(i);y < R.y2;y+=options.h) {
options.draw(i, {x:R.x,y:y,w:R.w,h:options.h}); options.draw(i, {x:R.x,y:y,w:R.w,h:options.h});
i++; i++;
@ -93,7 +93,7 @@
g.setClipRect(R.x,R.y,R.x2,y); g.setClipRect(R.x,R.y,R.x2,y);
let i = YtoIdx(y); let i = YtoIdx(y);
y = idxToY(i); y = idxToY(i);
for (y = idxToY(i);y > R.y-options.h;y-=options.h) { for (y = idxToY(i);y > R.y-options.h;y-=options.h) {
options.draw(i, {x:R.x,y:y,w:R.w,h:options.h}); options.draw(i, {x:R.x,y:y,w:R.w,h:options.h});
i--; i--;
@ -101,7 +101,7 @@
} }
g.setClipRect(0,0,g.getWidth()-1,g.getHeight()-1); g.setClipRect(0,0,g.getWidth()-1,g.getHeight()-1);
}; };
const dragHandler = e=>{ const dragHandler = e=>{
if ((velocity <0 && e.dy>0) || (velocity > 0 && e.dy<0)){ if ((velocity <0 && e.dy>0) || (velocity > 0 && e.dy<0)){
velocity *= -1; velocity *= -1;
@ -117,45 +117,46 @@
accDy += e.dy; accDy += e.dy;
} }
velocity = accDy / (Date.now() - lastDragStart) * MAX_VELOCITY; velocity = accDy / (Date.now() - lastDragStart) * MAX_VELOCITY;
if (lastDragStart && e.b == 0){ if (lastDragStart && e.b == 0){
accDy = 0; accDy = 0;
lastDragStart = 0; lastDragStart = 0;
} }
velocity = E.clip(velocity,-MAX_VELOCITY,MAX_VELOCITY); velocity = E.clip(velocity,-MAX_VELOCITY,MAX_VELOCITY);
//lastDrag=Date.now(); //lastDrag=Date.now();
if (!scheduledDraw){ if (!scheduledDraw){
scheduledDraw = setTimeout(draw,0); scheduledDraw = setTimeout(draw,0);
} }
}; };
let uiOpts = { let uiOpts = {
mode : "custom", mode : "custom",
back : options.back, back : options.back,
drag : dragHandler, drag : dragHandler,
touch : touchHandler, touch : touchHandler,
redraw : uiDraw redraw : uiDraw
} };
if (options.remove) uiOpts.remove = () => { if (options.remove) uiOpts.remove = () => {
if (scheduledDraw) if (scheduledDraw)
clearTimeout(scheduledDraw); clearTimeout(scheduledDraw);
clearInterval(scheduledBrake); clearInterval(scheduledBrake);
options.remove(); options.remove();
} };
Bangle.setUI(uiOpts); Bangle.setUI(uiOpts);
function idxToY(i) { function idxToY(i) {
return i*options.h + R.y - rScroll; return i*options.h + R.y - rScroll;
} }
function YtoIdx(y) { function YtoIdx(y) {
return Math.floor((y + rScroll - R.y)/options.h); return Math.floor((y + rScroll - R.y)/options.h);
} }
let s = { let s = {
scroll : E.clip(0|options.scroll,menuScrollMin,menuScrollMax), scroll : E.clip(0|options.scroll,menuScrollMin,menuScrollMax),
draw : () => { draw : () => {
@ -172,7 +173,7 @@
g.setClipRect(0,0,g.getWidth()-1,g.getHeight()-1); g.setClipRect(0,0,g.getWidth()-1,g.getHeight()-1);
}, isActive : () => Bangle.uiRedraw == uiDraw }, isActive : () => Bangle.uiRedraw == uiDraw
}; };
let rScroll = s.scroll&~1; // rendered menu scroll (we only shift by 2 because of dither) let rScroll = s.scroll&~1; // rendered menu scroll (we only shift by 2 because of dither)
s.draw(); // draw the full scroller s.draw(); // draw the full scroller
g.flip(); // force an update now to make this snappier g.flip(); // force an update now to make this snappier