-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4446_Unite_dialogs_Close_New_Sign_Out
- Loading branch information
1 parent
50b6108
commit ba978ec
Showing
6 changed files
with
189 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include "MRUISaveChangesPopup.h" | ||
|
||
#include "MRRibbonConstants.h" | ||
#include "MRUIStyle.h" | ||
#include "ImGuiHelpers.h" | ||
#include "MRRibbonFontManager.h" | ||
#include "MRFileDialog.h" | ||
#include "MRProgressBar.h" | ||
#include "MRShowModal.h" | ||
#include "MRViewer.h" | ||
#include "ImGuiMenu.h" | ||
#include "MRSceneCache.h" | ||
#include "MRMesh/MRSceneRoot.h" | ||
#include "MRMesh/MRIOFormatsRegistry.h" | ||
#include "MRMesh/MRObjectSave.h" | ||
#include "MRMesh/MRVisualObject.h" | ||
|
||
namespace MR | ||
{ | ||
|
||
namespace UI | ||
{ | ||
|
||
void saveChangesPopup( const char* str_id, const SaveChangesPopupSettings& settings ) | ||
{ | ||
const ImVec2 windowSize{ cModalWindowWidth * settings.scaling, -1 }; | ||
ImGui::SetNextWindowSize( windowSize, ImGuiCond_Always ); | ||
|
||
ImGui::PushStyleVar( ImGuiStyleVar_ItemSpacing, { 2.0f * cDefaultItemSpacing * settings.scaling, 3.0f * cDefaultItemSpacing * settings.scaling } ); | ||
ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, { cModalWindowPaddingX * settings.scaling, cModalWindowPaddingY * settings.scaling } ); | ||
if ( ImGui::BeginModalNoAnimation( str_id, nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar ) ) | ||
{ | ||
auto headerFont = RibbonFontManager::getFontByTypeStatic( RibbonFontManager::FontType::Headline ); | ||
if ( headerFont ) | ||
ImGui::PushFont( headerFont ); | ||
|
||
const auto headerWidth = ImGui::CalcTextSize( settings.header.c_str() ).x; | ||
ImGui::SetCursorPosX( ( windowSize.x - headerWidth ) * 0.5f ); | ||
ImGui::Text( "%s", settings.header.c_str() ); | ||
|
||
if ( headerFont ) | ||
ImGui::PopFont(); | ||
|
||
// do not suggest saving empty scene | ||
const bool showSave = !SceneCache::getAllObjects<VisualObject, ObjectSelectivityType::Selectable>().empty(); | ||
if ( showSave ) | ||
{ | ||
const char* text = "Save your changes?"; | ||
ImGui::SetCursorPosX( ( windowSize.x - ImGui::CalcTextSize( text ).x ) * 0.5f ); | ||
ImGui::Text( "%s", text ); | ||
} | ||
|
||
const auto style = ImGui::GetStyle(); | ||
ImGui::PushStyleVar( ImGuiStyleVar_FramePadding, { style.FramePadding.x, cButtonPadding * settings.scaling } ); | ||
|
||
const float p = ImGui::GetStyle().ItemSpacing.x; | ||
const Vector2f btnSize{ showSave ? ( ImGui::GetContentRegionAvail().x - p * 2 ) / 3.f : ( ImGui::GetContentRegionAvail().x - p ) / 2.f, 0 }; | ||
|
||
if ( showSave ) | ||
{ | ||
if ( UI::button( "Save", btnSize, ImGuiKey_Enter ) ) | ||
{ | ||
auto savePath = SceneRoot::getScenePath(); | ||
if ( savePath.empty() ) | ||
savePath = saveFileDialog( { .filters = SceneSave::getFilters() } ); | ||
|
||
ImGui::CloseCurrentPopup(); | ||
if ( !savePath.empty() ) | ||
ProgressBar::orderWithMainThreadPostProcessing( "Saving scene", [customFunction = settings.onOk, savePath, &root = SceneRoot::get()] ()->std::function<void()> | ||
{ | ||
auto res = ObjectSave::toAnySupportedSceneFormat( root, savePath, ProgressBar::callBackSetProgress ); | ||
|
||
return[customFunction = customFunction, savePath, res] () | ||
{ | ||
if ( res ) | ||
{ | ||
getViewerInstance().onSceneSaved( savePath ); | ||
if ( customFunction ) | ||
customFunction(); | ||
} | ||
else | ||
showError( "Error saving scene: " + res.error() ); | ||
}; | ||
} ); | ||
} | ||
if( !settings.saveTooltip.empty() ) | ||
UI::setTooltipIfHovered( settings.saveTooltip.c_str(), settings.scaling ); | ||
ImGui::SameLine(); | ||
} | ||
|
||
if ( UI::buttonCommonSize( showSave ? settings.dontSaveText.c_str() : settings.shortCloseText.c_str(), btnSize, ImGuiKey_N) ) | ||
{ | ||
ImGui::CloseCurrentPopup(); | ||
if ( settings.onOk ) | ||
settings.onOk(); | ||
} | ||
if ( !settings.dontSaveTooltip.empty() ) | ||
UI::setTooltipIfHovered( settings.dontSaveTooltip.c_str(), settings.scaling ); | ||
ImGui::SameLine(); | ||
if ( UI::buttonCommonSize( "Cancel", btnSize, ImGuiKey_Escape ) ) | ||
ImGui::CloseCurrentPopup(); | ||
|
||
if ( !settings.cancelTooltip.empty() ) | ||
UI::setTooltipIfHovered( settings.cancelTooltip.c_str(), settings.scaling); | ||
|
||
if ( ImGui::IsMouseClicked( 0 ) && !( ImGui::IsAnyItemHovered() || ImGui::IsWindowHovered( ImGuiHoveredFlags_AnyWindow ) ) ) | ||
ImGui::CloseCurrentPopup(); | ||
ImGui::PopStyleVar(); | ||
ImGui::EndPopup(); | ||
} | ||
ImGui::PopStyleVar( 2 ); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <functional> | ||
|
||
#include "exports.h" | ||
|
||
namespace MR | ||
{ | ||
|
||
namespace UI | ||
{ | ||
|
||
struct SaveChangesPopupSettings | ||
{ | ||
// menu scaling | ||
float scaling = 1.0f; | ||
// text that is shown if we have nothing to save | ||
std::string shortCloseText = "Close"; | ||
// text that is shown if we have changes but don't want to save them | ||
std::string dontSaveText = "Don't Save"; | ||
|
||
std::string saveTooltip = "Save current scene"; | ||
std::string dontSaveTooltip = "Donh't save current scene"; | ||
std::string cancelTooltip = "Cansel"; | ||
// header that is used in dialog | ||
std::string header; | ||
// if not empty this function is called on "save" and "not save" options( if succeed ) | ||
std::function<void()> onOk = {}; | ||
}; | ||
// Shows ImGui popup that suggests user to save changes, | ||
// user need to call ImGui::OpenPopup( str_id ) to open this popup. | ||
// It has 3 options: save, don't save, cancel | ||
// str_id - ImGui string id for the popup window | ||
// settings - settings for dialog | ||
MRVIEWER_API void saveChangesPopup( const char* str_id, const SaveChangesPopupSettings& settings = {} ); | ||
} | ||
|
||
} |
Oops, something went wrong.