Skip to content

Commit 004b3be

Browse files
authored
Add console ui (#124)
* Add console ui * Camera/atoms update flags in Camera/VisualisationState * Remove jLog from tests
1 parent 6c9c028 commit 004b3be

48 files changed

Lines changed: 585 additions & 212 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if (NOT WINDOWS)
6464
add_compile_definitions(LUA_USE_POSIX)
6565
endif()
6666

67-
file(GLOB LUA_SRC "include/vendored/lua/src/*.c")
67+
file(GLOB LUA_SRC "include/vendored/lua/src/*.cpp")
6868
include_directories(include/vendored/lua/src)
6969
add_library(Lua STATIC ${LUA_SRC})
7070

include/camera.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Camera
8282

8383
pv = projection*view;
8484
invPv = glm::inverse(pv);
85+
updated = true;
8586
}
8687

8788
/**
@@ -317,6 +318,8 @@ class Camera
317318
*/
318319
inline int lua_getCameraFieldOfView(lua_State * lua);
319320

321+
bool updated = false;
322+
320323
private:
321324

322325
uint16_t resX;
@@ -345,6 +348,7 @@ class Camera
345348
invView = glm::inverse(view);
346349
pv = projection*view;
347350
invPv = glm::inverse(pv);
351+
updated = true;
348352
}
349353

350354
};

include/cameraWindow.h

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ class CameraWindow
2626
{
2727
public:
2828

29-
/**
30-
* @brief If the Camera or Atoms require updates.
31-
*
32-
*/
33-
struct CameraUpdates
34-
{
35-
CameraUpdates()
36-
: cameraMoved(false),
37-
atomsMoved(false)
38-
{}
39-
40-
bool cameraMoved;
41-
bool atomsMoved;
42-
};
43-
4429
CameraWindow() {}
4530

4631
/**
@@ -146,77 +131,73 @@ class CameraWindow
146131
*
147132
* @param camera the Camera to update.
148133
* @param atoms the Atoms to update.
134+
* @param atomsMoved signal if atoms have been modified.
149135
* @param options the options to use for actions.
150136
* @param dr the length unit.
151137
* @param dtheta the rotation unit.
152138
* @param dphi the incline unit.
153-
* @return CameraUpdates Whether the Camera or Atoms were updated.
154139
*/
155-
CameraUpdates applyQueueActions
140+
void applyQueueActions
156141
(
157142
Camera & camera,
158143
std::vector<Atom> & atoms,
144+
bool & atomsMoved,
159145
const CommandLine & options,
160146
const float & dr,
161147
const float & dtheta,
162148
const float & dphi
163149
)
164150
{
165-
CameraUpdates moved;
166-
167151
if (zoomQueue != 0)
168152
{
169153
camera.zoom(dr*options.cameraZoomSpeed.value*zoomQueue);
170154
zoomQueue = 0;
171-
moved.cameraMoved = true;
155+
atomsMoved = true;
172156
}
173157

174158
if (rotateQueue != 0)
175159
{
176160
camera.rotate(dtheta*options.cameraRotateSpeed.value*rotateQueue);
177161
rotateQueue = 0;
178-
moved.cameraMoved = true;
162+
atomsMoved = true;
179163
}
180164

181165
if (inclineQueue != 0)
182166
{
183167
camera.incline(dphi*options.cameraInclineSpeed.value*inclineQueue);
184168
inclineQueue = 0;
185-
moved.cameraMoved = true;
169+
atomsMoved = true;
186170
}
187171

188172
if (resetCamera)
189173
{
190174
if (!options.noCentering.value) { center(atoms); }
191175
if (options.focus.value < atoms.size()) { centerOn(atoms, options.focus.value); }
192176
camera.reset(atoms);
193-
moved.atomsMoved = true;
194-
moved.cameraMoved = true;
177+
atomsMoved = true;
195178
resetCamera = false;
196179
}
197180

198181
if (panxQueue != 0)
199182
{
200183
translate(atoms, {dr*options.cameraPanSpeed.value*panxQueue, 0.0, 0.0});
201184
panxQueue = 0;
202-
moved.atomsMoved = true;
185+
atomsMoved = true;
203186
}
204187

205188
if (panyQueue != 0)
206189
{
207190
translate(atoms, {0.0, dr*options.cameraPanSpeed.value*panyQueue, 0.0});
208191
panyQueue = 0;
209-
moved.atomsMoved = true;
192+
atomsMoved = true;
210193
}
211194

212195
if (panzQueue != 0)
213196
{
214197
translate(atoms, {0.0, 0.0, dr*options.cameraPanSpeed.value*panzQueue});
215198
panzQueue = 0;
216-
moved.atomsMoved = true;
199+
atomsMoved = true;
217200
}
218-
219-
return moved;
220201
}
221202

222203
private:

include/console.h

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include <chrono>
77
#include <string>
88
#include <sstream>
9-
10-
#include <jLog/jLog.h>
9+
#include <iostream>
1110

1211
#include <lua.h>
1312
#include <LuaNumber.h>
@@ -178,18 +177,16 @@ class Console
178177
public:
179178

180179
/**
181-
* @brief Construct a new Console with a jLog::Log.
180+
* @brief Construct a new Console.
182181
*
183-
* @param l jLog::Log outputting Lua's messages.
184182
*/
185183
Console
186184
(
187-
jLog::Log & l,
188185
VisualisationState * visualisationState,
189186
CommandLine * options,
190187
Camera * camera
191188
)
192-
: lastCommandOrProgram(""), lastStatus(false), log(l)
189+
: lastCommandOrProgram(""), lastStatus(false)
193190
{
194191
lua = luaL_newstate();
195192
luaL_openlibs(lua);
@@ -199,6 +196,17 @@ class Console
199196
extraSpace.camera = camera;
200197
extraSpace.exit = &exit;
201198
*static_cast<LuaExtraSpace**>(lua_getextraspace(lua)) = &extraSpace;
199+
200+
const char * init = R"(
201+
function use(module)
202+
for k,v in pairs(module) do
203+
if not _G[k] then
204+
_G[k] = module[k]
205+
end
206+
end
207+
end
208+
use(sfoav))";
209+
runString(init);
202210
}
203211

