SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
console.h
Go to the documentation of this file.
1#ifndef CONSOLE_H
2#define CONSOLE_H
3
4#include <memory>
5#include <vector>
6#include <chrono>
7#include <string>
8#include <sstream>
9
10#include <jLog/jLog.h>
11
12#include <lua.h>
13#include <LuaNumber.h>
14#include <LuaString.h>
15#include <LuaTable.h>
16#include <LuaArray.h>
17#include <LuaVec.h>
18#include <LuaBool.h>
19#include <visualisationState.h>
20#include <commandLine.h>
21#include <camera.h>
22
35
41
50template <VisualisationStateMember function>
52{
53 LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
54 VisualisationState * ptr = store->visualisationState;
55 return ((*ptr).*function)(lua);
56}
57
62typedef int (Camera::*CameraMember)(lua_State * lua);
63
72template <CameraMember function>
74{
75 LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
76 Camera * ptr = store->camera;
77 return ((*ptr).*function)(lua);
78}
79
85
94template <CommandLineMember function>
96{
97 LuaExtraSpace * store = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
98 CommandLine * ptr = store->options;
99 return ((*ptr).*function)(lua);
100}
101
109{
110 LuaExtraSpace * extraSpace = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
111 if (!extraSpace->visualisationState->recording)
112 {
113 extraSpace->visualisationState->toggleRecord(*extraSpace->options);
114 }
115 return 0;
116}
117
125{
126 LuaExtraSpace * extraSpace = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
127 if (extraSpace->visualisationState->recording)
128 {
129 extraSpace->visualisationState->toggleRecord(*extraSpace->options);
130 }
131 return 0;
132}
133
141{
142 LuaExtraSpace * extraSpace = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
143 extraSpace->options->play.value = true;
144 return 0;
145}
146
154{
155 LuaExtraSpace * extraSpace = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
156 *(extraSpace->exit) = true;
157 return 0;
158}
159
167{
168 LuaExtraSpace * extraSpace = *static_cast<LuaExtraSpace**>(lua_getextraspace(lua));
169 extraSpace->options->play.value = false;
170 return 0;
171}
172
177{
178public:
179
186 (
187 jLog::Log & l,
188 VisualisationState * visualisationState,
189 CommandLine * options,
190 Camera * camera
191 )
192 : lastCommandOrProgram(""), lastStatus(false), log(l)
193 {
194 lua = luaL_newstate();
195 luaL_openlibs(lua);
196 luaL_requiref(lua,"sfoav",load_sfoavLib,1);
197 extraSpace.visualisationState = visualisationState;
198 extraSpace.options = options;
199 extraSpace.camera = camera;
200 extraSpace.exit = &exit;
201 *static_cast<LuaExtraSpace**>(lua_getextraspace(lua)) = &extraSpace;
202 }
203
205
213 bool runFile(std::string file)
214 {
215 if (luaIsOk())
216 {
217 lastCommandOrProgram = file;
218 lastStatus = luaL_loadfile(lua, file.c_str());
219 int epos = lua_gettop(lua);
220 lua_pushcfunction(lua, traceback);
221 lua_insert(lua, epos);
222 lastStatus = lastStatus || lua_pcall(lua, 0, LUA_MULTRET, epos);
223 lua_remove(lua, epos);
224 return handleErrors();
225 }
226 return false;
227 }
228
236 bool runString(std::string program)
237 {
238 if (luaIsOk())
239 { lastCommandOrProgram = program;
240 lastStatus = luaL_dostring(lua,program.c_str());
241 return handleErrors();
242 }
243 return false;
244 }
245
246 bool luaIsOk(){ return lua_status(lua) == LUA_OK ? true : false; }
247
253 std::string luaStatus()
254 {
255 int s = lua_status(lua);
256
257 if (s == LUA_OK){return "LUA_OK";}
258
259 std::string status = lastCommandOrProgram + " | ";
260
261 switch(s)
262 {
263 case LUA_YIELD:
264 status += "LUA_YIELD";
265 break;
266 case LUA_ERRRUN:
267 status += "LUA_ERRRUN";
268 break;
269 case LUA_ERRSYNTAX:
270 status += "LUA_ERRSYNTAX";
271 break;
272 case LUA_ERRMEM:
273 status += "LUA_ERRMEM";
274 break;
275 case LUA_ERRERR:
276 status += "LUA_ERRERR";
277 break;
278 default:
279 status += "LUA_STATUS_UNKOWN";
280 break;
281 }
282
283 return status;
284 }
285
286 template <class T>
287 T getGlobal(const char * n)
288 {
289 T value;
290 value.readGlobal(lua, n);
291 return value;
292 }
293
300 bool exitCalled() const { return exit; }
301
302private:
303
304 lua_State * lua;
305 LuaExtraSpace extraSpace;
306
307 std::string lastCommandOrProgram;
308 std::stringstream input;
309 static std::string stackTrace;
310 bool lastStatus;
311 bool exit = false;
312
313 jLog::Log & log;
314
315 static int traceback(lua_State * lua) {
316 if (lua_isstring(lua, -1))
317 {
318 stackTrace = lua_tostring(lua, -1);
319 lua_pop(lua, 1);
320 }
321 luaL_traceback(lua, lua, NULL, 1);
322 stackTrace += std::string("\n") + lua_tostring(lua, -1);
323 lua_pop(lua, 1);
324 return 0;
325 }
326
327 bool handleErrors()
328 {
329 if (lastStatus)
330 {
331 std::string msg = "Exited with error running "+lastCommandOrProgram+"\n";
332 msg += stackTrace;
333 jLog::ERR(jLog::ERRORCODE::LUA_ERROR, msg) >> log;
334 return true;
335 }
336 else
337 {
338 return false;
339 }
340 }
341
342 static int load_sfoavLib(lua_State * lua)
343 {
344 luaL_Reg sfoavLib[23] =
345 {
356 {"startRecording", &lua_startRecord},
357 {"stopRecording", &lua_stopRecord},
358 {"play", &lua_play},
359 {"pause", &lua_pause},
365 {"exit", &lua_exit},
368 {NULL, NULL}
369 };
370
372 return 1;
373 }
374};
375
376#endif /* CONSOLE_H */
A 3D projective camera centered on a focus moving on a sphere.
Definition camera.h:30
Lua console.
Definition console.h:177
bool exitCalled() const
If sfoav.exit() has been called.
Definition console.h:300
Console(jLog::Log &l, VisualisationState *visualisationState, CommandLine *options, Camera *camera)
Construct a new Console with a jLog::Log.
Definition console.h:186
~Console()
Definition console.h:204
bool runFile(std::string file)
Attempt to run a Lua script from a file on disc.
Definition console.h:213
std::string luaStatus()
Convert Lua's status to a std::string.
Definition console.h:253
bool runString(std::string program)
Attempt to run a Lua script from std::string.
Definition console.h:236
T getGlobal(const char *n)
Definition console.h:287
bool luaIsOk()
Definition console.h:246
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214
int(VisualisationState::* VisualisationStateMember)(lua_State *lua)
A Lua binding in VisualisationState;.
Definition console.h:40
int dispatchCamera(lua_State *lua)
Dispath to a Lua binding of Camera.
Definition console.h:73
int lua_pause(lua_State *lua)
Stop playing frames.
Definition console.h:166
int lua_exit(lua_State *lua)
Exit SFOAV.
Definition console.h:153
int lua_startRecord(lua_State *lua)
Start video recording (if not already recording).
Definition console.h:108
int lua_stopRecord(lua_State *lua)
Stop video recording (if already recording).
Definition console.h:124
int(Camera::* CameraMember)(lua_State *lua)
A Lua binding in Camera;.
Definition console.h:62
int lua_play(lua_State *lua)
Start playing frames.
Definition console.h:140
int(CommandLine::* CommandLineMember)(lua_State *lua)
A Lua binding in CommandLine;.
Definition console.h:84
int dispatchCommandLine(lua_State *lua)
Dispath to a Lua binding of CommandLine.
Definition console.h:95
int dispatchVisualisationState(lua_State *lua)
Dispath to a Lua binding of VisualisationState.
Definition console.h:51
T value
Definition commandLine.h:33
Extract command line arguments.
Definition commandLine.h:240
Argument< bool > play
Definition commandLine.h:368
Store for lua global state. For the console to have access to these classes they must be set into Lua...
Definition console.h:29
bool * exit
Definition console.h:33
CommandLine * options
Definition console.h:31
Camera * camera
Definition console.h:32
VisualisationState * visualisationState
Definition console.h:30
Holds editable data for the visualisation state.
Definition visualisationState.h:34
bool recording
Definition visualisationState.h:131
void toggleRecord(const CommandLine &options)
Toggle recording to video.
Definition visualisationState.h:147