22 #ifndef EMBAJAXJOYSTICK_H
23 #define EMBAJAXJOYSTICK_H
27 const char EmbAJAXJoystick_SNAP_BACK[] =
"if (!pressed) { x = 0; y = 0; }\n";
28 const char EmbAJAXJoystick_NO_SNAP_BACK[] =
"";
29 const char EmbAJAXJoystick_FREE_POSITION[] =
"";
30 const char EmbAJAXJoystick_POSITION_9_DIRECTIONS[] =
"if (pressed) {\n"
31 " if (x < -500) x = -1000;\n"
32 " else if (x > 500) x = 1000\n"
35 " if (y < -500) y = -1000;\n"
36 " else if (y > 500) y = 1000\n"
48 EmbAJAXJoystick(
const char*
id,
int width,
int height,
const char* position_adjust=EmbAJAXJoystick_FREE_POSITION,
const char* snap_back=EmbAJAXJoystick_SNAP_BACK) :
EmbAJAXElement(id) {
51 _position_adjust = position_adjust;
52 _snap_back = snap_back;
54 void print()
const override {
56 " style=\"cursor: all-scroll\"></canvas>"
59 EmbAJAXBase::_driver->printFormatted(
60 "elem.__defineSetter__('coords', function(value) {\n"
61 " var vals = value.split(',');\n"
62 " this.update(vals[0], vals[1], false);\n"
65 "elem.updateFromClient = function(x, y, nomerge=false) {\n"
66 " var width = this.width;\n"
67 " var height = this.height;\n"
68 " var pressed = this.pressed;\n"
69 " x = Math.round(((x - width / 2) * 2000) / (width-40));\n"
70 " y = Math.round(((y - height / 2) * 2000) / (height-40));\n",
73 " this.update(x, y, true, nomerge);\n"
76 "elem.update = function(x, y, send=true, nomerge=false) {\n"
77 " var oldx = this.posx;\n"
78 " var oldy = this.posy;\n"
81 " if (this.posx != oldx || this.posy != oldy) {\n"
82 " var ctx = this.getContext('2d');\n"
83 " ctx.clearRect(0, 0, this.width, this.height);\n"
84 " this.drawKnob(ctx, this.posx, this.posy);\n"
85 " if(send) doRequest(this.id,this.pressed + ',' + this.posx + ',' + this.posy, nomerge ? 2 : 1);\n"
90 "elem.drawKnob = function(ctx, x, y) {\n"
91 " var width = this.width;\n"
92 " var height = this.height;\n"
93 " x = x * (width-40) / 2000 + width / 2;\n"
94 " y = y * (height-40) / 2000 + height / 2;\n"
96 " ctx.arc(x, y, 15, 0, 2 * Math.PI);\n"
101 "elem.press = function(x, y) {\n"
102 " this.pressed = 1;\n"
103 " this.updateFromClient(x, y, true);\n"
106 "elem.move = function(x, y) {\n"
107 " this.updateFromClient(x, y);\n"
110 "elem.release = function(x, y) {\n"
111 " this.pressed = 0;\n"
112 " this.updateFromClient(x, y, true);\n"
115 "elem.addEventListener('mousedown', function(event) { this.press(event.offsetX, event.offsetY); }.bind(elem), false);\n"
116 "elem.addEventListener('mousemove', function(event) { this.move(event.offsetX, event.offsetY); }.bind(elem), false);\n"
117 "elem.addEventListener('mouseup', function(event) { this.release(event.offsetX, event.offsetY); }.bind(elem), false);\n"
118 "elem.addEventListener('mouseleave', function(event) { this.release(event.offsetX, event.offsetY); }.bind(elem), false);\n"
119 "elem.addEventListener('touchstart', function(event) { this.press(event.touches[0].offsetX, event.touches[0].offsetY); }.bind(elem), false);\n"
120 "elem.addEventListener('touchmove', function(event) { this.move(event.touches[0].offsetX, event.touches[0].offsetY); }.bind(elem), false);\n"
121 "elem.addEventListener('touchend', function(event) { this.release(event.touches[0].offsetX, event.touches[0].offsetY); }.bind(elem), false);\n"
125 const int bufsize = 16;
127 _driver->getArg(argname, buf, bufsize);
130 while (--p >= 0)
if (buf[p] ==
'\0')
break;
131 while (--p >= 0)
if (buf[p] ==
',')
break;
132 _cury = atoi(buf + p + 1);
134 while (--p >= 0)
if (buf[p] ==
',')
break;
135 _curx = atoi(buf + p + 1);
137 _pressed = atoi(buf);
141 int getX()
const {
return _curx; };
143 int getY()
const {
return _cury; };
146 if (x != _curx || y != _cury) {
153 const char*
valueProperty(uint8_t which = EmbAJAXBase::Value)
const override {
154 if (which == EmbAJAXBase::Value)
return "coords";
157 const char*
value(uint8_t which = EmbAJAXBase::Value)
const override {
158 if (which == EmbAJAXBase::Value)
return _value;
162 void updateValueString() {
163 itoa(_curx, _value, 10);
165 while (_value[p] !=
'\0') ++p;
167 itoa(_cury, _value + p + 1, 10);
172 const char* _snap_back;
173 const char* _position_adjust;
Abstract base class for modifiable elements.
Definition: EmbAJAX.h:296
virtual const char * value(uint8_t which=EmbAJAXBase::Value) const
Definition: EmbAJAX.h:309
virtual const char * valueProperty(uint8_t which=EmbAJAXBase::Value) const
Definition: EmbAJAX.h:324
Definition: EmbAJAXJoystick.h:41
void updateFromDriverArg(const char *argname)
Definition: EmbAJAXJoystick.h:124
int getY() const
Definition: EmbAJAXJoystick.h:143
EmbAJAXJoystick(const char *id, int width, int height, const char *position_adjust=EmbAJAXJoystick_FREE_POSITION, const char *snap_back=EmbAJAXJoystick_SNAP_BACK)
Definition: EmbAJAXJoystick.h:48
int getX() const
Definition: EmbAJAXJoystick.h:141
const char * valueProperty(uint8_t which=EmbAJAXBase::Value) const override
Definition: EmbAJAXJoystick.h:153
void setPosition(int x, int y)
Definition: EmbAJAXJoystick.h:145
const char * value(uint8_t which=EmbAJAXBase::Value) const override
Definition: EmbAJAXJoystick.h:157
#define INTEGER_VALUE(X)
Definition: macro_definitions.h:56
#define PLAIN_STRING(X)
Definition: macro_definitions.h:54
#define HTML_QUOTED_STRING(X)
Definition: macro_definitions.h:49
#define JS_QUOTED_STRING(X)
Definition: macro_definitions.h:47