From c6d88f284698f47f8d0c1d3acd8d23ad4bdedb88 Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Wed, 30 Nov 2022 13:11:01 +0100 Subject: [PATCH] sim: removed CObject remnant, working text editing --- src/sim/Line.h | 3 +++ src/sim/Object.h | 4 --- src/sim/PrefabManager.cpp | 1 - src/sim/SaveLoad.cpp | 28 ++++++++++++++++--- src/sim/SaveLoad.h | 18 +++++++++++++ src/sim/Simulation.cpp | 2 +- src/sim/Simulator.cpp | 45 +++++++++++++++++++++++++++++-- src/sim/Simulator.h | 5 +++- src/sim/Text.cpp | 57 ++++++++++++++++++++++++++++++++++++++- src/sim/Text.h | 25 ++++++++++++++--- src/sim/Tool_Base.h | 3 +++ src/sim/Tool_Copy.cpp | 1 - src/sim/Tool_Text.cpp | 27 +++++++++++++++++-- src/sim/Tool_Text.h | 3 ++- src/sim/sim_sdl.cpp | 1 - 15 files changed, 201 insertions(+), 22 deletions(-) delete mode 100644 src/sim/Object.h diff --git a/src/sim/Line.h b/src/sim/Line.h index 33d5e55f1..49a6ee025 100644 --- a/src/sim/Line.h +++ b/src/sim/Line.h @@ -24,6 +24,9 @@ public: virtual void recalcBoundsSelf(); virtual void drawShape(); virtual CShape *cloneShape(); + virtual const char *getClassName() const { + return "CLine"; + } }; #endif // __LINE_H__ diff --git a/src/sim/Object.h b/src/sim/Object.h deleted file mode 100644 index 3f74b9a38..000000000 --- a/src/sim/Object.h +++ /dev/null @@ -1,4 +0,0 @@ - -#include "sim_local.h" -#include "Shape.h" - diff --git a/src/sim/PrefabManager.cpp b/src/sim/PrefabManager.cpp index db22c9387..e97a1e202 100644 --- a/src/sim/PrefabManager.cpp +++ b/src/sim/PrefabManager.cpp @@ -1,7 +1,6 @@ #ifdef WINDOWS #include "PrefabManager.h" #include "Shape.h" -#include "Object.h" #include "Wire.h" #include "Controller_Button.h" #include "Controller_Bulb.h" diff --git a/src/sim/SaveLoad.cpp b/src/sim/SaveLoad.cpp index 6712e7643..e6a974f46 100644 --- a/src/sim/SaveLoad.cpp +++ b/src/sim/SaveLoad.cpp @@ -2,11 +2,11 @@ #include "SaveLoad.h" #include "Simulator.h" #include "Shape.h" -#include "Object.h" #include "Junction.h" #include "Wire.h" #include "Simulation.h" #include "PrefabManager.h" +#include "Text.h" #include "../cJSON/cJSON.h" class CProject *CSaveLoad::loadProjectFile(const char *fname) { @@ -55,15 +55,29 @@ class CSimulation *CSaveLoad::loadSimulationFromFile(const char *fname) { { cJSON *jX = cJSON_GetObjectItemCaseSensitive(jObject, "x"); cJSON *jY = cJSON_GetObjectItemCaseSensitive(jObject, "y"); + cJSON *jClassName = cJSON_GetObjectItemCaseSensitive(jObject, "classname"); cJSON *jName = cJSON_GetObjectItemCaseSensitive(jObject, "name"); + cJSON *jText = cJSON_GetObjectItemCaseSensitive(jObject, "text"); cJSON *rot = cJSON_GetObjectItemCaseSensitive(jObject, "rotation"); int rotInteger = rot->valuedouble; rotInteger %= 360; CShape *o = sim->getPfbs()->instantiatePrefab(jName->valuestring); - o->setPosition(jX->valuedouble, jY->valuedouble); - s->addObject(o); - o->rotateDegreesAroundSelf(rotInteger); + if (o == 0) { + o = sim->allocByClassName(jClassName->valuestring); + } + if (o == 0) { + printf("Failed to alloc object of class %s\n", jClassName->valuestring); + } + else { + CText *as_text = dynamic_cast(o); + o->setPosition(jX->valuedouble, jY->valuedouble); + s->addObject(o); + o->rotateDegreesAroundSelf(rotInteger); + if (jText != 0 && as_text != 0) { + as_text->setText(jText->valuestring); + } + } } cJSON_ArrayForEach(jWire, n_jWires) { @@ -89,10 +103,16 @@ void CSaveLoad::saveSimulationToFile(class CSimulation *simToSave, const char *f const Coord &pos = obj->getPosition(); float rot = obj->getRotationAccum(); const char *name = obj->getName(); + const char *classname = obj->getClassName(); + CText *as_text = dynamic_cast(obj); cJSON_AddStringToObject(j_obj, "name", name); + cJSON_AddStringToObject(j_obj, "classname", classname); cJSON_AddNumberToObject(j_obj, "rotation", rot); cJSON_AddNumberToObject(j_obj, "x", pos.getX()); cJSON_AddNumberToObject(j_obj, "y", pos.getY()); + if (as_text) { + cJSON_AddStringToObject(j_obj, "text", as_text->getText()); + } } cJSON *main_wires = cJSON_AddObjectToObject(main_sim, "wires"); for (int i = 0; i < simToSave->getWiresCount(); i++) { diff --git a/src/sim/SaveLoad.h b/src/sim/SaveLoad.h index 6e90cf7f6..f50dd4d26 100644 --- a/src/sim/SaveLoad.h +++ b/src/sim/SaveLoad.h @@ -3,10 +3,28 @@ #include "sim_local.h" + + +#include + class CProject { CString created; CString lastModified; public: + CProject() { + char text[64]; + time_t now = time(NULL); + struct tm *t = localtime(&now); + strftime(text, sizeof(text) - 1, "%Y-%m-%d %H:%M", t); + created = text; + } + void setModifiedDate() { + char text[64]; + time_t now = time(NULL); + struct tm *t = localtime(&now); + strftime(text, sizeof(text) - 1, "%Y-%m-%d %H:%M", t); + lastModified = text; + } const char *getLastModified() const { return lastModified.c_str(); } diff --git a/src/sim/Simulation.cpp b/src/sim/Simulation.cpp index 815d58a8a..f7cfaab0b 100644 --- a/src/sim/Simulation.cpp +++ b/src/sim/Simulation.cpp @@ -1,7 +1,6 @@ #ifdef WINDOWS #include "Simulation.h" #include "Shape.h" -#include "Object.h" #include "Wire.h" #include "Junction.h" #include "Text.h" @@ -109,6 +108,7 @@ CShape * CSimulation::addObject(CShape *o) { } objects.push_back(o); registerJunctions(o); + o->recalcBoundsAll(); return o; } void CSimulation::createDemoOnlyWB3S() { diff --git a/src/sim/Simulator.cpp b/src/sim/Simulator.cpp index d0a4d7625..631608e79 100644 --- a/src/sim/Simulator.cpp +++ b/src/sim/Simulator.cpp @@ -1,8 +1,8 @@ #ifdef WINDOWS #include "Simulator.h" #include "Shape.h" -#include "Object.h" #include "Wire.h" +#include "Text.h" #include "Controller_Button.h" #include "Junction.h" #include "Tool_Base.h" @@ -23,6 +23,7 @@ CSimulator::CSimulator() { + currentlyEditingText = 0; memset(bMouseButtonStates, 0, sizeof(bMouseButtonStates)); activeTool = 0; Window = 0; @@ -70,7 +71,15 @@ void CSimulator::drawWindow() { } if (Event.type == SDL_KEYDOWN) { - onKeyDown(Event.key.keysym.sym); + if (currentlyEditingText) { + if (currentlyEditingText->processKeyDown(Event.key.keysym.sym)) { + currentlyEditingText->setTextEditMode(false); + currentlyEditingText = 0; + } + } + else { + onKeyDown(Event.key.keysym.sym); + } } else if (Event.type == SDL_MOUSEBUTTONDOWN) { @@ -94,6 +103,22 @@ void CSimulator::drawWindow() { } bMouseButtonStates[Event.button.button] = false; } + else if (Event.type == SDL_TEXTEDITING) + { + printf("SDL_TEXTEDITING %s %i %i\n", Event.edit.text,Event.edit.start, Event.edit.length); + } + else if (Event.type == SDL_TEXTINPUT) + { + printf("SDL_TEXTINPUT %s\n", Event.text.text); + if (currentlyEditingText) { + currentlyEditingText->appendText(Event.text.text); + } + else { + if (activeTool) { + activeTool->onTextInput(Event.text.text); + } + } + } else if (Event.type == SDL_QUIT) { Running = 0; @@ -178,6 +203,21 @@ void CSimulator::drawWindow() { //glDisable(GL_TEXTURE_2D); SDL_GL_SwapWindow(Window); } +class CShape *CSimulator::allocByClassName(const char *className) { + if (!stricmp(className, "CText")) + return new CText(); + printf("CSimulator::allocByClassName: unhandled class %s\n", className); + return 0; +} +void CSimulator::beginEditingText(class CText *txt) { + if (currentlyEditingText != 0) { + currentlyEditingText->setTextEditMode(false); + } + currentlyEditingText = txt; + if (currentlyEditingText) { + currentlyEditingText->setTextEditMode(true); + } +} void CSimulator::destroyObject(CShape *s) { sim->destroyObject(s); } @@ -245,6 +285,7 @@ bool CSimulator::saveSimulation() { bool CSimulator::saveSimulationAs(const char *s) { printf("CSimulator::saveSimulationAs: called with name %s\n", s); projectPath = s; + project->setModifiedDate(); CString simPath = CString::constructPathByStrippingExt(projectPath.c_str(), "simulation.json"); CString memPath = CString::constructPathByStrippingExt(projectPath.c_str(), "flashMemory.bin"); printf("CSimulator::saveSimulationAs: simPath %s\n", simPath.c_str()); diff --git a/src/sim/Simulator.h b/src/sim/Simulator.h index 13ae06341..dd742d348 100644 --- a/src/sim/Simulator.h +++ b/src/sim/Simulator.h @@ -21,9 +21,10 @@ class CSimulator { class CSaveLoad *saveLoad; class CProject *project; bool bSchematicModified; + class CText *currentlyEditingText; void onKeyDown(int keyCode); - void setTool(Tool_Base *tb); + void setTool(class Tool_Base *tb); public: CSimulator(); void drawWindow(); @@ -31,6 +32,7 @@ public: void onUserClose(); void showExitSaveMessageBox(); void destroyObject(class CShape *s); + void beginEditingText(class CText *txt); class CShape *getShapeUnderCursor(); class CSimulation*getSim() { return sim; @@ -41,6 +43,7 @@ public: class CursorManager*getCursorMgr() { return cur; } + class CShape *allocByClassName(const char *className); bool isMouseButtonHold(int idx) const { return bMouseButtonStates[idx]; } diff --git a/src/sim/Text.cpp b/src/sim/Text.cpp index 8c109a3b6..f29b35e64 100644 --- a/src/sim/Text.cpp +++ b/src/sim/Text.cpp @@ -1,6 +1,15 @@ #ifdef WINDOWS #include "Text.h" +void CText::recalcBoundsSelf() { + bounds.clear(); + //bounds.addPoint(pos); + int useLen = txt.size(); + if (bTextEditMode) + useLen += 1; + bounds.addPoint(Coord(9 * useLen, 5)); + bounds.addPoint(Coord(0, -8)); +} CShape *CText::cloneShape() { CText *r = new CText(); r->pos = this->pos; @@ -8,10 +17,56 @@ CShape *CText::cloneShape() { this->cloneShapeTo(r); return r; } +bool CText::processKeyDown(int keyCode) { + if (keyCode == SDLK_BACKSPACE) { + if (cursorPos > 0) { + txt.erase(cursorPos-1, 1); + cursorPos--; + } + } + if (keyCode == SDLK_DELETE) { + if (txt.size() > cursorPos) { + txt.erase(cursorPos, 1); + } + } + if (keyCode == SDLK_RIGHT) { + cursorPos++; + if (cursorPos > txt.size()) + cursorPos = txt.size(); + } + if (keyCode == SDLK_LEFT) { + cursorPos--; + if (cursorPos < 0) + cursorPos = 0; + } + if (keyCode == SDLK_ESCAPE) { + return true; + } + return false; +} +void CText::appendText(const char *s) { + //txt.append(s); + txt.insert(cursorPos, s); + cursorPos += strlen(s); + recalcBoundsSelf(); +} void CText::drawShape() { - drawText(getX(), getY(), txt.c_str()); + char buffer[512]; + if (bTextEditMode) { + recalcBoundsSelf(); + } + if (bTextEditMode) { + strncpy(buffer, txt.c_str(), cursorPos); + buffer[cursorPos] = 0; + strcat(buffer, "|"); + strcat(buffer, txt.c_str()+ cursorPos); + drawText(getX(), getY(), buffer); + } + else { + drawText(getX(), getY(), txt.c_str()); + } } diff --git a/src/sim/Text.h b/src/sim/Text.h index 0ed494ded..8b26a58ea 100644 --- a/src/sim/Text.h +++ b/src/sim/Text.h @@ -6,24 +6,43 @@ class CText : public CShape { std::string txt; - + bool bTextEditMode; + int cursorPos; void rotateDegreesAround_internal(float f, const class Coord &p); + virtual void recalcBoundsSelf(); public: CText() { - + cursorPos = 0; + bTextEditMode = false; } CText(const Coord &np, const char *s) { this->setPosition(np); this->txt = s; + cursorPos = strlen(s); + bTextEditMode = false; } CText(int _x, int _y, const char *s) { this->setPosition(_x, _y); this->txt = s; + cursorPos = strlen(s); + bTextEditMode = false; } + void setTextEditMode(bool b) { + bTextEditMode = b; + } + void setText(const char *s) { + this->txt = s; + cursorPos = strlen(s); + } + const char *getText() const { + return this->txt.c_str(); + } + void appendText(const char *s); + bool processKeyDown(int keyCode); virtual CShape *cloneShape(); virtual float drawPrivateInformation2D(float x, float h); virtual const char *getClassName() const { - return "CShape"; + return "CText"; } virtual void drawShape(); }; diff --git a/src/sim/Tool_Base.h b/src/sim/Tool_Base.h index 63230e414..911ddfcd8 100644 --- a/src/sim/Tool_Base.h +++ b/src/sim/Tool_Base.h @@ -21,6 +21,9 @@ public: } virtual void onMouseDown(const class Coord &pos, int button) { + } + virtual void onTextInput(const char *inputText) { + } virtual void onMouseUp(const class Coord &pos, int button) { diff --git a/src/sim/Tool_Copy.cpp b/src/sim/Tool_Copy.cpp index 7f5052344..9d5c817d9 100644 --- a/src/sim/Tool_Copy.cpp +++ b/src/sim/Tool_Copy.cpp @@ -3,7 +3,6 @@ #include "Tool_Copy.h" #include "Simulator.h" #include "Shape.h" -#include "Object.h" #include "CursorManager.h" #include "Simulation.h" diff --git a/src/sim/Tool_Text.cpp b/src/sim/Tool_Text.cpp index d32daa573..e554043d6 100644 --- a/src/sim/Tool_Text.cpp +++ b/src/sim/Tool_Text.cpp @@ -3,10 +3,18 @@ #include "Simulator.h" #include "Simulation.h" #include "Shape.h" +#include "Text.h" #include "CursorManager.h" Tool_Text::Tool_Text() { currentTarget = 0; +} +void Tool_Text::onTextInput(const char *inputText) { + +} +void Tool_Text::onKeyDown(int button) { + + } void Tool_Text::onMouseUp(const Coord &pos, int button) { @@ -17,8 +25,23 @@ int Tool_Text::drawTextStats(int h) { return h; } void Tool_Text::onMouseDown(const Coord &pos, int button) { - currentTarget = sim->getSim()->addText(pos, "TEXT"); - + if (currentTarget) { + sim->beginEditingText(0); + currentTarget = 0; + return; + } + CShape *candid = sim->getShapeUnderCursor(); + if (candid != 0) { + CText *txt = dynamic_cast(candid); + if (txt != 0) { + currentTarget = txt; + sim->beginEditingText(currentTarget); + } + } + else { + currentTarget = sim->getSim()->addText(pos, "TEXT"); + sim->beginEditingText(currentTarget); + } } void Tool_Text::drawTool() { diff --git a/src/sim/Tool_Text.h b/src/sim/Tool_Text.h index f737d08d3..1405bd356 100644 --- a/src/sim/Tool_Text.h +++ b/src/sim/Tool_Text.h @@ -17,7 +17,8 @@ public: virtual void drawTool(); virtual void onMouseDown(const Coord &pos, int button); virtual void onMouseUp(const Coord &pos, int button); + virtual void onTextInput(const char *inputText); + virtual void onKeyDown(int button); }; - #endif // __TOOL_TEXT_H__ diff --git a/src/sim/sim_sdl.cpp b/src/sim/sim_sdl.cpp index ff30a71a7..9eec39f39 100644 --- a/src/sim/sim_sdl.cpp +++ b/src/sim/sim_sdl.cpp @@ -3,7 +3,6 @@ #include "sim_local.h" #include "Texture.h" #include "Shape.h" -#include "Object.h" #include "Coord.h" #include "Simulator.h" #include "Simulation.h"