summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-03-15 23:24:22 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-03-15 23:24:22 -0400
commit213cab3c04b81428ab715f340bd7c12438fdb980 (patch)
tree295d25eac4b94357ac5b9bb4b3fd03130181b956
parent25240241e91dc913d20fbb93aa4acc9433dda6a0 (diff)
downloadtherapy-213cab3c04b81428ab715f340bd7c12438fdb980.tar.gz
therapy-213cab3c04b81428ab715f340bd7c12438fdb980.tar.bz2
therapy-213cab3c04b81428ab715f340bd7c12438fdb980.zip
Map editor can now add entities to maps
Also fixed a big bug regarding scrolling
-rw-r--r--tools/mapedit/src/frame.cpp129
-rw-r--r--tools/mapedit/src/frame.h12
-rw-r--r--tools/mapedit/src/map.cpp4
-rw-r--r--tools/mapedit/src/map.h4
-rw-r--r--tools/mapedit/src/tile_widget.cpp10
-rw-r--r--tools/mapedit/src/tile_widget.h4
-rw-r--r--tools/mapedit/src/widget.cpp123
-rw-r--r--tools/mapedit/src/widget.h19
8 files changed, 260 insertions, 45 deletions
diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index e0df030..aa77f1f 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp
@@ -47,33 +47,82 @@ MapeditFrame::MapeditFrame(Map map, std::string filename) : wxFrame(NULL, wxID_A
47 SetMenuBar(menuBar); 47 SetMenuBar(menuBar);
48 48
49 // Layout 1: Splitter between map tree and layout 2 49 // Layout 1: Splitter between map tree and layout 2
50 // Layout 2: Non-splitter between layout 3 and tile chooser 50 // Layout 2: Non-splitter between layout 3 and notebook
51 // Layout 3: Splitter between map editor and properties editor 51 // Layout 3: Splitter between map editor and properties editor
52 52
53 wxSplitterWindow* layout3 = new wxSplitterWindow(this, wxID_ANY); 53 wxSplitterWindow* layout3 = new wxSplitterWindow(this, wxID_ANY);
54 layout3->SetSashGravity(1.0); 54 layout3->SetSashGravity(1.0);
55 layout3->SetMinimumPaneSize(20); 55 layout3->SetMinimumPaneSize(20);
56 56
57 tileEditor = new TileWidget(this, wxID_ANY, 6, 6, wxPoint(0,0), wxSize(TILE_WIDTH*6*7,TILE_HEIGHT*10*6)); 57 notebook = new wxNotebook(this, wxID_ANY);
58
59 tileEditor = new TileWidget(notebook, wxID_ANY, 6, 6, wxPoint(0,0), wxSize(TILE_WIDTH*6*6,TILE_HEIGHT*10*6));
60 notebook->AddPage(tileEditor, "Tile Chooser", true);
61
58 mapEditor = new MapeditWidget(layout3, wxID_ANY, &this->map, tileEditor, wxPoint(0,0), wxSize(GAME_WIDTH*2, GAME_HEIGHT*2)); 62 mapEditor = new MapeditWidget(layout3, wxID_ANY, &this->map, tileEditor, wxPoint(0,0), wxSize(GAME_WIDTH*2, GAME_HEIGHT*2));
63 mapEditor->frame = this;
59 64
65 // Set up property editor
60 wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY); 66 wxPanel* propertyEditor = new wxPanel(layout3, wxID_ANY);
61 titleBox = new wxTextCtrl(propertyEditor, wxID_ANY, map.getTitle()); 67 titleBox = new wxTextCtrl(propertyEditor, wxID_ANY, map.getTitle());
62 titleBox->Bind(wxEVT_TEXT, &MapeditFrame::OnTitleChange, this); 68 titleBox->Bind(wxEVT_TEXT, &MapeditFrame::OnTitleChange, this);
63 69
64 wxStaticText* titleLabel = new wxStaticText(propertyEditor, wxID_ANY, "Title:"); 70 wxStaticText* titleLabel = new wxStaticText(propertyEditor, wxID_ANY, "Title:");
65 71
66 wxFlexGridSizer* propertySizer = new wxFlexGridSizer(1, 2, 9, 25); 72 wxBoxSizer* propertySizer = new wxBoxSizer(wxVERTICAL);
67 propertySizer->Add(titleLabel); 73 wxBoxSizer* propertySizer1 = new wxBoxSizer(wxHORIZONTAL);
68 propertySizer->Add(titleBox, 1, wxEXPAND); 74 propertySizer1->Add(titleLabel, 0, wxALIGN_RIGHT | wxLEFT, 10);
75 propertySizer1->Add(titleBox, 1, wxALIGN_LEFT | wxLEFT | wxRIGHT, 10);
76 propertySizer->Add(propertySizer1, 1, wxEXPAND | wxTOP, 10);
69 propertyEditor->SetSizer(propertySizer); 77 propertyEditor->SetSizer(propertySizer);
70 propertySizer->SetSizeHints(propertyEditor); 78 propertySizer->SetSizeHints(propertyEditor);
71 79
80 // Set up entity editor
81 wxPanel* entityEditor = new wxPanel(notebook, wxID_ANY);
82 notebook->AddPage(entityEditor, "Entity Manager", false);
83
84 wxStaticText* entityHeader = new wxStaticText(entityEditor, wxID_ANY, "Add Entity");
85 wxFont font = entityHeader->GetFont();
86 font.SetWeight(wxFONTWEIGHT_BOLD);
87 entityHeader->SetFont(font);
88
89 wxStaticText* entityTypeLabel = new wxStaticText(entityEditor, wxID_ANY, "Entity Type:");
90
91 entityTypeBox = new wxChoice(entityEditor, wxID_ANY);
92 for (auto entry : MapObject::getAllObjects())
93 {
94 entityTypeBox->Append(entry.second->getType(), entry.second.get());
95 }
96
97 addEntityButton = new wxButton(entityEditor, wxID_ANY, "Add Entity");
98 addEntityButton->Bind(wxEVT_BUTTON, &MapeditFrame::OnAddEntity, this);
99
100 cancelEntityButton = new wxButton(entityEditor, wxID_ANY, "Cancel");
101 cancelEntityButton->Disable();
102 cancelEntityButton->Bind(wxEVT_BUTTON, &MapeditFrame::OnCancelAddEntity, this);
103
104 wxBoxSizer* entitySizer = new wxBoxSizer(wxVERTICAL);
105 entitySizer->Add(entityHeader, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
106 wxBoxSizer* entitySizer1 = new wxBoxSizer(wxHORIZONTAL);
107 entitySizer1->Add(entityTypeLabel, 0, wxALIGN_LEFT | wxRIGHT, 5);
108 entitySizer1->Add(entityTypeBox, 1, wxALIGN_LEFT, 0);
109 entitySizer->Add(entitySizer1, 0, wxEXPAND | wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
110 wxBoxSizer* entitySizer2 = new wxBoxSizer(wxHORIZONTAL);
111 entitySizer2->Add(addEntityButton, 1, wxEXPAND | wxRIGHT, 2);
112 entitySizer2->Add(cancelEntityButton, 1, wxEXPAND | wxLEFT, 2);
113 entitySizer->Add(entitySizer2, 0, wxEXPAND | wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
114 entityEditor->SetSizer(entitySizer);
115 entitySizer->SetSizeHints(entityEditor);
116
117 // Finish setting up the layouts
72 layout3->SplitHorizontally(mapEditor, propertyEditor); 118 layout3->SplitHorizontally(mapEditor, propertyEditor);
73 119
120 notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, &MapeditFrame::OnTabChange, this);
121 notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGING, &MapeditFrame::OnTabChanging, this);
122
74 wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL); 123 wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL);
75 sizer2->Add(layout3, 1, wxEXPAND, 0); 124 sizer2->Add(layout3, 1, wxEXPAND, 0);
76 sizer2->Add(tileEditor, 0, wxALIGN_TOP | wxALIGN_CENTER_HORIZONTAL | wxLEFT, 2); 125 sizer2->Add(notebook, 0, wxALIGN_TOP | wxALIGN_CENTER_HORIZONTAL | wxLEFT, 2);
77 this->SetSizer(sizer2); 126 this->SetSizer(sizer2);
78 sizer2->SetSizeHints(this); 127 sizer2->SetSizeHints(this);
79} 128}
@@ -115,22 +164,22 @@ MapeditWidget* MapeditFrame::GetMapEditor()
115 return mapEditor; 164 return mapEditor;
116} 165}
117 166
118void MapeditFrame::ZoomIn(wxCommandEvent& event) 167void MapeditFrame::ZoomIn(wxCommandEvent&)
119{ 168{
120 mapEditor->ZoomIn(); 169 mapEditor->ZoomIn();
121} 170}
122 171
123void MapeditFrame::ZoomOut(wxCommandEvent& event) 172void MapeditFrame::ZoomOut(wxCommandEvent&)
124{ 173{
125 mapEditor->ZoomOut(); 174 mapEditor->ZoomOut();
126} 175}
127 176
128void MapeditFrame::OnNew(wxCommandEvent& event) 177void MapeditFrame::OnNew(wxCommandEvent&)
129{ 178{
130 NewMap(); 179 NewMap();
131} 180}
132 181
133void MapeditFrame::OnOpen(wxCommandEvent& event) 182void MapeditFrame::OnOpen(wxCommandEvent&)
134{ 183{
135 wxFileDialog openFileDialog(this, "Open map", "", "", "XML files (*.xml)|*.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST); 184 wxFileDialog openFileDialog(this, "Open map", "", "", "XML files (*.xml)|*.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
136 if (openFileDialog.ShowModal() == wxID_CANCEL) 185 if (openFileDialog.ShowModal() == wxID_CANCEL)
@@ -141,7 +190,7 @@ void MapeditFrame::OnOpen(wxCommandEvent& event)
141 OpenMap(openFileDialog.GetPath().c_str()); 190 OpenMap(openFileDialog.GetPath().c_str());
142} 191}
143 192
144void MapeditFrame::OnSave(wxCommandEvent& event) 193void MapeditFrame::OnSave(wxCommandEvent&)
145{ 194{
146 if (filename == "") 195 if (filename == "")
147 { 196 {
@@ -157,12 +206,12 @@ void MapeditFrame::OnSave(wxCommandEvent& event)
157 map.save(filename); 206 map.save(filename);
158} 207}
159 208
160void MapeditFrame::OnClose(wxCommandEvent& event) 209void MapeditFrame::OnClose(wxCommandEvent&)
161{ 210{
162 Close(false); 211 Close(false);
163} 212}
164 213
165void MapeditFrame::OnQuit(wxCommandEvent& event) 214void MapeditFrame::OnQuit(wxCommandEvent&)
166{ 215{
167 for (auto window : openWindows) 216 for (auto window : openWindows)
168 { 217 {
@@ -173,7 +222,7 @@ void MapeditFrame::OnQuit(wxCommandEvent& event)
173 } 222 }
174} 223}
175 224
176void MapeditFrame::OnTitleChange(wxCommandEvent& event) 225void MapeditFrame::OnTitleChange(wxCommandEvent&)
177{ 226{
178 map.setTitle(titleBox->GetLineText(0).ToStdString()); 227 map.setTitle(titleBox->GetLineText(0).ToStdString());
179} 228}
@@ -194,3 +243,55 @@ void MapeditFrame::LaunchWindow(Map map, const char* filename)
194 frame->closer = openWindows.insert(end(openWindows), frame); 243 frame->closer = openWindows.insert(end(openWindows), frame);
195 frame->Show(true); 244 frame->Show(true);
196} 245}
246
247void MapeditFrame::OnTabChange(wxBookCtrlEvent& event)
248{
249 switch (event.GetSelection())
250 {
251 case 0:
252 mapEditor->SetEditMode(EditTiles);
253 break;
254
255 case 1:
256 mapEditor->SetEditMode(EditEntities);
257 break;
258 }
259
260 event.Skip();
261}
262
263void MapeditFrame::OnTabChanging(wxBookCtrlEvent& event)
264{
265 if (addingEntity)
266 {
267 event.Veto();
268 return;
269 }
270
271 event.Skip();
272}
273
274void MapeditFrame::OnAddEntity(wxCommandEvent&)
275{
276 addingEntity = true;
277 addEntityButton->Disable();
278 cancelEntityButton->Enable();
279
280 mapEditor->StartAddingEntity((MapObject*) entityTypeBox->GetClientData(entityTypeBox->GetSelection()));
281}
282
283void MapeditFrame::OnCancelAddEntity(wxCommandEvent&)
284{
285 addingEntity = false;
286 addEntityButton->Enable();
287 cancelEntityButton->Disable();
288
289 mapEditor->CancelAddingEntity();
290}
291
292void MapeditFrame::FinishAddingEntity()
293{
294 addingEntity = false;
295 addEntityButton->Enable();
296 cancelEntityButton->Disable();
297}
diff --git a/tools/mapedit/src/frame.h b/tools/mapedit/src/frame.h index 5e119f3..cb29480 100644 --- a/tools/mapedit/src/frame.h +++ b/tools/mapedit/src/frame.h
@@ -11,6 +11,7 @@
11#include "widget.h" 11#include "widget.h"
12#include "tile_widget.h" 12#include "tile_widget.h"
13#include <list> 13#include <list>
14#include <wx/notebook.h>
14 15
15class MapeditFrame : public wxFrame { 16class MapeditFrame : public wxFrame {
16 public: 17 public:
@@ -18,6 +19,7 @@ class MapeditFrame : public wxFrame {
18 MapeditFrame(Map map, std::string filename); 19 MapeditFrame(Map map, std::string filename);
19 20
20 MapeditWidget* GetMapEditor(); 21 MapeditWidget* GetMapEditor();
22 void FinishAddingEntity();
21 23
22 static void NewMap(); 24 static void NewMap();
23 static void OpenMap(const char* filename); 25 static void OpenMap(const char* filename);
@@ -36,12 +38,22 @@ class MapeditFrame : public wxFrame {
36 void OnExit(wxCloseEvent& event); 38 void OnExit(wxCloseEvent& event);
37 void OnQuit(wxCommandEvent& event); 39 void OnQuit(wxCommandEvent& event);
38 void OnTitleChange(wxCommandEvent& event); 40 void OnTitleChange(wxCommandEvent& event);
41 void OnTabChange(wxBookCtrlEvent& event);
42 void OnTabChanging(wxBookCtrlEvent& event);
43 void OnAddEntity(wxCommandEvent& event);
44 void OnCancelAddEntity(wxCommandEvent& event);
39 45
40 Map map; 46 Map map;
41 MapeditWidget* mapEditor; 47 MapeditWidget* mapEditor;
42 TileWidget* tileEditor; 48 TileWidget* tileEditor;
43 wxTextCtrl* titleBox; 49 wxTextCtrl* titleBox;
44 std::string filename; 50 std::string filename;
51 wxNotebook* notebook;
52 wxChoice* entityTypeBox;
53 wxButton* addEntityButton;
54 wxButton* cancelEntityButton;
55
56 bool addingEntity = false;
45 57
46 wxDECLARE_EVENT_TABLE(); 58 wxDECLARE_EVENT_TABLE();
47}; 59};
diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index b0440fb..1414e16 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp
@@ -66,7 +66,7 @@ Map::Map(std::string filename)
66 if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) 66 if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type"))
67 { 67 {
68 xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); 68 xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1);
69 data.object = MapObject::getAllObjects().at((char*) key); 69 data.object = MapObject::getAllObjects().at((char*) key).get();
70 xmlFree(key); 70 xmlFree(key);
71 } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) 71 } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position"))
72 { 72 {
@@ -226,7 +226,7 @@ void Map::setTitle(std::string title)
226 this->title = title; 226 this->title = title;
227} 227}
228 228
229std::list<MapObjectEntry> Map::getObjects() 229std::list<MapObjectEntry>& Map::getObjects()
230{ 230{
231 return objects; 231 return objects;
232} 232}
diff --git a/tools/mapedit/src/map.h b/tools/mapedit/src/map.h index 52e047d..850a66b 100644 --- a/tools/mapedit/src/map.h +++ b/tools/mapedit/src/map.h
@@ -44,7 +44,7 @@ class MapWriteException: public std::exception
44}; 44};
45 45
46struct MapObjectEntry { 46struct MapObjectEntry {
47 std::shared_ptr<MapObject> object; 47 MapObject* object;
48 std::pair<double, double> position; 48 std::pair<double, double> position;
49}; 49};
50 50
@@ -64,7 +64,7 @@ class Map {
64 bool hasUnsavedChanges() const; 64 bool hasUnsavedChanges() const;
65 void setTileAt(int x, int y, int tile); 65 void setTileAt(int x, int y, int tile);
66 int getTileAt(int x, int y) const; 66 int getTileAt(int x, int y) const;
67 std::list<MapObjectEntry> getObjects(); 67 std::list<MapObjectEntry>& getObjects();
68 68
69 private: 69 private:
70 std::list<MapObjectEntry> objects; 70 std::list<MapObjectEntry> objects;
diff --git a/tools/mapedit/src/tile_widget.cpp b/tools/mapedit/src/tile_widget.cpp index 3b00e59..dc39fdd 100644 --- a/tools/mapedit/src/tile_widget.cpp +++ b/tools/mapedit/src/tile_widget.cpp
@@ -34,7 +34,7 @@ void TileWidget::Init()
34 SetVirtualSize(numTilesWidth*TILE_WIDTH*scale, (numTiles / numTilesWidth + 1) * TILE_HEIGHT*scale); 34 SetVirtualSize(numTilesWidth*TILE_WIDTH*scale, (numTiles / numTilesWidth + 1) * TILE_HEIGHT*scale);
35} 35}
36 36
37void TileWidget::OnPaint(wxPaintEvent& event) 37void TileWidget::OnPaint(wxPaintEvent&)
38{ 38{
39 wxPaintDC dc(this); 39 wxPaintDC dc(this);
40 wxMemoryDC tiles_dc; 40 wxMemoryDC tiles_dc;
@@ -42,6 +42,10 @@ void TileWidget::OnPaint(wxPaintEvent& event)
42 42
43 int vX, vY, vW, vH, aW, aH; 43 int vX, vY, vW, vH, aW, aH;
44 GetViewStart(&vX, &vY); 44 GetViewStart(&vX, &vY);
45 int vXX, vYX;
46 GetScrollPixelsPerUnit(&vXX, &vYX);
47 vX *= vXX;
48 vY *= vYX;
45 GetVirtualSize(&vW, &vH); 49 GetVirtualSize(&vW, &vH);
46 GetSize(&aW, &aH); 50 GetSize(&aW, &aH);
47 51
@@ -63,6 +67,10 @@ void TileWidget::OnClick(wxMouseEvent& event)
63{ 67{
64 int vX, vY, vW, vH; 68 int vX, vY, vW, vH;
65 GetViewStart(&vX, &vY); 69 GetViewStart(&vX, &vY);
70 int vXX, vYX;
71 GetScrollPixelsPerUnit(&vXX, &vYX);
72 vX *= vXX;
73 vY *= vYX;
66 GetVirtualSize(&vW, &vH); 74 GetVirtualSize(&vW, &vH);
67 75
68 wxPoint pos = event.GetPosition(); 76 wxPoint pos = event.GetPosition();
diff --git a/tools/mapedit/src/tile_widget.h b/tools/mapedit/src/tile_widget.h index 2f1872d..ff45a37 100644 --- a/tools/mapedit/src/tile_widget.h +++ b/tools/mapedit/src/tile_widget.h
@@ -28,8 +28,8 @@ class TileWidget : public wxScrolledCanvas {
28 int selected = 0; 28 int selected = 0;
29 int scale; 29 int scale;
30 30
31 DECLARE_DYNAMIC_CLASS(MapeditWidget); 31 DECLARE_DYNAMIC_CLASS(MapeditWidget)
32 DECLARE_EVENT_TABLE(); 32 DECLARE_EVENT_TABLE()
33}; 33};
34 34
35#endif 35#endif
diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index d7f1a51..ba3b6a5 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp
@@ -1,4 +1,5 @@
1#include "widget.h" 1#include "widget.h"
2#include "frame.h"
2 3
3IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxScrolledWindow) 4IMPLEMENT_DYNAMIC_CLASS(MapeditWidget,wxScrolledWindow)
4 5
@@ -36,13 +37,17 @@ wxSize MapeditWidget::DoGetBestSize() const
36 return {GAME_WIDTH*2, GAME_HEIGHT*2}; 37 return {GAME_WIDTH*2, GAME_HEIGHT*2};
37} 38}
38 39
39void MapeditWidget::OnPaint(wxPaintEvent& event) 40void MapeditWidget::OnPaint(wxPaintEvent&)
40{ 41{
41 wxPaintDC dc(this); 42 wxPaintDC dc(this);
42 wxMemoryDC tiles_dc; 43 wxMemoryDC tiles_dc;
43 tiles_dc.SelectObject(tiles); 44 tiles_dc.SelectObject(tiles);
44 int vX, vY; 45 int vX, vY;
45 GetViewStart(&vX, &vY); 46 GetViewStart(&vX, &vY);
47 int vXX, vYX;
48 GetScrollPixelsPerUnit(&vXX, &vYX);
49 vX *= vXX;
50 vY *= vYX;
46 51
47 for (int y=0; y<MAP_HEIGHT; y++) 52 for (int y=0; y<MAP_HEIGHT; y++)
48 { 53 {
@@ -63,20 +68,38 @@ void MapeditWidget::OnPaint(wxPaintEvent& event)
63 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()); 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());
64 } 69 }
65 70
66 if (mouseIsIn) 71 if (editMode == EditTiles)
67 { 72 {
68 int tile = tileWidget->getSelected(); 73 if (mouseIsIn)
69 int x = (mousePos.x + vX) / (TILE_WIDTH * scale); 74 {
70 int y = (mousePos.y + vY) / (TILE_HEIGHT * scale); 75 int tile = tileWidget->getSelected();
76 int x = (mousePos.x + vX) / (TILE_WIDTH * scale);
77 int y = (mousePos.y + vY) / (TILE_HEIGHT * scale);
71 78
72 tiles_dc.SelectObject(wxNullBitmap); 79 tiles_dc.SelectObject(wxNullBitmap);
73 tiles_dc.SelectObject(tiles); 80 tiles_dc.SelectObject(tiles);
74 dc.StretchBlit(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); 81 dc.StretchBlit(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
75 82
76 wxPen pen(*wxGREEN, 2); 83 wxPen pen(*wxGREEN, 2);
77 dc.SetPen(pen); 84 dc.SetPen(pen);
78 dc.SetBrush(*wxTRANSPARENT_BRUSH); 85 dc.SetBrush(*wxTRANSPARENT_BRUSH);
79 dc.DrawRectangle(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale); 86 dc.DrawRectangle(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale);
87 }
88 } else if (editMode == EditEntities)
89 {
90 if ((addingEntity != nullptr) && (mouseIsIn))
91 {
92 wxBitmap sprite = addingEntity->getSprite();
93 tiles_dc.SelectObject(wxNullBitmap);
94 tiles_dc.SelectObject(sprite);
95
96 dc.StretchBlit(mousePos.x - addingEntity->getWidth()/2*scale, mousePos.y - addingEntity->getHeight()/2*scale, addingEntity->getWidth()*scale, addingEntity->getHeight()*scale, &tiles_dc, 0, 0, addingEntity->getWidth(), addingEntity->getHeight());
97
98 wxPen pen(*wxGREEN, 2);
99 dc.SetPen(pen);
100 dc.SetBrush(*wxTRANSPARENT_BRUSH);
101 dc.DrawRectangle(mousePos.x - addingEntity->getWidth()/2*scale, mousePos.y - addingEntity->getHeight()/2*scale, addingEntity->getWidth()*scale, addingEntity->getHeight()*scale);
102 }
80 } 103 }
81} 104}
82 105
@@ -84,6 +107,10 @@ void MapeditWidget::SetTile(wxPoint pos)
84{ 107{
85 int vX, vY; 108 int vX, vY;
86 GetViewStart(&vX, &vY); 109 GetViewStart(&vX, &vY);
110 int vXX, vYX;
111 GetScrollPixelsPerUnit(&vXX, &vYX);
112 vX *= vXX;
113 vY *= vYX;
87 114
88 int x = (pos.x + vX) / (TILE_WIDTH * scale); 115 int x = (pos.x + vX) / (TILE_WIDTH * scale);
89 int y = (pos.y + vY) / (TILE_HEIGHT * scale); 116 int y = (pos.y + vY) / (TILE_HEIGHT * scale);
@@ -96,32 +123,64 @@ void MapeditWidget::OnClick(wxMouseEvent& event)
96{ 123{
97 mouseIsDown = true; 124 mouseIsDown = true;
98 125
99 SetTile(event.GetPosition()); 126 if (editMode == EditTiles)
127 {
128 SetTile(event.GetPosition());
129 } else if (editMode == EditEntities)
130 {
131 if (addingEntity != nullptr)
132 {
133 int vX, vY;
134 GetViewStart(&vX, &vY);
135 int vXX, vYX;
136 GetScrollPixelsPerUnit(&vXX, &vYX);
137 vX *= vXX;
138 vY *= vYX;
139
140 int x = (event.GetPosition().x + vX) / scale - (addingEntity->getWidth() / 2);
141 int y = (event.GetPosition().y + vY) / scale - (addingEntity->getHeight() / 2);
142 printf("%d,%d\n",x,y);
143 MapObjectEntry data;
144 data.object = addingEntity;
145 data.position = std::make_pair(x,y);
146 map->getObjects().push_back(data);
147
148 addingEntity = nullptr;
149
150 frame->FinishAddingEntity();
151
152 Refresh();
153 }
154 }
100 155
101 event.Skip(); 156 event.Skip();
102} 157}
103 158
104void MapeditWidget::OnMouseMove(wxMouseEvent& event) 159void MapeditWidget::OnMouseMove(wxMouseEvent& event)
105{ 160{
106 if (mouseIsDown) 161 mousePos = event.GetPosition();
162 mouseIsIn = true;
163
164 if (editMode == EditTiles)
107 { 165 {
108 SetTile(event.GetPosition()); 166 if (mouseIsDown)
167 {
168 SetTile(event.GetPosition());
169 }
109 } 170 }
110 171
111 mouseIsIn = true;
112 mousePos = event.GetPosition();
113 Refresh(); 172 Refresh();
114} 173}
115 174
116void MapeditWidget::OnMouseUp(wxMouseEvent& event) 175void MapeditWidget::OnMouseUp(wxMouseEvent&)
117{ 176{
118 mouseIsDown = false; 177 mouseIsDown = false;
119} 178}
120 179
121void MapeditWidget::OnMouseOut(wxMouseEvent& event) 180void MapeditWidget::OnMouseOut(wxMouseEvent&)
122{ 181{
123 mouseIsIn = false; 182 mouseIsIn = false;
124 183
125 Refresh(); 184 Refresh();
126} 185}
127 186
@@ -132,7 +191,10 @@ void MapeditWidget::ZoomIn()
132 191
133void MapeditWidget::ZoomOut() 192void MapeditWidget::ZoomOut()
134{ 193{
135 SetZoomSize(scale-1); 194 if (scale > 1)
195 {
196 SetZoomSize(scale-1);
197 }
136} 198}
137 199
138void MapeditWidget::SetZoomSize(int zoom) 200void MapeditWidget::SetZoomSize(int zoom)
@@ -141,5 +203,22 @@ void MapeditWidget::SetZoomSize(int zoom)
141 203
142 SetVirtualSize(MAP_WIDTH*TILE_WIDTH*scale, MAP_HEIGHT*TILE_HEIGHT*scale); 204 SetVirtualSize(MAP_WIDTH*TILE_WIDTH*scale, MAP_HEIGHT*TILE_HEIGHT*scale);
143 205
144 GetParent()->Refresh(); 206 Refresh();
207}
208
209void MapeditWidget::SetEditMode(EditMode editMode)
210{
211 this->editMode = editMode;
212
213 Refresh();
214}
215
216void MapeditWidget::StartAddingEntity(MapObject* object)
217{
218 addingEntity = object;
219}
220
221void MapeditWidget::CancelAddingEntity()
222{
223 addingEntity = nullptr;
145} 224}
diff --git a/tools/mapedit/src/widget.h b/tools/mapedit/src/widget.h index c8bfb57..9de5b02 100644 --- a/tools/mapedit/src/widget.h +++ b/tools/mapedit/src/widget.h
@@ -10,6 +10,13 @@
10#include "map.h" 10#include "map.h"
11#include "tile_widget.h" 11#include "tile_widget.h"
12 12
13class MapeditFrame;
14
15enum EditMode {
16 EditTiles,
17 EditEntities
18};
19
13class MapeditWidget : public wxScrolledWindow { 20class MapeditWidget : public wxScrolledWindow {
14 public: 21 public:
15 MapeditWidget(); 22 MapeditWidget();
@@ -17,6 +24,11 @@ class MapeditWidget : public wxScrolledWindow {
17 24
18 void ZoomIn(); 25 void ZoomIn();
19 void ZoomOut(); 26 void ZoomOut();
27 void SetEditMode(EditMode editMode);
28 void StartAddingEntity(MapObject* object);
29 void CancelAddingEntity();
30
31 MapeditFrame* frame;
20 32
21 protected: 33 protected:
22 void Init(); 34 void Init();
@@ -38,9 +50,12 @@ class MapeditWidget : public wxScrolledWindow {
38 int scale; 50 int scale;
39 wxPoint mousePos; 51 wxPoint mousePos;
40 bool mouseIsIn = false; 52 bool mouseIsIn = false;
53 EditMode editMode = EditTiles;
54
55 MapObject* addingEntity = nullptr;
41 56
42 DECLARE_DYNAMIC_CLASS(MapeditWidget); 57 DECLARE_DYNAMIC_CLASS(MapeditWidget)
43 DECLARE_EVENT_TABLE(); 58 DECLARE_EVENT_TABLE()
44}; 59};
45 60
46#endif 61#endif