diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-16 10:30:31 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-16 10:30:31 -0400 |
commit | c46db36fe1c319a76cb6bd089b25952db0d98e11 (patch) | |
tree | a4ede892db1dc292a221ff33f991e1b92028a9b4 | |
parent | 54eee8e0a79898825b39749b38134f4369ccd3ab (diff) | |
download | therapy-c46db36fe1c319a76cb6bd089b25952db0d98e11.tar.gz therapy-c46db36fe1c319a76cb6bd089b25952db0d98e11.tar.bz2 therapy-c46db36fe1c319a76cb6bd089b25952db0d98e11.zip |
Added ability to edit and remove entities to map editor
-rw-r--r-- | tools/mapedit/src/frame.cpp | 7 | ||||
-rw-r--r-- | tools/mapedit/src/frame.h | 1 | ||||
-rw-r--r-- | tools/mapedit/src/map.cpp | 25 | ||||
-rw-r--r-- | tools/mapedit/src/map.h | 11 | ||||
-rw-r--r-- | tools/mapedit/src/object.cpp | 6 | ||||
-rw-r--r-- | tools/mapedit/src/tile_widget.cpp | 2 | ||||
-rw-r--r-- | tools/mapedit/src/widget.cpp | 89 | ||||
-rw-r--r-- | tools/mapedit/src/widget.h | 4 |
8 files changed, 126 insertions, 19 deletions
diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index 6119ad3..0b03448 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp | |||
@@ -301,6 +301,13 @@ void MapeditFrame::OnCancelAddEntity(wxCommandEvent&) | |||
301 | mapEditor->CancelAddingEntity(); | 301 | mapEditor->CancelAddingEntity(); |
302 | } | 302 | } |
303 | 303 | ||
304 | void MapeditFrame::StartAddingEntity() | ||
305 | { | ||
306 | addingEntity = true; | ||
307 | addEntityButton->Disable(); | ||
308 | cancelEntityButton->Enable(); | ||
309 | } | ||
310 | |||
304 | void MapeditFrame::FinishAddingEntity() | 311 | void MapeditFrame::FinishAddingEntity() |
305 | { | 312 | { |
306 | addingEntity = false; | 313 | addingEntity = false; |
diff --git a/tools/mapedit/src/frame.h b/tools/mapedit/src/frame.h index cb29480..1cce4f3 100644 --- a/tools/mapedit/src/frame.h +++ b/tools/mapedit/src/frame.h | |||
@@ -19,6 +19,7 @@ class MapeditFrame : public wxFrame { | |||
19 | MapeditFrame(Map map, std::string filename); | 19 | MapeditFrame(Map map, std::string filename); |
20 | 20 | ||
21 | MapeditWidget* GetMapEditor(); | 21 | MapeditWidget* GetMapEditor(); |
22 | void StartAddingEntity(); | ||
22 | void FinishAddingEntity(); | 23 | void FinishAddingEntity(); |
23 | 24 | ||
24 | static void NewMap(); | 25 | static void NewMap(); |
diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index 1414e16..2799a4e 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp | |||
@@ -60,18 +60,19 @@ Map::Map(std::string filename) | |||
60 | { | 60 | { |
61 | if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) | 61 | if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) |
62 | { | 62 | { |
63 | MapObjectEntry data; | 63 | auto data = std::make_shared<MapObjectEntry>(); |
64 | |||
64 | for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) | 65 | for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) |
65 | { | 66 | { |
66 | if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) | 67 | if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) |
67 | { | 68 | { |
68 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); | 69 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); |
69 | data.object = MapObject::getAllObjects().at((char*) key).get(); | 70 | data->object = MapObject::getAllObjects().at((char*) key).get(); |
70 | xmlFree(key); | 71 | xmlFree(key); |
71 | } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) | 72 | } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) |
72 | { | 73 | { |
73 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); | 74 | xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); |
74 | sscanf((char*) key, "%lf,%lf", &data.position.first, &data.position.second); | 75 | sscanf((char*) key, "%lf,%lf", &data->position.first, &data->position.second); |
75 | xmlFree(key); | 76 | xmlFree(key); |
76 | } | 77 | } |
77 | } | 78 | } |
@@ -172,11 +173,11 @@ void Map::save(std::string name) | |||
172 | rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity"); | 173 | rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity"); |
173 | if (rc < 0) throw MapWriteException(name); | 174 | if (rc < 0) throw MapWriteException(name); |
174 | 175 | ||
175 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object.object->getType().c_str()); | 176 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object->object->getType().c_str()); |
176 | if (rc < 0) throw MapWriteException(name); | 177 | if (rc < 0) throw MapWriteException(name); |
177 | 178 | ||
178 | std::ostringstream entpos_out; | 179 | std::ostringstream entpos_out; |
179 | entpos_out << object.position.first << "," << object.position.second; | 180 | entpos_out << object->position.first << "," << object->position.second; |
180 | 181 | ||
181 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str()); | 182 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str()); |
182 | if (rc < 0) throw MapWriteException(name); | 183 | if (rc < 0) throw MapWriteException(name); |
@@ -226,7 +227,19 @@ void Map::setTitle(std::string title) | |||
226 | this->title = title; | 227 | this->title = title; |
227 | } | 228 | } |
228 | 229 | ||
229 | std::list<MapObjectEntry>& Map::getObjects() | 230 | const std::list<std::shared_ptr<MapObjectEntry>>& Map::getObjects() const |
230 | { | 231 | { |
231 | return objects; | 232 | return objects; |
232 | } | 233 | } |
234 | |||
235 | void Map::addObject(std::shared_ptr<MapObjectEntry>& obj) | ||
236 | { | ||
237 | dirty = true; | ||
238 | objects.push_back(obj); | ||
239 | } | ||
240 | |||
241 | void Map::removeObject(std::shared_ptr<MapObjectEntry>& obj) | ||
242 | { | ||
243 | dirty = true; | ||
244 | objects.remove(obj); | ||
245 | } | ||
diff --git a/tools/mapedit/src/map.h b/tools/mapedit/src/map.h index 850a66b..16bbb4c 100644 --- a/tools/mapedit/src/map.h +++ b/tools/mapedit/src/map.h | |||
@@ -46,6 +46,11 @@ class MapWriteException: public std::exception | |||
46 | struct MapObjectEntry { | 46 | struct MapObjectEntry { |
47 | MapObject* object; | 47 | MapObject* object; |
48 | std::pair<double, double> position; | 48 | std::pair<double, double> position; |
49 | |||
50 | bool operator==(MapObjectEntry& other) const | ||
51 | { | ||
52 | return (object == other.object) && (position == other.position); | ||
53 | } | ||
49 | }; | 54 | }; |
50 | 55 | ||
51 | class Map { | 56 | class Map { |
@@ -64,10 +69,12 @@ class Map { | |||
64 | bool hasUnsavedChanges() const; | 69 | bool hasUnsavedChanges() const; |
65 | void setTileAt(int x, int y, int tile); | 70 | void setTileAt(int x, int y, int tile); |
66 | int getTileAt(int x, int y) const; | 71 | int getTileAt(int x, int y) const; |
67 | std::list<MapObjectEntry>& getObjects(); | 72 | const std::list<std::shared_ptr<MapObjectEntry>>& getObjects() const; |
73 | void addObject(std::shared_ptr<MapObjectEntry>& obj); | ||
74 | void removeObject(std::shared_ptr<MapObjectEntry>& obj); | ||
68 | 75 | ||
69 | private: | 76 | private: |
70 | std::list<MapObjectEntry> objects; | 77 | std::list<std::shared_ptr<MapObjectEntry>> objects; |
71 | int* mapdata; | 78 | int* mapdata; |
72 | std::string title; | 79 | std::string title; |
73 | std::string leftmap; | 80 | std::string leftmap; |
diff --git a/tools/mapedit/src/object.cpp b/tools/mapedit/src/object.cpp index 4cd267d..a783691 100644 --- a/tools/mapedit/src/object.cpp +++ b/tools/mapedit/src/object.cpp | |||
@@ -10,7 +10,7 @@ const std::map<std::string, std::shared_ptr<MapObject>> MapObject::getAllObjects | |||
10 | { | 10 | { |
11 | if (!objsInit) | 11 | if (!objsInit) |
12 | { | 12 | { |
13 | DIR* dir = opendir("../../../entities/"); | 13 | DIR* dir = opendir("entities/"); |
14 | if (dir != NULL) | 14 | if (dir != NULL) |
15 | { | 15 | { |
16 | struct dirent* ent; | 16 | struct dirent* ent; |
@@ -37,7 +37,7 @@ MapObject::MapObject(const char* filename) | |||
37 | { | 37 | { |
38 | type = filename; | 38 | type = filename; |
39 | 39 | ||
40 | xmlDocPtr doc = xmlParseFile(("../../../entities/" + std::string(filename) + ".xml").c_str()); | 40 | xmlDocPtr doc = xmlParseFile(("entities/" + std::string(filename) + ".xml").c_str()); |
41 | if (doc == nullptr) throw MapObjectLoadException(filename); | 41 | if (doc == nullptr) throw MapObjectLoadException(filename); |
42 | 42 | ||
43 | xmlNodePtr top = xmlDocGetRootElement(doc); | 43 | xmlNodePtr top = xmlDocGetRootElement(doc); |
@@ -56,7 +56,7 @@ MapObject::MapObject(const char* filename) | |||
56 | std::string spriteFile = (char*) key; | 56 | std::string spriteFile = (char*) key; |
57 | xmlFree(key); | 57 | xmlFree(key); |
58 | 58 | ||
59 | sprite = wxImage("../../" + spriteFile); | 59 | sprite = wxImage(spriteFile); |
60 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "action")) | 60 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "action")) |
61 | { | 61 | { |
62 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | 62 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); |
diff --git a/tools/mapedit/src/tile_widget.cpp b/tools/mapedit/src/tile_widget.cpp index dc39fdd..7ffaf30 100644 --- a/tools/mapedit/src/tile_widget.cpp +++ b/tools/mapedit/src/tile_widget.cpp | |||
@@ -26,7 +26,7 @@ int TileWidget::getSelected() | |||
26 | 26 | ||
27 | void TileWidget::Init() | 27 | void TileWidget::Init() |
28 | { | 28 | { |
29 | tiles = wxBitmap(wxImage("../../../res/tiles.png")); | 29 | tiles = wxBitmap(wxImage("res/tiles.png")); |
30 | 30 | ||
31 | this->FitInside(); | 31 | this->FitInside(); |
32 | this->SetScrollRate(5, 5); | 32 | this->SetScrollRate(5, 5); |
diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index a78caae..c1f044c 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp | |||
@@ -6,6 +6,7 @@ IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxScrolledWindow) | |||
6 | BEGIN_EVENT_TABLE(MapeditWidget, wxScrolledWindow) | 6 | BEGIN_EVENT_TABLE(MapeditWidget, wxScrolledWindow) |
7 | EVT_PAINT(MapeditWidget::OnPaint) | 7 | EVT_PAINT(MapeditWidget::OnPaint) |
8 | EVT_LEFT_DOWN(MapeditWidget::OnClick) | 8 | EVT_LEFT_DOWN(MapeditWidget::OnClick) |
9 | EVT_RIGHT_DOWN(MapeditWidget::OnRightClick) | ||
9 | EVT_LEFT_UP(MapeditWidget::OnMouseUp) | 10 | EVT_LEFT_UP(MapeditWidget::OnMouseUp) |
10 | EVT_MOTION(MapeditWidget::OnMouseMove) | 11 | EVT_MOTION(MapeditWidget::OnMouseMove) |
11 | EVT_LEAVE_WINDOW(MapeditWidget::OnMouseOut) | 12 | EVT_LEAVE_WINDOW(MapeditWidget::OnMouseOut) |
@@ -24,7 +25,7 @@ MapeditWidget::MapeditWidget(wxWindow* parent, wxWindowID winid, Map* map, TileW | |||
24 | 25 | ||
25 | void MapeditWidget::Init() | 26 | void MapeditWidget::Init() |
26 | { | 27 | { |
27 | tiles = wxBitmap(wxImage("../../../res/tiles.png")); | 28 | tiles = wxBitmap(wxImage("res/tiles.png")); |
28 | 29 | ||
29 | this->FitInside(); | 30 | this->FitInside(); |
30 | this->SetScrollRate(5, 5); | 31 | this->SetScrollRate(5, 5); |
@@ -62,10 +63,18 @@ void MapeditWidget::OnPaint(wxPaintEvent&) | |||
62 | { | 63 | { |
63 | tiles_dc.SelectObject(wxNullBitmap); | 64 | tiles_dc.SelectObject(wxNullBitmap); |
64 | 65 | ||
65 | wxBitmap sprite = object.object->getSprite(); | 66 | wxBitmap sprite = object->object->getSprite(); |
66 | tiles_dc.SelectObject(sprite); | 67 | tiles_dc.SelectObject(sprite); |
67 | 68 | ||
68 | dc.StretchBlit(object.position.first*scale-vX, object.position.second*scale-vY, object.object->getWidth()*scale, object.object->getHeight()*scale, &tiles_dc, 0, 0, object.object->getWidth(), object.object->getHeight()); | 69 | dc.StretchBlit(object->position.first*scale-vX, object->position.second*scale-vY, object->object->getWidth()*scale, object->object->getHeight()*scale, &tiles_dc, 0, 0, object->object->getWidth(), object->object->getHeight()); |
70 | |||
71 | if ((editMode == EditEntities) && (selectedEntity) && (object == selectedEntity)) | ||
72 | { | ||
73 | wxPen pen(*wxGREEN, 2); | ||
74 | dc.SetPen(pen); | ||
75 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | ||
76 | dc.DrawRectangle(object->position.first*scale-vX, object->position.second*scale-vY, object->object->getWidth()*scale, object->object->getHeight()*scale); | ||
77 | } | ||
69 | } | 78 | } |
70 | 79 | ||
71 | if (editMode == EditTiles) | 80 | if (editMode == EditTiles) |
@@ -140,22 +149,87 @@ void MapeditWidget::OnClick(wxMouseEvent& event) | |||
140 | int x = (event.GetPosition().x + vX) / scale - (addingEntity->getWidth() / 2); | 149 | int x = (event.GetPosition().x + vX) / scale - (addingEntity->getWidth() / 2); |
141 | int y = (event.GetPosition().y + vY) / scale - (addingEntity->getHeight() / 2); | 150 | int y = (event.GetPosition().y + vY) / scale - (addingEntity->getHeight() / 2); |
142 | 151 | ||
143 | MapObjectEntry data; | 152 | auto data = std::make_shared<MapObjectEntry>(); |
144 | data.object = addingEntity; | 153 | data->object = addingEntity; |
145 | data.position = std::make_pair(x,y); | 154 | data->position = std::make_pair(x,y); |
146 | map->getObjects().push_back(data); | 155 | map->addObject(data); |
147 | 156 | ||
148 | addingEntity = nullptr; | 157 | addingEntity = nullptr; |
149 | 158 | ||
150 | frame->FinishAddingEntity(); | 159 | frame->FinishAddingEntity(); |
151 | 160 | ||
152 | Refresh(); | 161 | Refresh(); |
162 | } else { | ||
163 | int vX, vY; | ||
164 | GetViewStart(&vX, &vY); | ||
165 | int vXX, vYX; | ||
166 | GetScrollPixelsPerUnit(&vXX, &vYX); | ||
167 | vX *= vXX; | ||
168 | vY *= vYX; | ||
169 | |||
170 | int x = (event.GetPosition().x + vX) / scale; | ||
171 | int y = (event.GetPosition().y + vY) / scale; | ||
172 | |||
173 | if (selectedEntity) | ||
174 | { | ||
175 | if ((x > selectedEntity->position.first) && (x < selectedEntity->position.first + selectedEntity->object->getWidth()) | ||
176 | && (y > selectedEntity->position.second) && (y < selectedEntity->position.second + selectedEntity->object->getHeight())) | ||
177 | { | ||
178 | addingEntity = selectedEntity->object; | ||
179 | map->removeObject(selectedEntity); | ||
180 | selectedEntity.reset(); | ||
181 | frame->StartAddingEntity(); | ||
182 | } else { | ||
183 | selectedEntity.reset(); | ||
184 | } | ||
185 | |||
186 | Refresh(); | ||
187 | } else { | ||
188 | for (auto object : map->getObjects()) | ||
189 | { | ||
190 | if ((x >= object->position.first) && (x <= object->position.first + object->object->getWidth()) | ||
191 | && (y >= object->position.second) && (y <= object->position.second + object->object->getHeight())) | ||
192 | { | ||
193 | selectedEntity = object; | ||
194 | |||
195 | Refresh(); | ||
196 | |||
197 | break; | ||
198 | } | ||
199 | } | ||
200 | } | ||
153 | } | 201 | } |
154 | } | 202 | } |
155 | 203 | ||
156 | event.Skip(); | 204 | event.Skip(); |
157 | } | 205 | } |
158 | 206 | ||
207 | void MapeditWidget::OnRightClick(wxMouseEvent& event) | ||
208 | { | ||
209 | if (editMode == EditEntities) | ||
210 | { | ||
211 | if (selectedEntity) | ||
212 | { | ||
213 | int vX, vY; | ||
214 | GetViewStart(&vX, &vY); | ||
215 | int vXX, vYX; | ||
216 | GetScrollPixelsPerUnit(&vXX, &vYX); | ||
217 | vX *= vXX; | ||
218 | vY *= vYX; | ||
219 | |||
220 | int x = (event.GetPosition().x + vX) / scale; | ||
221 | int y = (event.GetPosition().y + vY) / scale; | ||
222 | |||
223 | if ((x > selectedEntity->position.first) && (x < selectedEntity->position.first + selectedEntity->object->getWidth()) | ||
224 | && (y > selectedEntity->position.second) && (y < selectedEntity->position.second + selectedEntity->object->getHeight())) | ||
225 | { | ||
226 | map->removeObject(selectedEntity); | ||
227 | selectedEntity.reset(); | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | |||
159 | void MapeditWidget::OnMouseMove(wxMouseEvent& event) | 233 | void MapeditWidget::OnMouseMove(wxMouseEvent& event) |
160 | { | 234 | { |
161 | mousePos = event.GetPosition(); | 235 | mousePos = event.GetPosition(); |
@@ -216,6 +290,7 @@ void MapeditWidget::SetEditMode(EditMode editMode) | |||
216 | void MapeditWidget::StartAddingEntity(MapObject* object) | 290 | void MapeditWidget::StartAddingEntity(MapObject* object) |
217 | { | 291 | { |
218 | addingEntity = object; | 292 | addingEntity = object; |
293 | selectedEntity = nullptr; | ||
219 | } | 294 | } |
220 | 295 | ||
221 | void MapeditWidget::CancelAddingEntity() | 296 | void MapeditWidget::CancelAddingEntity() |
diff --git a/tools/mapedit/src/widget.h b/tools/mapedit/src/widget.h index 9de5b02..a660f82 100644 --- a/tools/mapedit/src/widget.h +++ b/tools/mapedit/src/widget.h | |||
@@ -9,6 +9,8 @@ | |||
9 | 9 | ||
10 | #include "map.h" | 10 | #include "map.h" |
11 | #include "tile_widget.h" | 11 | #include "tile_widget.h" |
12 | #include <list> | ||
13 | #include <memory> | ||
12 | 14 | ||
13 | class MapeditFrame; | 15 | class MapeditFrame; |
14 | 16 | ||
@@ -35,6 +37,7 @@ class MapeditWidget : public wxScrolledWindow { | |||
35 | virtual wxSize DoGetBestSize() const; | 37 | virtual wxSize DoGetBestSize() const; |
36 | void OnPaint(wxPaintEvent& event); | 38 | void OnPaint(wxPaintEvent& event); |
37 | void OnClick(wxMouseEvent& event); | 39 | void OnClick(wxMouseEvent& event); |
40 | void OnRightClick(wxMouseEvent& event); | ||
38 | void OnMouseMove(wxMouseEvent& event); | 41 | void OnMouseMove(wxMouseEvent& event); |
39 | void OnMouseUp(wxMouseEvent& event); | 42 | void OnMouseUp(wxMouseEvent& event); |
40 | void OnMouseOut(wxMouseEvent& event); | 43 | void OnMouseOut(wxMouseEvent& event); |
@@ -53,6 +56,7 @@ class MapeditWidget : public wxScrolledWindow { | |||
53 | EditMode editMode = EditTiles; | 56 | EditMode editMode = EditTiles; |
54 | 57 | ||
55 | MapObject* addingEntity = nullptr; | 58 | MapObject* addingEntity = nullptr; |
59 | std::shared_ptr<MapObjectEntry> selectedEntity; | ||
56 | 60 | ||
57 | DECLARE_DYNAMIC_CLASS(MapeditWidget) | 61 | DECLARE_DYNAMIC_CLASS(MapeditWidget) |
58 | DECLARE_EVENT_TABLE() | 62 | DECLARE_EVENT_TABLE() |