using Toybox.Graphics as Gfx; using Toybox.Math as Math; using C as C; class Compass { var title; var max; var tick; var len; var width; var color; var h_vert = new [4]; var a_vert = new [8]; var letter_N; var letter_E; var letter_S; var letter_W; public function initialize(title, max, tick, len, width, color) { me.title = title; me.max = max; me.tick = tick; me.len = len; me.width = width; me.color = color; // // One hash mark. This is translated/rotated around // the center of the screen to create all the hash marks. // var w2 = width / 2; h_vert[0] = [ -w2, -max + len]; h_vert[1] = [ -w2, -max ]; h_vert[2] = [ +w2, -max ]; h_vert[3] = [ +w2, -max + len ]; // // Arrow pointer. This will not be translated so set // the actual coordinates for the pointer. // var l = me.max / 2; var w4 = l / 8; var t = w4 / 1.5; var x = C.cx; var y = C.cy - me.max + l + len; a_vert[0] = [ x - w4, y ]; a_vert[1] = [ x - w4, y - (w4*4) ]; a_vert[2] = [ x - w4 - t, y - (w4*4) ]; a_vert[3] = [ x, y - l ]; a_vert[4] = [ x + w4 + t, y - (w4*4) ]; a_vert[5] = [ x + w4, y - (w4*4) ]; a_vert[6] = [ x + w4, y ]; a_vert[7] = [ x - w4, y ]; letter_N = new Letter('N', max, len, len, 3, color); letter_E = new Letter('E', max, len, len, 3, color); letter_S = new Letter('S', max, len, len, 3, color); letter_W = new Letter('W', max, len, len, 3, color); } public function arrow(dc) { C.draw_lines(dc, a_vert, 2); } public function show(dc, h) { var v, aim; var fhs = dc.getFontHeight(Graphics.FONT_SMALL); var fht = dc.getFontHeight(Graphics.FONT_XTINY); if (h == null) { dc.drawText(C.cx, C.cy - (fhs/2), Graphics.FONT_SMALL, "Wait GPS", Graphics.TEXT_JUSTIFY_CENTER); dc.drawText(C.cx, C.cy + (fht/2) + 4, Graphics.FONT_XTINY, C.gps_timer, Graphics.TEXT_JUSTIFY_CENTER); return; } aim = h[0]; while (aim < 0) { aim += 360; } arrow(dc); var off = -aim; for (var i = 0; i < 360; i += tick) { if (i == 0) { letter_N.show(dc, off); } else if (i == 90) { letter_E.show(dc, off); } else if (i == 180) { letter_S.show(dc, off); } else if (i == 270) { letter_W.show(dc, off); } else { v = C.translate(h_vert, C.cx, C.cy, i + off); dc.fillPolygon(v); } } dc.setPenWidth(3); dc.drawCircle(C.cx, C.cy, C.radius); dc.drawText(C.cx, C.cy - (fhs/2), Graphics.FONT_SMALL, aim.format("%.1f"), Graphics.TEXT_JUSTIFY_CENTER); var sub = h[1]; if (h[2] != null) { sub += " " + h[2].format("%.1f") + "°"; } dc.drawText(C.cx, C.cy + (fht/2) + 4, Graphics.FONT_XTINY, sub, Graphics.TEXT_JUSTIFY_CENTER); } }