Merge branch 'main' into update-gfx-pipeline
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "assetload_funcs.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
@@ -352,21 +353,35 @@ assetload::postfunc assetload::model(
|
||||
throw;
|
||||
}
|
||||
}
|
||||
path = paths.find(file + ".xml");
|
||||
if (io::exists(path)) {
|
||||
auto text = io::read_string(path);
|
||||
try {
|
||||
auto model = vcm::parse(path.string(), text).release();
|
||||
return [=](Assets* assets) {
|
||||
request_textures(loader, *model);
|
||||
assets->store(std::unique_ptr<model::Model>(model), name);
|
||||
};
|
||||
} catch (const parsing_error& err) {
|
||||
std::cerr << err.errorLog() << std::endl;
|
||||
throw;
|
||||
|
||||
std::array<std::string, 2> extensions {
|
||||
".xml",
|
||||
".vcm"
|
||||
};
|
||||
|
||||
path = "";
|
||||
for (const auto& ext : extensions) {
|
||||
auto newPath = paths.find(file + ext);
|
||||
if (io::exists(newPath)) {
|
||||
path = std::move(newPath);
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("could not to find model " + util::quote(file));
|
||||
if (path.empty()) {
|
||||
throw std::runtime_error("could not to find model " + util::quote(file));
|
||||
}
|
||||
|
||||
auto text = io::read_string(path);
|
||||
try {
|
||||
auto model = vcm::parse(path.string(), text).release();
|
||||
return [=](Assets* assets) {
|
||||
request_textures(loader, *model);
|
||||
assets->store(std::unique_ptr<model::Model>(model), name);
|
||||
};
|
||||
} catch (const parsing_error& err) {
|
||||
std::cerr << err.errorLog() << std::endl;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
static void read_anim_file(
|
||||
|
||||
+14
-7
@@ -5,6 +5,7 @@
|
||||
#include "xml.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "io/io.hpp"
|
||||
|
||||
using namespace vcm;
|
||||
using namespace xml;
|
||||
@@ -143,12 +144,18 @@ static std::unique_ptr<model::Model> load_model(const xmlelement& root) {
|
||||
std::unique_ptr<model::Model> vcm::parse(
|
||||
std::string_view file, std::string_view src
|
||||
) {
|
||||
auto doc = xml::parse(file, src);
|
||||
const auto& root = *doc->getRoot();
|
||||
if (root.getTag() != "model") {
|
||||
throw std::runtime_error(
|
||||
"'model' tag expected as root, got '" + root.getTag() + "'"
|
||||
);
|
||||
try {
|
||||
auto doc = io::path(std::string(file)).extension() == ".xml"
|
||||
? xml::parse(file, src) : xml::parse_vcm(file, src, "model");
|
||||
const auto& root = *doc->getRoot();
|
||||
if (root.getTag() != "model") {
|
||||
throw std::runtime_error(
|
||||
"'model' tag expected as root, got '" + root.getTag() + "'"
|
||||
);
|
||||
}
|
||||
std::cout << xml::stringify(*doc) << std::endl;
|
||||
return load_model(root);
|
||||
} catch (const parsing_error& err) {
|
||||
throw std::runtime_error(err.errorLog());
|
||||
}
|
||||
return load_model(root);
|
||||
}
|
||||
|
||||
@@ -355,6 +355,83 @@ std::unique_ptr<Document> xml::parse(
|
||||
return parser.parse();
|
||||
}
|
||||
|
||||
namespace {
|
||||
class VcmParser : public BasicParser<char> {
|
||||
public:
|
||||
VcmParser(std::string_view filename, std::string_view source)
|
||||
: BasicParser(filename, source) {
|
||||
document = std::make_unique<Document>("1.0", "UTF-8");
|
||||
hashComment = true;
|
||||
}
|
||||
|
||||
std::string parseValue() {
|
||||
char c = peek();
|
||||
if (c == '"' || c == '\'') {
|
||||
nextChar();
|
||||
return parseString(c);
|
||||
}
|
||||
if (c == '(') {
|
||||
nextChar();
|
||||
// TODO: replace with array parsing after moving to dv::value's
|
||||
std::string value = std::string(readUntil(')'));
|
||||
expect(')');
|
||||
return value;
|
||||
}
|
||||
return std::string(readUntilWhitespace());
|
||||
}
|
||||
|
||||
void parseSubElements(Node& node) {
|
||||
skipWhitespace();
|
||||
while (hasNext()) {
|
||||
if (peek() != '@') {
|
||||
throw error("unexpected character in element");
|
||||
}
|
||||
nextChar();
|
||||
auto subnodePtr = std::make_unique<Node>(parseName());
|
||||
auto subnode = subnodePtr.get();
|
||||
node.add(std::move(subnodePtr));
|
||||
|
||||
skipWhitespace();
|
||||
while (hasNext() && peek() != '@' && peek() != '{' && peek() != '}') {
|
||||
std::string attrname = parseName();
|
||||
skipWhitespace();
|
||||
std::string value = parseValue();
|
||||
subnode->set(attrname, value);
|
||||
skipWhitespace();
|
||||
}
|
||||
if (!hasNext()) {
|
||||
break;
|
||||
}
|
||||
char c = peek();
|
||||
if (c == '{') {
|
||||
nextChar();
|
||||
parseSubElements(*subnode);
|
||||
expect('}');
|
||||
skipWhitespace();
|
||||
} else if (c == '}') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Document> parse(const std::string& rootTag) {
|
||||
auto root = std::make_unique<Node>(rootTag);
|
||||
parseSubElements(*root);
|
||||
document->setRoot(std::move(root));
|
||||
return std::move(document);
|
||||
}
|
||||
private:
|
||||
std::unique_ptr<Document> document;
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<Document> xml::parse_vcm(
|
||||
std::string_view filename, std::string_view source, std::string_view tag
|
||||
) {
|
||||
VcmParser parser(filename, source);
|
||||
return parser.parse(std::string(tag));
|
||||
}
|
||||
|
||||
inline void newline(
|
||||
std::stringstream& ss, bool nice, const std::string& indentStr, int indent
|
||||
) {
|
||||
|
||||
@@ -126,5 +126,9 @@ namespace xml {
|
||||
std::string_view filename, std::string_view source
|
||||
);
|
||||
|
||||
std::unique_ptr<Document> parse_vcm(
|
||||
std::string_view filename, std::string_view source, std::string_view tag
|
||||
);
|
||||
|
||||
using xmlelement = Node;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <array>
|
||||
#include <glm/ext.hpp>
|
||||
#include "ModelViewer.hpp"
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "SplitBox.hpp"
|
||||
|
||||
#include "util/stack_vector.hpp"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
SplitBox::SplitBox(GUI& gui, const glm::vec2& size, float splitPos, Orientation orientation)
|
||||
@@ -28,18 +30,31 @@ void SplitBox::mouseMove(int x, int y) {
|
||||
void SplitBox::refresh() {
|
||||
Container::refresh();
|
||||
|
||||
if (nodes.empty()) {
|
||||
util::stack_vector<UINode*, 2> visibleNodes;
|
||||
|
||||
for (const auto& node : nodes) {
|
||||
if (!node->isVisible()) {
|
||||
continue;
|
||||
}
|
||||
visibleNodes.push_back(node.get());
|
||||
if (visibleNodes.full()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleNodes.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
glm::vec2 size = getSize();
|
||||
if (nodes.size() == 1) {
|
||||
auto node = nodes.at(0);
|
||||
if (visibleNodes.size() == 1) {
|
||||
auto node = visibleNodes.at(0);
|
||||
node->setPos(glm::vec2());
|
||||
node->setSize(size);
|
||||
return;
|
||||
}
|
||||
auto nodeA = nodes.at(0);
|
||||
auto nodeB = nodes.at(1);
|
||||
auto nodeA = visibleNodes.at(0);
|
||||
auto nodeB = visibleNodes.at(1);
|
||||
|
||||
float sepRadius = interval / 2.0f;
|
||||
|
||||
|
||||
@@ -372,7 +372,7 @@ static std::shared_ptr<UINode> read_split_box(
|
||||
static std::shared_ptr<UINode> read_model_viewer(
|
||||
UiXmlReader& reader, const xml::xmlelement& element
|
||||
) {
|
||||
auto model = element.attr("model", "").getText();
|
||||
auto model = element.attr("src", "").getText();
|
||||
auto viewer = std::make_shared<ModelViewer>(
|
||||
reader.getGUI(), glm::vec2(), model
|
||||
);
|
||||
|
||||
@@ -192,6 +192,9 @@ std::string EnginePaths::createWriteableDevice(const std::string& name) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (name == "core") {
|
||||
folder = "res:";
|
||||
}
|
||||
if (folder.emptyOrInvalid()) {
|
||||
throw std::runtime_error("pack not found");
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ static int l_parse_model(lua::State* L) {
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
auto name = lua::require_string(L, 3);
|
||||
|
||||
if (format == "xml") {
|
||||
if (format == "xml" || format == "vcm") {
|
||||
engine->getAssets()->store(vcm::parse(name, string), name);
|
||||
} else {
|
||||
throw std::runtime_error("unknown format " + util::quote(std::string(format)));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "api_lua.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
@@ -156,7 +155,8 @@ static int l_write_bytes(lua::State* L) {
|
||||
return lua::pushboolean(L, res);
|
||||
}
|
||||
|
||||
static int l_list_all_res(lua::State* L, const std::string& path) {
|
||||
static int l_list_all_res(lua::State* L) {
|
||||
std::string path = lua::require_string(L, 1);
|
||||
auto files = engine->getResPaths().listdirRaw(path);
|
||||
lua::createtable(L, files.size(), 0);
|
||||
for (size_t i = 0; i < files.size(); i++) {
|
||||
@@ -169,7 +169,7 @@ static int l_list_all_res(lua::State* L, const std::string& path) {
|
||||
static int l_list(lua::State* L) {
|
||||
std::string dirname = lua::require_string(L, 1);
|
||||
if (dirname.find(':') == std::string::npos) {
|
||||
return l_list_all_res(L, dirname);
|
||||
return l_list_all_res(L);
|
||||
}
|
||||
io::path path = dirname;
|
||||
if (!io::is_directory(path)) {
|
||||
@@ -265,6 +265,7 @@ const luaL_Reg filelib[] = {
|
||||
{"isfile", lua::wrap<l_isfile>},
|
||||
{"length", lua::wrap<l_length>},
|
||||
{"list", lua::wrap<l_list>},
|
||||
{"list_all_res", lua::wrap<l_list_all_res>},
|
||||
{"mkdir", lua::wrap<l_mkdir>},
|
||||
{"mkdirs", lua::wrap<l_mkdirs>},
|
||||
{"read_bytes", lua::wrap<l_read_bytes>},
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "graphics/ui/elements/TextBox.hpp"
|
||||
#include "graphics/ui/elements/TrackBar.hpp"
|
||||
#include "graphics/ui/elements/InlineFrame.hpp"
|
||||
#include "graphics/ui/elements/ModelViewer.hpp"
|
||||
#include "graphics/ui/gui_util.hpp"
|
||||
#include "graphics/ui/markdown.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
@@ -114,6 +115,16 @@ static int l_node_destruct(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_container_refresh(lua::State* L) {
|
||||
auto docnode = get_document_node(L);
|
||||
auto node = dynamic_cast<Container*>(docnode.node.get());
|
||||
if (node == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
node->refresh();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_node_reposition(lua::State* L) {
|
||||
auto docnode = get_document_node(L);
|
||||
docnode.node->reposition();
|
||||
@@ -341,6 +352,8 @@ static int p_get_src(UINode* node, lua::State* L) {
|
||||
return lua::pushstring(L, image->getTexture());
|
||||
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
||||
return lua::pushstring(L, iframe->getSrc());
|
||||
} else if (auto modelviewer = dynamic_cast<ModelViewer*>(node)) {
|
||||
return lua::pushstring(L, modelviewer->getModel());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -408,6 +421,13 @@ static int p_get_destruct(UINode*, lua::State* L) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_node_destruct>);
|
||||
}
|
||||
|
||||
static int p_refresh(UINode* node, lua::State* L) {
|
||||
if (dynamic_cast<Container*>(node)) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_container_refresh>);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int p_get_reposition(UINode*, lua::State* L) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_node_reposition>);
|
||||
}
|
||||
@@ -539,6 +559,7 @@ static int l_gui_getattr(lua::State* L) {
|
||||
{"moveInto", p_move_into},
|
||||
{"add", p_get_add},
|
||||
{"destruct", p_get_destruct},
|
||||
{"refresh", p_refresh},
|
||||
{"reposition", p_get_reposition},
|
||||
{"clear", p_get_clear},
|
||||
{"setInterval", p_set_interval},
|
||||
@@ -675,6 +696,8 @@ static void p_set_src(UINode* node, lua::State* L, int idx) {
|
||||
image->setTexture(lua::require_string(L, idx));
|
||||
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
||||
iframe->setSrc(lua::require_string(L, idx));
|
||||
} else if (auto modelviewer = dynamic_cast<ModelViewer*>(node)) {
|
||||
modelviewer->setModel(lua::require_string(L, idx));
|
||||
}
|
||||
}
|
||||
static void p_set_region(UINode* node, lua::State* L, int idx) {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
namespace util {
|
||||
template<typename T, int capacity>
|
||||
class stack_vector {
|
||||
public:
|
||||
stack_vector() : size_(0) {}
|
||||
|
||||
stack_vector(const stack_vector<T, capacity>& other)
|
||||
: size_(other.size_) {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
new (&data_[i]) T(data_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
stack_vector(stack_vector<T, capacity>&& other) noexcept
|
||||
: size_(other.size_) {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
new (&data_[i]) T(std::move(other.data_[i]));
|
||||
}
|
||||
other.size_ = 0;
|
||||
}
|
||||
|
||||
stack_vector(const std::initializer_list<T>& init) : size_(0) {
|
||||
static_assert(
|
||||
init.size() <= capacity,
|
||||
"initializer list exceeds stack vector capacity"
|
||||
);
|
||||
for (const auto& value : init) {
|
||||
new (&data_[size_++]) T(value);
|
||||
}
|
||||
}
|
||||
|
||||
~stack_vector() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void push_back(const T& value) {
|
||||
if (size_ < capacity) {
|
||||
data_[size_++] = value;
|
||||
} else {
|
||||
throw std::overflow_error("stack vector capacity exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
void pop_back() {
|
||||
if (size_ > 0) {
|
||||
data_[size_ - 1].~T();
|
||||
--size_;
|
||||
} else {
|
||||
throw std::underflow_error("stack vector is empty");
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
data_[i].~T();
|
||||
}
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
T& operator[](int index) {
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
const T& operator[](int index) const {
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
T& at(int index) {
|
||||
if (index < 0 || index >= size_) {
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
const T& at(int index) const {
|
||||
if (index < 0 || index >= size_) {
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
int size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return size_ == 0;
|
||||
}
|
||||
|
||||
bool full() const {
|
||||
return size_ == capacity;
|
||||
}
|
||||
private:
|
||||
T data_[capacity];
|
||||
int size_;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user