From bc72742ee33afa224fbf35a0e786a394baa6e0c2 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 2 Feb 2024 20:02:30 +0300 Subject: [PATCH] locale independent util::parse_double --- src/coders/commons.cpp | 17 ++-------------- src/coders/commons.h | 13 ++++++++++++ src/coders/xml.cpp | 9 ++------- src/coders/xml.h | 1 + src/delegates.h | 10 ++++++++++ src/frontend/gui/GUI.h | 3 --- src/frontend/gui/UINode.cpp | 9 +++++++++ src/frontend/gui/UINode.h | 21 ++++++++++++++++++++ src/frontend/gui/controls.cpp | 2 +- src/frontend/gui/controls.h | 6 +++++- src/frontend/gui/gui_util.cpp | 5 +++-- src/frontend/gui/gui_util.h | 5 +++-- src/frontend/gui/panels.cpp | 33 ++++++++++++++++++++----------- src/frontend/gui/panels.h | 14 +++++++++---- src/frontend/menu.cpp | 2 +- src/graphics/Texture.cpp | 2 +- src/logic/scripting/scripting.cpp | 14 +++++++++++++ src/logic/scripting/scripting.h | 8 ++++++++ src/util/stringutil.cpp | 12 +++++++++++ src/util/stringutil.h | 2 ++ 20 files changed, 140 insertions(+), 48 deletions(-) create mode 100644 src/delegates.h diff --git a/src/coders/commons.cpp b/src/coders/commons.cpp index 422159ba..ad4a8677 100644 --- a/src/coders/commons.cpp +++ b/src/coders/commons.cpp @@ -4,19 +4,6 @@ #include #include -inline int char2int(int c) { - if (c >= '0' && c <= '9') { - return c - '0'; - } - if (c >= 'a' && c <= 'f') { - return 10 + c - 'a'; - } - if (c >= 'A' && c <= 'F') { - return 10 + c - 'A'; - } - return -1; -} - inline double power(double base, int64_t power) { double result = 1.0; for (int64_t i = 0; i < power; i++) { @@ -216,7 +203,7 @@ std::string BasicParser::parseName() { int64_t BasicParser::parseSimpleInt(int base) { char c = peek(); - int index = char2int(c); + int index = hexchar2int(c); if (index == -1 || index >= base) { throw error("invalid number literal"); } @@ -227,7 +214,7 @@ int64_t BasicParser::parseSimpleInt(int base) { while (c == '_') { c = source[++pos]; } - index = char2int(c); + index = hexchar2int(c); if (index == -1 || index >= base) { return value; } diff --git a/src/coders/commons.h b/src/coders/commons.h index 431f247d..509ff0d6 100644 --- a/src/coders/commons.h +++ b/src/coders/commons.h @@ -41,6 +41,19 @@ inline bool is_identifier_part(int c) { return is_identifier_start(c) || is_digit(c); } +inline int hexchar2int(int c) { + if (c >= '0' && c <= '9') { + return c - '0'; + } + if (c >= 'a' && c <= 'f') { + return 10 + c - 'a'; + } + if (c >= 'A' && c <= 'F') { + return 10 + c - 'A'; + } + return -1; +} + extern std::string escape_string(std::string s); class parsing_error : public std::runtime_error { diff --git a/src/coders/xml.cpp b/src/coders/xml.cpp index 8613ceee..aeb08dd1 100644 --- a/src/coders/xml.cpp +++ b/src/coders/xml.cpp @@ -25,12 +25,7 @@ int64_t Attribute::asInt() const { } double Attribute::asFloat() const { - double value; - auto res = std::from_chars(text.data(), text.data()+text.size(), value); - if (res.ptr != text.data()+text.size()) { - throw std::runtime_error("invalid number format "+escape_string(text)); - } - return value; + return util::parse_double(text); } bool Attribute::asBool() const { @@ -45,7 +40,7 @@ void Node::add(xmlelement element) { } void Node::set(std::string name, std::string text) { - attrs.insert_or_assign(name, Attribute(name, text)); + attrs[name] = Attribute(name, text); } const std::string& Node::getTag() const { diff --git a/src/coders/xml.h b/src/coders/xml.h index c198889c..275bd279 100644 --- a/src/coders/xml.h +++ b/src/coders/xml.h @@ -22,6 +22,7 @@ namespace xml { std::string name; std::string text; public: + Attribute() {}; Attribute(std::string name, std::string text); const std::string& getName() const; diff --git a/src/delegates.h b/src/delegates.h new file mode 100644 index 00000000..06f0d3a5 --- /dev/null +++ b/src/delegates.h @@ -0,0 +1,10 @@ +#ifndef DELEGATES_H_ +#define DELEGATES_H_ + +#include +#include + +typedef std::function runnable; +typedef std::function stringconsumer; + +#endif // DELEGATES_H_ diff --git a/src/frontend/gui/GUI.h b/src/frontend/gui/GUI.h index 4cd272ef..6ff90b4a 100644 --- a/src/frontend/gui/GUI.h +++ b/src/frontend/gui/GUI.h @@ -44,9 +44,6 @@ class Camera; */ namespace gui { - typedef std::function runnable; - typedef std::function stringconsumer; - class UINode; class Container; class PagesControl; diff --git a/src/frontend/gui/UINode.cpp b/src/frontend/gui/UINode.cpp index d87e1c9e..ff4e16b6 100644 --- a/src/frontend/gui/UINode.cpp +++ b/src/frontend/gui/UINode.cpp @@ -115,6 +115,15 @@ void UINode::setSize(vec2 size) { void UINode::setColor(vec4 color) { this->color = color; + this->hoverColor = color; +} + +void UINode::setHoverColor(glm::vec4 newColor) { + this->hoverColor = newColor; +} + +glm::vec4 UINode::getHoverColor() const { + return hoverColor; } vec4 UINode::getColor() const { diff --git a/src/frontend/gui/UINode.h b/src/frontend/gui/UINode.h index 56859cd7..487deae1 100644 --- a/src/frontend/gui/UINode.h +++ b/src/frontend/gui/UINode.h @@ -24,6 +24,7 @@ namespace gui { glm::vec2 coord; glm::vec2 size; glm::vec4 color {1.0f}; + glm::vec4 hoverColor {1.0f}; glm::vec4 margin {1.0f}; bool visible = true; bool hover = false; @@ -50,9 +51,16 @@ namespace gui { virtual void setParent(UINode* node); UINode* getParent() const; + /* Set element color (doesn't affect inner elements). + Also replaces hover color to avoid adding extra properties. */ virtual void setColor(glm::vec4 newColor); + + /* Get element color */ glm::vec4 getColor() const; + virtual void setHoverColor(glm::vec4 newColor); + glm::vec4 getHoverColor() const; + virtual void setMargin(glm::vec4 margin); glm::vec4 getMargin() const; @@ -66,18 +74,31 @@ namespace gui { bool isPressed() const; void defocus(); bool isFocused() const; + + /* Check if elements catches all user input when focused */ virtual bool isFocuskeeper() const {return false;} virtual void typed(unsigned int codepoint) {}; virtual void keyPressed(int key) {}; + /* Check if screen position is inside of the element + @param pos screen position */ virtual bool isInside(glm::vec2 pos); + + /* Get element under the cursor. + @param pos cursor screen position + @param self shared pointer to element + @return self, sub-element or nullptr if element is not interractive */ virtual std::shared_ptr getAt(glm::vec2 pos, std::shared_ptr self); + /* Check if element is opaque for cursor */ virtual bool isInteractive() const; + /* Make the element opaque (true) or transparent (false) for cursor */ virtual void setInteractive(bool flag); + /* Get inner content offset. Used for scroll */ virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);}; + /* Calculate screen position of the element */ virtual glm::vec2 calcCoord() const; virtual void setCoord(glm::vec2 coord); virtual glm::vec2 getSize() const; diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 0d23ea2e..ab72d2c4 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -255,7 +255,7 @@ bool TextBox::isValid() const { return valid; } -void TextBox::setOnEditStart(gui::runnable oneditstart) { +void TextBox::setOnEditStart(runnable oneditstart) { onEditStart = oneditstart; } diff --git a/src/frontend/gui/controls.h b/src/frontend/gui/controls.h index f5c550b8..4bd40f68 100644 --- a/src/frontend/gui/controls.h +++ b/src/frontend/gui/controls.h @@ -6,10 +6,12 @@ #include #include #include + #include "GUI.h" #include "UINode.h" #include "panels.h" #include "../../window/input.h" +#include "../../delegates.h" class Batch2D; class Assets; @@ -60,7 +62,9 @@ namespace gui { std::vector actions; std::shared_ptr