Skip to content

Commit

Permalink
Added number formatting function for UI.
Browse files Browse the repository at this point in the history
Added ImGui user function for using regular C API for formatting.
  • Loading branch information
MStachowicz committed Oct 8, 2024
1 parent 336f206 commit 3dbed31
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions source/External/ImGuiUser/imgui_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
// Add extra functions within the ImGui:: namespace here.
namespace ImGui
{
inline void Text_Manual(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
TextV(fmt, args);
va_end(args);
}
inline void Text(const char* p_label, const char* p_string)
{
Text("%s: [%s]", p_label, p_string);
Expand Down
40 changes: 40 additions & 0 deletions source/Utility/Utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <filesystem>
#include <functional>
#include <random>
#include <sstream>
#include <string>
#include <vector>

namespace Geometry
{
Expand Down Expand Up @@ -101,6 +103,44 @@ namespace Utility
return power;
}

//@param value The number to format.
//@param decimal_places The number of decimal places to display.
//@returns A string representation of the number with a metric prefix.
template <typename T>
std::string format_number(T value, uint8_t decimal_places = 3)
{
const std::pair<double, const char*> metric_prefixes[] = {
{1'000'000'000'000.0, "T"}, // Tera
{1'000'000'000.0, "G"}, // Giga
{1'000'000.0, "M"}, // Mega
{1'000.0, "k"}, // Kilo
{1.0, ""}, // No prefix for values between 1 and 999
{0.001, "m"}, // Milli
{0.000001, "µ"}, // Micro
{0.000000001, "n"} // Nano
};

// Iterate through the metric prefixes to find the appropriate scale
for (const auto& prefix : metric_prefixes)
{
if (value >= prefix.first || value <= -prefix.first)
{
double scaled_value = static_cast<double>(value) / prefix.first;

// Create a string stream to format the number
std::ostringstream out;
out << std::fixed << std::setprecision(decimal_places) << scaled_value << prefix.second;

return out.str();
}
}

// If the value is extremely small (even smaller than nano)
std::ostringstream out;
out << std::fixed << std::setprecision(decimal_places) << static_cast<double>(value);
return out.str();
}

// Construct a model matrix from position, rotation and scale
glm::mat4 make_model_matrix(const glm::vec3& p_position, const glm::vec3& p_rotation, const glm::vec3& p_scale);
// Converts a quaternion rotation into XYZ (Roll-Pitch-Yaw) Euler angles in radians.
Expand Down

0 comments on commit 3dbed31

Please sign in to comment.