using Toybox.WatchUi;
using Toybox.Application;
using Toybox.Position;
class MapWidgetView extends WatchUi.View
{ var screenCenterPoint;
var curClip;
var offscreenBuffer;
function initialize() {
View.initialize();
}
// Load your resources here
function onLayout(dc) {
x = dc.getWidth()/2;
y = dc.getHeight()/2;
screenCenterPoint = [dc.getWidth()/2, dc.getHeight()/2];}
const ut=83.208, un=-23.709,
dt=33.910, dn=29.895,
imgs=250, rx=2000/imgs, ry=2000/imgs,
tt=ut-dt, nn=dn-un;
var x,y, ar=new[9], pos, mx,my;
// Called when this View is brought to the foreground. Restore
// the state of this View and prepare it to be shown. This includes
// loading resources into memory.
function onShow() {
}
function find(t,n)
{
var ayy = ((ut-t)*ry/tt),
axx = (rx-(dn-n)*rx/nn),
yy = ayy.toNumber(),
xx = axx.toNumber();
my = (ayy-yy)*imgs;
mx = (axx-xx)*imgs;
var npos = yy*rx+xx;
if (pos != npos)
{
var r = [ :m0, :m1, :m2, :m3, :m4, :m5, :m6, :m7,
:m8, :m9,:m10,:m11,:m12,:m13,:m14,:m15,
:m16,:m17,:m18,:m19,:m20,:m21,:m22,:m23,
:m24,:m25,:m26,:m27,:m28,:m29,:m30,:m31,
:m32,:m33,:m34,:m35,:m36,:m37,:m38,:m39,
:m40,:m41,:m42,:m43,:m44,:m45,:m46,:m47,
:m48,:m49,:m50,:m51,:m52,:m53,:m54,:m55,
:m56,:m57,:m58,:m59,:m60,:m61,:m62,:m63];
pos = npos;
for(var i=0, iy=yy-1; iy<=yy+1; iy++) {
for(var ix=xx-1; ix<=xx+1; i++, ix++) {
if(iy>=0 && iy<ry && ix>=0 && ix<rx) {
ar[i] = Rez.Drawables[r[iy*rx+ix]];}
else {ar[i]=null;} }}
}
}
function generateHandCoordinates(centerPoint, angle, handLength, tailLength, width) {
// Map out the coordinates of the watch hand
var coords = [[-(width / 1), tailLength], [-(width / 4), -handLength], [width / 5, -handLength], [width / 1, tailLength]];
var result = new [4];
var cos = Math.cos(angle);
var sin = Math.sin(angle);
// Transform the coordinates
for (var i = 0; i < 4; i += 1) {
var x = (coords[i][0] * cos) - (coords[i][1] * sin) + 0.5;
var y = (coords[i][0] * sin) + (coords[i][1] * cos) + 0.5;
result[i] = [centerPoint[0] + x, centerPoint[1] + y];
}
return result;
}
// Update the view
function onUpdate(dc) {
{ var g = Position.getInfo.Location;
var width;
var height;
var screenWidth = dc.getWidth();
var heading = Position.getInfo().currentHeading;
var minuteHandAngle;
var targetDc = null;
if(null != offscreenBuffer) {
dc.clearClip();
curClip = null;
// If we have an offscreen buffer that we are using to draw the background,
// set the draw context of that buffer as our target.
targetDc = offscreenBuffer.getDc();
} else {
targetDc = dc;
if (g!=null)
{
g = g.toDegrees();
find(g[0], g[1]);
g = null;
for(var i=0, yy=y-imgs-my; yy<y+imgs; yy+=imgs) {
for(var xx=x-imgs-mx; xx<x+imgs; xx+=imgs, i++) {
if(ar[i]!=null){
dc.drawBitmap(xx, yy, WatchUi.loadResource(ar[i]));}}}
}
//Use white to draw the hour and minute hands
width = targetDc.getWidth();
height = targetDc.getHeight();
targetDc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_TRANSPARENT);
minuteHandAngle = heading;
targetDc.fillPolygon(generateHandCoordinates(screenCenterPoint, minuteHandAngle, 22, 0, 8));
dc.setColor(0xFF0000,-1);
dc.fillRectangle(x-4, y-4, 8,8);
// Call the parent onUpdate function to redraw the layout
View.onUpdate(dc);
}
// Called when this View is removed from the screen. Save the
// state of this View here. This includes freeing resources from
// memory.
}
}}