Skip to content

Commit

Permalink
More work on the UI (good enough for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Sep 26, 2023
1 parent f41767a commit 51b928c
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 182 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/Build
/.idea
StaticAnalysis.sh
/.PVS-Studio
/.PVS-Studio
.i
.cfg
314 changes: 179 additions & 135 deletions Apps/CellSim/src/CellSimInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,19 @@ namespace CS
{
// ----- Private -----

void CellSimInterface::SetOverlayParameters()
void CellSimInterface::CalculateSidebarDimensions()
{
const ImGuiViewport* viewport = ImGui::GetMainViewport();
const ImVec2 workPos = viewport->WorkPos;
const auto windowWidth = (float)Engine::AppSettings::WINDOW_WIDTH;
const auto windowHeight = (float)Engine::AppSettings::WINDOW_HEIGHT;
const float menuBarHeight = 25.0f;
const float sidebarWidth = 415.0f;

_sidebarPos = ImVec2(workPos.x + windowWidth, menuBarHeight);
_sidebarSize = ImVec2(sidebarWidth,windowHeight - menuBarHeight);
_sidebarPos = ImVec2(workPos.x + windowWidth, _menuBarHeight);
_sidebarSize = ImVec2(_sidebarWidth,windowHeight - _menuBarHeight);
}

// ----- Public -----

CellSimInterface::CellSimInterface()
: _windowFlags(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav |
ImGuiWindowFlags_NoMove),
_windowAlphaValue(0.85f),
_showOverlay(true)
{
// --- Load custom font
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("../Res/Assets/Fonts/JetBrainsMono-Medium.ttf", 19);

// --- Set style
ImGui::StyleColorsDark();
SetDarkThemeColors();

// --- Calculate and set overlay parameters
SetOverlayParameters();
}

void CellSimInterface::AddElements()
void CellSimInterface::AddMenuBar()
{
// --- Discard old plotting data every 120 frames
if(Engine::Window::GetFrameCounter() > 120)
{
ImGui::PlotVarFlushOldEntries();
}

// --- Menu bar
if(ImGui::BeginMainMenuBar())
{
if(ImGui::BeginMenu("Settings"))
Expand All @@ -72,125 +42,199 @@ namespace CS
}
}
ImGui::EndMainMenuBar();
}

// --- Overlay
if(_showOverlay)
void CellSimInterface::CheckCellBoundaries()
{
for(Engine::uint8 i = 0; i < 3; i++)
{
SetOverlayParameters();
if(Engine::AppSettings::selectedCellCoords[i] < 0)
{
Engine::AppSettings::selectedCellCoords[i] = 0;
}
else if((uint32_t)Engine::AppSettings::selectedCellCoords[i] > Engine::AppSettings::CELL_FRAME_SIZE)
{
Engine::AppSettings::selectedCellCoords[i] = Engine::AppSettings::CELL_FRAME_SIZE;
}
}
}

void CellSimInterface::AddSideBar()
{
ImGui::SetNextWindowBgAlpha(_windowAlphaValue);
ImGui::SetNextWindowPos(_sidebarPos, ImGuiCond_Always, _overlayPivot);
ImGui::SetNextWindowSize(_sidebarSize);

// --- Sidebar
if(ImGui::Begin("Sidebar", nullptr, _windowFlags))
{
// --- Stats
{
ImGui::SetNextWindowBgAlpha(_windowAlphaValue);
ImGui::SetNextWindowPos(_sidebarPos, ImGuiCond_Always, _overlayPivot);
ImGui::SetNextWindowSize(_sidebarSize);
// --- Application stats
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime() * 1000.0f, Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime() * 1000.0f, 0.0f, 30.0f);

// --- Render stats
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("DrawCalls: %d", Engine::AppSettings::renderStats.drawCalls);
ImGui::Text("DrawnVertices: %d", Engine::AppSettings::renderStats.drawnVertices);
ImGui::Separator();

ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Model passes: %d", Engine::AppSettings::renderStats.modelPasses);
ImGui::Text("Sprite passes: %d", Engine::AppSettings::renderStats.spritePasses);
ImGui::Text("Cell passes: %d", Engine::AppSettings::renderStats.cellPasses);
ImGui::Separator();

// --- Camera stats
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Camera-Position: ( %.1f, %.1f, %.1f )", Engine::Camera3D::GetPosition().x, Engine::Camera3D::GetPosition().y, Engine::Camera3D::GetPosition().z);
ImGui::Text("Camera-Front: ( %.1f, %.1f, %.1f )", Engine::Camera3D::GetFront().x, Engine::Camera3D::GetFront().y, Engine::Camera3D::GetFront().z);
ImGui::Text("Camera-Yaw, Pitch: ( %.2f, %.2f )", Engine::Camera3D::GetYaw(), Engine::Camera3D::GetPitch());
ImGui::Separator();

// --- Profiling/Timing-Results
ImGui::NewLine();
ImGui::Separator();
for(auto const& entry : Engine::Profiler::_results)
{
ImGui::Text("%.3fms - %s", entry.second, entry.first);
}
ImGui::Separator();
}

if(ImGui::Begin("Sidebar", &_showOverlay, _windowFlags))
// --- Cell spawn menu
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 10));

