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 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 5 deletions(-) (limited to 'tools/mapedit/src/frame.cpp') 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 +} -- cgit 1.4.1