Add generic confirmation popup to editor

Add `CEditor::ShowPopupConfirm` to show a generic confirmation popup.

This popups shows a message and buttons to confirm or cancel an operation. The result will be available to the caller in the given `SConfirmPopupContext`.
This commit is contained in:
Robert Müller 2022-11-26 13:57:44 +01:00
parent 3ba51c476f
commit f000fcea29
2 changed files with 70 additions and 0 deletions

View file

@ -1137,6 +1137,27 @@ public:
static int PopupMessage(CEditor *pEditor, CUIRect View, void *pContext); static int PopupMessage(CEditor *pEditor, CUIRect View, void *pContext);
void ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext); void ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext);
struct SConfirmPopupContext
{
enum EConfirmationResult
{
UNSET = 0,
CONFIRMED,
CANCELED
};
static constexpr float POPUP_MAX_WIDTH = 200.0f;
static constexpr float POPUP_FONT_SIZE = 10.0f;
static constexpr float POPUP_BUTTON_HEIGHT = 12.0f;
static constexpr float POPUP_BUTTON_SPACING = 5.0f;
char m_aMessage[256];
EConfirmationResult m_Result;
SConfirmPopupContext();
void Reset();
};
static int PopupConfirm(CEditor *pEditor, CUIRect View, void *pContext);
void ShowPopupConfirm(float X, float Y, SConfirmPopupContext *pContext);
struct SSelectionPopupContext struct SSelectionPopupContext
{ {
static constexpr float POPUP_MAX_WIDTH = 300.0f; static constexpr float POPUP_MAX_WIDTH = 300.0f;

View file

@ -1895,6 +1895,55 @@ int CEditor::PopupMessage(CEditor *pEditor, CUIRect View, void *pContext)
return 0; return 0;
} }
CEditor::SConfirmPopupContext::SConfirmPopupContext()
{
Reset();
}
void CEditor::SConfirmPopupContext::Reset()
{
m_Result = SConfirmPopupContext::UNSET;
}
void CEditor::ShowPopupConfirm(float X, float Y, SConfirmPopupContext *pContext)
{
const float TextWidth = minimum(TextRender()->TextWidth(nullptr, SConfirmPopupContext::POPUP_FONT_SIZE, pContext->m_aMessage, -1, -1.0f), SConfirmPopupContext::POPUP_MAX_WIDTH);
const int LineCount = TextRender()->TextLineCount(nullptr, SConfirmPopupContext::POPUP_FONT_SIZE, pContext->m_aMessage, TextWidth);
const float PopupHeight = LineCount * SConfirmPopupContext::POPUP_FONT_SIZE + SConfirmPopupContext::POPUP_BUTTON_HEIGHT + SConfirmPopupContext::POPUP_BUTTON_SPACING + 10.0f;
pContext->m_Result = SConfirmPopupContext::UNSET;
UiInvokePopupMenu(pContext, 0, X, Y, TextWidth + 10.0f, PopupHeight, PopupConfirm, pContext);
}
int CEditor::PopupConfirm(CEditor *pEditor, CUIRect View, void *pContext)
{
SConfirmPopupContext *pConfirmPopup = static_cast<SConfirmPopupContext *>(pContext);
CUIRect Label, ButtonBar, CancelButton, ConfirmButton;
View.HSplitBottom(SConfirmPopupContext::POPUP_BUTTON_HEIGHT, &Label, &ButtonBar);
ButtonBar.VSplitMid(&CancelButton, &ConfirmButton, SConfirmPopupContext::POPUP_BUTTON_SPACING);
CTextCursor Cursor;
pEditor->TextRender()->SetCursor(&Cursor, Label.x, Label.y, SConfirmPopupContext::POPUP_FONT_SIZE, TEXTFLAG_RENDER);
Cursor.m_LineWidth = Label.w;
pEditor->TextRender()->TextEx(&Cursor, pConfirmPopup->m_aMessage, -1);
static int s_CancelButton = 0;
if(pEditor->DoButton_Editor(&s_CancelButton, "Cancel", 0, &CancelButton, 0, nullptr))
{
pConfirmPopup->m_Result = SConfirmPopupContext::CANCELED;
return 1;
}
static int s_ConfirmButton = 0;
if(pEditor->DoButton_Editor(&s_ConfirmButton, "Confirm", 0, &ConfirmButton, 0, nullptr))
{
pConfirmPopup->m_Result = SConfirmPopupContext::CONFIRMED;
return 1;
}
return 0;
}
void CEditor::ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext) void CEditor::ShowPopupMessage(float X, float Y, SMessagePopupContext *pContext)
{ {
const float TextWidth = minimum(TextRender()->TextWidth(nullptr, SMessagePopupContext::POPUP_FONT_SIZE, pContext->m_aMessage, -1, -1.0f), SMessagePopupContext::POPUP_MAX_WIDTH); const float TextWidth = minimum(TextRender()->TextWidth(nullptr, SMessagePopupContext::POPUP_FONT_SIZE, pContext->m_aMessage, -1, -1.0f), SMessagePopupContext::POPUP_MAX_WIDTH);