From 91640f2f35d703898edb14abaae7dd63f5346027 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 15 Mar 2015 17:20:42 -0400 Subject: Added file management to map editor (only edits environment at current time) --- tools/mapedit/src/frame.cpp | 93 +++++++++++++++++++++++++++++++++++++++++--- tools/mapedit/src/frame.h | 16 +++++--- tools/mapedit/src/map.cpp | 84 ++++++++++++++++++++++++++++++++++++++- tools/mapedit/src/map.h | 23 ++++++++++- tools/mapedit/src/widget.cpp | 4 +- 5 files changed, 204 insertions(+), 16 deletions(-) (limited to 'tools/mapedit') diff --git a/tools/mapedit/src/frame.cpp b/tools/mapedit/src/frame.cpp index 537cd16..3cd1c15 100644 --- a/tools/mapedit/src/frame.cpp +++ b/tools/mapedit/src/frame.cpp @@ -6,18 +6,31 @@ enum { MENU_VIEW_ZOOM_IN, - MENU_VIEW_ZOOM_OUT + MENU_VIEW_ZOOM_OUT, + MENU_FILE_NEW, + MENU_FILE_OPEN, + MENU_FILE_SAVE, + MENU_FILE_CLOSE }; wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) - EVT_MENU(wxID_EXIT, MapeditFrame::OnExit) + EVT_MENU(wxID_EXIT, MapeditFrame::OnQuit) EVT_MENU(MENU_VIEW_ZOOM_IN, MapeditFrame::ZoomIn) EVT_MENU(MENU_VIEW_ZOOM_OUT, MapeditFrame::ZoomOut) + EVT_MENU(MENU_FILE_NEW, MapeditFrame::OnNew) + EVT_MENU(MENU_FILE_OPEN, MapeditFrame::OnOpen) + EVT_MENU(MENU_FILE_SAVE, MapeditFrame::OnSave) + EVT_MENU(MENU_FILE_CLOSE, MapeditFrame::OnClose) + EVT_CLOSE(MapeditFrame::OnExit) wxEND_EVENT_TABLE() -MapeditFrame::MapeditFrame(Map map) : wxFrame(NULL, wxID_ANY, "Map Editor", wxPoint(50, 50), wxSize(GAME_WIDTH*3, GAME_HEIGHT*2)), map(map) +MapeditFrame::MapeditFrame(Map map, std::string filename) : wxFrame(NULL, wxID_ANY, "Map Editor", wxDefaultPosition, wxSize(GAME_WIDTH*3, GAME_HEIGHT*2)), map(map), filename(filename) { wxMenu* menuFile = new wxMenu; + menuFile->Append(MENU_FILE_NEW, "New\tCtrl-N"); + menuFile->Append(MENU_FILE_OPEN, "Open\tCtrl-O"); + menuFile->Append(MENU_FILE_SAVE, "Save\tCtrl-S"); + menuFile->Append(MENU_FILE_CLOSE, "Close\tCtrl-W"); menuFile->Append(wxID_EXIT); wxMenu* menuView = new wxMenu; @@ -54,9 +67,34 @@ MapeditFrame::MapeditFrame(Map map) : wxFrame(NULL, wxID_ANY, "Map Editor", wxPo sizermain->SetSizeHints(this); } -void MapeditFrame::OnExit(wxCommandEvent& event) +void MapeditFrame::OnExit(wxCloseEvent& event) { - Close(true); + if (event.CanVeto() && map.hasUnsavedChanges()) + { + switch (wxMessageBox("Current map has unsaved changes. Save before closing?", "Please confirm", wxICON_QUESTION|wxYES_NO|wxCANCEL, this)) + { + case wxYES: + if (filename == "") + { + wxFileDialog saveFileDialog(this, "Save map", "", "", "XML files (*.xml)|*.xml", wxFD_SAVE); + if (saveFileDialog.ShowModal() == wxID_CANCEL) + { + return; + } + + filename = saveFileDialog.GetPath().ToStdString(); + } + + map.save(filename); + break; + + case wxCANCEL: + event.Veto(true); + return; + } + } + + event.Skip(); } MapeditWidget* MapeditFrame::GetMapEditor() @@ -73,3 +111,48 @@ void MapeditFrame::ZoomOut(wxCommandEvent& event) { mapEditor->ZoomOut(); } + +void MapeditFrame::OnNew(wxCommandEvent& event) +{ + MapeditFrame* frame = new MapeditFrame(); + frame->Show(true); +} + +void MapeditFrame::OnOpen(wxCommandEvent& event) +{ + wxFileDialog openFileDialog(this, "Open map", "", "", "XML files (*.xml)|*.xml", wxFD_OPEN|wxFD_FILE_MUST_EXIST); + if (openFileDialog.ShowModal() == wxID_CANCEL) + { + return; + } + + std::string filename = openFileDialog.GetPath().ToStdString(); + MapeditFrame* frame = new MapeditFrame(Map(filename), filename); + frame->Show(true); +} + +void MapeditFrame::OnSave(wxCommandEvent& event) +{ + if (filename == "") + { + wxFileDialog saveFileDialog(this, "Save map", "", "", "XML files (*.xml)|*.xml", wxFD_SAVE); + if (saveFileDialog.ShowModal() == wxID_CANCEL) + { + return; + } + + filename = saveFileDialog.GetPath().ToStdString(); + } + + map.save(filename); +} + +void MapeditFrame::OnClose(wxCommandEvent& event) +{ + Close(false); +} + +void MapeditFrame::OnQuit(wxCommandEvent& event) +{ + // TODO +} diff --git a/tools/mapedit/src/frame.h b/tools/mapedit/src/frame.h index 4d6c3dc..53d8998 100644 --- a/tools/mapedit/src/frame.h +++ b/tools/mapedit/src/frame.h @@ -13,21 +13,25 @@ class MapeditFrame : public wxFrame { public: - MapeditFrame() : MapeditFrame(Map()) {} - MapeditFrame(Map map); + MapeditFrame() : MapeditFrame(Map(), "") {} + MapeditFrame(Map map, std::string filename); MapeditWidget* GetMapEditor(); - protected: + private: void ZoomIn(wxCommandEvent& event); void ZoomOut(wxCommandEvent& event); - - private: - void OnExit(wxCommandEvent& event); + void OnNew(wxCommandEvent& event); + void OnOpen(wxCommandEvent& event); + void OnSave(wxCommandEvent& event); + void OnClose(wxCommandEvent& event); + void OnExit(wxCloseEvent& event); + void OnQuit(wxCommandEvent& event); Map map; MapeditWidget* mapEditor; TileWidget* tileEditor; + std::string filename; wxDECLARE_EVENT_TABLE(); }; diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index 52a2096..7976419 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp @@ -1,12 +1,15 @@ #include "map.h" #include +#include +#include Map::Map() { mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); + dirty = true; } -Map::Map(const std::string filename) +Map::Map(std::string filename) { xmlDocPtr doc = xmlParseFile(filename.c_str()); if (doc == nullptr) @@ -56,6 +59,8 @@ Map::Map(const std::string filename) } xmlFreeDoc(doc); + + dirty = false; } Map::Map(const Map& map) @@ -66,6 +71,7 @@ Map::Map(const Map& map) title = map.title; leftmap = map.leftmap; rightmap = map.rightmap; + dirty = map.dirty; } Map::Map(Map&& map) : Map() @@ -91,4 +97,78 @@ void swap(Map& first, Map& second) std::swap(first.title, second.title); std::swap(first.leftmap, second.leftmap); std::swap(first.rightmap, second.rightmap); -} \ No newline at end of file + std::swap(first.dirty, second.dirty); +} + +#define MY_ENCODING "ISO-8859-1" + +void Map::save(std::string name) +{ + if (!dirty) return; + + int rc; + + xmlTextWriterPtr writer = xmlNewTextWriterFilename(name.c_str(), 0); + if (writer == NULL) throw MapWriteException(name); + + rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterStartElement(writer, (xmlChar*) "map-def"); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterWriteElement(writer, (xmlChar*) "name", (xmlChar*) title.c_str()); + if (rc < 0) throw MapWriteException(name); + + std::ostringstream mapdata_out; + for (int y=0; ymapdata[x+y*MAP_WIDTH]; + int tile = map->getTileAt(x, y); 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); } } @@ -76,7 +76,7 @@ void MapeditWidget::SetTile(wxPoint pos) int x = (pos.x + vX) / (TILE_WIDTH * scale); int y = (pos.y + vY) / (TILE_HEIGHT * scale); - map->mapdata[x+y*MAP_WIDTH] = tileWidget->getSelected(); + map->setTileAt(x, y, tileWidget->getSelected()); Refresh(); } -- cgit 1.4.1