aboutsummaryrefslogtreecommitdiff
path: root/core/src/UI
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/UI')
-rw-r--r--core/src/UI/UI_Templates.cpp194
1 files changed, 149 insertions, 45 deletions
diff --git a/core/src/UI/UI_Templates.cpp b/core/src/UI/UI_Templates.cpp
index b612662..b4e3aa5 100644
--- a/core/src/UI/UI_Templates.cpp
+++ b/core/src/UI/UI_Templates.cpp
@@ -16,6 +16,7 @@
#include <fstream>
#include <iostream>
#include <utility>
+#include <variant>
namespace {
class TemplateUI
@@ -59,14 +60,18 @@ private:
/* Selection states */
union
{
+ // Initialize to this element
+ std::monostate mIdleState{};
+
/// "CS" stands for "Constant cell selection States"
struct
{
- } mCS{}; // Initialize to this element
+ } mCS;
/// "SS" stands for "Singular parameter selection States".
struct
{
+ std::string EditBuffer;
bool ErrorDuplicateVarName;
bool HasLeftAG;
bool HasRightAG;
@@ -75,10 +80,20 @@ private:
/// "AS" stands for "Array group parameter selection States".
struct
{
+ std::string EditBuffer;
bool ErrorDuplicateVarName;
} mAS;
+
+ // "RS" stands for "Range selection States".
+ struct
+ {
+ } mRS;
};
+ /* Table resizer dialog states */
+ int mNewTableWidth;
+ int mNewTableHeight;
+
/* Table states */
bool mDirty = false;
bool mFirstDraw = true;
@@ -93,6 +108,36 @@ public:
Resize(6, 5);
}
+ virtual ~TableTemplateUI() override
+ {
+ // We can't move this to be a destructor of the union
+ // because that way it would run after the destruction of mTable
+ if (!IsSelected()) {
+ // Case: mIdleState
+ // Noop
+ } else if (mSelectionTL == mSelectionBR) {
+ switch (mTable->GetCell(mSelectionTL).Type) {
+ case TableCell::ConstantCell:
+ // Case: mCS
+ // Noop
+ break;
+
+ case TableCell::SingularParametricCell:
+ // Case: mSS
+ mSS.EditBuffer.std::string::~string();
+ break;
+
+ case TableCell::ArrayParametricCell:
+ // Case: mAS
+ mAS.EditBuffer.std::string::~string();
+ break;
+ }
+ } else {
+ // Case: mRS
+ // Noop
+ }
+ }
+
virtual void Display() override
{
ImGui::Columns(2);
@@ -157,16 +202,24 @@ private:
ImGui::EndTabItem();
}
+ auto OpenPopup = [](const char* name) {
+ // Act as if ImGui::OpenPopup is executed in the previous id stack frame (tab bar level)
+ // Note: we can't simply use ImGui::GetItemID() here, because that would return the id of the ImGui::Button
+ auto tabBar = ImGui::GetCurrentContext()->CurrentTabBar;
+ auto id = tabBar->Tabs[tabBar->LastTabItemIdx].ID;
+ ImGui::PopID();
+ ImGui::OpenPopup(name);
+ ImGui::PushOverrideID(id);
+ };
if (ImGui::BeginTabItem(I18N_TEXT("Table", L10N_TABLE))) {
- // Table properties
if (ImGui::Button(I18N_TEXT("Configure table properties...", L10N_TABLE_CONFIGURE_PROPERTIES))) {
- // Act as if ImGui::OpenPopup is executed in the previous id stack frame (tab bar level)
- // Note: we can't simply use ImGui::GetItemID() here, because that would return the id of the ImGui::Button
- auto tabBar = ImGui::GetCurrentContext()->CurrentTabBar;
- auto id = tabBar->Tabs[tabBar->LastTabItemIdx].ID;
- ImGui::PopID();
- ImGui::OpenPopup(I18N_TEXT("Table properties", L10N_TABLE_PROPERTIES));
- ImGui::PushOverrideID(id);
+ OpenPopup(I18N_TEXT("Table properties", L10N_TABLE_PROPERTIES));
+ }
+
+ if (ImGui::Button(I18N_TEXT("Resize table...", L10N_TABLE_DO_RESIZE))) {
+ mNewTableWidth = mTable->GetTableWidth();
+ mNewTableHeight = mTable->GetTableHeight();
+ OpenPopup(I18N_TEXT("Resize table", L10N_TABLE_RESIZE));
}
// Table contents
@@ -178,6 +231,10 @@ private:
DisplayTableProperties();
ImGui::EndPopup();
}
+ if (ImGui::BeginPopupModal(I18N_TEXT("Resize table", L10N_TABLE_RESIZE), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
+ DisplayTableResizer();
+ ImGui::EndPopup();
+ }
ImGui::EndTabBar();
}
@@ -236,8 +293,7 @@ private:
ExcelRow(pos.y + 1, std::begin(rowStr), std::end(rowStr));
ImGui::Text(I18N_TEXT("Location: %s%s", L10N_TABLE_CELL_POS), colBegin, rowStr);
- switch (cell.Type)
- {
+ switch (cell.Type) {
case TableCell::ConstantCell:
ImGui::Text(I18N_TEXT("Type: Constant", L10N_TABLE_CELL_TYPE_CONST));
break;
@@ -253,31 +309,36 @@ private:
ImGui::OpenPopup("ConvertCtxMenu");
}
if (ImGui::BeginPopup("ConvertCtxMenu")) {
- bool constantDisabled = cell.Type == TableCell::ConstantCell;
- if (ImGui::MenuItem(I18N_TEXT("Convert to regular cell", L10N_TABLE_CELL_CONV_CONST), nullptr, false, !constantDisabled)) {
+ bool constantEnabled = cell.Type != TableCell::ConstantCell;
+ if (ImGui::MenuItem(I18N_TEXT("Convert to regular cell", L10N_TABLE_CELL_CONV_CONST), nullptr, false, constantEnabled)) {
mTable->SetCellType(pos, TableCell::ConstantCell);
+ ResetCS();
}
- bool singleDisabled = cell.Type == TableCell::SingularParametricCell;
- if (ImGui::MenuItem(I18N_TEXT("Convert to parameter cell", L10N_TABLE_CELL_CONV_PARAM), nullptr, false, !singleDisabled)) {
+ bool singleEnabled = cell.Type != TableCell::SingularParametricCell;
+ if (ImGui::MenuItem(I18N_TEXT("Convert to parameter cell", L10N_TABLE_CELL_CONV_PARAM), nullptr, false, singleEnabled)) {
mTable->SetCellType(pos, TableCell::SingularParametricCell);
+ ResetSS(pos);
}
- bool arrayDisabled = cell.Type == TableCell::ArrayParametricCell;
- if (ImGui::MenuItem(I18N_TEXT("Add to a new array group", L10N_TABLE_CELL_CONV_CREATE_AG), nullptr, false, !arrayDisabled)) {
- mTable->AddArrayGroup(pos.x, pos.x, pos.y);
+ bool arrayEnabled = cell.Type != TableCell::ArrayParametricCell;
+ if (ImGui::MenuItem(I18N_TEXT("Add to a new array group", L10N_TABLE_CELL_CONV_CREATE_AG), nullptr, false, arrayEnabled)) {
+ mTable->AddArrayGroup(pos.x, pos.x, pos.y); // Use automatically generated name
+ ResetAS(pos);
}
- bool leftDisabled = !mSS.HasLeftAG || arrayDisabled;
- if (ImGui::MenuItem(I18N_TEXT("Add to the array group to the left", L10N_TABLE_CELL_CONV_ADD_AG_LEFT), nullptr, false, !leftDisabled)) {
- auto& leftCell = mTable->GetCell((Vec2i){ pos.x - 1, pos.y });
+ bool leftEnabled = mSS.HasLeftAG && arrayEnabled;
+ if (ImGui::MenuItem(I18N_TEXT("Add to the array group to the left", L10N_TABLE_CELL_CONV_ADD_AG_LEFT), nullptr, false, leftEnabled)) {
+ auto& leftCell = mTable->GetCell({ pos.x - 1, pos.y });
mTable->ExtendArrayGroupRight(leftCell.DataId, 1);
+ ResetAS(pos);
}
- bool rightDisabled = !mSS.HasRightAG || arrayDisabled;
- if (ImGui::MenuItem(I18N_TEXT("Add to the array group to the right", L10N_TABLE_CELL_CONV_ADD_AG_RIGHT), nullptr, false, !rightDisabled)) {
- auto& rightCell = mTable->GetCell((Vec2i){ pos.x + 1, pos.y });
+ bool rightEnabled = mSS.HasRightAG && arrayEnabled;
+ if (ImGui::MenuItem(I18N_TEXT("Add to the array group to the right", L10N_TABLE_CELL_CONV_ADD_AG_RIGHT), nullptr, false, rightEnabled)) {
+ auto& rightCell = mTable->GetCell({ pos.x + 1, pos.y });
mTable->ExtendArrayGroupLeft(rightCell.DataId, 1);
+ ResetAS(pos);
}
ImGui::EndPopup();
@@ -339,9 +400,16 @@ private:
break;
case TableCell::SingularParametricCell:
- if (ImGui::InputText(I18N_TEXT("Variable name", L10N_TABLE_CELL_VAR_NAME), &cell.Content)) {
- auto c = mTable->FindCell(cell.Content);
- mSS.ErrorDuplicateVarName = c != nullptr;
+ if (ImGui::InputText(I18N_TEXT("Variable name", L10N_TABLE_CELL_VAR_NAME), &mSS.EditBuffer)) {
+ // Sync name change to table
+ bool success = mTable->UpdateParameterName(cell.Content, mSS.EditBuffer);
+ if (success) {
+ // Flush name to display content
+ cell.Content = mSS.EditBuffer;
+ mSS.ErrorDuplicateVarName = false;
+ } else {
+ mSS.ErrorDuplicateVarName = true;
+ }
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(I18N_TEXT("Name of the parameter link to this cell.", L10N_TABLE_CELL_VAR_TOOLTIP));
@@ -353,10 +421,15 @@ private:
break;
case TableCell::ArrayParametricCell:
- if (ImGui::InputText(I18N_TEXT("Variable name", L10N_TABLE_CELL_VAR_NAME), &cell.Content)) {
+ if (ImGui::InputText(I18N_TEXT("Variable name", L10N_TABLE_CELL_VAR_NAME), &mAS.EditBuffer)) {
auto ag = mTable->GetArrayGroup(cell.DataId);
- auto cl = ag.FindCell(cell.Content);
- mAS.ErrorDuplicateVarName = cl.x != -1 && cl.y != -1;
+ bool success = ag.UpdateCellName(cell.Content, mAS.EditBuffer);
+ if (success) {
+ cell.Content = mAS.EditBuffer;
+ mAS.ErrorDuplicateVarName = false;
+ } else {
+ mAS.ErrorDuplicateVarName = true;
+ }
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip(I18N_TEXT("Name of the parameter lnk to this cell; local within the array group.", L10N_TABLE_CELL_ARRAY_VAR_TOOLTIP));
@@ -413,6 +486,21 @@ private:
// TODO
}
+ void DisplayTableResizer()
+ {
+ ImGui::InputInt(I18N_TEXT("Width", L10N_TABLE_WIDTH), &mNewTableWidth);
+ ImGui::InputInt(I18N_TEXT("Height", L10N_TABLE_HEIGHT), &mNewTableHeight);
+
+ if (ImGui::Button(I18N_TEXT("Confirm", L10N_CONFIRM))) {
+ ImGui::CloseCurrentPopup();
+ Resize(mNewTableWidth, mNewTableHeight);
+ }
+ ImGui::SameLine();
+ if (ImGui::Button(I18N_TEXT("Cancel", L10N_CANCEL))) {
+ ImGui::CloseCurrentPopup();
+ }
+ }
+
void DisplayTable()
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8, 8));
@@ -561,12 +649,12 @@ private:
mSelectionTL = { -1, -1 };
mSelectionBR = { -1, -1 };
- ResetCS();
+ ResetIdleState();
}
- void ResetCS()
+ void ResetIdleState()
{
- mCS = {};
+ mIdleState = {};
}
void SelectRange(Vec2i p1, Vec2i p2)
@@ -588,33 +676,49 @@ private:
uiCell.Selected = true;
});
- ResetAS();
+ ResetRS();
}
- void ResetSS(Vec2i pos)
+ void ResetRS()
{
- mSS = {};
- mSS.ErrorDuplicateVarName = false;
- mSS.HasLeftAG = pos.x > 1 && mTable->GetCell({ pos.x - 1, pos.y }).Type == TableCell::ArrayParametricCell;
- mSS.HasRightAG = pos.x < mTable->GetTableWidth() - 1 && mTable->GetCell({ pos.x + 1, pos.y }).Type == TableCell::ArrayParametricCell;
+ mRS = {};
}
- void SelectCell(Vec2i cell)
+ void SelectCell(Vec2i pos)
{
ClearSelection();
- mSelectionTL = cell;
- mSelectionBR = cell;
+ mSelectionTL = pos;
+ mSelectionBR = pos;
- int i = cell.y * mTable->GetTableWidth() + cell.x;
+ int i = pos.y * mTable->GetTableWidth() + pos.x;
mUICells[i].Selected = true;
- ResetSS(cell);
+ switch (mTable->GetCell(pos).Type) {
+ case TableCell::ConstantCell: ResetCS(); break;
+ case TableCell::SingularParametricCell: ResetSS(pos); break;
+ case TableCell::ArrayParametricCell: ResetAS(pos); break;
+ }
+ }
+
+ void ResetCS()
+ {
+ mCS = {};
+ }
+
+ void ResetSS(Vec2i pos)
+ {
+ mSS = {};
+ mSS.EditBuffer = mTable->GetCell(pos).Content;
+ mSS.ErrorDuplicateVarName = false;
+ mSS.HasLeftAG = pos.x > 1 && mTable->GetCell({ pos.x - 1, pos.y }).Type == TableCell::ArrayParametricCell;
+ mSS.HasRightAG = pos.x < mTable->GetTableWidth() - 1 && mTable->GetCell({ pos.x + 1, pos.y }).Type == TableCell::ArrayParametricCell;
}
- void ResetAS()
+ void ResetAS(Vec2i pos)
{
mAS = {};
+ mAS.EditBuffer = mTable->GetCell(pos).Content;
mAS.ErrorDuplicateVarName = false;
}
};