Merge pull request #527 from MihailRis/vcm-format

new simple text 3d models format
This commit is contained in:
MihailRis
2025-06-02 21:41:39 +03:00
committed by GitHub
42 changed files with 1112 additions and 207 deletions
+1
View File
@@ -15,6 +15,7 @@
// Libraries
extern const luaL_Reg applib[];
extern const luaL_Reg assetslib[];
extern const luaL_Reg audiolib[];
extern const luaL_Reg base64lib[];
extern const luaL_Reg bjsonlib[];
@@ -0,0 +1,66 @@
#include "api_lua.hpp"
#include "assets/Assets.hpp"
#include "coders/png.hpp"
#include "coders/vcm.hpp"
#include "debug/Logger.hpp"
#include "engine/Engine.hpp"
#include "graphics/commons/Model.hpp"
#include "graphics/core/Texture.hpp"
#include "util/Buffer.hpp"
using namespace scripting;
static void load_texture(
const ubyte* bytes, size_t size, const std::string& destname
) {
try {
engine->getAssets()->store(png::load_texture(bytes, size), destname);
} catch (const std::runtime_error& err) {
debug::Logger logger("lua.assetslib");
logger.error() << err.what();
}
}
static int l_load_texture(lua::State* L) {
if (lua::istable(L, 1)) {
lua::pushvalue(L, 1);
size_t size = lua::objlen(L, 1);
util::Buffer<ubyte> buffer(size);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1);
buffer[i] = lua::tointeger(L, -1);
lua::pop(L);
}
lua::pop(L);
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
} else {
auto string = lua::bytearray_as_string(L, 1);
load_texture(
reinterpret_cast<const ubyte*>(string.data()),
string.size(),
lua::require_string(L, 2)
);
lua::pop(L);
}
return 0;
}
static int l_parse_model(lua::State* L) {
auto format = lua::require_lstring(L, 1);
auto string = lua::require_lstring(L, 2);
auto name = lua::require_string(L, 3);
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)));
}
return 0;
}
const luaL_Reg assetslib[] = {
{"load_texture", lua::wrap<l_load_texture>},
{"parse_model", lua::wrap<l_parse_model>},
{NULL, NULL}
};
+1 -38
View File
@@ -1,13 +1,12 @@
#include <memory>
#include <vector>
#include <sstream>
#include "api_lua.hpp"
#include "coders/png.hpp"
#include "constants.hpp"
#include "assets/Assets.hpp"
#include "content/Content.hpp"
#include "content/ContentControl.hpp"
#include "debug/Logger.hpp"
#include "engine/Engine.hpp"
#include "io/engine_paths.hpp"
#include "io/io.hpp"
@@ -225,41 +224,6 @@ static int l_get_setting_info(lua::State* L) {
throw std::runtime_error("unsupported setting type");
}
static void load_texture(
const ubyte* bytes, size_t size, const std::string& destname
) {
try {
engine->getAssets()->store(png::load_texture(bytes, size), destname);
} catch (const std::runtime_error& err) {
debug::Logger logger("lua.corelib");
logger.error() << err.what();
}
}
static int l_load_texture(lua::State* L) {
if (lua::istable(L, 1)) {
lua::pushvalue(L, 1);
size_t size = lua::objlen(L, 1);
util::Buffer<ubyte> buffer(size);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1);
buffer[i] = lua::tointeger(L, -1);
lua::pop(L);
}
lua::pop(L);
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
} else {
auto string = lua::bytearray_as_string(L, 1);
load_texture(
reinterpret_cast<const ubyte*>(string.data()),
string.size(),
lua::require_string(L, 2)
);
lua::pop(L);
}
return 0;
}
static int l_open_folder(lua::State* L) {
platform::open_folder(io::resolve(lua::require_string(L, 1)));
return 0;
@@ -322,6 +286,5 @@ const luaL_Reg corelib[] = {
{"open_folder", lua::wrap<l_open_folder>},
{"quit", lua::wrap<l_quit>},
{"capture_output", lua::wrap<l_capture_output>},
{"__load_texture", lua::wrap<l_load_texture>},
{NULL, NULL}
};
+41
View File
@@ -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>);
}
@@ -490,6 +510,12 @@ static int p_get_scroll(UINode* node, lua::State* L) {
}
static int l_gui_getattr(lua::State* L) {
if (!lua::isstring(L, 1)) {
throw std::runtime_error("document name is not a string");
}
if (!lua::isstring(L, 2)) {
throw std::runtime_error("element id is not a string");
}
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
if (lua::isnumber(L, 3)) {
@@ -507,6 +533,9 @@ static int l_gui_getattr(lua::State* L) {
const auto& id = request_node_id(DocumentNode {docnode.document, node});
return push_document_node(L, id);
}
if (!lua::isstring(L, 3)) {
throw std::runtime_error("attribute name is not a string");
}
auto attr = lua::require_string(L, 3);
static const std::unordered_map<
@@ -530,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},
@@ -666,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) {
@@ -755,6 +787,15 @@ static int p_set_scroll(UINode* node, lua::State* L, int idx) {
}
static int l_gui_setattr(lua::State* L) {
if (!lua::isstring(L, 1)) {
throw std::runtime_error("document name is not a string");
}
if (!lua::isstring(L, 2)) {
throw std::runtime_error("element id is not a string");
}
if (!lua::isstring(L, 3)) {
throw std::runtime_error("attribute name is not a string");
}
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
auto attr = lua::require_string(L, 3);
+1
View File
@@ -64,6 +64,7 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "__vc_app", applib);
}
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
openlib(L, "assets", assetslib);
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "core", corelib);