Added const variables to stylesheet
This commit is contained in:
parent
da0d6e0c19
commit
c28829bd72
7 changed files with 59 additions and 27 deletions
3
.clangd
3
.clangd
|
|
@ -1,3 +1,2 @@
|
||||||
CompileFlags:
|
CompileFlags:
|
||||||
CompilationDatabase: bin
|
CompilationDatabase: bin
|
||||||
Add: ["--target=x86_64-w64-mingw32"]
|
|
||||||
|
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
% ---- Variables ----
|
% ---- Variables ----
|
||||||
$accentColor = Color(rgba(255, 0, 255, 255))
|
$accentColor = Color(rgba(255, 0, 255, 255))
|
||||||
$color2 = Color(#0000FFFF)
|
const $color2 = Color(#0000FFFF)
|
||||||
% -------------------
|
% -------------------
|
||||||
|
|
||||||
window.backgroundColor = Color(rgba(120, 120, 120, 255))
|
window.backgroundColor = Color(rgba(120, 120, 120, 255))
|
||||||
|
|
||||||
|
$accentColor = Color(rgb(255, 0, 0))
|
||||||
|
|
||||||
(label) {
|
(label) {
|
||||||
textColor = $accentColor
|
textColor = $accentColor
|
||||||
backgroundColor = Color(#000000FF)
|
backgroundColor = Color(#000000FF)
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
2052
|
2053
|
||||||
|
|
|
||||||
|
|
@ -41,54 +41,66 @@ namespace ostd
|
||||||
return loadFromString(outContent, filePath, clearCurrentRules);
|
return loadFromString(outContent, filePath, clearCurrentRules);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stylesheet& Stylesheet::loadFromString(const ostd::String& content, const ostd::String& filePath, bool clearCurrentRules)
|
Stylesheet& Stylesheet::loadFromString(const ostd::String& content, const ostd::String& filePath, bool clearCurrentRules, std::unordered_map<ostd::String, std::pair<ostd::String, bool>> variables)
|
||||||
{
|
{
|
||||||
if (clearCurrentRules)
|
if (clearCurrentRules)
|
||||||
m_values.clear();
|
m_values.clear();
|
||||||
std::vector<ostd::String> lines = content.tokenize("\n", false, true).getRawData();
|
std::vector<ostd::String> lines = content.tokenize("\n", false, true).getRawData();
|
||||||
|
std::vector<ostd::String> originalLines = lines;
|
||||||
uint32_t lineNumber = 0;
|
uint32_t lineNumber = 0;
|
||||||
ostd::String lineCopy = "";
|
|
||||||
std::unordered_map<ostd::String, ostd::String> variables;
|
|
||||||
auto l_warn = [&](const ostd::String& msg) -> void {
|
auto l_warn = [&](const ostd::String& msg) -> void {
|
||||||
OX_WARN("%s in theme line. File: <%s:%d>:\n\t%s", msg.c_str(), filePath.c_str(), lineNumber, lineCopy.c_str());
|
OX_WARN("%s in theme line. File: <%s:%d>", msg.c_str(), filePath.c_str(), lineNumber, originalLines[lineNumber - 1].c_str());
|
||||||
};
|
};
|
||||||
auto l_parseLine = [&](ostd::String& line) -> void {
|
auto l_parseLine = [&](ostd::String& line) -> void {
|
||||||
for (const auto&[var, val] : variables)
|
for (const auto&[var, val] : variables)
|
||||||
line.replaceAll(var, val);
|
line.replaceAll(var, val.first);
|
||||||
if (!parseThemeFileLine(line))
|
if (!parseThemeFileLine(line))
|
||||||
l_warn("Error");
|
l_warn("Error");
|
||||||
};
|
};
|
||||||
|
uint8_t lineNumberMaxWidth = ostd::String("").add(lines.size()).len();
|
||||||
ostd::String groupSelector = "";
|
ostd::String groupSelector = "";
|
||||||
bool groupLines = false;
|
bool groupLines = true;
|
||||||
std::vector<ostd::String> group;
|
std::vector<ostd::String> group;
|
||||||
|
bool debug_print = true;
|
||||||
for (auto& line : lines)
|
for (auto& line : lines)
|
||||||
{
|
{
|
||||||
lineNumber++;
|
lineNumber++;
|
||||||
lineCopy = line;
|
|
||||||
line.trim();
|
line.trim();
|
||||||
if (line.startsWith("%"))
|
if (line.startsWith("%"))
|
||||||
continue;
|
goto custom_continue;
|
||||||
if (line.contains("%"))
|
if (line.contains("%"))
|
||||||
line.substr(0, line.indexOf("%")).trim();
|
line.substr(0, line.indexOf("%")).trim();
|
||||||
if (line == "")
|
if (line == "")
|
||||||
continue;
|
goto custom_continue;
|
||||||
if (line.startsWith("$"))
|
if (line.startsWith("const ") || line.startsWith("$"))
|
||||||
{
|
{
|
||||||
|
bool is_const = false;
|
||||||
|
if (line.startsWith("const "))
|
||||||
|
{
|
||||||
|
line.substr(6).trim();
|
||||||
|
is_const = true;
|
||||||
|
}
|
||||||
line.substr(1);
|
line.substr(1);
|
||||||
if (line.count("=") != 1 || line.endsWith("="))
|
if (line.count("=") != 1 || line.endsWith("="))
|
||||||
{
|
{
|
||||||
l_warn("Invalid variable");
|
l_warn("Invalid variable");
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
ostd::String varName = line.new_substr(0, line.indexOf("=")).trim();
|
ostd::String varName = line.new_substr(0, line.indexOf("=")).trim();
|
||||||
ostd::String varValue = line.new_substr(line.indexOf("=") + 1).trim();
|
ostd::String varValue = line.new_substr(line.indexOf("=") + 1).trim();
|
||||||
if (!varName.regexMatches(m_validNameRegex))
|
if (!varName.regexMatches(m_validNameRegex))
|
||||||
{
|
{
|
||||||
l_warn("Invalid variable name");
|
l_warn("Invalid variable name");
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
variables["$" + varName] = varValue;
|
auto var = variables.find("$" + varName);
|
||||||
continue;
|
if (var != variables.end() && var->second.second)
|
||||||
|
{
|
||||||
|
l_warn("Trying to re-assign a const variable");
|
||||||
|
goto custom_continue;
|
||||||
|
}
|
||||||
|
variables["$" + varName] = { varValue, is_const };
|
||||||
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
if (groupSelector != "")
|
if (groupSelector != "")
|
||||||
{
|
{
|
||||||
|
|
@ -98,11 +110,16 @@ namespace ostd
|
||||||
{
|
{
|
||||||
groupLines = false;
|
groupLines = false;
|
||||||
auto newLines = parseGroup(groupSelector, group);
|
auto newLines = parseGroup(groupSelector, group);
|
||||||
for (auto& l : newLines)
|
lineNumber -= newLines.size();
|
||||||
|
for (int32_t i = 0; i < newLines.size(); i++)
|
||||||
|
{
|
||||||
|
auto& l = newLines[i];
|
||||||
l_parseLine(l);
|
l_parseLine(l);
|
||||||
|
lineNumber++;
|
||||||
|
}
|
||||||
groupSelector = "";
|
groupSelector = "";
|
||||||
group.clear();
|
group.clear();
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
group.push_back(line);
|
group.push_back(line);
|
||||||
}
|
}
|
||||||
|
|
@ -111,30 +128,39 @@ namespace ostd
|
||||||
if (line == "{")
|
if (line == "{")
|
||||||
groupLines = true;
|
groupLines = true;
|
||||||
}
|
}
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
if (line.startsWith("("))
|
if (line.startsWith("("))
|
||||||
{
|
{
|
||||||
if (line.count(")") != 1 || line.indexOf(")") < 3)
|
if (line.count(")") != 1 || line.indexOf(")") < 3)
|
||||||
{
|
{
|
||||||
l_warn("Invalid group selector");
|
l_warn("Invalid group selector");
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
ostd::String rawSelector = line.new_substr(1, line.indexOf(")")).trim();
|
ostd::String rawSelector = line.new_substr(1, line.indexOf(")")).trim();
|
||||||
groupSelector = parseGroupSelector(rawSelector);
|
groupSelector = parseGroupSelector(rawSelector);
|
||||||
|
if (groupSelector == "")
|
||||||
|
{
|
||||||
|
l_warn("Invalid group selector");
|
||||||
|
goto custom_continue;
|
||||||
|
}
|
||||||
if (line.contains("{"))
|
if (line.contains("{"))
|
||||||
{
|
{
|
||||||
line.substr(line.indexOf("{")).trim();
|
line.substr(line.indexOf("{")).trim();
|
||||||
if (line != "{")
|
if (line != "{")
|
||||||
{
|
{
|
||||||
l_warn("Invalid group selector");
|
l_warn("Invalid group selector");
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
groupLines = true;
|
groupLines = true;
|
||||||
}
|
}
|
||||||
continue;
|
goto custom_continue;
|
||||||
}
|
}
|
||||||
l_parseLine(line);
|
l_parseLine(line);
|
||||||
|
continue;
|
||||||
|
custom_continue:
|
||||||
|
if (debug_print)
|
||||||
|
std::cout << ostd::String("").add(lineNumber).addLeftPadding(lineNumberMaxWidth, ' ') << "| " << originalLines[lineNumber - 1] << "\n";
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ namespace ostd
|
||||||
Stylesheet(void);
|
Stylesheet(void);
|
||||||
Stylesheet& clear(void);
|
Stylesheet& clear(void);
|
||||||
Stylesheet& loadFromFile(const ostd::String& filePath, bool clearCurrentRules = true);
|
Stylesheet& loadFromFile(const ostd::String& filePath, bool clearCurrentRules = true);
|
||||||
Stylesheet& loadFromString(const ostd::String& content, const ostd::String& filePath = "memory://", bool clearCurrentRules = true);
|
Stylesheet& loadFromString(const ostd::String& content, const ostd::String& filePath = "memory://", bool clearCurrentRules = true, std::unordered_map<ostd::String, std::pair<ostd::String, bool>> variables = {});
|
||||||
void set(const std::string& key, TypeVariant value, const ostd::String& themeID);
|
void set(const std::string& key, TypeVariant value, const ostd::String& themeID);
|
||||||
void removeRule(const ostd::String& fullKey);
|
void removeRule(const ostd::String& fullKey);
|
||||||
void setFull(const ostd::String& fullKey, TypeVariant value);
|
void setFull(const ostd::String& fullKey, TypeVariant value);
|
||||||
|
|
|
||||||
|
|
@ -584,9 +584,9 @@ namespace ostd
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (allow_white_space_only_tokens && __token != "")
|
if (allow_white_space_only_tokens && __token == "")
|
||||||
tokens.m_tokens.push_back(__token.cpp_str());
|
tokens.m_tokens.push_back(__token.cpp_str());
|
||||||
else if (!allow_white_space_only_tokens && __token.new_trim() != "")
|
else if (__token.new_trim() != "")
|
||||||
tokens.m_tokens.push_back(__token.cpp_str());
|
tokens.m_tokens.push_back(__token.cpp_str());
|
||||||
}
|
}
|
||||||
sindex = eindex + delimiter.len();
|
sindex = eindex + delimiter.len();
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,11 @@ class Window : public ogfx::gui::Window
|
||||||
if (signal.ID == ostd::BuiltinSignals::FileDragAndDropped)
|
if (signal.ID == ostd::BuiltinSignals::FileDragAndDropped)
|
||||||
{
|
{
|
||||||
auto& data = (ogfx::DropEventData&)signal.userData;
|
auto& data = (ogfx::DropEventData&)signal.userData;
|
||||||
|
if (ostd::FileSystem::fileExists(data.textOrFilePath))
|
||||||
|
{
|
||||||
|
m_theme.loadFromFile(data.textOrFilePath);
|
||||||
|
m_rootWidget.reloadTheme();
|
||||||
|
}
|
||||||
std::cout << data.textOrFilePath << "\n";
|
std::cout << data.textOrFilePath << "\n";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue