GUI: finish the modified thing

This commit is contained in:
tildearrow 2021-12-30 18:52:36 -05:00
parent 9a1853249b
commit 0f7ea7c269
2 changed files with 75 additions and 9 deletions

View file

@ -2499,6 +2499,12 @@ int FurnaceGUI::load(String path) {
return 0; return 0;
} }
void FurnaceGUI::showWarning(String what, FurnaceGUIWarnings type) {
warnString=what;
warnAction=type;
warnQuit=true;
}
void FurnaceGUI::showError(String what) { void FurnaceGUI::showError(String what) {
errorString=what; errorString=what;
ImGui::OpenPopup("Error"); ImGui::OpenPopup("Error");
@ -2598,8 +2604,12 @@ bool FurnaceGUI::loop() {
} }
break; break;
case SDL_QUIT: case SDL_QUIT:
if (modified) {
showWarning("Unsaved changes! Are you sure you want to quit?",GUI_WARN_QUIT);
} else {
quit=true; quit=true;
return true; return true;
}
break; break;
} }
} }
@ -2613,13 +2623,23 @@ bool FurnaceGUI::loop() {
ImGui::BeginMainMenuBar(); ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("file")) { if (ImGui::BeginMenu("file")) {
if (ImGui::MenuItem("new")) { if (ImGui::MenuItem("new")) {
if (modified) {
showWarning("Unsaved changes! Are you sure?",GUI_WARN_NEW);
} else {
e->createNew(); e->createNew();
undoHist.clear(); undoHist.clear();
redoHist.clear(); redoHist.clear();
curFileName="";
modified=false;
}
} }
if (ImGui::MenuItem("open...")) { if (ImGui::MenuItem("open...")) {
if (modified) {
showWarning("Unsaved changes! Are you sure?",GUI_WARN_OPEN);
} else {
openFileDialog(GUI_FILE_OPEN); openFileDialog(GUI_FILE_OPEN);
} }
}
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("save")) { if (ImGui::MenuItem("save")) {
if (curFileName=="") { if (curFileName=="") {
@ -2650,8 +2670,12 @@ bool FurnaceGUI::loop() {
} }
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("exit")) { if (ImGui::MenuItem("exit")) {
if (modified) {
showWarning("Unsaved changes! Are you sure you want to quit?",GUI_WARN_QUIT);
} else {
quit=true; quit=true;
} }
}
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("edit")) { if (ImGui::BeginMenu("edit")) {
@ -2773,6 +2797,11 @@ bool FurnaceGUI::loop() {
ImGuiFileDialog::Instance()->Close(); ImGuiFileDialog::Instance()->Close();
} }
if (warnQuit) {
warnQuit=false;
ImGui::OpenPopup("Warning");
}
if (aboutOpen) drawAbout(); if (aboutOpen) drawAbout();
if (ImGui::BeginPopupModal("Error",NULL,ImGuiWindowFlags_AlwaysAutoResize)) { if (ImGui::BeginPopupModal("Error",NULL,ImGuiWindowFlags_AlwaysAutoResize)) {
@ -2783,6 +2812,33 @@ bool FurnaceGUI::loop() {
ImGui::EndPopup(); ImGui::EndPopup();
} }
if (ImGui::BeginPopupModal("Warning",NULL,ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::Text("%s",warnString.c_str());
if (ImGui::Button("Yes")) {
ImGui::CloseCurrentPopup();
switch (warnAction) {
case GUI_WARN_QUIT:
quit=true;
break;
case GUI_WARN_NEW:
e->createNew();
undoHist.clear();
redoHist.clear();
curFileName="";
modified=false;
break;
case GUI_WARN_OPEN:
openFileDialog(GUI_FILE_OPEN);
break;
}
}
ImGui::SameLine();
if (ImGui::Button("No")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
SDL_SetRenderDrawColor(sdlRend,uiColors[GUI_COLOR_BACKGROUND].x*255, SDL_SetRenderDrawColor(sdlRend,uiColors[GUI_COLOR_BACKGROUND].x*255,
uiColors[GUI_COLOR_BACKGROUND].y*255, uiColors[GUI_COLOR_BACKGROUND].y*255,
uiColors[GUI_COLOR_BACKGROUND].z*255, uiColors[GUI_COLOR_BACKGROUND].z*255,
@ -2904,10 +2960,12 @@ bool FurnaceGUI::finish() {
FurnaceGUI::FurnaceGUI(): FurnaceGUI::FurnaceGUI():
e(NULL), e(NULL),
quit(false), quit(false),
warnQuit(false),
willCommit(false), willCommit(false),
edit(false), edit(false),
modified(false), modified(false),
curFileDialog(GUI_FILE_OPEN), curFileDialog(GUI_FILE_OPEN),
warnAction(GUI_WARN_OPEN),
scrW(1280), scrW(1280),
scrH(800), scrH(800),
dpiScale(1), dpiScale(1),

View file

@ -73,6 +73,12 @@ enum FurnaceGUIFileDialogs {
GUI_FILE_SAMPLE_SAVE GUI_FILE_SAMPLE_SAVE
}; };
enum FurnaceGUIWarnings {
GUI_WARN_QUIT,
GUI_WARN_NEW,
GUI_WARN_OPEN
};
struct SelectionPoint { struct SelectionPoint {
int xCoarse, xFine; int xCoarse, xFine;
int y; int y;
@ -131,11 +137,12 @@ class FurnaceGUI {
SDL_Window* sdlWin; SDL_Window* sdlWin;
SDL_Renderer* sdlRend; SDL_Renderer* sdlRend;
String workingDir, fileName, clipboard, errorString, lastError, curFileName; String workingDir, fileName, clipboard, warnString, errorString, lastError, curFileName;
bool quit, willCommit, edit, modified; bool quit, warnQuit, willCommit, edit, modified;
FurnaceGUIFileDialogs curFileDialog; FurnaceGUIFileDialogs curFileDialog;
FurnaceGUIWarnings warnAction;
int scrW, scrH; int scrW, scrH;
@ -246,6 +253,7 @@ class FurnaceGUI {
int save(String path); int save(String path);
int load(String path); int load(String path);
void showWarning(String what, FurnaceGUIWarnings type);
void showError(String what); void showError(String what);
public: public: