locale independent util::parse_double

This commit is contained in:
MihailRis 2024-02-02 20:02:30 +03:00
parent 84e8fd1af6
commit bc72742ee3
20 changed files with 140 additions and 48 deletions

View File

@ -4,19 +4,6 @@
#include <stdexcept>
#include <math.h>
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;
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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;

10
src/delegates.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef DELEGATES_H_
#define DELEGATES_H_
#include <functional>
#include <string>
typedef std::function<void()> runnable;
typedef std::function<void(const std::string&)> stringconsumer;
#endif // DELEGATES_H_

View File

@ -44,9 +44,6 @@ class Camera;
*/
namespace gui {
typedef std::function<void()> runnable;
typedef std::function<void(const std::string&)> stringconsumer;
class UINode;
class Container;
class PagesControl;

View File

@ -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 {

View File

@ -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<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> 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;

View File

@ -255,7 +255,7 @@ bool TextBox::isValid() const {
return valid;
}
void TextBox::setOnEditStart(gui::runnable oneditstart) {
void TextBox::setOnEditStart(runnable oneditstart) {
onEditStart = oneditstart;
}

View File

@ -6,10 +6,12 @@
#include <vector>
#include <functional>
#include <glm/glm.hpp>
#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<onaction> actions;
std::shared_ptr<Label> label = nullptr;
public:
Button(std::shared_ptr<UINode> content, glm::vec4 padding=glm::vec4(2.0f));
Button(std::shared_ptr<UINode> content,
glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text,
glm::vec4 padding,
onaction action);

View File

@ -5,6 +5,7 @@
#include <glm/glm.hpp>
#include "../locale/langs.h"
#include "../../delegates.h"
using namespace gui;
using glm::vec2;
@ -29,7 +30,7 @@ std::shared_ptr<Button> guiutil::gotoButton(
});
}
void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) {
void guiutil::alert(GUI* gui, const std::wstring& text, runnable on_hidden) {
auto menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f);
panel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
@ -69,7 +70,7 @@ void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden)
void guiutil::confirm(
GUI* gui,
const std::wstring& text,
gui::runnable on_confirm,
runnable on_confirm,
std::wstring yestext,
std::wstring notext) {
if (yestext.empty()) yestext = langs::get(L"Yes");

View File

@ -4,6 +4,7 @@
#include <memory>
#include <string>
#include "GUI.h"
#include "../../delegates.h"
namespace gui {
class Button;
@ -23,13 +24,13 @@ namespace guiutil {
void alert(
gui::GUI* gui,
const std::wstring& text,
gui::runnable on_hidden=nullptr
runnable on_hidden=nullptr
);
void confirm(
gui::GUI* gui,
const std::wstring& text,
gui::runnable on_confirm=nullptr,
runnable on_confirm=nullptr,
std::wstring yestext=L"",
std::wstring notext=L"");
}

View File

@ -14,6 +14,7 @@ using glm::vec4;
Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
actualLength = size.y;
setColor(glm::vec4());
}
std::shared_ptr<UINode> Container::getAt(vec2 pos, std::shared_ptr<UINode> self) {
@ -96,6 +97,17 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
}
}
void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
if (color.a <= 0.0f)
return;
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = color;
batch->rect(coord.x, coord.y, size.x, size.y);
}
void Container::addBack(std::shared_ptr<UINode> node) {
nodes.insert(nodes.begin(), node);
node->setParent(this);
@ -138,15 +150,6 @@ Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
Panel::~Panel() {
}
void Panel::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = color;
batch->rect(coord.x, coord.y, size.x, size.y);
}
void Panel::maxLength(int value) {
maxLength_ = value;
}
@ -155,6 +158,14 @@ int Panel::maxLength() const {
return maxLength_;
}
void Panel::setPadding(glm::vec4 padding) {
this->padding = padding;
}
glm::vec4 Panel::getPadding() const {
return padding;
}
void Panel::refresh() {
float x = padding.x;
float y = padding.y;
@ -182,7 +193,7 @@ void Panel::refresh() {
y += nodesize.y + margin.w + interval;
float width = size.x - padding.x - padding.z - margin.x - margin.z;
node->setSize(vec2(width, nodesize.y));;
//node->setSize(vec2(width, nodesize.y));;
node->refresh();
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
}
@ -203,7 +214,7 @@ void Panel::refresh() {
x += nodesize.x + margin.z + interval;
float height = size.y - padding.y - padding.w - margin.y - margin.w;
node->setSize(vec2(nodesize.x, height));
//node->setSize(vec2(nodesize.x, height));
node->refresh();
maxh = fmax(maxh, y+margin.y+node->getSize().y+margin.w+padding.w);
}

View File

@ -34,7 +34,7 @@ namespace gui {
Container(glm::vec2 coord, glm::vec2 size);
virtual void act(float delta) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) {};
virtual void drawBackground(const GfxContext* pctx, Assets* assets);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void addBack(std::shared_ptr<UINode> node);
@ -55,11 +55,14 @@ namespace gui {
bool resizing_;
int maxLength_ = 0;
public:
Panel(glm::vec2 size, glm::vec4 padding=glm::vec4(2.0f), float interval=2.0f, bool resizing=true);
Panel(
glm::vec2 size,
glm::vec4 padding=glm::vec4(2.0f),
float interval=2.0f,
bool resizing=true
);
virtual ~Panel();
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void orientation(Orientation orientation);
Orientation orientation() const;
@ -67,6 +70,9 @@ namespace gui {
virtual void maxLength(int value);
int maxLength() const;
virtual void setPadding(glm::vec4 padding);
glm::vec4 getPadding() const;
};
struct Page {

View File

@ -584,7 +584,7 @@ void create_settings_panel(Engine* engine) {
langs::get(L"Backlight", L"settings"), vec2(400, 32)
);
checkbox->supplier([=]() {
return engine->getSettings().graphics.backlight != 0;
return engine->getSettings().graphics.backlight;
});
checkbox->consumer([=](bool checked) {
engine->getSettings().graphics.backlight = checked;

View File

@ -19,7 +19,7 @@ Texture::Texture(ubyte* data, int width, int height, uint format)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -101,6 +101,20 @@ void scripting::initialize(Engine* engine) {
load_script(fs::path("stdlib.lua"));
}
// todo: luaL state check
runnable scripting::create_runnable(
const std::string& filename,
const std::string& src
) {
return [=](){
if (luaL_loadbuffer(L, src.c_str(), src.length(), filename.c_str())) {
handleError(L);
return;
}
call_func(L, 0, filename);
};
}
void scripting::on_world_load(Level* level, BlocksController* blocks) {
scripting::level = level;
scripting::content = level->content;

View File

@ -1,6 +1,8 @@
#include <string>
#include <filesystem>
#include "../../delegates.h"
namespace fs = std::filesystem;
class Engine;
@ -20,6 +22,12 @@ namespace scripting {
extern BlocksController* blocks;
void initialize(Engine* engine);
runnable create_runnable(
const std::string& filename,
const std::string& source
);
void on_world_load(Level* level, BlocksController* blocks);
void on_world_save();
void on_world_quit();

View File

@ -267,3 +267,15 @@ int util::replaceAll(std::string& str, const std::string& from, const std::strin
}
return count;
}
// replace it with std::from_chars in the far far future
double util::parse_double(const std::string& str) {
std::istringstream ss(str);
ss.imbue(std::locale("C"));
double d;
ss >> d;
if (ss.fail()) {
throw std::runtime_error("invalid number format");
}
return d;
}

View File

@ -28,6 +28,8 @@ namespace util {
extern std::vector<ubyte> base64_decode(const std::string& str);
extern int replaceAll(std::string& str, const std::string& from, const std::string& to);
extern double parse_double(const std::string& str);
}
#endif // UTIL_STRINGUTIL_H_