// --- Titlebar
ImGui::NewLine();
ImGui::Separator();
CenterText("Cell management panel");
ImGui::Separator();

// --- General information
ImGui::Text("Frame dimensions: (%d, %d, %d)", Engine::AppSettings::CELL_FRAME_SIZE, Engine::AppSettings::CELL_FRAME_SIZE, Engine::AppSettings::CELL_FRAME_SIZE);
ImGui::Text("Cells alive: %d", Engine::AppSettings::cellsAlive);
ImGui::Separator();

// --- Cell selection menu
std::string cellTypeString = std::string("Selected cell type: ") + Engine::CellTypeStrings[Engine::AppSettings::selectedCellType];
if(ImGui::BeginMenu(cellTypeString.c_str()))
{
// --- Application stats
ImGui::Text("Application average %.2f ms/frame (%.1f FPS)", Engine::Window::GetDeltaTime() * 1000.0f, Engine::Window::GetFps());
ImGui::PlotVar("", (float)Engine::Window::GetDeltaTime() * 1000.0f, 0.0f, 30.0f);

// --- Render stats
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("DrawCalls: %d", Engine::AppSettings::renderStats.drawCalls);
ImGui::Text("DrawnVertices: %d", Engine::AppSettings::renderStats.drawnVertices);
ImGui::Separator();

ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Model passes: %d", Engine::AppSettings::renderStats.modelPasses);
ImGui::Text("Sprite passes: %d", Engine::AppSettings::renderStats.spritePasses);
ImGui::Text("Cell passes: %d", Engine::AppSettings::renderStats.cellPasses);
ImGui::Separator();

// --- Camera stats
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Camera-Position: ( %.1f, %.1f, %.1f )", Engine::Camera3D::GetPosition().x, Engine::Camera3D::GetPosition().y, Engine::Camera3D::GetPosition().z);
ImGui::Text("Camera-Front: ( %.1f, %.1f, %.1f )", Engine::Camera3D::GetFront().x, Engine::Camera3D::GetFront().y, Engine::Camera3D::GetFront().z);
ImGui::Text("Camera-Yaw, Pitch: ( %.2f, %.2f )", Engine::Camera3D::GetYaw(), Engine::Camera3D::GetPitch());
ImGui::Separator();

// --- Profiling/Timing-Results
ImGui::NewLine();
ImGui::Separator();
for(auto const& entry : Engine::Profiler::_results)
if(ImGui::MenuItem("None"))
{
ImGui::Text("%.3fms - %s", entry.second, entry.first);
Engine::AppSettings::selectedCellType = Engine::CellTypes::None;
}
ImGui::Separator();

// --- Cell spawn menu
ImGui::NewLine();
ImGui::Separator();
TextCentered("Cell spawn menu");
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 10));
ImGui::Separator();
ImGui::Text("Frame dimensions: (%d, %d, %d)", Engine::AppSettings::CELL_FRAME_SIZE, Engine::AppSettings::CELL_FRAME_SIZE, Engine::AppSettings::CELL_FRAME_SIZE);

// --- Drop down menu
std::string cellTypeString = std::string("Cell type: ") + Engine::CellTypeStrings[Engine::AppSettings::selectedCellType];
if(ImGui::BeginMenu(cellTypeString.c_str()))

if(ImGui::MenuItem("Water"))
{
if(ImGui::MenuItem("None"))
{
Engine::AppSettings::selectedCellType = Engine::CellTypes::None;
}

if(ImGui::MenuItem("Water"))
{
Engine::AppSettings::selectedCellType = Engine::CellTypes::Water;
}

if(ImGui::MenuItem("Magma"))
{
Engine::AppSettings::selectedCellType = Engine::CellTypes::Magma;
}

if(ImGui::MenuItem("Stone"))
{
Engine::AppSettings::selectedCellType = Engine::CellTypes::Stone;
}
ImGui::EndMenu();

Engine::CameraController3D::DeFocusWindow(Engine::Window::GetWindow());
Engine::AppSettings::selectedCellType = Engine::CellTypes::Water;
}

// --- Coordinate input
float width = ImGui::CalcItemWidth();
ImGui::PushItemWidth(width / 1.5f);
if(ImGui::MenuItem("Magma"))
{
Engine::AppSettings::selectedCellType = Engine::CellTypes::Magma;
}

ImGui::Text("X Coordinate:\t");
ImGui::SameLine();
ImGui::InputInt("##1", &Engine::AppSettings::selectedXCoord, 10, 0, ImGuiInputTextFlags_CharsDecimal);
ImGui::EndMenu();

