diff options
author | rtk0c <[email protected]> | 2021-05-16 20:56:04 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-05-16 21:15:43 -0700 |
commit | dc13110c14bf49e495d4b4243fd4758232f8716f (patch) | |
tree | 8b5f6bbefab78c4956951e40cf18d6f3a3f604d6 /core/src/Model | |
parent | 203d65e8873f2f1d240b22899ac89855b64974c8 (diff) |
TableTemplate draft
Diffstat (limited to 'core/src/Model')
-rw-r--r-- | core/src/Model/Template/TableTemplate.cpp | 119 | ||||
-rw-r--r-- | core/src/Model/Template/TableTemplate.hpp | 89 | ||||
-rw-r--r-- | core/src/Model/Template/fwd.hpp | 7 | ||||
-rw-r--r-- | core/src/Model/Workflow/Workflow.hpp | 4 | ||||
-rw-r--r-- | core/src/Model/Workflow/Workflow_Main.cpp | 4 | ||||
-rw-r--r-- | core/src/Model/fwd.hpp | 1 |
6 files changed, 220 insertions, 4 deletions
diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp new file mode 100644 index 0000000..caf0e21 --- /dev/null +++ b/core/src/Model/Template/TableTemplate.cpp @@ -0,0 +1,119 @@ +#include "TableTemplate.hpp" + +bool TableCell::IsPrimaryCell() const +{ + return PrimaryCellLocation == Location; +} + +bool TableCell::IsMergedCell() const +{ + return PrimaryCellLocation.x == -1 || PrimaryCellLocation.y == -1; +} + +int TableTemplate::GetTableWidth() const +{ + return mTableWidth; +} + +int TableTemplate::GetTableHeight() const +{ + return mTableHeight; +} + +void TableTemplate::Resize(int newWidth, int newHeight) +{ + // TODO this doesn't gracefully handle resizing to a smaller size which trims some merged cells + + std::vector<TableCell> cells; + cells.reserve(newWidth * newHeight); + + int yEnd = std::min(mTableHeight, newHeight); + int xEnd = std::min(mTableWidth, newWidth); + for (int y = 0; y < yEnd; ++y) { + if (y >= mTableHeight) { + for (int x = 0; x < xEnd; ++x) { + cells.push_back(TableCell{}); + } + continue; + } + + for (int x = 0; x < xEnd; ++x) { + if (x >= mTableWidth) { + cells.push_back(TableCell{}); + } else { + auto& cell = GetCell({ x, y }); + cells.push_back(std::move(cell)); + } + } + } + + mCells = std::move(cells); +} + +const TableCell& TableTemplate::GetCell(Vec2i pos) const +{ + return mCells[pos.y * mTableWidth + pos.x]; +} + +TableCell& TableTemplate::GetCell(Vec2i pos) +{ + return const_cast<TableCell&>(const_cast<const TableTemplate*>(this)->GetCell(pos)); +} + +TableTemplate::MergeCellsResult TableTemplate::MergeCells(Vec2i topLeft, Vec2i bottomRight) +{ + auto SortTwo = [](int& a, int& b) { + if (a > b) { + std::swap(a, b); + } + }; + SortTwo(topLeft.x, bottomRight.x); + SortTwo(topLeft.y, bottomRight.y); + + auto ResetProgress = [&]() { + for (int y = topLeft.y; y < bottomRight.y; ++y) { + for (int x = topLeft.x; x < bottomRight.x; ++x) { + auto& cell = GetCell({ x, y }); + cell.PrimaryCellLocation = { -1, -1 }; + } + } + }; + + for (int y = topLeft.y; y < bottomRight.y; ++y) { + for (int x = topLeft.x; x < bottomRight.x; ++x) { + auto& cell = GetCell({ x, y }); + if (cell.IsMergedCell()) { + ResetProgress(); + return MCR_CellAlreadyMerged; + } + + cell.PrimaryCellLocation = topLeft; + } + } + + auto& primaryCell = GetCell(topLeft); + primaryCell.SpanX = bottomRight.x - topLeft.x; + primaryCell.SpanY = bottomRight.y - topLeft.y; + + return MCR_Success; +} + +TableTemplate::BreakCellsResult TableTemplate::BreakCells(Vec2i topLeft) +{ + auto& primaryCell = GetCell(topLeft); + if (!primaryCell.IsMergedCell()) { + return BCR_CellNotMerged; + } + + for (int dy = 0; dy < primaryCell.SpanY; ++dy) { + for (int dx = 0; dx < primaryCell.SpanX; ++dx) { + auto& cell = GetCell({ topLeft.x + dx, topLeft.y + dy }); + cell.PrimaryCellLocation = { -1, -1 }; + } + } + + primaryCell.SpanX = 1; + primaryCell.SpanY = 1; + + return BCR_Success; +} diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp new file mode 100644 index 0000000..d704b1f --- /dev/null +++ b/core/src/Model/Template/TableTemplate.hpp @@ -0,0 +1,89 @@ +#pragma once + +#include "Utils/Vector.hpp" +#include "Utils/VectorHash.hpp" + +#include <string> +#include <vector> + +class TableCell +{ +public: + enum TextAlignment + { + /// For horizontal alignment, this means align left. For vertical alignment, this means align top. + AlignAxisMin, + /// Align middle of the text to the middle of the axis. + AlignCenter, + /// For horizontal alignment, this means align right. For vertical alignment, this means align bottom. + AlignAxisMax, + }; + + enum CellType + { + ConstantCell, + ParametricCell, + }; + +public: + std::string Content; + Vec2i Location; + /// Location of the primary (top left) cell, if this cell is a part of a merged group. + /// Otherwise, either component of this field shall be -1. + Vec2i PrimaryCellLocation; + int SpanX; + int SpanY; + TextAlignment HorizontalAlignment = AlignAxisMin; + TextAlignment VerticalAlignment = AlignAxisMin; + CellType Type; + +public: + bool IsPrimaryCell() const; + bool IsMergedCell() const; +}; + +class TableArrayParameterGroup +{ +public: + enum ExpansionDirection + { + ExpandUp, + ExpandDown, + ExpandLeft, + ExpandRight, + }; + +public: + ExpansionDirection ExpandDirection; +}; + +class TableTemplate +{ +private: + std::vector<TableCell> mCells; + std::vector<TableArrayParameterGroup> mArrayGroups; + int mTableWidth; + int mTableHeight; + +public: + int GetTableWidth() const; + int GetTableHeight() const; + void Resize(int newWidth, int newHeight); + + const TableCell& GetCell(Vec2i pos) const; + TableCell& GetCell(Vec2i pos); + + enum MergeCellsResult + { + MCR_CellAlreadyMerged, + MCR_Success, + }; + MergeCellsResult MergeCells(Vec2i topLeft, Vec2i bottomRight); + + enum BreakCellsResult + { + BCR_CellNotMerged, + BCR_Success, + }; + BreakCellsResult BreakCells(Vec2i topLeft); +}; diff --git a/core/src/Model/Template/fwd.hpp b/core/src/Model/Template/fwd.hpp new file mode 100644 index 0000000..2bf6814 --- /dev/null +++ b/core/src/Model/Template/fwd.hpp @@ -0,0 +1,7 @@ +#pragma once + +// TableTemplate.hpp +class TableCell; +class ConstantTableCell; +class ParametricTableCell; +class TableTemplate; diff --git a/core/src/Model/Workflow/Workflow.hpp b/core/src/Model/Workflow/Workflow.hpp index 847aa72..0aabcdc 100644 --- a/core/src/Model/Workflow/Workflow.hpp +++ b/core/src/Model/Workflow/Workflow.hpp @@ -263,8 +263,8 @@ public: enum ReadResult { - ReadSuccess, - ReadInvalidVersion, + RR_Success, + RR_InvalidVersion, }; ReadResult ReadFrom(std::istream& stream); void WriteTo(std::ostream& stream); diff --git a/core/src/Model/Workflow/Workflow_Main.cpp b/core/src/Model/Workflow/Workflow_Main.cpp index 1b50064..c9ae328 100644 --- a/core/src/Model/Workflow/Workflow_Main.cpp +++ b/core/src/Model/Workflow/Workflow_Main.cpp @@ -653,9 +653,9 @@ Workflow::ReadResult Workflow::ReadFrom(std::istream& stream) switch (version) { case 0: DeserializeV0(); break; - default: return ReadInvalidVersion; + default: return RR_InvalidVersion; } - return ReadSuccess; + return RR_Success; } void Workflow::WriteTo(std::ostream& stream) diff --git a/core/src/Model/fwd.hpp b/core/src/Model/fwd.hpp index 594599c..6fa29c5 100644 --- a/core/src/Model/fwd.hpp +++ b/core/src/Model/fwd.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Model/Template/fwd.hpp" #include "Model/Workflow/fwd.hpp" // Filter.hpp |