1
0
Fork 0
BangleApps/apps/golfview/golfview.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-04-05 18:43:31 +00:00
let course = require("Storage").readJSON("courses.json").holes;
2022-04-04 01:00:50 +00:00
let current_hole = 1;
let hole = course[current_hole.toString()];
2022-04-03 23:43:19 +00:00
function distance(a, b) {
2022-04-04 01:00:50 +00:00
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
2022-04-03 23:43:19 +00:00
}
2022-04-03 23:43:19 +00:00
function drawHole(l) {
2022-04-04 01:00:50 +00:00
//console.log(l);
2022-04-03 23:43:19 +00:00
let hole_straight_distance = distance(
hole.nodesXY[0],
hole.nodesXY[hole.nodesXY.length - 1]
);
let scale = 0.9 * l.h / hole_straight_distance;
2022-04-03 23:43:19 +00:00
let transform = {
2022-04-04 01:00:50 +00:00
x: l.x + l.w / 2, // center in the box
y: l.h * 0.95, // pad it just a bit
2022-04-03 23:43:19 +00:00
scale: scale, // scale factor (default 1)
rotate: hole.angle - Math.PI / 2.0, // angle in radians (default 0)
2022-04-04 01:00:50 +00:00
};
2022-04-03 21:57:02 +00:00
2022-04-03 23:43:19 +00:00
for (var feature of hole.features) {
//console.log(Object.keys(feature));
if (feature.type === "fairway") {
2022-04-04 01:00:50 +00:00
g.setColor(1, 0, 1); // magenta
2022-04-03 23:43:19 +00:00
} else if (feature.type === "tee") {
2022-04-04 01:00:50 +00:00
g.setColor(1, 0, 0); // red
2022-04-03 23:43:19 +00:00
} else if (feature.type === "green") {
2022-04-04 01:00:50 +00:00
g.setColor(0, 1, 0); // green
2022-04-03 23:43:19 +00:00
} else if (feature.type === "bunker") {
2022-04-04 01:00:50 +00:00
g.setColor(1, 1, 0); // yellow
} else if (feature.type === "water_hazard") {
g.setColor(0, 0, 1); // blue
2022-04-03 23:43:19 +00:00
} else {
continue;
}
var nodelist = [];
feature.nodesXY.forEach(function (node) {
nodelist.push(node.x);
nodelist.push(node.y);
});
newnodelist = g.transformVertices(nodelist, transform);
g.fillPoly(newnodelist, true);
//console.log(feature.type);
//console.log(newnodelist);
}
var waynodelist = [];
hole.nodesXY.forEach(function (node) {
waynodelist.push(node.x);
waynodelist.push(node.y);
});
2022-04-03 21:57:02 +00:00
2022-04-03 23:43:19 +00:00
newnodelist = g.transformVertices(waynodelist, transform);
2022-04-04 01:00:50 +00:00
g.setColor(0, 1, 1); // cyan
2022-04-03 23:43:19 +00:00
g.drawPoly(newnodelist);
}
2022-04-03 23:43:19 +00:00
// The layout, referencing the custom renderer
var Layout = require("Layout");
var layout = new Layout({
type: "h", c: [
{
type: "v", c: [
2022-04-04 01:00:50 +00:00
{ type: "txt", font: "10%", id: "hole", label: "Hole 1" },
{ type: "txt", font: "10%", id: "par", label: "Par 4" },
{ type: "txt", font: "10%", id: "hcp", label: "Hcp 15" },
{ type: "txt", font: "35%", id: "postyardage", label: "420" },
{ type: "txt", font: "20%", id: "measyardage", label: "69" },
2022-04-03 23:43:19 +00:00
]
},
{ type: "custom", render: drawHole, id: "graph", bgCol: g.theme.bg, fillx: 1, filly: 1 }
2022-04-04 01:00:50 +00:00
],
lazy: true
});
2022-04-03 23:43:19 +00:00
g.clear();
layout.render();
2022-04-04 01:00:50 +00:00
//layout.debug();
Bangle.on('swipe', function (direction) {
if (direction > 0) {
current_hole--;
} else {
current_hole++;
}
if (current_hole > 18) { current_hole = 1; }
if (current_hole < 1) { current_hole = 18; }
hole = course[current_hole.toString()];
layout.hole.label = "Hole " + current_hole;
layout.par.label = "Par " + course[current_hole.toString()].par;
layout.hcp.label = "Hcp " + course[current_hole.toString()].handicap;
layout.postyardage.label = 420; //TODO
g.clear();
layout.render();
});