This is my delegate and view. The main behaviour of this widget is to show an offline map made up from a bitmap that shows my location according to gps /position info. I’m struggling with the delegate to allow the user to touch on the screen in the top, bottom, left or right to change the position, this would allow for panning the map, however on interacting with the touchscreen on the sim I get this error:
Error: Not Enough Arguments Error
Details: Failed invoking <symbol>
Stack:
- onSelect() at C:\Users\Me\eclipse-workspace\MyProjects\Map Widget\source\MapWidgetDelegate.mc:28 0x10001285
- handleEvent() at T:\mbsimulator\submodules\technology\monkeybrains\virtual-machine\api\WatchUi.mb:1261 0x30002484
Delegate:
using Toybox.WatchUi;
using Toybox.Graphics;
using Toybox.System;
using Toybox.Position;
var last_key = null;
var last_behavior = null;
var buttons_pressed = null;
var buttons_expected = null;
var g = Position.getInfo().position;
class MapWidgetDelegate extends WatchUi.BehaviorDelegate {
enum {
ON_NEXT_PAGE,
ON_PREV_PAGE,
ON_MENU,
ON_BACK,
ON_NEXT_MODE,
ON_PREV_MODE,
ON_SELECT
}
function initialize() {
BehaviorDelegate.initialize();
}
function onSelect(evt)
{
var coord = evt.getCoordinates();
var x = coord[0];
var y = coord[1];
if (posnInfo != null) {
if(x > 0 && x < 60 && y > 40 && y < 200)
{
System.println("left");
g[1] = (g[1] - 1.0);
WatchUi.requestUpdate();
}
else if(x > 180 && x < 240 && y > 40 && y < 200)
{
System.println("right");
g[1] = (g[1] + 1.0);
WatchUi.requestUpdate();
}
else if(x > 60 && x < 180 && y > 0 && y < 60)
{
System.println("up");
g[0] = (g[0] + 1.0);
WatchUi.requestUpdate();
}
else if(x > 60 && x < 180 && y > 180 && y < 240)
{
System.println("down");
g[0] = (g[0] - 1.0);
WatchUi.requestUpdate();
}
else
{
System.println("cl");
WatchUi.requestUpdate();
}
}
else
{
System.println("no");
}
}
}
View:
using Toybox.WatchUi;
using Toybox.Application;
using Toybox.Position;
using Toybox.Sensor;
using Toybox.Timer;
var posnInfo = null;
class MapWidgetView extends WatchUi.View
{
var screenCenterPoint;
var curClip;
var offscreenBuffer;
var gps;
var up;
var down;
var right;
var left;
function initialize() {
View.initialize();
gps = new WatchUi.Bitmap({:rezId=>Rez.Drawables.gps,:locX=>100,:locY=>200});
up = new WatchUi.Bitmap({:rezId=>Rez.Drawables.up,:locX=>100,:locY=>140});
down = new WatchUi.Bitmap({:rezId=>Rez.Drawables.down,:locX=>100,:locY=>200});
right = new WatchUi.Bitmap({:rezId=>Rez.Drawables.left,:locX=>140,:locY=>100});
left = new WatchUi.Bitmap({:rezId=>Rez.Drawables.right,:locX=>200,:locY=>100});
}
function onShow() {
var timer = new Timer.Timer();
timer.start(method(:timerCallback), 1000, true);
}
function timerCallback() {
WatchUi.requestUpdate();
}
function onStop() {
var timer = Timer.stop();
timer.stop();
}
// Load your resources here
function onLayout(dc) {
x = dc.getWidth()/2;
y = dc.getHeight()/2;
screenCenterPoint = [dc.getWidth()/2, dc.getHeight()/2];}
const ut=84.950, un=-25.097,
dt=28.838, dn=30.697,
imgs=250, rx=3000/imgs, ry=3000/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 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,:m64,:m65,:m66,:m67,:m68,:m69,:m70,:m71,
:m72, :m73,:m74,:m75,:m76,:m77, :m78,:m79,:m80,:m81,:m81,:m83,
:m84, :m85,:m86,:m87,:m88, :m89,:m90,:m91,:m92,:m93,:m94,:m95,
:m96,:m97,:m98,:m99,:m100,:m101,:m102,:m103,:m104,:m105,:m106,:m107,
:m108, :m109,:m110, :m111,:m112,:m113,:m114,:m115,:m116,:m117,:m118,:m119,
:m120,:m121, :m122,:m123,:m124,:m125,:m126,:m127, :m128,:m129,:m130,:m131,
:m132,:m133,:m134,:m135,:m136,:m137,:m138,:m139,:m140,:m141,:m142,:m143];
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 string;
var width;
var height;
var screenWidth = dc.getWidth();
var heading = Sensor.getInfo().heading;
var minuteHandAngle;
var targetDc = null;
var targDc = null;
var r;
var g = Position.getInfo().position;
View.onUpdate(dc);
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();
targDc = offscreenBuffer.getDc();
} else {
targetDc = dc;
targDc = 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();
width = targDc.getWidth();
height = targDc.getHeight();
targetDc.setColor(0x0000FF, Graphics.COLOR_TRANSPARENT);
targDc.setColor(0xFF0000, Graphics.COLOR_TRANSPARENT);
minuteHandAngle = heading;
targetDc.fillPolygon(generateHandCoordinates(screenCenterPoint, minuteHandAngle, 22, 0, 8));
targDc.fillPolygon(generateHandCoordinates(screenCenterPoint, minuteHandAngle, 20, 0, 6));
dc.setColor(0x0000FF,-1);
dc.fillRectangle(x-4, y-4, 8,8);
up.draw(dc);
down.draw(dc);
left.draw(dc);
right.draw(dc);
dc.setColor( Graphics.COLOR_GREEN, Graphics.COLOR_TRANSPARENT );
if( posnInfo != null ) {
gps.draw(dc);
}
else {dc.setColor( Graphics.COLOR_RED, Graphics.COLOR_TRANSPARENT );
dc.drawText( (dc.getWidth() / 2), (dc.getHeight() / 2), Graphics.FONT_TINY, "No position info", Graphics.TEXT_JUSTIFY_CENTER );
}
}
}
} function setPosition(info) {
posnInfo = info;
WatchUi.requestUpdate();
Application.Storage.Position;
}
}
Help would be much appreciated.