diff --git a/src/sim/Object.h b/src/sim/Object.h index dda1534ae..0dc92b1d6 100644 --- a/src/sim/Object.h +++ b/src/sim/Object.h @@ -3,7 +3,7 @@ #include "Shape.h" class CObject : public CShape { - + CString name; public: CObject() { @@ -13,4 +13,10 @@ public: } virtual CShape *cloneShape(); class CObject *cloneObject(); + void setName(const char *s) { + name = s; + } + const char *getName() const { + return name.c_str(); + } }; diff --git a/src/sim/Shape.cpp b/src/sim/Shape.cpp index 8e4a97de4..49da9b36c 100644 --- a/src/sim/Shape.cpp +++ b/src/sim/Shape.cpp @@ -78,6 +78,7 @@ void CShape::rotateDegreesAround(float f, const Coord &p) { void CShape::rotateDegreesAroundSelf(float f) { //rotateDegreesAround(f, Coord(0, 0)); rotateDegreesAround(f, getPosition()); + rotationAccum += f; } void CShape::recalcBoundsAll() { bounds.clear(); diff --git a/src/sim/Shape.h b/src/sim/Shape.h index b87080262..632fb0f03 100644 --- a/src/sim/Shape.h +++ b/src/sim/Shape.h @@ -11,6 +11,7 @@ class CShape { CShape *parent; TArray shapes; class CControllerBase *controller; + float rotationAccum; //class CControlelrBase *referencedController; virtual void rotateDegreesAround_internal(float f, const Coord &p); @@ -25,6 +26,7 @@ public: controller = 0; bActive = true; bFill = false; + rotationAccum = 0; } void snapToGrid(); void cloneShapeTo(CShape *o); @@ -98,6 +100,9 @@ public: void translate(float oX, float oY) { translate(Coord(oX, oY)); } + float getRotationAccum() const { + return rotationAccum; + } virtual void translate(const Coord &o); float getX() const { return pos.getX(); diff --git a/src/sim/Simulation.cpp b/src/sim/Simulation.cpp index 364d05acf..872951792 100644 --- a/src/sim/Simulation.cpp +++ b/src/sim/Simulation.cpp @@ -164,6 +164,7 @@ void CSimulation::matchAllJunctions() { class CObject *CSimulation::generateVDD() { CObject *o = new CObject(); + o->setName("VDD"); o->addJunction(0, 0, "VDD"); o->addLine(0, -20, 0, 0); o->addCircle(0, -30, 10); @@ -171,6 +172,7 @@ class CObject *CSimulation::generateVDD() { } class CObject *CSimulation::generateGND() { CObject *o = new CObject(); + o->setName("GND"); o->addJunction(0, -20, "GND"); o->addLine(0, -20, 0, 0); o->addLine(-10, 0, 10, 0); @@ -181,6 +183,7 @@ class CObject *CSimulation::generateGND() { } class CObject *CSimulation::generateButton() { CObject *o = new CObject(); + o->setName("Button"); CJunction *a = o->addJunction(-40, -10, "pad_a"); CJunction *b = o->addJunction(40, -10, "pad_b"); o->addLine(40, -10, 20, -10); @@ -205,6 +208,7 @@ class CObject *CSimulation::generateButton() { class CObject *CSimulation::generateTest() { CObject *o = new CObject(); + o->setName("test"); o->addLine(50, 10, -50, 10); o->addLine(50, -10, -50, -10); o->addLine(50, -10, 50, 10); @@ -214,6 +218,7 @@ class CObject *CSimulation::generateTest() { class CObject *CSimulation::generateWB3S() { CObject *o = new CObject(); + o->setName("WB3S"); CControllerSimulatorLink *link = new CControllerSimulatorLink(); o->setController(link); o->addText(-40, -25, "WB3S"); @@ -259,6 +264,7 @@ class CObject *CSimulation::generateLED_CW() { float bulb_radius = 20.0f; CObject *o = new CObject(); + o->setName("LED_CW"); o->addText(-40, -25, "CW"); //CShape *filler = o->addCircle(0, 0, bulb_radius); CShape *body = o->addCircle(0, 0, bulb_radius); @@ -281,6 +287,7 @@ class CObject *CSimulation::generateLED_RGB() { CObject *o = new CObject(); o->addText(-40, -25, "RGB"); + o->setName("LED_RGB"); //CShape *filler = o->addCircle(0, 0, bulb_radius); CShape *body = o->addCircle(0, 0, bulb_radius); //filler->setFill(true); @@ -305,6 +312,7 @@ class CObject *CSimulation::generateBulb() { CObject *o = new CObject(); o->addText(-40, -25, "Bulb"); + o->setName("Bulb"); CShape *filler = o->addCircle(0, 0, bulb_radius); CShape *body = o->addCircle(0, 0, bulb_radius); filler->setFill(true); diff --git a/src/sim/Simulation.h b/src/sim/Simulation.h index 9388aa0b0..3e867028b 100644 --- a/src/sim/Simulation.h +++ b/src/sim/Simulation.h @@ -19,6 +19,18 @@ class CSimulation { void registerJunctions(class CShape *s); public: + int getObjectsCount() const { + return objects.size(); + } + class CObject *getObject(int i) { + return objects[i]; + } + int getWiresCount() const { + return wires.size(); + } + class CWire *getWires(int i) { + return wires[i]; + } int getJunctionsCount() const { return junctions.size(); } diff --git a/src/sim/Simulator.cpp b/src/sim/Simulator.cpp index 3cff1acd8..962fa9d79 100644 --- a/src/sim/Simulator.cpp +++ b/src/sim/Simulator.cpp @@ -16,6 +16,7 @@ #include "CursorManager.h" #include "Solver.h" #include "WinMenuBar.h" +#include "../cJSON/cJSON.h" CSimulator::CSimulator() { @@ -155,15 +156,86 @@ class CShape *CSimulator::getShapeUnderCursor() { Coord p = GetMousePos(); return sim->findShapeByBoundsPoint(p); } +bool CSimulator::createSimulation(const char *s) { + CString memPath = CString::constructPathByStrippingExt(s, "flashMemory.bin"); + CString simPath = CString::constructPathByStrippingExt(s, "simulation.txt"); + + cJSON *root_proj = cJSON_CreateObject(); + cJSON_AddStringToObject(root_proj, "created", "qqq"); + cJSON_AddStringToObject(root_proj, "lastModified", "qqq"); + + //sim->saveTo(simPath.c_str()); + cJSON *root_sim = cJSON_CreateObject(); + cJSON *main_sim = cJSON_AddObjectToObject(root_sim, "simulation"); + cJSON *main_objects = cJSON_AddObjectToObject(main_sim, "objects"); + for (int i = 0; i < sim->getObjectsCount(); i++) { + CObject *obj = sim->getObject(i); + cJSON *j_obj = cJSON_AddObjectToObject(main_objects, "object"); + const Coord &pos = obj->getPosition(); + float rot = obj->getRotationAccum(); + const char *name = obj->getName(); + cJSON_AddStringToObject(j_obj, "name", name); + cJSON_AddNumberToObject(j_obj, "rotation", rot); + cJSON_AddNumberToObject(j_obj, "x", pos.getX()); + cJSON_AddNumberToObject(j_obj, "y", pos.getY()); + } + cJSON *main_wires = cJSON_AddObjectToObject(main_sim, "wires"); + for (int i = 0; i < sim->getWiresCount(); i++) { + CWire *wire = sim->getWires(i); + cJSON *j_wire = cJSON_AddObjectToObject(main_wires, "wire"); + CJunction *jA = wire->getJunction(0); + CJunction *jB = wire->getJunction(1); + cJSON_AddNumberToObject(j_wire, "x0", jA->getX()); + cJSON_AddNumberToObject(j_wire, "y0", jB->getY()); + cJSON_AddNumberToObject(j_wire, "x1", jA->getX()); + cJSON_AddNumberToObject(j_wire, "y1", jB->getY()); + } + char *msg = cJSON_Print(root_sim); + cJSON *n_jSim = cJSON_Parse(msg); + cJSON *n_jSimSim = cJSON_GetObjectItemCaseSensitive(n_jSim, "simulation"); + cJSON *n_jWires = cJSON_GetObjectItemCaseSensitive(n_jSimSim, "wires"); + cJSON *n_jObjects = cJSON_GetObjectItemCaseSensitive(n_jSimSim, "objects"); + cJSON *jObject; + cJSON *jWire; + cJSON_ArrayForEach(jObject, n_jObjects) + { + cJSON *jX = cJSON_GetObjectItemCaseSensitive(jObject, "x"); + cJSON *jY = cJSON_GetObjectItemCaseSensitive(jObject, "y"); + cJSON *jName = cJSON_GetObjectItemCaseSensitive(jObject, "name"); + + } + cJSON_ArrayForEach(jWire, n_jWires) + { + cJSON *jX0 = cJSON_GetObjectItemCaseSensitive(jWire, "x0"); + cJSON *jY0 = cJSON_GetObjectItemCaseSensitive(jWire, "y0"); + cJSON *jX1 = cJSON_GetObjectItemCaseSensitive(jWire, "x1"); + cJSON *jY1 = cJSON_GetObjectItemCaseSensitive(jWire, "y1"); + + } + FILE *f = fopen("test.txt", "w"); + fprintf(f, msg); + fclose(f); + return false; +} +bool CSimulator::loadSimulation(const char *s) { + + return false; +} +bool CSimulator::saveSimulationAs(const char *s) { + + return false; +} void CSimulator::createWindow() { Window = SDL_CreateWindow("OpenGL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WinWidth, WinHeight, WindowFlags); assert(Window); winMenu = new CWinMenuBar(); + winMenu->setSimulator(this); winMenu->createMenuBar(Window); Context = SDL_GL_CreateContext(Window); cur = new CursorManager(); + createSimulation("default.obkproj"); } void CSimulator::onKeyDown(int keyCode) { if (keyCode == '1') { diff --git a/src/sim/Simulator.h b/src/sim/Simulator.h index 4ebbde00e..caaa646b9 100644 --- a/src/sim/Simulator.h +++ b/src/sim/Simulator.h @@ -34,6 +34,9 @@ public: bool isMouseButtonHold(int idx) const { return bMouseButtonStates[idx]; } + bool createSimulation(const char *s); + bool loadSimulation(const char *s); + bool saveSimulationAs(const char *s); }; #endif \ No newline at end of file diff --git a/src/sim/WinMenuBar.cpp b/src/sim/WinMenuBar.cpp index 68cd63114..f3b9b0eba 100644 --- a/src/sim/WinMenuBar.cpp +++ b/src/sim/WinMenuBar.cpp @@ -1,6 +1,9 @@ #if WINDOWS #include "WinMenuBar.h" +#include "Simulator.h" +#include +#pragma comment (lib, "nfd_d.lib") CWinMenuBar::CWinMenuBar() { @@ -11,6 +14,8 @@ enum { ID_LOAD, ID_SAVEAS, ID_EXIT, + ID_OPTIONS, + ID_ABOUT, }; void CWinMenuBar::createWindowsMenu(HWND windowRef) { @@ -23,14 +28,14 @@ void CWinMenuBar::createWindowsMenu(HWND windowRef) { AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hEdit, "Edit"); AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hHelp, "Help"); - AppendMenu(hFile, MF_STRING, 1, "New..."); - AppendMenu(hFile, MF_STRING, 1, "Load..."); - AppendMenu(hFile, MF_STRING, 1, "Save as..."); + AppendMenu(hFile, MF_STRING, ID_NEW, "New..."); + AppendMenu(hFile, MF_STRING, ID_LOAD, "Load..."); + AppendMenu(hFile, MF_STRING, ID_SAVEAS, "Save as..."); AppendMenu(hFile, MF_STRING, ID_EXIT, "Exit"); - AppendMenu(hEdit, MF_STRING, 1, "Options"); + AppendMenu(hEdit, MF_STRING, ID_OPTIONS, "Options"); - AppendMenu(hHelp, MF_STRING, 1, "About"); + AppendMenu(hHelp, MF_STRING, ID_ABOUT, "About"); SetMenu(windowRef, hMenuBar); @@ -51,6 +56,8 @@ void CWinMenuBar::createMenuBar(SDL_Window *win) { SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE); } void CWinMenuBar::processEvent(const SDL_Event &Event) { + nfdchar_t *outPath = NULL; + nfdresult_t result; if (Event.type == SDL_SYSWMEVENT) { if (Event.syswm.msg->msg.win.msg == WM_COMMAND) { @@ -60,14 +67,26 @@ void CWinMenuBar::processEvent(const SDL_Event &Event) { } else if (LOWORD(Event.syswm.msg->msg.win.wParam) == ID_NEW) { - + //nfdchar_t *outPath = NULL; + //nfdresult_t result = NFD_PickFolder(NULL, &outPath); + result = NFD_SaveDialog("sim",NULL, &outPath); + if (result == NFD_OKAY) { + sim->createSimulation(outPath); + } } else if (LOWORD(Event.syswm.msg->msg.win.wParam) == ID_LOAD) { - + result = NFD_OpenDialog("sim", NULL, &outPath); + if (result == NFD_OKAY) { + sim->loadSimulation(outPath); + } } else if (LOWORD(Event.syswm.msg->msg.win.wParam) == ID_SAVEAS) { + result = NFD_SaveDialog("sim", NULL, &outPath); + if (result == NFD_OKAY) { + sim->saveSimulationAs(outPath); + } } } diff --git a/src/sim/WinMenuBar.h b/src/sim/WinMenuBar.h index d201da3b1..928230e9e 100644 --- a/src/sim/WinMenuBar.h +++ b/src/sim/WinMenuBar.h @@ -6,6 +6,7 @@ #include class CWinMenuBar { + class CSimulator *sim; HMENU hMenuBar; HMENU hFile; HMENU hEdit; @@ -16,6 +17,10 @@ class CWinMenuBar { public: CWinMenuBar(); + void setSimulator(class CSimulator *s) { + this->sim = s; + } + void createMenuBar(SDL_Window *win); void processEvent(const SDL_Event &Event); diff --git a/src/sim/sim_local.h b/src/sim/sim_local.h index 011e720ef..a50d3ec0e 100644 --- a/src/sim/sim_local.h +++ b/src/sim/sim_local.h @@ -58,5 +58,32 @@ public: } }; +class CString : public std::string { + +public: + CString() { + + } + CString(const char *s) : std::string(s) { + } + void stripExtension() { + int lst = this->find_last_of("."); + if (lst > 0) { + this->resize(lst); + } + } + static CString getWithoutExtension(const char *s) { + CString r = s; + r.stripExtension(); + return r; + } + static CString constructPathByStrippingExt(const char *dir, const char *subfile) { + CString r = dir; + r.stripExtension(); + r.append("/"); + r.append(subfile); + return r; + } +}; #endif #endif