diff --git a/src/sim/Circle.cpp b/src/sim/Circle.cpp index d2f4c7f56..d70e92f50 100644 --- a/src/sim/Circle.cpp +++ b/src/sim/Circle.cpp @@ -1,6 +1,14 @@ #ifdef WINDOWS #include "Circle.h" +CShape *CCircle::cloneShape() { + CCircle *r = new CCircle(); + r->pos = this->pos; + r->radius = this->radius; + r->corners = this->corners; + this->cloneShapeTo(r); + return r; +} void CCircle::recalcBoundsSelf() { bounds.clear(); bounds.addPoint(Coord(0, 0)); diff --git a/src/sim/Circle.h b/src/sim/Circle.h index 6ee493062..be5a5c4f0 100644 --- a/src/sim/Circle.h +++ b/src/sim/Circle.h @@ -10,11 +10,16 @@ class CCircle : public CShape { virtual void recalcBoundsSelf(); void rotateDegreesAround_internal(float f, const Coord &p); public: + CCircle() { + radius = 0; + corners = 0; + } CCircle(float _x, float _y, float _r, int _corners = 20) { this->setPosition(_x, _y); this->radius = _r; this->corners = _corners; } + virtual CShape *cloneShape(); virtual const char *getClassName() const { return "CCircle"; } diff --git a/src/sim/Junction.cpp b/src/sim/Junction.cpp index 861758aae..d816a9dae 100644 --- a/src/sim/Junction.cpp +++ b/src/sim/Junction.cpp @@ -7,6 +7,18 @@ CJunction::~CJunction() { oj->unlink(this); } } +CShape *CJunction::cloneShape() { + CJunction *r = new CJunction(); + r->pos = this->pos; + r->name = this->name; + r->gpioIndex = this->gpioIndex; + r->voltage = this->voltage; + r->visitCount = this->visitCount; + r->bCurrentSource = this->bCurrentSource; + this->cloneShapeTo(r); + return r; +} + float CJunction::drawInformation2D(float x, float h) { h = CShape::drawInformation2D(x, h); h = drawText(x, h, "Position: %f %f", getX(), getY()); diff --git a/src/sim/Junction.h b/src/sim/Junction.h index 884972ba9..949a19f3e 100644 --- a/src/sim/Junction.h +++ b/src/sim/Junction.h @@ -35,6 +35,9 @@ class CJunction : public CShape { int visitCount; bool bCurrentSource; public: + CJunction() { + + } CJunction(int _x, int _y, const char *s, int gpio = -1) { this->setPosition(_x, _y); this->name = s; @@ -44,6 +47,7 @@ public: this->bCurrentSource = false; } virtual ~CJunction(); + virtual CShape *cloneShape(); void setCurrentSource(bool b) { bCurrentSource = b; } diff --git a/src/sim/Line.cpp b/src/sim/Line.cpp index 405a33601..f1ef725eb 100644 --- a/src/sim/Line.cpp +++ b/src/sim/Line.cpp @@ -35,6 +35,13 @@ void CLine::recalcBoundsSelf() { bounds.addPoint(pos2 - getPosition()); } +CShape *CLine::cloneShape() { + CLine *n = new CLine(); + n->pos = this->pos; + n->pos2 = this->pos2; + this->cloneShapeTo(n); + return n; +} void CLine::drawShape() { glBegin(GL_LINES); glVertex2f(getX(), getY()); diff --git a/src/sim/Line.h b/src/sim/Line.h index e594a991c..33d5e55f1 100644 --- a/src/sim/Line.h +++ b/src/sim/Line.h @@ -8,6 +8,9 @@ class CLine : public CShape { Coord pos2; public: + CLine() { + + } CLine(float _x, float _y, float _x2, float _y2) { this->setPosition(_x, _y); this->pos2.set(_x2, _y2); @@ -20,6 +23,7 @@ public: virtual void rotateDegreesAround_internal(float f, const Coord &p); virtual void recalcBoundsSelf(); virtual void drawShape(); + virtual CShape *cloneShape(); }; #endif // __LINE_H__ diff --git a/src/sim/Object.h b/src/sim/Object.h index 4197e2ad9..dda1534ae 100644 --- a/src/sim/Object.h +++ b/src/sim/Object.h @@ -11,4 +11,6 @@ public: CObject(CShape *s) { addShape(s); } + virtual CShape *cloneShape(); + class CObject *cloneObject(); }; diff --git a/src/sim/Rect.cpp b/src/sim/Rect.cpp index d684270ef..b1cad99af 100644 --- a/src/sim/Rect.cpp +++ b/src/sim/Rect.cpp @@ -4,6 +4,14 @@ +CShape *CRect::cloneShape() { + CRect *n = new CRect(); + n->pos = this->pos; + n->w = this->w; + n->h = this->h; + this->cloneShapeTo(n); + return n; +} void CRect::drawShape() { glBegin(GL_LINE_LOOP); glVertex2f(getX(), getY()); diff --git a/src/sim/Rect.h b/src/sim/Rect.h index 82d277975..327e4f9ff 100644 --- a/src/sim/Rect.h +++ b/src/sim/Rect.h @@ -11,11 +11,16 @@ class CRect : public CShape { virtual void recalcBoundsSelf(); void rotateDegreesAround_internal(float f, const class Coord &p); public: + CRect() { + this->w = 0; + this->h = 0; + } CRect(int _x, int _y, int _w, int _h) { this->setPosition(_x, _y); this->w = _w; this->h = _h; } + virtual CShape *cloneShape(); virtual const char *getClassName() const { return "CRect"; } diff --git a/src/sim/Shape.cpp b/src/sim/Shape.cpp index abc10d703..8e4a97de4 100644 --- a/src/sim/Shape.cpp +++ b/src/sim/Shape.cpp @@ -18,6 +18,22 @@ CShape::~CShape() { controller = 0; } } +void CShape::snapToGrid() { + Coord n = roundToGrid(getPosition()); + setPosition(n); +} +void CShape::cloneShapeTo(CShape *o) { + o->pos = this->pos; + o->bounds = this->bounds; + for (int i = 0; i < shapes.size(); i++) { + o->addShape(shapes[i]->cloneShape()); + } +} +CShape *CShape::cloneShape() { + CShape *r = new CShape(); + this->cloneShapeTo(r); + return r; +} float CShape::drawPrivateInformation2D(float x, float h) { h = drawText(x, h, "Position: %f %f", this->getX(), getY()); diff --git a/src/sim/Shape.h b/src/sim/Shape.h index a66ab6e24..b87080262 100644 --- a/src/sim/Shape.h +++ b/src/sim/Shape.h @@ -26,6 +26,9 @@ public: bActive = true; bFill = false; } + void snapToGrid(); + void cloneShapeTo(CShape *o); + virtual CShape *cloneShape(); void setFill(bool b) { bFill = b; } diff --git a/src/sim/Simulation.cpp b/src/sim/Simulation.cpp index 280831b2c..b5ba11588 100644 --- a/src/sim/Simulation.cpp +++ b/src/sim/Simulation.cpp @@ -98,7 +98,8 @@ CObject * CSimulation::addObject(CObject *o) { return o; } void CSimulation::createDemo() { - addObject(generateWB3S())->setPosition(300, 200); + CObject *wb3s = addObject(generateWB3S()); + wb3s->setPosition(300, 200); addObject(generateButton())->setPosition(500, 260)->rotateDegreesAroundSelf(180); addObject(generateTest())->setPosition(500, 400); addObject(generateGND())->setPosition(600, 420); @@ -119,14 +120,34 @@ void CSimulation::createDemo() { addWire(Coord(700, 400), Coord(700, 240)); addObject(generateGND())->setPosition(700, 420); //addObject(new CObject(new CCircle(800, 500, 100))); - addObject(generateBulb())->setPosition(440, 140)->rotateDegreesAroundSelf(90); + CObject *bulb2 = addObject(generateBulb()); + bulb2->setPosition(440, 140)->rotateDegreesAroundSelf(90); addWire(Coord(440, 60), Coord(440, 120)); addWire(Coord(440, 200), Coord(440, 160)); addWire(Coord(440, 200), Coord(380, 200)); addObject(generateVDD())->setPosition(440, 60); + + CObject *bulb2_copy = bulb2->cloneObject(); + bulb2_copy->setPosition(640, 140); + addObject(bulb2_copy); + + + CObject *wb3s_copy = wb3s->cloneObject(); + wb3s_copy->setPosition(640, 440); + addObject(wb3s_copy); + matchAllJunctions(); recalcBounds(); } +CShape *CObject::cloneShape() { + CObject *no = new CObject(); + this->cloneShapeTo(no); + return no; +} +class CObject *CObject::cloneObject() { + CShape *sh = cloneShape(); + return dynamic_cast(sh); +} void CSimulation::matchAllJunctions() { for (int i = 0; i < wires.size(); i++) { CWire *w = wires[i]; diff --git a/src/sim/Simulator.cpp b/src/sim/Simulator.cpp index cc26067c6..065dd14ac 100644 --- a/src/sim/Simulator.cpp +++ b/src/sim/Simulator.cpp @@ -11,6 +11,7 @@ #include "Tool_Delete.h" #include "Tool_Move.h" #include "Tool_Info.h" +#include "Tool_Copy.h" #include "Simulation.h" #include "CursorManager.h" #include "Solver.h" @@ -171,6 +172,9 @@ void CSimulator::onKeyDown(int keyCode) { setTool(new Tool_Delete()); } if (keyCode == '5') { + setTool(new Tool_Copy()); + } + if (keyCode == '6') { setTool(new Tool_Info()); } //SDL_Cursor* cursor; diff --git a/src/sim/Text.cpp b/src/sim/Text.cpp index 668a96893..8c109a3b6 100644 --- a/src/sim/Text.cpp +++ b/src/sim/Text.cpp @@ -1,6 +1,15 @@ #ifdef WINDOWS #include "Text.h" +CShape *CText::cloneShape() { + CText *r = new CText(); + r->pos = this->pos; + r->txt = this->txt; + this->cloneShapeTo(r); + return r; +} + + void CText::drawShape() { drawText(getX(), getY(), txt.c_str()); } diff --git a/src/sim/Text.h b/src/sim/Text.h index 558391e3c..b2c33e0b7 100644 --- a/src/sim/Text.h +++ b/src/sim/Text.h @@ -9,10 +9,14 @@ class CText : public CShape { void rotateDegreesAround_internal(float f, const class Coord &p); public: + CText() { + + } CText(int _x, int _y, const char *s) { this->setPosition(_x, _y); this->txt = s; } + virtual CShape *cloneShape(); virtual float drawPrivateInformation2D(float x, float h); virtual const char *getClassName() const { return "CShape"; diff --git a/src/sim/Tool_Copy.h b/src/sim/Tool_Copy.h index 983c6b676..734dc7732 100644 --- a/src/sim/Tool_Copy.h +++ b/src/sim/Tool_Copy.h @@ -1,20 +1,21 @@ -#ifndef __TOOL_WIRE_H__ -#define __TOOL_WIRE_H__ +#ifndef __TOOL_COPY_H__ +#define __TOOL_COPY_H__ #include "sim_local.h" #include "Tool_Base.h" #include "Coord.h" class Tool_Copy : public Tool_Base { - CObject *copyingObject; + class CObject *copyingObject; + Coord prevMousePos; public: Tool_Copy(); + ~Tool_Copy(); virtual const char *getName() const { return "Copy"; } - virtual void onKeyDown(int button); - virtual void onMouseDown(const Coord &pos, int button); + virtual void onMouseDown(const class Coord &pos, int button); virtual void drawTool(); }; -#endif // __TOOL_WIRE_H__ +#endif // __TOOL_COPY_H__ diff --git a/src/sim/Tool_Delete.cpp b/src/sim/Tool_Delete.cpp index 90505d534..1ce59ccdd 100644 --- a/src/sim/Tool_Delete.cpp +++ b/src/sim/Tool_Delete.cpp @@ -13,6 +13,9 @@ void Tool_Delete::onMouseDown(const Coord &pos, int button) { sim->destroyObject(currentTarget); } } +void Tool_Delete::onEnd() { + sim->getCursorMgr()->setCursor(SDL_SYSTEM_CURSOR_ARROW); +} void Tool_Delete::drawTool() { currentTarget = sim->getShapeUnderCursor(); if (currentTarget) { diff --git a/src/sim/Tool_Delete.h b/src/sim/Tool_Delete.h index 7fc1320e6..01bef80f3 100644 --- a/src/sim/Tool_Delete.h +++ b/src/sim/Tool_Delete.h @@ -13,6 +13,7 @@ public: return "Delete"; } virtual void drawTool(); + virtual void onEnd(); virtual void onMouseDown(const Coord &pos, int button); };