aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-rw-r--r--core/src/Locale/zh_CN.h3
-rw-r--r--core/src/Model/Template/TableTemplate.cpp65
-rw-r--r--core/src/Model/Template/TableTemplate.hpp3
-rw-r--r--core/src/UI/UI.hpp2
-rw-r--r--core/src/UI/UI_Templates.cpp24
-rw-r--r--core/src/UI/UI_Utils.cpp9
-rw-r--r--core/src/Utils/Math.hpp2
7 files changed, 77 insertions, 31 deletions
diff --git a/core/src/Locale/zh_CN.h b/core/src/Locale/zh_CN.h
index d1f32ff..02b1e9a 100644
--- a/core/src/Locale/zh_CN.h
+++ b/core/src/Locale/zh_CN.h
@@ -126,7 +126,8 @@
#define L10N_TABLE "表格"
#define L10N_TABLE_CELL "单元格"
#define L10N_TABLE_CELL_CONV "转换类型..."
-#define L10N_TABLE_CELL_CONV_PARAM "单元格参数"
+#define L10N_TABLE_CELL_CONV_CONST "转换为普通单元格"
+#define L10N_TABLE_CELL_CONV_PARAM "转换为参数单元格"
#define L10N_TABLE_CELL_CONV_CREATE_AG "创建列表参数组"
#define L10N_TABLE_CELL_CONV_ADD_AG_LEFT "添加至左侧的列表参数组"
#define L10N_TABLE_CELL_CONV_ADD_AG_RIGHT "添加至右侧的列表参数组"
diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp
index 88980ae..e01af02 100644
--- a/core/src/Model/Template/TableTemplate.cpp
+++ b/core/src/Model/Template/TableTemplate.cpp
@@ -1,6 +1,7 @@
#include "TableTemplate.hpp"
#include <xlsxwriter.h>
+#include <charconv>
#include <iostream>
#include <map>
@@ -153,18 +154,50 @@ void TableTemplate::SetCellType(Vec2i pos, TableCell::CellType type)
return;
}
- switch (type) {
- case TableCell::ConstantCell: {
- // TODO
+ switch (cell.Type) {
+ // Nothing to change
+ case TableCell::ConstantCell: break;
+
+ case TableCell::SingularParametricCell:
+ mName2Parameters.erase(cell.Content);
+ break;
+
+ case TableCell::ArrayParametricCell: {
+ auto& ag = mArrayGroups[cell.DataId];
+ if (cell.Location.x == ag.LeftCell) {
+
+ } else if (cell.Location.x == ag.RightCell) {
+
+ } else {
+ }
} break;
+ }
+
+ switch (type) {
+ // Nothing to do
+ case TableCell::ConstantCell: break;
case TableCell::SingularParametricCell: {
- cell.Type = type;
+ int idx = pos.y * GetTableWidth() + pos.x;
+ auto [DISCARD, inserted] = mName2Parameters.emplace(cell.Content, idx);
+
+ // Duplicate name
+ if (!inserted) {
+ return;
+ }
} break;
- // Setting the cell to be a part of a group requires calling the overload that supplies the group
- case TableCell::ArrayParametricCell: break;
+ case TableCell::ArrayParametricCell: {
+ auto ptr = AddArrayGroup(pos.y, pos.x, pos.x);
+
+ // Duplicate name
+ if (ptr == nullptr) {
+ return;
+ }
+ } break;
}
+
+ cell.Type = type;
}
int TableTemplate::GetArrayGroupCount() const
@@ -182,7 +215,18 @@ TableCellArrayGroup& TableTemplate::GetArrayGroup(int id)
return mArrayGroups[id];
}
-TableCellArrayGroup& TableTemplate::AddArrayGroup(int row, int left, int right)
+TableCellArrayGroup* TableTemplate::AddArrayGroup(int row, int left, int right)
+{
+ // size_t max value: 18446744073709551615
+ // ^~~~~~~~~~~~~~~~~~~~ 20 chars
+ char name[20];
+ auto res = std::to_chars(std::begin(name), std::end(name), mArrayGroups.size());
+ std::string_view nameStr(name, res.ptr - name);
+
+ return AddArrayGroup(nameStr, row, left, right);
+}
+
+TableCellArrayGroup* TableTemplate::AddArrayGroup(std::string_view name, int row, int left, int right)
{
assert(row >= 0 && row < GetTableHeight());
assert(left >= 0 && left < GetTableWidth());
@@ -194,12 +238,17 @@ TableCellArrayGroup& TableTemplate::AddArrayGroup(int row, int left, int right)
std::swap(left, right);
}
+ auto [DISCARD, inserted] = mName2ArrayGroups.emplace(name, (int)mArrayGroups.size());
+ if (!inserted) {
+ return nullptr;
+ }
+
mArrayGroups.push_back(TableCellArrayGroup{
.Row = row,
.LeftCell = left,
.RightCell = right,
});
- return mArrayGroups.back();
+ return &mArrayGroups.back();
}
bool TableTemplate::ExtendArrayGroupLeft(int id, int n)
diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp
index 754ebe1..7f331fa 100644
--- a/core/src/Model/Template/TableTemplate.hpp
+++ b/core/src/Model/Template/TableTemplate.hpp
@@ -167,7 +167,8 @@ public:
int GetArrayGroupCount() const;
const TableCellArrayGroup& GetArrayGroup(int id) const;
TableCellArrayGroup& GetArrayGroup(int id);
- TableCellArrayGroup& AddArrayGroup(int row, int left, int right);
+ TableCellArrayGroup* AddArrayGroup(int row, int left, int right);
+ TableCellArrayGroup* AddArrayGroup(std::string_view name, int row, int left, int right);
bool ExtendArrayGroupLeft(int id, int n);
bool ExtendArrayGroupRight(int id, int n);
diff --git a/core/src/UI/UI.hpp b/core/src/UI/UI.hpp
index b0f0a6f..0a80b4c 100644
--- a/core/src/UI/UI.hpp
+++ b/core/src/UI/UI.hpp
@@ -13,8 +13,6 @@ void PopDisabled();
bool Button(const char* label, bool disabled);
bool Button(const char* label, const ImVec2& sizeArg, bool disabled);
-bool MenuItemConditional(const char* name, bool disabled);
-
void ErrorIcon();
void ErrorMessage(const char* fmt, ...);
void WarningIcon();
diff --git a/core/src/UI/UI_Templates.cpp b/core/src/UI/UI_Templates.cpp
index e77bef3..5255b6a 100644
--- a/core/src/UI/UI_Templates.cpp
+++ b/core/src/UI/UI_Templates.cpp
@@ -170,23 +170,29 @@ private:
ImGui::OpenPopup("ConvertCtxMenu");
}
if (ImGui::BeginPopup("ConvertCtxMenu")) {
- if (ImGui::MenuItem(I18N_TEXT("Single parameter", L10N_TABLE_CELL_CONV_PARAM))) {
+ bool constantDisabled = cell.Type == TableCell::ConstantCell;
+ if (ImGui::MenuItem(I18N_TEXT("Convert to regular cell", L10N_TABLE_CELL_CONV_CONST), nullptr, false, constantDisabled)) {
+ mTable->SetCellType(pos, TableCell::ConstantCell);
+ }
+
+ bool singleDisabled = cell.Type == TableCell::SingularParametricCell;
+ if (ImGui::MenuItem(I18N_TEXT("Convert to parameter cell", L10N_TABLE_CELL_CONV_PARAM), nullptr, false, singleDisabled)) {
mTable->SetCellType(pos, TableCell::SingularParametricCell);
}
- bool createDisabled = cell.Type == TableCell::ArrayParametricCell;
- if (ImGui::MenuItemConditional(I18N_TEXT("Create array group", L10N_TABLE_CELL_CONV_CREATE_AG), createDisabled)) {
+ 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 leftDisabled = !mSS.HasLeftAG || cell.Type == TableCell::ArrayParametricCell;
- if (ImGui::MenuItemConditional(I18N_TEXT("Add to array group to the left", L10N_TABLE_CELL_CONV_ADD_AG_LEFT), leftDisabled)) {
+ 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 });
mTable->ExtendArrayGroupRight(leftCell.DataId, 1);
}
- bool rightDisabled = !mSS.HasRightAG || cell.Type == TableCell::ArrayParametricCell;
- if (ImGui::MenuItemConditional(I18N_TEXT("Add to array group to the right", L10N_TABLE_CELL_CONV_ADD_AG_RIGHT), rightDisabled)) {
+ 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 });
mTable->ExtendArrayGroupLeft(rightCell.DataId, 1);
}
@@ -472,8 +478,8 @@ private:
{
mSS = {};
mSS.ErrorDuplicateVarName = false;
- mSS.HasLeftAG = pos.x >= 1;
- mSS.HasRightAG = pos.x < mTable->GetTableWidth();
+ 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 SelectCell(Vec2i cell)
diff --git a/core/src/UI/UI_Utils.cpp b/core/src/UI/UI_Utils.cpp
index 0dcde8f..e3ac097 100644
--- a/core/src/UI/UI_Utils.cpp
+++ b/core/src/UI/UI_Utils.cpp
@@ -44,15 +44,6 @@ bool ImGui::Button(const char* label, const ImVec2& sizeArg, bool disabled)
return res;
}
-bool ImGui::MenuItemConditional(const char* name, bool disabled)
-{
- if (disabled) PushDisabled();
- bool res = MenuItem(name, nullptr, false, disabled);
- if (disabled) PopDisabled();
-
- return res;
-}
-
void ImGui::ErrorIcon()
{
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 237 / 255.0f, 67 / 255.0f, 55 / 255.0f, 1.0f }); // #ED4337
diff --git a/core/src/Utils/Math.hpp b/core/src/Utils/Math.hpp
index 2d0c0cd..da53da2 100644
--- a/core/src/Utils/Math.hpp
+++ b/core/src/Utils/Math.hpp
@@ -8,4 +8,4 @@ constexpr T Abs(T t)
return t < 0 ? -t : t;
}
-} // namespace Math
+} // namespace MathUtils