-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrenchWrapper.h
228 lines (195 loc) · 8.67 KB
/
WrenchWrapper.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Created by T.Arnold on 19.12.2024.
//
#ifndef WRENCHWRAPPER_H
#define WRENCHWRAPPER_H
#include "animations/Splash.h"
#include "wrench.h"
#include "ControlManager.h"
struct ControlElements
{
ControlManager* cm;
MatrixManager* mm;
};
namespace wrench_wrapper
{
inline void print(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
char buf[128];
for( int i=0; i<argn; ++i )
{
printf( "%s\n", argv[i].asString(buf, 128) );
}
}
inline void set_status(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn == 0) return;
auto* ce = static_cast<ControlElements*>(usr);
char buf[128];
std::string status = "";
for (int i=0;i<argn;i++) {
status += argv[i].asString(buf, 128);
}
ce->cm->set_status(status);
}
inline void get_status(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto* ce = static_cast<ControlElements*>(usr);
wr_makeString(c, &retVal, ce->cm->get_status().c_str(), strlen(ce->cm->get_status().c_str()));
}
inline void get_current_tps(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto* ce = static_cast<ControlElements*>(usr);
wr_makeFloat(&retVal, ce->mm->get_current_tps());
}
inline void set_tps(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 1) return;
auto* ce = static_cast<ControlElements*>(usr);
ce->mm->set_tps(argv[0].asFloat());
wr_makeInt(&retVal, 1);
}
inline void get_controls(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto* ce = static_cast<ControlElements*>(usr);
wr_makeInt(&retVal, ce->cm->get_controls());
}
inline void set_controls(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 1) return;
auto ce = static_cast<ControlElements*>(usr);
ce->cm->set_controls(argv[0].asInt());
wr_makeInt(&retVal, 1);
}
inline void reset_controls(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto ce = static_cast<ControlElements*>(usr);
ce->cm->reset();
wr_makeInt(&retVal, 1);
}
inline void is_animation_running(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto ce = static_cast<ControlElements*>(usr);
wr_makeInt(&retVal, ce->cm->is_animation_running());
}
/*void run_animation(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr) {
if (argn != 2) return;
ControlElements* ce = static_cast<ControlElements*>(usr);
Animation* anim = new Animation(); // Assuming Animation has a default constructor
ce->cm->run_animation(anim, argv[0].asInt(), argv[1].asInt());
wr_makeInt(&retVal, 1);
}
*/
inline void stop_animation(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto ce = static_cast<ControlElements*>(usr);
ce->cm->stop_animation();
wr_makeInt(&retVal, 1);
}
inline void set_pixel(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 3) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->set(argv[0].asInt(), argv[1].asInt(), argv[2].asInt());
wr_makeInt(&retVal, 1);
}
inline void off_pixel(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 2) return;
auto* ce = static_cast<ControlElements*>(usr);
ce->mm->off(argv[0].asInt(), argv[1].asInt());
wr_makeInt(&retVal, 1);
}
inline void fill_matrix(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 1) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->fill(argv[0].asInt());
wr_makeInt(&retVal, 1);
}
inline void clear_matrix(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
auto ce = static_cast<ControlElements*>(usr);
ce->mm->clear();
wr_makeInt(&retVal, 1);
}
inline void draw_line(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 5) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->line(argv[0].asInt(), argv[1].asInt(), argv[2].asInt(), argv[3].asInt(), argv[4].asInt());
wr_makeInt(&retVal, 1);
}
inline void draw_rect_filled(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 5) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->rect(argv[0].asInt(), argv[1].asInt(), argv[2].asInt(), argv[3].asInt(), argv[4].asInt(),true); //make fillable controlable
wr_makeInt(&retVal, 1);
}
inline void draw_rect(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 5) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->rect(argv[0].asInt(), argv[1].asInt(), argv[2].asInt(), argv[3].asInt(), argv[4].asInt()); //make fillable controlable
wr_makeInt(&retVal, 1);
}
inline void draw_circle(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 4) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->circle(argv[0].asInt(), argv[1].asInt(), argv[2].asInt(), argv[3].asInt());
wr_makeInt(&retVal, 1);
}
inline void draw_number(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 4) return;
auto ce = static_cast<ControlElements*>(usr);
ce->mm->number(argv[0].asInt(), argv[1].asInt(), argv[2].asInt(), argv[3].asInt());
wr_makeInt(&retVal, 1);
}
//animations
inline void run_animation_splash(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 6) return;
auto ce = static_cast<ControlElements*>(usr);
auto anim = new Splash(argv[0].asInt(), argv[1].asInt(), argv[2].asInt(), argv[3].asInt() > 0);
ce->cm->run_animation(anim, argv[4].asInt(), argv[5].asInt());
wr_makeInt(&retVal, 1);
}
inline void wrench_random(WRContext* c, const WRValue* argv, const int argn, WRValue& retVal, void* usr)
{
if (argn != 2) return;
int range = argv[1].asInt() - argv[0].asInt() + 1;
int num = rand() % range + argv[0].asInt();
wr_makeInt(&retVal, num);
}
static void register_wrench_functions(WRState* w, ControlElements* ce)
{
wr_registerFunction(w, "print", wrench_wrapper::print, &ce); // bind a function
wr_registerFunction(w, "set_status", wrench_wrapper::set_status, ce);
wr_registerFunction(w, "get_status", wrench_wrapper::get_status, ce);
wr_registerFunction(w, "get_controls", wrench_wrapper::get_controls, ce);
wr_registerFunction(w, "set_controls", wrench_wrapper::set_controls, ce);
wr_registerFunction(w, "get_current_tps", wrench_wrapper::get_current_tps, ce);
wr_registerFunction(w, "set_tps", wrench_wrapper::set_tps, ce);
wr_registerFunction(w, "reset_controls", wrench_wrapper::reset_controls, ce);
wr_registerFunction(w, "is_animation_running", wrench_wrapper::is_animation_running, ce);
// wr_registerFunction(w, "run_animation", wrench_wrapper::run_animation, ce);
wr_registerFunction(w, "stop_animation", wrench_wrapper::stop_animation, ce);
wr_registerFunction(w, "set", wrench_wrapper::set_pixel, ce);
wr_registerFunction(w, "off", wrench_wrapper::off_pixel, ce);
wr_registerFunction(w, "fill", wrench_wrapper::fill_matrix, ce);
wr_registerFunction(w, "clear", wrench_wrapper::clear_matrix, ce);
wr_registerFunction(w, "line", wrench_wrapper::draw_line, ce);
wr_registerFunction(w, "rect", wrench_wrapper::draw_rect, ce);
wr_registerFunction(w, "rect_filled", wrench_wrapper::draw_rect_filled, ce);
wr_registerFunction(w, "circle", wrench_wrapper::draw_circle, ce);
wr_registerFunction(w, "number", wrench_wrapper::draw_number, ce);
//animations
wr_registerFunction(w, "run_animation_splash", wrench_wrapper::run_animation_splash, ce);
//utils
wr_registerFunction(w, "random", wrench_wrapper::wrench_random, ce);
}
}
#endif //WRENCHWRAPPER_H