gui refactor

This commit is contained in:
2024-05-23 15:16:29 +02:00
parent e784a7b471
commit bb650e4f2d

View File

@@ -8,6 +8,12 @@
static std::string equationsResultImage;
struct GuiMatrix {
std::vector<std::vector<int>> matrixValues;
int matrixSizeX = 4;
int matrixSizeY = 4;
};
static Matrix LoadMatrixFromStdVect(const std::vector<std::vector<int>>& data) {
Matrix result {data.size(), data.empty() ? 0 : data[0].size()};
for (std::size_t i = 0; i < result.GetRawCount(); i++) {
@@ -45,41 +51,35 @@ static std::string PrintVect(const Vect& vect) {
void PivotGui::Init() {}
void PivotGui::Render() {
ImGuiIO& io = ImGui::GetIO();
static std::vector<std::vector<int>> matrixValues;
static int matrixSizeX = 4;
static int matrixSizeY = 4;
static Solver solver;
static bool refresh = true;
static void RenderSystemTab() {}
static void RenderLeftGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
// divisions des fenetres
ImVec2 topLeftWindowSize(io.DisplaySize.x * 0.5f, io.DisplaySize.y);
ImVec2 topRightWindowSize(io.DisplaySize.x * 0.5f, io.DisplaySize.y);
ImVec2 topLeftWindowSize(ImGui::GetContentRegionAvail().x * 0.5f, 0);
// Begin fenetre top left
ImGui::SetNextWindowSize(topLeftWindowSize);
ImGui::SetNextWindowPos(ImVec2(0, 0)); // Position at the top-left corner
ImGui::Begin("Left Top Window", nullptr,
// ImGui::SetNextWindowPos(ImVec2(0, 0)); // Position at the top-left corner
ImGui::BeginChild("Left Top Window", topLeftWindowSize, ImGuiChildFlags_Border);
/*ImGui::Begin(, nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
*/
// ImGui::BeginTabBar("MainMenu");
// Get window position
ImVec2 windowPos = ImGui::GetWindowPos();
ImGui::Text("Matrice initiale:");
if (ImGui::InputInt("##RowsMatriceInitiale", &matrixSizeY))
if (ImGui::InputInt("##RowsMatriceInitiale", &guiMatrix.matrixSizeY))
refresh = true;
matrixSizeY = std::max(1, matrixSizeY);
guiMatrix.matrixSizeY = std::max(1, guiMatrix.matrixSizeY);
ImGui::SameLine();
ImGui::Text("Lignes");
if (ImGui::InputInt("##ColumnsMatriceInitiale", &matrixSizeX))
if (ImGui::InputInt("##ColumnsMatriceInitiale", &guiMatrix.matrixSizeX))
refresh = true;
matrixSizeX = std::max(1, matrixSizeX);
guiMatrix.matrixSizeX = std::max(1, guiMatrix.matrixSizeX);
ImGui::SameLine();
ImGui::Text("Colonnes");
@@ -90,19 +90,19 @@ void PivotGui::Render() {
// Resize matrixValues and initialize new elements to 0
if (refresh) {
matrixValues.resize(matrixSizeY);
for (auto& row : matrixValues) {
row.resize(matrixSizeX, 0);
guiMatrix.matrixValues.resize(guiMatrix.matrixSizeY);
for (auto& row : guiMatrix.matrixValues) {
row.resize(guiMatrix.matrixSizeX, 0);
}
}
for (int y = 0; y < matrixSizeY; y++) {
for (int x = 0; x < matrixSizeX; x++) {
for (int y = 0; y < guiMatrix.matrixSizeY; y++) {
for (int x = 0; x < guiMatrix.matrixSizeX; x++) {
if (x > 0)
ImGui::SameLine();
ImGui::PushID(y * matrixSizeX + x);
ImGui::PushID(y * guiMatrix.matrixSizeX + x);
ImGui::PushItemWidth(60); // Adjust this value to change the cell size
if (ImGui::InputInt("", &matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal))
if (ImGui::InputInt("", &guiMatrix.matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal))
refresh = true;
ImGui::PopItemWidth();
ImGui::PopID();
@@ -116,7 +116,7 @@ void PivotGui::Render() {
ImGui::Text("Matrice échelonnée:");
// Convert the "result" string back to a matrix
Matrix resultMatrix = LoadMatrixFromStdVect(matrixValues);
Matrix resultMatrix = LoadMatrixFromStdVect(guiMatrix.matrixValues);
// Apply the Gauss-Jordan elimination to the matrix
Gauss::GaussJordan(resultMatrix, true, true); // Assuming you want to reduce and normalize the matrix
@@ -134,13 +134,17 @@ void PivotGui::Render() {
}
}
ImGui::End(); // End fenetre top left
ImGui::EndChild();
}
static void RenderRightGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
static Solver solver;
// Begin fenetre top right
ImGui::SetNextWindowSize(topRightWindowSize);
ImGui::SetNextWindowPos(ImVec2(windowPos.x + topLeftWindowSize.x, 0)); // Position at the top-right corner
ImGui::Begin("Right Top Window", nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
// ImGui::SetNextWindowSize(topRightWindowSize);
// ImGui::SetNextWindowPos(ImVec2(windowPos.x + topLeftWindowSize.x, 0)); // Position at the top-right corner
ImGui::BeginChild("Right Top Window", {0, 0}, ImGuiChildFlags_Border);
// ImGui::Begin("Right Top Window", nullptr,
// ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
// rajouter le code pour la partie top right
@@ -149,7 +153,7 @@ void PivotGui::Render() {
if (refresh) {
// Calculate the kernel and image
Vect image = solver.Image(LoadMatrixFromStdVect(matrixValues));
Vect image = solver.Image(LoadMatrixFromStdVect(guiMatrix.matrixValues));
Matrix linearSystem = image.GetLinearSystem();
// Store the equationsResult strings in the global variable
@@ -162,9 +166,9 @@ void PivotGui::Render() {
equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n";
}
result = std::string("Noyau: ") + "\n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" +
"Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" + "Image: " + "\n" +
PrintVect(image);
result = std::string("Noyau: ") + "\n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" +
"\n" + "Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" + "\n" +
"Image: " + "\n" + PrintVect(image);
}
refresh = false;
@@ -176,7 +180,45 @@ void PivotGui::Render() {
ImGui::TextWrapped("%s", result.c_str());
ImGui::End(); // End fenetre top right
ImGui::EndChild(); // End fenetre top right
}
static void RenderGaussTab() {
static GuiMatrix guiMatrix;
static bool refresh = true;
RenderLeftGaussChild(refresh, guiMatrix);
ImGui::SameLine();
RenderRightGaussChild(refresh, guiMatrix);
}
static void RenderMainWindow() {
ImGuiIO& io = ImGui::GetIO();
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::SetNextWindowPos({0, 0});
ImGui::Begin("MainWindow", nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
ImGui::BeginTabBar("MainBar");
if (ImGui::BeginTabItem("Noyau et Image")) {
RenderGaussTab();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Systèmes")) {
RenderSystemTab();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
ImGui::End();
}
void PivotGui::Render() {
RenderMainWindow();
#ifndef NDEBUG
ImGui::ShowDemoWindow(nullptr);
#endif
}
void PivotGui::Destroy() {}