Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pecholt <pecholt@gmail.com>
  • Loading branch information
tpecholt committed Jan 21, 2025
1 parent 34d3189 commit b07d970
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 305 deletions.
3 changes: 1 addition & 2 deletions src/binding_field.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "binding.h"
#include "node.h"
#include "binding_property.h"
#include "cppgen.h"

template <class T>
Expand Down
2 changes: 1 addition & 1 deletion src/binding_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <misc/cpp/imgui_stdlib.h>
#include "stx.h"
#include "utils.h"
#include "binding.h"
#include "binding_property.h"
#include "cppgen.h"
#include "ui_new_field.h"
#include "ui_binding.h"
Expand Down
79 changes: 1 addition & 78 deletions src/binding.cpp → src/binding_property.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
#include "binding.h"
#include "binding_property.h"
#include "cppgen.h"
#include <imgui.h>

std::string CodeShortcut(std::string_view sh)
{
if (sh.empty())
return "";
size_t i = -1;
std::string code;
while (true)
{
size_t j = sh.find_first_of("+-", i + 1);
if (j == std::string::npos)
j = sh.size();
std::string key(sh.substr(i + 1, j - i - 1));
if (key.empty() && j < sh.size()) //like in ctrl-+
key = sh[j];
stx::erase_if(key, [](char c) { return std::isspace(c); });
if (key.empty())
break;
std::string lk = key;
stx::for_each(lk, [](char& c) { c = std::tolower(c); });
//todo
if (lk == "ctrl")
code += "ImGuiMod_Ctrl";
else if (lk == "alt")
code += "ImGuiMod_Alt";
else if (lk == "shift")
code += "ImGuiMod_Shift";
else if (key == "+")
code += "ImGuiKey_Equal";
else if (key == "-")
code += "ImGuiKey_Minus";
else if (key == ".")
code += "ImGuiKey_Period";
else if (key == ",")
code += "ImGuiKey_Comma";
else if (key == "/")
code += "ImGuiKey_Slash";
else
code += std::string("ImGuiKey_")
+ (char)std::toupper(key[0])
+ key.substr(1);
code += " | ";
i = j;
if (j == sh.size())
break;
}
if (code.size())
code.resize(code.size() - 3);
return code;
}

std::string ParseShortcut(std::string_view line)
{
std::string sh;
size_t i = 0;
while (true)
{
size_t j1 = line.find("ImGuiKey_", i);
size_t j2 = line.find("ImGuiMod_", i);
if (j1 == std::string::npos && j2 == std::string::npos)
break;
if (j1 < j2)
i = j1 + 9;
else
i = j2 + 9;
for (; i < line.size(); ++i)
{
if (!isalnum(line[i]))
break;
sh += line[i];
}
sh += "+";
}
if (sh.size())
sh.pop_back();
return sh;
}

float direct_val<dimension>::eval_px(const UIContext& ctx) const
{
return val * ctx.zoomFactor;
Expand Down
139 changes: 1 addition & 138 deletions src/binding.h → src/binding_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,145 +8,8 @@
#include "stx.h"
#include "utils.h"
#include "cpp_parser.h"
#include "binding_type.h"

struct UIContext;

std::string CodeShortcut(std::string_view sh);
std::string ParseShortcut(std::string_view line);

//negative values allowed
struct dimension
{
float value;
dimension(float v = 0) : value(v) {}
dimension& operator= (float v) { value = v; return *this; }
operator float& () { return value; }
operator const float& () const { return value; }
};

//positive or zero dimension (so that neg values represent empty value)
struct pzdimension
{
float value;
pzdimension(float v = 0) : value(v) {}
pzdimension& operator= (float v) { value = v; return *this; }
operator float& () { return value; }
operator const float& () const { return value; }
};

struct pzdimension2
{
ImVec2 value;
pzdimension2(ImVec2 v = {}) : value(v) {}
pzdimension2& operator= (ImVec2 v) { value = v; return *this; }
operator ImVec2& () { return value; }
operator const ImVec2& () const { return value; }
float& operator[] (int i) { return value[i]; }
const float operator[] (int i) const { return value[i]; } //no & as in ImVec2
bool operator== (ImVec2 v) const { return value[0] == v[0] && value[1] == v[1]; }
bool operator!= (ImVec2 v) const { return !(*this == v); }
};

struct font_name
{
std::string value;
font_name(const std::string& v = {}) : value(v) {}
font_name& operator= (const std::string& v) { value = v; return *this; }
operator std::string& () { return value; }
operator const std::string& () const { return value; }
operator std::string_view() const { return value; }
/*friend std::ostream& operator<< (std::ostream& os, const font_name& fn)
{
return os << "\"" << fn.value << "\"";
}
friend std::istream& operator>> (std::istream& is, font_name& fn)
{
if (is.get() != '"') {
is.setstate(std::ios::failbit);
return is;
}
std::getline(is, fn.value);
if (value.empty() || fn.value.back() != '"') {
is.setstate(std::ios::failbit);
return is;
}
value.pop_back();
return is;
}*/
};

struct shortcut_
{
std::string value;
shortcut_(const std::string& v = {}) : value(v) {}
operator const std::string& () { return value; }
};

struct color32
{
color32(ImU32 c = 0) : c(c) {} //0 means style default color
color32(const ImVec4& v) : c(IM_COL32(255*v.x, 255*v.y, 255*v.z, 255*v.w)) {}
color32& operator= (ImU32 cc) { c = cc; return *this; }
operator ImU32 () const { return c; }
operator ImU32& () { return c; }
bool operator== (const color32& a) const { return c == a.c; }
bool operator!= (const color32& a) const { return c != a.c; }
std::string string() const {
std::ostringstream os;
os << *this;
return os.str();
}
friend std::ostream& operator<< (std::ostream& os, color32 clr)
{
if (!clr)
return os;
return os << "0x" << std::hex << std::setw(2*4)
<< std::setfill('0') << (ImU32)clr;
/*os.fill('0');
return os << "#" << std::hex
<< std::setw(2) << ((clr >> IM_COL32_R_SHIFT) & 0xff)
<< std::setw(2) << ((clr >> IM_COL32_G_SHIFT) & 0xff)
<< std::setw(2) << ((clr >> IM_COL32_B_SHIFT) & 0xff)
<< std::setw(2) << ((clr >> IM_COL32_A_SHIFT) & 0xff);*/
}
friend std::istream& operator>> (std::istream& is, color32& c)
{
if (is.peek() == EOF) {
c = color32();
return is;
}
if (is.get() != '0') {
//c = color32();
is.setstate(std::ios::failbit);
return is;
}
if (is.get() != 'x') {
//c = color32();
is.setstate(std::ios::failbit);
return is;
}
ImU32 cc = 0;
for (int i = 0; i < 8; ++i)
{
int v = is.get();
if (v >= 'a' && v <= 'f')
v = v - 'a' + 10;
else if (v >= 'A' && v <= 'F')
v = v - 'A' + 10;
else
v -= '0';
cc = (cc << 4) | v;
}
c = cc;
return is;
}


private:
ImU32 c;
};

//------------------------------------------------------------

template <class T>
struct two_step
Expand Down
Loading

0 comments on commit b07d970

Please sign in to comment.