ImGui::Text("Y Coordinate:\t");
ImGui::SameLine();
ImGui::InputInt("##2", &Engine::AppSettings::selectedYCoord, 10, 0, ImGuiInputTextFlags_CharsDecimal);
Engine::CameraController3D::DeFocusWindow(Engine::Window::GetWindow());
}

ImGui::Text("Z Coordinate:\t");
ImGui::SameLine();
ImGui::InputInt("##3", &Engine::AppSettings::selectedZCoord, 10, 0, ImGuiInputTextFlags_CharsDecimal);
// --- Cell spawn coordinate input
float width = ImGui::CalcItemWidth();
ImGui::PushItemWidth(width / 1.5f);

ImGui::Text("X Coordinate:\t");
ImGui::SameLine();
ImGui::InputInt("##1", &Engine::AppSettings::selectedCellCoords[0], 10, 0, ImGuiInputTextFlags_CharsDecimal);
ImGui::Text("Y Coordinate:\t");
ImGui::SameLine();
ImGui::InputInt("##2", &Engine::AppSettings::selectedCellCoords[1], 10, 0, ImGuiInputTextFlags_CharsDecimal);
ImGui::Text("Z Coordinate:\t");
ImGui::SameLine();
ImGui::InputInt("##3", &Engine::AppSettings::selectedCellCoords[2], 10, 0, ImGuiInputTextFlags_CharsDecimal);

ImGui::PopItemWidth();
CheckCellBoundaries();

// Cell spawn amount
ImGui::Text("Amount:\t");
ImGui::SameLine();
ImGui::RadioButton("1", &Engine::AppSettings::selectedCellAmount, 1);
ImGui::SameLine();
ImGui::Text("\t");
ImGui::SameLine();
ImGui::RadioButton("9", &Engine::AppSettings::selectedCellAmount, 9);
ImGui::SameLine();
ImGui::Text("\t");
ImGui::SameLine();
ImGui::RadioButton("25", &Engine::AppSettings::selectedCellAmount, 25);
ImGui::SameLine();
ImGui::Text("\t");
ImGui::SameLine();
ImGui::RadioButton("49", &Engine::AppSettings::selectedCellAmount, 49);

// --- Spawn button
CenterCursor("Spawn");
if (ImGui::Button("Spawn"))
{
//ToDo: Implement cell spawn logic
}
ImGui::Separator();

//ToDo: Implement boundary checks
//ToDo: Add "delete all cells" Button

// --- Spawn button
//ToDo: Integrate in base class
ImGui::PopItemWidth();
auto windowWidth = ImGui::GetWindowSize().x;
auto textWidth = ImGui::CalcTextSize("Spawn").x;
//ToDo: Add "generate stone ground" Button

ImGui::SetCursorPosX((windowWidth - textWidth) * 0.5f);
if (ImGui::Button("Spawn"))
{
//ToDo: Implement spawn logic
}
ImGui::PopStyleVar();
ImGui::Separator();
}
ImGui::End();
ImGui::PopStyleVar();
}
}
ImGui::End();
}

// ----- Public -----

CellSimInterface::CellSimInterface()
: _windowFlags(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoMove),
_overlayPivot(ImVec2(1.0f, 0.0f)), _windowAlphaValue(0.5f), _menuBarHeight(25.0f), _sidebarWidth(415.0f), _showOverlay(true)
{
// --- Load custom font
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("../Res/Assets/Fonts/JetBrainsMono-Medium.ttf", 19);

// --- Set style
ImGui::StyleColorsDark();
SetDarkThemeColors();

// --- Calculate overlay parameters
CalculateSidebarDimensions();
}

void CellSimInterface::AddElements()
{
// --- Discard old plotting data every 120 frames
if(Engine::Window::GetFrameCounter() > 120)
{
ImGui::PlotVarFlushOldEntries();
}

// --- Remove all window borders
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);

// --- Menu bar
AddMenuBar();

// --- Overlay/Sidebar
if(_showOverlay)
{
AddSideBar();
}

ImGui::PopStyleVar();
}
}
10 changes: 6 additions & 4 deletions Apps/CellSim/src/CellSimInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace CS
class CellSimInterface final : public Engine::Interface
{
private:
ImVec2 _sidebarPos, _sidebarSize;
ImVec2 _overlayPivot = ImVec2(1.0f, 0.0f);
ImGuiWindowFlags _windowFlags;
float _windowAlphaValue;
ImVec2 _sidebarPos, _sidebarSize, _overlayPivot;
float _windowAlphaValue, _menuBarHeight, _sidebarWidth;
bool _showOverlay;

void SetOverlayParameters();
void CalculateSidebarDimensions();
void AddMenuBar();
void CheckCellBoundaries();
void AddSideBar();

public:
CellSimInterface();
Expand Down
Loading

0 comments on commit 51b928c

Please sign in to comment.