Files
o3de/Gems/ImGui/Code/Source/LYImGuiUtils/HistogramContainer.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

163 lines
6.9 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include "LYImGuiUtils/HistogramContainer.h"
#ifdef IMGUI_ENABLED
#include "ImGuiColorDefines.h"
namespace ImGui
{
namespace LYImGuiUtils
{
void HistogramContainer::Init(const char* histogramName, int maxValueCountSize, ViewType viewType, bool displayOverlays, float minScale, float maxScale
, bool autoExpandScale, bool startCollapsed/* = false*/, bool drawMostRecentValue/* = true*/)
{
m_histogramName = histogramName;
m_minScale = minScale;
m_maxScale = maxScale;
m_viewType = viewType;
m_dispalyOverlays = displayOverlays;
m_autoExpandScale = autoExpandScale;
m_collapsed = startCollapsed;
m_drawMostRecentValueText = drawMostRecentValue;
SetMaxSize(maxValueCountSize);
}
void HistogramContainer::PushValue(float val)
{
if (m_maxSize == 0)
{
return;
}
if (m_values.size() == m_maxSize)
{
m_values.pop_back();
}
else if (m_values.size() > m_maxSize)
{
m_values.erase(m_values.begin() + (m_maxSize - 1), m_values.end());
}
m_values.push_front(val);
if (m_autoExpandScale)
{
if (val < m_minScale)
{
m_minScale = val;
}
else if (val > m_maxScale)
{
m_maxScale = val;
}
}
}
static auto s_viewTypeToStringLambda = []([[maybe_unused]] void* histogramPtr, int idx, const char** out_text)
{
*out_text = HistogramContainer::ViewTypeToString(static_cast<HistogramContainer::ViewType>(idx));
return true;
};
void HistogramContainer::Draw(float histogramWidth, float histogramHeight)
{
// If we are collapsed. Force a much smaller height
if (m_collapsed)
{
histogramHeight = 24.0f;
}
// Begin Child area
ImGui::BeginChild(AZStd::string::format("##%s_child", m_histogramName.c_str()).c_str(), ImVec2(histogramWidth, histogramHeight), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
// Right Click Options menu
if (ImGui::BeginPopupContextItem(AZStd::string::format("histoContextMenu##%s", m_histogramName.c_str()).c_str(), 1))
{
ImGui::TextColored(ImGui_Col_White, "'%s' Histogram Options", m_histogramName.c_str());
ImGui::Combo("View Type", reinterpret_cast<int*>(&m_viewType), s_viewTypeToStringLambda, this, static_cast<int>(ViewType::COUNT));
ImGui::Checkbox("Show Overlays", &m_dispalyOverlays);
ImGui::Checkbox("Show Most Recent Value (requires overlays)", &m_drawMostRecentValueText);
ImGui::DragInt("History Size", &m_maxSize, 1, 1, 1000, "%f");
ImGui::DragFloat("Max Scale", &m_maxScale, 0.0001f, -100.0f, 100.0f);
ImGui::DragFloat("Min Scale", &m_minScale, 0.0001f, -100.0f, 100.0f);
ImGui::Checkbox("Auto Expand Scale", &m_autoExpandScale);
ImGui::EndPopup();
}
// Toggle collapsing when double clicking this "window"
if (ImGui::IsMouseDoubleClicked(0) && ImGui::IsWindowHovered())
{
m_collapsed = !m_collapsed;
}
// Plot the actual Imgui Histogram/Lines widgets.. Plotting size zero can crash ImGui!
float imGuiHistoWidgetHeight = m_collapsed ? histogramHeight : (histogramHeight - 15);
if (GetSize() > 0)
{
switch (m_viewType)
{
default:
break;
case ViewType::Histogram:
ImGui::PlotHistogram(AZStd::string::format("##%s_histo", m_histogramName.c_str()).c_str(), ImGui::LYImGuiUtils::s_histogramContainerGetter, this, GetSize(), 0, m_histogramName.c_str(), m_minScale, m_maxScale, ImVec2(histogramWidth - 10, imGuiHistoWidgetHeight));
break;
case ViewType::Lines:
ImGui::PlotLines(AZStd::string::format("##%s_lines", m_histogramName.c_str()).c_str(), ImGui::LYImGuiUtils::s_histogramContainerGetter, this, GetSize(), 0, m_histogramName.c_str(), m_minScale, m_maxScale, ImVec2(histogramWidth - 10, imGuiHistoWidgetHeight));
break;
}
}
// Display optional overlays to help with scale
if (m_dispalyOverlays && !m_collapsed)
{
AZStd::string labelText = AZStd::string::format("% .03f", m_maxScale);
ImGui::GetWindowDrawList()->AddText(ImVec2(ImGui::GetWindowPos().x + 12.0f, ImGui::GetWindowPos().y + 10.0f), ImGui::ColorConvertFloat4ToU32(ImGui_Col_White), labelText.c_str());
float midPointY = ImGui::GetWindowPos().y + histogramHeight * 0.5f + 1.0f;
labelText = AZStd::string::format("% .03f", (m_maxScale - m_minScale) * 0.5f + m_minScale);
ImGui::GetWindowDrawList()->AddText(ImVec2(ImGui::GetWindowPos().x + 12.0f, midPointY - 6.0f), ImGui::ColorConvertFloat4ToU32(ImGui_Col_White), labelText.c_str());
labelText = AZStd::string::format("% .03f", m_minScale);
ImGui::GetWindowDrawList()->AddText(ImVec2(ImGui::GetWindowPos().x + 12.0f, ImGui::GetWindowPos().y + histogramHeight - 22.0f), ImGui::ColorConvertFloat4ToU32(ImGui_Col_White), labelText.c_str());
ImGui::GetWindowDrawList()->AddLine(ImVec2(ImGui::GetWindowPos().x + 50.0f, midPointY), ImVec2(ImGui::GetWindowPos().x + histogramWidth - 7.0f, midPointY), ImGui_Col_White, 0.5f);
if (m_drawMostRecentValueText && !m_values.empty())
{
labelText = AZStd::string::format("% .03f", m_values.front());
ImGui::GetWindowDrawList()->AddText(ImVec2(ImGui::GetWindowPos().x + 12.0f, midPointY - 18.0f), ImGui::ColorConvertFloat4ToU32(ImGui_Col_DarkBlue), labelText.c_str());
}
}
// End Child Area
ImGui::EndChild();
}
const char* HistogramContainer::ViewTypeToString(ViewType viewType)
{
switch (viewType)
{
default:
return "*Unimplemented";
case ViewType::Histogram:
return "Histogram";
case ViewType::Lines:
return "Lines";
}
}
}
}
#endif // #ifdef IMGUI_ENABLED