]> Shamusworld >> Repos - architektonas/blob - src/utils.cpp
Initial work on making Rotate tool work, plus removal of unneeded files.
[architektonas] / src / utils.cpp
1 // utils.cpp: Stuff that's useful to have kicking around, in one spot
2 //
3 // Part of the Architektonas Project
4 // (C) 2015 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  05/01/2015  Created this file
12 //
13
14 #include "utils.h"
15 #include <string.h>             // For memcpy()
16
17
18 //
19 // Copy objects in one vector to another, creating copies and placing them in
20 // the other vector. Clearing & etc. of vectors is responsibility of the caller!
21 //
22 void CopyObjects(std::vector<void *> & from, std::vector<void *> & to)
23 {
24         std::vector<void *>::iterator i;
25
26         for(i=from.begin(); i!=from.end(); i++)
27         {
28                 Object * obj = (Object *)(*i);
29                 Object * newObject = CopyObject(obj);
30                 to.push_back(newObject);
31         }
32 }
33
34
35 //
36 // Create a copy of the passed in object.
37 //
38 Object * CopyObject(Object * obj)
39 {
40         Object * newObject = NULL;
41
42         switch (obj->type)
43         {
44         case OTLine:
45                 newObject = (Object *)new Line();
46                 memcpy(newObject, obj, sizeof(Line));
47                 break;
48         case OTCircle:
49                 newObject = (Object *)new Circle();
50                 memcpy(newObject, obj, sizeof(Circle));
51                 break;
52         case OTEllipse:
53                 newObject = (Object *)new Ellipse();
54                 memcpy(newObject, obj, sizeof(Ellipse));
55                 break;
56         case OTArc:
57                 newObject = (Object *)new Arc();
58                 memcpy(newObject, obj, sizeof(Arc));
59                 break;
60         case OTDimension:
61                 newObject = (Object *)new Dimension();
62                 memcpy(newObject, obj, sizeof(Dimension));
63                 break;
64 #if 0
65         case OTSpline:
66                 newObject = (Object *)new Spline();
67                 memcpy(newObject, obj, sizeof(Spline));
68                 break;
69 #endif
70         case OTText:
71                 newObject = (Object *)new Text();
72                 memcpy(newObject, obj, sizeof(Text));
73                 ((Text *)newObject)->s = ((Text *)obj)->s;
74                 break;
75         case OTContainer:
76                 newObject = (Object *)new Container();
77 //this won't work...
78 //              memcpy(newObject, obj, sizeof(Line));
79                 CopyObjects(((Container *)obj)->objects, ((Container *)newObject)->objects);
80                 break;
81         default:
82                 break;
83         }
84
85         // Fix objectID
86         if (newObject && (newObject->type != OTContainer))
87                 newObject->id = Global::objectID;
88
89         return newObject;
90 }
91
92
93 void ClearSelected(std::vector<void *> & v)
94 {
95         std::vector<void *>::iterator i;
96
97         for(i=v.begin(); i!=v.end(); i++)
98                 ((Object *)(*i))->selected = false;
99 }
100
101