-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEmbAJAXJoystick.h
178 lines (173 loc) · 8.57 KB
/
EmbAJAXJoystick.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
*
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
*
* Copyright (C) 2018-2019 Thomas Friedrichsmeier
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/
#ifndef EMBAJAXJOYSTICK_H
#define EMBAJAXJOYSTICK_H
#include "EmbAJAX.h"
const char EmbAJAXJoystick_SNAP_BACK[] = "if (!pressed) { x = 0; y = 0; }\n";
const char EmbAJAXJoystick_NO_SNAP_BACK[] = "";
const char EmbAJAXJoystick_FREE_POSITION[] = "";
const char EmbAJAXJoystick_POSITION_9_DIRECTIONS[] = "if (pressed) {\n"
" if (x < -500) x = -1000;\n"
" else if (x > 500) x = 1000\n"
" else x = 0;\n"
"\n"
" if (y < -500) y = -1000;\n"
" else if (y > 500) y = 1000\n"
" else y = 0;\n"
"}\n";
/** This class provides a basic joystick for directional control. WORK IN PROGRESS, API and behavior may be subject to change in future versions of EmbAJAX. */
class EmbAJAXJoystick : public EmbAJAXElement {
public:
/** C'tor.
* @param width: Element width
* @param height: Element height
* @param position_adjust: Custom javascript that will be applied to "correct" the user supplied position, e.g. snapping it to certain allowed positions. @See e.g. EmbAJAXJoystick_POSITION_9_DIRECTIONS
* @param snap_back: Custom javascript that will be applied to snap back the position on mouse release. @See EmbAJAXJoystick_SNAP_BACK */
EmbAJAXJoystick(const char* id, int width, int height, const char* position_adjust=EmbAJAXJoystick_FREE_POSITION, const char* snap_back=EmbAJAXJoystick_SNAP_BACK) : EmbAJAXElement(id) {
_width = width;
_height = height;
_position_adjust = position_adjust;
_snap_back = snap_back;
}
void print() const override {
EmbAJAXBase::_driver->printFormatted("<canvas id=", HTML_QUOTED_STRING(_id), " width=", INTEGER_VALUE(_width), " height=", INTEGER_VALUE(_height),
" style=\"cursor: all-scroll\"></canvas>" // style="border-radius:50%; background-color:grey; cursor: all-scroll"
"<script>\n"
"var elem = document.getElementById(", JS_QUOTED_STRING(_id), ");\n");
EmbAJAXBase::_driver->printFormatted(
"elem.__defineSetter__('coords', function(value) {\n"
" var vals = value.split(',');\n"
" this.update(vals[0], vals[1], false);\n"
"});\n"
"\n"
"elem.updateFromClient = function(x, y, nomerge=false) {\n"
" var width = this.width;\n"
" var height = this.height;\n"
" var pressed = this.pressed;\n"
" x = Math.round(((x - width / 2) * 2000) / (width-40));\n" // Scale values to +/-1000, independent of display size
" y = Math.round(((y - height / 2) * 2000) / (height-40));\n",
PLAIN_STRING(_snap_back), "", // NOTE: inserted "", because printFormatted macro assumed static string between each arg
PLAIN_STRING(_position_adjust),
" this.update(x, y, true, nomerge);\n"
"}\n"
"\n"
"elem.update = function(x, y, send=true, nomerge=false) {\n"
" var oldx = this.posx;\n"
" var oldy = this.posy;\n"
" this.posx = x;\n"
" this.posy = y;\n"
" if (this.posx != oldx || this.posy != oldy) {\n"
" var ctx = this.getContext('2d');\n"
" ctx.clearRect(0, 0, this.width, this.height);\n"
" this.drawKnob(ctx, this.posx, this.posy);\n"
" if(send) doRequest(this.id,this.pressed + ',' + this.posx + ',' + this.posy, nomerge ? 2 : 1);\n"
" }\n"
"}\n"
"\n"
// TODO: This should be customizable
"elem.drawKnob = function(ctx, x, y) {\n"
" var width = this.width;\n"
" var height = this.height;\n"
" x = x * (width-40) / 2000 + width / 2;\n"
" y = y * (height-40) / 2000 + height / 2;\n"
" ctx.beginPath();\n"
" ctx.arc(x, y, 15, 0, 2 * Math.PI);\n"
" ctx.stroke();\n"
" ctx.fill();\n"
"}\n"
"\n"
"elem.press = function(x, y) {\n"
" this.pressed = 1;\n"
" this.updateFromClient(x, y, true);\n"
"}\n"
"\n"
"elem.move = function(x, y) {\n"
" this.updateFromClient(x, y);\n"
"}\n"
"\n"
"elem.release = function(x, y) {\n"
" this.pressed = 0;\n"
" this.updateFromClient(x, y, true);\n"
"}\n"
"\n"
"elem.addEventListener('mousedown', function(event) { this.press(event.offsetX, event.offsetY); }.bind(elem), false);\n"
"elem.addEventListener('mousemove', function(event) { this.move(event.offsetX, event.offsetY); }.bind(elem), false);\n"
"elem.addEventListener('mouseup', function(event) { this.release(event.offsetX, event.offsetY); }.bind(elem), false);\n"
"elem.addEventListener('mouseleave', function(event) { this.release(event.offsetX, event.offsetY); }.bind(elem), false);\n"
"elem.addEventListener('touchstart', function(event) { this.press(event.touches[0].offsetX, event.touches[0].offsetY); }.bind(elem), false);\n"
"elem.addEventListener('touchmove', function(event) { this.move(event.touches[0].offsetX, event.touches[0].offsetY); }.bind(elem), false);\n"
"elem.addEventListener('touchend', function(event) { this.release(event.touches[0].offsetX, event.touches[0].offsetY); }.bind(elem), false);\n"
"</script>\n");
}
void updateFromDriverArg(const char* argname) {
const int bufsize = 16;
char buf[bufsize];
_driver->getArg(argname, buf, bufsize);
// format: "P,X,Y", each a number. Parsing from reverse
int p = bufsize;
while (--p >= 0) if (buf[p] == '\0') break;
while (--p >= 0) if (buf[p] == ',') break;
_cury = atoi(buf + p + 1);
buf[p] = '\0';
while (--p >= 0) if (buf[p] == ',') break;
_curx = atoi(buf + p + 1);
buf[p] = '\0';
_pressed = atoi(buf);
updateValueString();
}
/** Get current x position. Position is returned as a value between -1000 and +1000 (center 0), independent of the size of the control. */
int getX() const { return _curx; };
/** Get current y position. Position is returned as a value between -1000 and +1000 (center 0), independent of the size of the control. */
int getY() const { return _cury; };
/** Set x/y position in the client(s). Range -1000 to +1000. */
void setPosition (int x, int y) {
if (x != _curx || y != _cury) {
_curx = x;
_cury = y;
setChanged();
updateValueString();
}
}
const char* valueProperty(uint8_t which = EmbAJAXBase::Value) const override {
if (which == EmbAJAXBase::Value) return "coords";
return EmbAJAXElement::valueProperty(which);
}
const char* value(uint8_t which = EmbAJAXBase::Value) const override {
if (which == EmbAJAXBase::Value) return _value;
return EmbAJAXElement::value(which);
}
private:
void updateValueString() {
itoa(_curx, _value, 10);
int p = 0;
while (_value[p] != '\0') ++p;
_value[p] = ',';
itoa(_cury, _value + p + 1, 10);
}
char _value[16];
int _width;
int _height;
const char* _snap_back;
const char* _position_adjust;
int _curx, _cury;
bool _pressed;
};
#endif