204212
~Console(){ lua_close(lua); }
@@ -216,11 +224,7 @@ class Console
216224
{
217225
lastCommandOrProgram = file;
218226
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);
227+
lastStatus = lastStatus || lua_pcall(lua, 0, LUA_MULTRET, 0);
224228
return handleErrors();
225229
}
226230
return false;
@@ -236,13 +240,31 @@ class Console
236240
bool runString(std::string program)
237241
{
238242
if (luaIsOk())
239-
{ lastCommandOrProgram = program;
243+
{
244+
lastCommandOrProgram = program;
240245
lastStatus = luaL_dostring(lua,program.c_str());
241246
return handleErrors();
242247
}
243248
return false;
244249
}
245250

251+
/**
252+
* @brief Attempt to run a Lua script from std::string.
253+
*
254+
* @param file Lua script.
255+
* @return true Error occured.
256+
* @return false OK.
257+
*/
258+
bool runString(const char * program)
259+
{
260+
if (luaIsOk())
261+
{ lastCommandOrProgram = std::string(program);
262+
lastStatus = luaL_dostring(lua,program);
263+
return handleErrors();
264+
}
265+
return false;
266+
}
267+
246268
bool luaIsOk(){ return lua_status(lua) == LUA_OK ? true : false; }
247269

248270
/**
@@ -310,27 +332,12 @@ class Console
310332
bool lastStatus;
311333
bool exit = false;
312334

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-
327335
bool handleErrors()
328336
{
329337
if (lastStatus)
330338
{
331-
std::string msg = "Exited with error running "+lastCommandOrProgram+"\n";
332-
msg += stackTrace;
333-
jLog::ERR(jLog::ERRORCODE::LUA_ERROR, msg) >> log;
339+
if (lua_isstring(lua, -1)) { std::cerr << lua_tostring(lua, -1) << "\n"; }
340+
else { std::cerr << "Exited with error running "+lastCommandOrProgram+"\n"; }
334341
return true;
335342
}
336343
else

0 commit comments

Comments
 (0)