commit
1ae052e0b6
3
res/devtools/syntax/vcm.toml
Normal file
3
res/devtools/syntax/vcm.toml
Normal file
@ -0,0 +1,3 @@
|
||||
language = "VCM"
|
||||
extensions = ["vcm"]
|
||||
line-comment-start = "#"
|
||||
4
res/devtools/syntax/xml.toml
Normal file
4
res/devtools/syntax/xml.toml
Normal file
@ -0,0 +1,4 @@
|
||||
language = "XML"
|
||||
extensions = ["xml"]
|
||||
multiline-comment-start = "<!--"
|
||||
multiline-comment-end = "-->"
|
||||
@ -1,12 +1,6 @@
|
||||
<splitbox id="editorRoot" orientation="horizontal" split-pos="0.3">
|
||||
<splitbox split-pos="0.75">
|
||||
<panel interval="2" color="0" padding="2">
|
||||
<textbox pos="2" padding="4,0,4,0" sub-consumer="filter_files" hint="@Filter"></textbox>
|
||||
<panel id="filesList" color="#00000030" interval="6" padding="4"
|
||||
size-func="-1,-45" pos="2,38">
|
||||
<!-- content is generated in script -->
|
||||
</panel>
|
||||
</panel>
|
||||
<iframe src="core:files_panel"></iframe>
|
||||
<panel id="problemsLog"
|
||||
color="#00000030"
|
||||
padding="5,15,5,15">
|
||||
@ -14,46 +8,49 @@
|
||||
</panel>
|
||||
</splitbox>
|
||||
<splitbox id="editorContainer" split-pos="0.8">
|
||||
<container color="#00000080"
|
||||
onclick="document.editor.focused = true document.editor.caret = -1">
|
||||
<container size-func="-1,30" color="#00000020">
|
||||
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
||||
interactive="true" onclick="unlock_access()"
|
||||
color="#FFFFFF80" size="16" pos="4,6"
|
||||
hover-color="#1080FF"></image>
|
||||
<panel orientation="horizontal" gravity="top-right"
|
||||
size="60,16" padding="8" interval="8" color="0">
|
||||
<image id="saveIcon" src="gui/save" tooltip="@Save"
|
||||
enabled="false" interactive="true"
|
||||
hover-color="#1080FF"
|
||||
onclick="save_current_file()"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
<image id="infoIcon" src="gui/info" tooltip="@editor.info.tooltip"
|
||||
enabled="true" interactive="true"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
<image id="syncIcon" src="gui/play" tooltip="@Run"
|
||||
enabled="true" interactive="true"
|
||||
hover-color="#1080FF"
|
||||
onclick="run_current_file()"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
</panel>
|
||||
<label id="title" pos="26,8"></label>
|
||||
<splitbox id="codePanel" orientation="horizontal">
|
||||
<container color="#00000080" cursor="text"
|
||||
onclick="document.editor.focused = true document.editor.caret = -1">
|
||||
<container size-func="-1,30" color="#00000020">
|
||||
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
||||
interactive="true" onclick="unlock_access()"
|
||||
color="#FFFFFF80" size="16" pos="4,6"
|
||||
hover-color="#1080FF"></image>
|
||||
<panel orientation="horizontal" gravity="top-right"
|
||||
size="60,16" padding="8" interval="8" color="0">
|
||||
<image id="saveIcon" src="gui/save" tooltip="@Save"
|
||||
enabled="false" interactive="true"
|
||||
hover-color="#1080FF"
|
||||
onclick="save_current_file()"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
<image id="infoIcon" src="gui/info" tooltip="@editor.info.tooltip"
|
||||
enabled="true" interactive="true"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
<image id="syncIcon" src="gui/play" tooltip="@Run"
|
||||
enabled="true" interactive="true"
|
||||
hover-color="#1080FF"
|
||||
onclick="run_current_file()"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
</panel>
|
||||
<label id="title" pos="26,8"></label>
|
||||
</container>
|
||||
<textbox
|
||||
id='editor'
|
||||
pos='0,30'
|
||||
color='0'
|
||||
autoresize='true'
|
||||
margin='0'
|
||||
padding='5'
|
||||
multiline='true'
|
||||
line-numbers='true'
|
||||
oncontrolkey='on_control_combination'
|
||||
size-func="-1,40"
|
||||
text-wrap='false'
|
||||
scroll-step='50'
|
||||
></textbox>
|
||||
</container>
|
||||
<textbox
|
||||
id='editor'
|
||||
pos='0,30'
|
||||
color='0'
|
||||
autoresize='true'
|
||||
margin='0'
|
||||
padding='5'
|
||||
multiline='true'
|
||||
line-numbers='true'
|
||||
oncontrolkey='on_control_combination'
|
||||
size-func="-1,40"
|
||||
text-wrap='false'
|
||||
scroll-step='50'
|
||||
></textbox>
|
||||
</container>
|
||||
<modelviewer id="modelviewer" visible="false" center="0.5,0.5,0.5" cam-rotation="45,-45,0"/>
|
||||
</splitbox>
|
||||
<splitbox orientation="horizontal" split-pos="0.4">
|
||||
<panel id="traceback" padding="4" color="#000000A0">
|
||||
</panel>
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
local writeables = {}
|
||||
local registry
|
||||
local filenames
|
||||
|
||||
local current_file = {
|
||||
filename = "",
|
||||
@ -49,6 +48,10 @@ events.on("core:error", function (msg, traceback)
|
||||
table.insert(errors_all, full)
|
||||
end)
|
||||
|
||||
events.on("core:open_in_editor", function(filename, linenum)
|
||||
open_file_in_editor(filename, linenum)
|
||||
end)
|
||||
|
||||
local function find_mutable(filename)
|
||||
local packid = file.prefix(filename)
|
||||
if packid == "core" then
|
||||
@ -80,16 +83,6 @@ local function refresh_file_title()
|
||||
..(edited and ' *' or '')
|
||||
end
|
||||
|
||||
function filter_files(text)
|
||||
local filtered = {}
|
||||
for _, filename in ipairs(filenames) do
|
||||
if filename:find(text) then
|
||||
table.insert(filtered, filename)
|
||||
end
|
||||
end
|
||||
build_files_list(filtered, text)
|
||||
end
|
||||
|
||||
function on_control_combination(keycode)
|
||||
if keycode == input.keycode("s") then
|
||||
save_current_file()
|
||||
@ -111,10 +104,29 @@ function unlock_access()
|
||||
)
|
||||
end
|
||||
|
||||
local function reload_model(filename, name)
|
||||
assets.parse_model("xml", document.editor.text, name)
|
||||
end
|
||||
|
||||
function run_current_file()
|
||||
if not current_file.filename then
|
||||
return
|
||||
end
|
||||
|
||||
local info = registry.get_info(current_file.filename)
|
||||
local script_type = info and info.type or "file"
|
||||
local unit = info and info.unit
|
||||
|
||||
if script_type == "model" then
|
||||
print(current_file.filename)
|
||||
clear_output()
|
||||
local _, err = pcall(reload_model, current_file.filename, unit)
|
||||
if err then
|
||||
document.output:paste(string.format("\n[#FF0000]%s[#FFFFFF]", err))
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
local chunk, err = loadstring(document.editor.text, current_file.filename)
|
||||
clear_output()
|
||||
if not chunk then
|
||||
@ -126,9 +138,7 @@ function run_current_file()
|
||||
)
|
||||
return
|
||||
end
|
||||
local info = registry.get_info(current_file.filename)
|
||||
local script_type = info and info.type or "file"
|
||||
local unit = info and info.unit
|
||||
|
||||
save_current_file()
|
||||
|
||||
local func = function()
|
||||
@ -198,6 +208,7 @@ events.on("core:open_traceback", function(traceback_b64)
|
||||
tb_list.size = srcsize
|
||||
end)
|
||||
|
||||
--- Save the current file in the code editor if has writeable path.
|
||||
function save_current_file()
|
||||
if not current_file.mutable then
|
||||
return
|
||||
@ -209,7 +220,22 @@ function save_current_file()
|
||||
document.editor.edited = false
|
||||
end
|
||||
|
||||
--- Open a file in the code editor.
|
||||
--- @param filename string - the path to the file to open.
|
||||
--- @param line integer - the line number to focus on (optional).
|
||||
--- @param mutable string - writeable file path (optional).
|
||||
function open_file_in_editor(filename, line, mutable)
|
||||
debug.log("opening file " .. string.escape(filename) .. " in editor")
|
||||
|
||||
local ext = file.ext(filename)
|
||||
if ext == "xml" or ext == "vcm" then
|
||||
document.modelviewer.src = file.stem(filename)
|
||||
document.modelviewer.visible = true
|
||||
else
|
||||
document.modelviewer.visible = false
|
||||
end
|
||||
document.codePanel:refresh()
|
||||
|
||||
local editor = document.editor
|
||||
local source = file.read(filename):gsub('\t', ' ')
|
||||
editor.scroll = 0
|
||||
@ -229,43 +255,11 @@ function open_file_in_editor(filename, line, mutable)
|
||||
document.saveIcon.enabled = current_file.modified
|
||||
end
|
||||
|
||||
function build_files_list(filenames, selected)
|
||||
local files_list = document.filesList
|
||||
files_list.scroll = 0
|
||||
files_list:clear()
|
||||
|
||||
for _, actual_filename in ipairs(filenames) do
|
||||
local filename = actual_filename
|
||||
if selected then
|
||||
filename = filename:gsub(selected, "**"..selected.."**")
|
||||
end
|
||||
local parent = file.parent(filename)
|
||||
local info = registry.get_info(actual_filename)
|
||||
local icon = "file"
|
||||
if info then
|
||||
icon = info.type == "component" and "entity" or info.type
|
||||
end
|
||||
files_list:add(gui.template("script_file", {
|
||||
path = parent .. (parent[#parent] == ':' and '' or '/'),
|
||||
name = file.name(filename),
|
||||
icon = icon,
|
||||
unit = info and info.unit or '',
|
||||
filename = actual_filename
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
||||
function on_open(mode)
|
||||
registry = require "core:internal/scripts_registry"
|
||||
|
||||
local files_list = document.filesList
|
||||
|
||||
document.editorContainer:setInterval(200, refresh_file_title)
|
||||
document.codePanel:setInterval(200, refresh_file_title)
|
||||
|
||||
clear_traceback()
|
||||
clear_output()
|
||||
|
||||
filenames = registry.filenames
|
||||
table.sort(filenames)
|
||||
build_files_list(filenames)
|
||||
end
|
||||
|
||||
@ -22,8 +22,7 @@
|
||||
markup="md"
|
||||
></textbox>
|
||||
</container>
|
||||
<iframe id="editorRoot" pos="0,30" size-func="-1,gui.get_viewport()[2]-30"
|
||||
src="core:code_editor">
|
||||
<iframe id="editorRoot" pos="0,30" size-func="-1,gui.get_viewport()[2]-30">
|
||||
</iframe>
|
||||
<textbox id='prompt'
|
||||
consumer='submit'
|
||||
|
||||
@ -104,8 +104,10 @@ function set_mode(mode)
|
||||
|
||||
if mode == 'debug' then
|
||||
document.root.color = {16, 18, 20, 220}
|
||||
document.editorRoot.src = "core:code_editor"
|
||||
else
|
||||
document.root.color = {0, 0, 0, 128}
|
||||
document.editorRoot.src = ""
|
||||
end
|
||||
|
||||
document.prompt.visible = show_prompt
|
||||
|
||||
7
res/layouts/files_panel.xml
Normal file
7
res/layouts/files_panel.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<panel interval="2" color="0" padding="2">
|
||||
<textbox pos="2" padding="4,0,4,0" sub-consumer="filter_files" hint="@Filter"></textbox>
|
||||
<panel id="filesList" color="#00000030" interval="6" padding="4"
|
||||
size-func="-1,-45" pos="2,38">
|
||||
<!-- content is generated in script -->
|
||||
</panel>
|
||||
</panel>
|
||||
54
res/layouts/files_panel.xml.lua
Normal file
54
res/layouts/files_panel.xml.lua
Normal file
@ -0,0 +1,54 @@
|
||||
local registry
|
||||
local filenames
|
||||
|
||||
function filter_files(text)
|
||||
local pattern_safe = text:pattern_safe();
|
||||
local filtered = {}
|
||||
for _, filename in ipairs(filenames) do
|
||||
if filename:find(pattern_safe) then
|
||||
table.insert(filtered, filename)
|
||||
end
|
||||
end
|
||||
build_files_list(filtered, pattern_safe)
|
||||
end
|
||||
|
||||
function open_file_in_editor(filename, linenum)
|
||||
events.emit("core:open_in_editor", filename, linenum)
|
||||
end
|
||||
|
||||
function build_files_list(filenames, highlighted_part)
|
||||
local files_list = document.filesList
|
||||
files_list.scroll = 0
|
||||
files_list:clear()
|
||||
|
||||
for _, actual_filename in ipairs(filenames) do
|
||||
local filename = actual_filename
|
||||
if highlighted_part then
|
||||
filename = filename:gsub(highlighted_part, "**"..highlighted_part.."**")
|
||||
end
|
||||
local parent = file.parent(filename)
|
||||
local info = registry.get_info(actual_filename)
|
||||
local icon = "file"
|
||||
if info then
|
||||
icon = info.type == "component" and "entity" or info.type
|
||||
end
|
||||
files_list:add(gui.template("script_file", {
|
||||
path = parent .. (parent[#parent] == ':' and '' or '/'),
|
||||
name = file.name(filename),
|
||||
icon = icon,
|
||||
unit = info and info.unit or '',
|
||||
filename = actual_filename,
|
||||
open_func = "open_file_in_editor",
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
||||
function on_open(mode)
|
||||
registry = require "core:internal/scripts_registry"
|
||||
|
||||
local files_list = document.filesList
|
||||
|
||||
filenames = registry.filenames
|
||||
table.sort(filenames)
|
||||
build_files_list(filenames)
|
||||
end
|
||||
@ -3,7 +3,7 @@
|
||||
<label hover-color='#30A0FF'
|
||||
pos="20,2"
|
||||
interactive="true"
|
||||
onclick='open_file_in_editor("%{filename}")'
|
||||
onclick='%{open_func}("%{filename}")'
|
||||
markup='md'
|
||||
tooltip='%{unit}'
|
||||
sizefunc="-1,-1">
|
||||
|
||||
4
res/models/stairs.vcm
Normal file
4
res/models/stairs.vcm
Normal file
@ -0,0 +1,4 @@
|
||||
@box from (0,0,0) to (1,0.5,1) delete (top,south)
|
||||
@box from (0,0.5,0) to (1,1,0.5) delete (bottom,south)
|
||||
@rect from (0,0.5,0.5) right (1,0,0) up (0,0,0.5) texture "$2"
|
||||
@rect from (0,0,0) right (1,0,0) up (0,1,0) texture "$1"
|
||||
@ -1,6 +0,0 @@
|
||||
<model>
|
||||
<box from="0,0,0" to="1,0.5,1" delete="top,south"/>
|
||||
<box from="0,0.5,0" to="1,1,0.5" delete="bottom,south"/>
|
||||
<rect from="0,0.5,0.5" right="1,0,0" up="0,0,0.5"/>
|
||||
<rect from="0,0,0" right="1,0,0" up="0,1,0"/>
|
||||
</model>
|
||||
@ -1,6 +1,6 @@
|
||||
local export = {
|
||||
filenames = {},
|
||||
classification = {}
|
||||
registry = {}
|
||||
}
|
||||
|
||||
local function collect_components(dirname, dest)
|
||||
@ -9,7 +9,7 @@ local function collect_components(dirname, dest)
|
||||
for i, filename in ipairs(files) do
|
||||
if file.ext(filename) == "lua" then
|
||||
table.insert(dest, filename)
|
||||
export.classification[filename] = {
|
||||
export.registry[filename] = {
|
||||
type="component",
|
||||
unit=file.prefix(filename)..":"..file.stem(filename)
|
||||
}
|
||||
@ -33,13 +33,13 @@ local function collect_scripts(dirname, dest, ismodule)
|
||||
end
|
||||
end
|
||||
|
||||
local function load_scripts_list()
|
||||
local packs = pack.get_installed()
|
||||
local function load_scripts_list(packs)
|
||||
local registry = export.registry
|
||||
for _, packid in ipairs(packs) do
|
||||
collect_scripts(packid..":modules", export.filenames, true)
|
||||
end
|
||||
for _, filename in ipairs(export.filenames) do
|
||||
export.classification[filename] = {
|
||||
registry[filename] = {
|
||||
type="module",
|
||||
unit=file.join(file.parent(file.prefix(filename)..":"..
|
||||
filename:sub(filename:find("/")+1)),
|
||||
@ -51,27 +51,39 @@ local function load_scripts_list()
|
||||
end
|
||||
end
|
||||
|
||||
function export.build_classification()
|
||||
local classification = {}
|
||||
local function load_models_list(packs)
|
||||
local registry = export.registry
|
||||
for _, filename in ipairs(file.list("models")) do
|
||||
local ext = file.ext(filename)
|
||||
if ext == "xml" or ext == "vcm" then
|
||||
registry[filename] = {type="model", unit=file.stem(filename)}
|
||||
table.insert(export.filenames, filename)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function export.build_registry()
|
||||
local registry = {}
|
||||
for id, props in pairs(block.properties) do
|
||||
classification[props["script-file"]] = {type="block", unit=block.name(id)}
|
||||
registry[props["script-file"]] = {type="block", unit=block.name(id)}
|
||||
end
|
||||
for id, props in pairs(item.properties) do
|
||||
classification[props["script-file"]] = {type="item", unit=item.name(id)}
|
||||
registry[props["script-file"]] = {type="item", unit=item.name(id)}
|
||||
end
|
||||
local packs = pack.get_installed()
|
||||
for _, packid in ipairs(packs) do
|
||||
classification[packid..":scripts/world.lua"] = {type="world", unit=packid}
|
||||
classification[packid..":scripts/hud.lua"] = {type="hud", unit=packid}
|
||||
registry[packid..":scripts/world.lua"] = {type="world", unit=packid}
|
||||
registry[packid..":scripts/hud.lua"] = {type="hud", unit=packid}
|
||||
end
|
||||
export.classification = classification
|
||||
export.registry = registry
|
||||
export.filenames = {}
|
||||
|
||||
load_scripts_list()
|
||||
load_scripts_list(packs)
|
||||
load_models_list(packs)
|
||||
end
|
||||
|
||||
function export.get_info(filename)
|
||||
return export.classification[filename]
|
||||
return export.registry[filename]
|
||||
end
|
||||
|
||||
return export
|
||||
|
||||
@ -63,4 +63,4 @@ cache_names(block)
|
||||
cache_names(item)
|
||||
|
||||
local scripts_registry = require "core:internal/scripts_registry"
|
||||
scripts_registry.build_classification()
|
||||
scripts_registry.build_registry()
|
||||
|
||||
@ -523,9 +523,6 @@ function time.post_runnable(runnable)
|
||||
table.insert(__post_runnables, runnable)
|
||||
end
|
||||
|
||||
assets = {}
|
||||
assets.load_texture = core.__load_texture
|
||||
|
||||
-- --------- Deprecated functions ------ --
|
||||
local function wrap_deprecated(func, name, alternatives)
|
||||
return function (...)
|
||||
|
||||
@ -352,21 +352,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(
|
||||
|
||||
@ -9,7 +9,11 @@ util::TextureRegion util::get_texture_region(
|
||||
) {
|
||||
size_t sep = name.find(':');
|
||||
if (sep == std::string::npos) {
|
||||
return {assets.get<Texture>(name), UVRegion(0,0,1,1)};
|
||||
auto texture = assets.get<Texture>(name);
|
||||
if (texture == nullptr && !fallback.empty()) {
|
||||
return util::get_texture_region(assets, fallback, "");
|
||||
}
|
||||
return {texture, UVRegion(0,0,1,1)};
|
||||
} else {
|
||||
auto atlas = assets.get<Atlas>(name.substr(0, sep));
|
||||
if (atlas) {
|
||||
|
||||
@ -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;
|
||||
@ -14,8 +15,8 @@ static const std::unordered_map<std::string, int> side_indices {
|
||||
{"south", 1},
|
||||
{"top", 2},
|
||||
{"bottom", 3},
|
||||
{"east", 4},
|
||||
{"west", 5},
|
||||
{"west", 4},
|
||||
{"east", 5},
|
||||
};
|
||||
|
||||
static void perform_rect(const xmlelement& root, model::Model& model) {
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -77,13 +77,13 @@ void Mesh::addBox(
|
||||
if (enabledSides[1]) // south
|
||||
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
|
||||
if (enabledSides[2]) // top
|
||||
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]);
|
||||
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1));
|
||||
if (enabledSides[3]) // bottom
|
||||
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]);
|
||||
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3] * glm::vec2(-1, 1));
|
||||
if (enabledSides[4]) // west
|
||||
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
|
||||
if (enabledSides[5]) // east
|
||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]);
|
||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1));
|
||||
}
|
||||
|
||||
void Mesh::scale(const glm::vec3& size) {
|
||||
|
||||
@ -6,6 +6,11 @@
|
||||
#include "typedefs.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
|
||||
namespace {
|
||||
const glm::vec3 SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
|
||||
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
|
||||
}
|
||||
|
||||
Batch3D::Batch3D(size_t capacity)
|
||||
: capacity(capacity) {
|
||||
|
||||
@ -31,20 +36,28 @@ void Batch3D::begin(){
|
||||
}
|
||||
|
||||
void Batch3D::vertex(
|
||||
float x, float y, float z, float u, float v,
|
||||
float r, float g, float b, float a
|
||||
float x,
|
||||
float y,
|
||||
float z,
|
||||
float u,
|
||||
float v,
|
||||
float r,
|
||||
float g,
|
||||
float b,
|
||||
float a
|
||||
) {
|
||||
buffer[index].position = {x, y, z};
|
||||
buffer[index].uv = {u, v};
|
||||
buffer[index].uv = {
|
||||
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
|
||||
buffer[index].color = {r, g, b, a};
|
||||
index++;
|
||||
}
|
||||
void Batch3D::vertex(
|
||||
glm::vec3 coord, float u, float v,
|
||||
float r, float g, float b, float a
|
||||
glm::vec3 coord, float u, float v, float r, float g, float b, float a
|
||||
) {
|
||||
buffer[index].position = coord;
|
||||
buffer[index].uv = {u, v};
|
||||
buffer[index].uv = {
|
||||
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
|
||||
buffer[index].color = {r, g, b, a};
|
||||
index++;
|
||||
}
|
||||
@ -54,7 +67,9 @@ void Batch3D::vertex(
|
||||
float r, float g, float b, float a
|
||||
) {
|
||||
buffer[index].position = point;
|
||||
buffer[index].uv = uvpoint;
|
||||
buffer[index].uv = {
|
||||
uvpoint.x * region.getWidth() + region.u1,
|
||||
uvpoint.y * region.getHeight() + region.v1};
|
||||
buffer[index].color = {r, g, b, a};
|
||||
index++;
|
||||
}
|
||||
@ -86,14 +101,18 @@ void Batch3D::face(
|
||||
}
|
||||
|
||||
void Batch3D::texture(const Texture* new_texture){
|
||||
if (currentTexture == new_texture)
|
||||
if (currentTexture == new_texture) {
|
||||
return;
|
||||
}
|
||||
flush();
|
||||
currentTexture = new_texture;
|
||||
if (new_texture == nullptr)
|
||||
if (new_texture == nullptr) {
|
||||
blank->bind();
|
||||
else
|
||||
region = blank->getUVRegion();
|
||||
} else {
|
||||
new_texture->bind();
|
||||
region = currentTexture->getUVRegion();
|
||||
}
|
||||
}
|
||||
|
||||
void Batch3D::sprite(
|
||||
@ -240,6 +259,16 @@ void Batch3D::blockCube(
|
||||
cube((1.0f - size) * -0.5f, size, texfaces, tint, shading);
|
||||
}
|
||||
|
||||
void Batch3D::setRegion(UVRegion region) {
|
||||
this->region = region;
|
||||
}
|
||||
|
||||
void Batch3D::vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm) {
|
||||
float d = glm::dot(glm::normalize(norm), SUN_VECTOR);
|
||||
d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
|
||||
vertex(pos, uv, glm::vec4(d, d, d, 1.0f));
|
||||
}
|
||||
|
||||
void Batch3D::vertex(
|
||||
const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint
|
||||
) {
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "typedefs.hpp"
|
||||
#include "commons.hpp"
|
||||
#include "MeshData.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
@ -32,8 +33,8 @@ class Batch3D : public Flushable {
|
||||
std::unique_ptr<Texture> blank;
|
||||
size_t index;
|
||||
glm::vec4 tint {1.0f};
|
||||
|
||||
const Texture* currentTexture;
|
||||
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
|
||||
void vertex(
|
||||
float x, float y, float z,
|
||||
@ -102,6 +103,8 @@ public:
|
||||
const glm::vec4& tint,
|
||||
bool shading = true
|
||||
);
|
||||
void setRegion(UVRegion region);
|
||||
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm);
|
||||
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec4& tint);
|
||||
void point(const glm::vec3& pos, const glm::vec4& tint);
|
||||
void flush() override;
|
||||
|
||||
@ -34,7 +34,7 @@ public:
|
||||
Batch2D* getBatch2D() const;
|
||||
|
||||
const glm::uvec2& getViewport() const;
|
||||
DrawContext sub(Flushable* flushable=nullptr) const;
|
||||
[[nodiscard]] DrawContext sub(Flushable* flushable=nullptr) const;
|
||||
|
||||
void setViewport(const glm::uvec2& viewport);
|
||||
void setFramebuffer(Framebuffer* fbo);
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
GLSLExtension* Shader::preprocessor = new GLSLExtension();
|
||||
Shader* Shader::used = nullptr;
|
||||
|
||||
Shader::Shader(uint id) : id(id){
|
||||
}
|
||||
@ -25,6 +26,7 @@ Shader::~Shader(){
|
||||
}
|
||||
|
||||
void Shader::use(){
|
||||
used = this;
|
||||
glUseProgram(id);
|
||||
}
|
||||
|
||||
@ -131,3 +133,7 @@ std::unique_ptr<Shader> Shader::create(
|
||||
}
|
||||
return std::make_unique<Shader>(id);
|
||||
}
|
||||
|
||||
Shader& Shader::getUsed() {
|
||||
return *used;
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
class GLSLExtension;
|
||||
|
||||
class Shader {
|
||||
static Shader* used;
|
||||
uint id;
|
||||
std::unordered_map<std::string, uint> uniformLocations;
|
||||
|
||||
@ -43,4 +44,6 @@ public:
|
||||
const std::string& vertexSource,
|
||||
const std::string& fragmentSource
|
||||
);
|
||||
|
||||
static Shader& getUsed();
|
||||
};
|
||||
|
||||
@ -98,7 +98,7 @@ model::Model ModelsGenerator::fromCustom(
|
||||
for (size_t i = 0; i < modelBoxes.size(); i++) {
|
||||
auto& mesh = model.addMesh("blocks:");
|
||||
mesh.lighting = lighting;
|
||||
const UVRegion boxtexfaces[6] = {
|
||||
UVRegion boxtexfaces[6] = {
|
||||
get_region_for(modelTextures[i * 6 + 5], assets),
|
||||
get_region_for(modelTextures[i * 6 + 4], assets),
|
||||
get_region_for(modelTextures[i * 6 + 3], assets),
|
||||
@ -106,6 +106,9 @@ model::Model ModelsGenerator::fromCustom(
|
||||
get_region_for(modelTextures[i * 6 + 1], assets),
|
||||
get_region_for(modelTextures[i * 6 + 0], assets)
|
||||
};
|
||||
boxtexfaces[2].scale(glm::vec2(-1));
|
||||
boxtexfaces[5].scale(glm::vec2(-1, 1));
|
||||
|
||||
bool enabled[6] {1,1,1,1,1,1};
|
||||
mesh.addBox(
|
||||
modelBoxes[i].center(),
|
||||
|
||||
@ -36,6 +36,7 @@ void InlineFrame::setDocument(const std::shared_ptr<UiDocument>& document) {
|
||||
}
|
||||
|
||||
void InlineFrame::act(float delta) {
|
||||
Container::act(delta);
|
||||
if (document || src.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
151
src/graphics/ui/elements/ModelViewer.cpp
Normal file
151
src/graphics/ui/elements/ModelViewer.cpp
Normal file
@ -0,0 +1,151 @@
|
||||
#include <glm/ext.hpp>
|
||||
#include "ModelViewer.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "graphics/core/Batch3D.hpp"
|
||||
#include "graphics/core/Shader.hpp"
|
||||
#include "graphics/core/Framebuffer.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "window/Window.hpp"
|
||||
#include "../GUI.hpp"
|
||||
|
||||
// TODO: remove
|
||||
#include <GL/glew.h>
|
||||
|
||||
using namespace gui;
|
||||
|
||||
ModelViewer::ModelViewer(
|
||||
GUI& gui, const glm::vec2& size, const std::string& modelName
|
||||
)
|
||||
: Container(gui, size),
|
||||
modelName(modelName),
|
||||
camera(),
|
||||
batch(std::make_unique<Batch3D>(1024)),
|
||||
fbo(std::make_unique<Framebuffer>(size.x, size.y)) {
|
||||
camera.perspective = true;
|
||||
camera.position = glm::vec3(2, 2, 2);
|
||||
}
|
||||
|
||||
ModelViewer::~ModelViewer() = default;
|
||||
|
||||
void ModelViewer::setModel(const std::string& modelName) {
|
||||
this->modelName = modelName;
|
||||
}
|
||||
|
||||
const std::string& ModelViewer::getModel() const {
|
||||
return modelName;
|
||||
}
|
||||
|
||||
Camera& ModelViewer::getCamera() {
|
||||
return camera;
|
||||
}
|
||||
|
||||
const Camera& ModelViewer::getCamera() const {
|
||||
return camera;
|
||||
}
|
||||
|
||||
void ModelViewer::act(float delta) {
|
||||
Container::act(delta);
|
||||
|
||||
auto& input = gui.getInput();
|
||||
|
||||
if (!grabbing && hover && input.jclicked(Mousecode::BUTTON_3)) {
|
||||
grabbing = true;
|
||||
}
|
||||
if (grabbing && input.clicked(Mousecode::BUTTON_3)) {
|
||||
auto cursor = input.getCursor();
|
||||
if (input.pressed(Keycode::LEFT_SHIFT)) {
|
||||
center -= camera.right * (cursor.delta.x / size.x) * distance;
|
||||
center += camera.up * (cursor.delta.y / size.y) * distance;
|
||||
} else {
|
||||
rotation.x -= cursor.delta.x / size.x * glm::two_pi<float>();
|
||||
rotation.y -= cursor.delta.y / size.y * glm::two_pi<float>();
|
||||
rotation.y = glm::max(
|
||||
-glm::half_pi<float>(), glm::min(glm::half_pi<float>(), rotation.y)
|
||||
);
|
||||
}
|
||||
} else if (grabbing) {
|
||||
grabbing = false;
|
||||
}
|
||||
if (hover) {
|
||||
distance *= 1.0f - 0.2f * input.getScroll();
|
||||
}
|
||||
camera.rotation = glm::mat4(1.0f);
|
||||
camera.rotate(rotation.y, rotation.x, rotation.z);
|
||||
camera.position = center - camera.front * distance;
|
||||
}
|
||||
|
||||
void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) {
|
||||
camera.setAspectRatio(size.x / size.y);
|
||||
camera.updateVectors();
|
||||
|
||||
auto model = assets.get<model::Model>(modelName);
|
||||
if (model == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto& prevShader = Shader::getUsed();
|
||||
|
||||
fbo->resize(size.x, size.y);
|
||||
{
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
auto ctx = pctx.sub();
|
||||
ctx.setFramebuffer(fbo.get());
|
||||
ctx.setViewport({size.x, size.y});
|
||||
ctx.setDepthTest(true);
|
||||
ctx.setCullFace(true);
|
||||
display::clear();
|
||||
|
||||
auto& ui3dShader = assets.require<Shader>("ui3d");
|
||||
ui3dShader.use();
|
||||
ui3dShader.uniformMatrix("u_apply", glm::mat4(1.0f));
|
||||
ui3dShader.uniformMatrix("u_projview", camera.getProjView());
|
||||
batch->begin();
|
||||
for (const auto& mesh : model->meshes) {
|
||||
util::TextureRegion region;
|
||||
if (!mesh.texture.empty() && mesh.texture[0] == '$') {
|
||||
// todo: refactor
|
||||
static std::array<std::string, 6> faces {
|
||||
"blocks:dbg_north",
|
||||
"blocks:dbg_south",
|
||||
"blocks:dbg_top",
|
||||
"blocks:dbg_bottom",
|
||||
"blocks:dbg_west",
|
||||
"blocks:dbg_east",
|
||||
};
|
||||
region = util::get_texture_region(
|
||||
assets,
|
||||
faces.at(mesh.texture.at(1) - '0'),
|
||||
"blocks:notfound"
|
||||
);
|
||||
} else {
|
||||
region = util::get_texture_region(assets, mesh.texture, "blocks:notfound");
|
||||
}
|
||||
batch->texture(region.texture);
|
||||
batch->setRegion(region.region);
|
||||
for (const auto& vertex : mesh.vertices) {
|
||||
batch->vertex(vertex.coord, vertex.uv, vertex.normal);
|
||||
}
|
||||
}
|
||||
batch->flush();
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
auto pos = calcPos();
|
||||
prevShader.use();
|
||||
auto& batch2d = *pctx.getBatch2D();
|
||||
batch2d.texture(fbo->getTexture());
|
||||
batch2d.rect(pos.x, pos.y, size.x, size.y, 0.0f, 0.0f, 0.0f, UVRegion {}, false, true, glm::vec4{1.0f});
|
||||
|
||||
Container::draw(pctx, assets);
|
||||
}
|
||||
|
||||
void ModelViewer::setCenter(const glm::vec3& center) {
|
||||
this->center = center;
|
||||
}
|
||||
|
||||
void ModelViewer::setRotation(const glm::vec3& euler) {
|
||||
this->rotation = euler;
|
||||
}
|
||||
40
src/graphics/ui/elements/ModelViewer.hpp
Normal file
40
src/graphics/ui/elements/ModelViewer.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "Container.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
|
||||
class Batch3D;
|
||||
class Framebuffer;
|
||||
|
||||
namespace gui {
|
||||
class ModelViewer : public Container {
|
||||
private:
|
||||
std::string modelName;
|
||||
Camera camera;
|
||||
std::unique_ptr<Batch3D> batch;
|
||||
std::unique_ptr<Framebuffer> fbo;
|
||||
|
||||
glm::vec3 rotation {};
|
||||
glm::vec3 center {};
|
||||
float distance = 4.0f;
|
||||
bool grabbing = false;
|
||||
public:
|
||||
ModelViewer(GUI& gui, const glm::vec2& size, const std::string& modelName);
|
||||
|
||||
~ModelViewer();
|
||||
|
||||
void setModel(const std::string& modelName);
|
||||
const std::string& getModel() const;
|
||||
|
||||
Camera& getCamera();
|
||||
const Camera& getCamera() const;
|
||||
|
||||
void act(float delta) override;
|
||||
void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||
|
||||
void setRotation(const glm::vec3& euler);
|
||||
void setCenter(const glm::vec3& center);
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -19,6 +19,7 @@
|
||||
#include "elements/Panel.hpp"
|
||||
#include "elements/TextBox.hpp"
|
||||
#include "elements/TrackBar.hpp"
|
||||
#include "elements/ModelViewer.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "frontend/menu.hpp"
|
||||
@ -368,6 +369,23 @@ static std::shared_ptr<UINode> read_split_box(
|
||||
return splitBox;
|
||||
}
|
||||
|
||||
static std::shared_ptr<UINode> read_model_viewer(
|
||||
UiXmlReader& reader, const xml::xmlelement& element
|
||||
) {
|
||||
auto model = element.attr("src", "").getText();
|
||||
auto viewer = std::make_shared<ModelViewer>(
|
||||
reader.getGUI(), glm::vec2(), model
|
||||
);
|
||||
read_container_impl(reader, element, *viewer);
|
||||
if (element.has("center")) {
|
||||
viewer->setCenter(element.attr("center").asVec3());
|
||||
}
|
||||
if (element.has("cam-rotation")) {
|
||||
viewer->setRotation(glm::radians(element.attr("cam-rotation").asVec3()));
|
||||
}
|
||||
return viewer;
|
||||
}
|
||||
|
||||
static std::shared_ptr<UINode> read_panel(
|
||||
UiXmlReader& reader, const xml::xmlelement& element
|
||||
) {
|
||||
@ -785,6 +803,7 @@ UiXmlReader::UiXmlReader(gui::GUI& gui, scriptenv&& env) : gui(gui), env(std::mo
|
||||
add("trackbar", read_track_bar);
|
||||
add("container", read_container);
|
||||
add("bindbox", read_input_bind_box);
|
||||
add("modelviewer", read_model_viewer);
|
||||
add("inventory", read_inventory);
|
||||
}
|
||||
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
@ -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[];
|
||||
|
||||
66
src/logic/scripting/lua/libs/libassets.cpp
Normal file
66
src/logic/scripting/lua/libs/libassets.cpp
Normal file
@ -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}
|
||||
};
|
||||
@ -2,12 +2,10 @@
|
||||
#include <vector>
|
||||
|
||||
#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 +223,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 +285,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}
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -43,8 +43,8 @@ struct UVRegion {
|
||||
}
|
||||
|
||||
void scale(float x, float y) {
|
||||
float w = getWidth();
|
||||
float h = getHeight();
|
||||
float w = u2 - u1;
|
||||
float h = v2 - v1;
|
||||
float cx = (u1 + u2) * 0.5f;
|
||||
float cy = (v1 + v2) * 0.5f;
|
||||
u1 = cx - w * 0.5f * x;
|
||||
@ -63,4 +63,10 @@ struct UVRegion {
|
||||
u2 = vec.z;
|
||||
v2 = vec.w;
|
||||
}
|
||||
|
||||
UVRegion operator*(const glm::vec2& scale) const {
|
||||
auto copy = UVRegion(*this);
|
||||
copy.scale(scale);
|
||||
return copy;
|
||||
}
|
||||
};
|
||||
|
||||
99
src/util/stack_vector.hpp
Normal file
99
src/util/stack_vector.hpp
Normal file
@ -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_;
|
||||
};
|
||||
}
|
||||
24
test/coders/xml.cpp
Normal file
24
test/coders/xml.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "coders/xml.hpp"
|
||||
#include "coders/commons.hpp"
|
||||
|
||||
TEST(XML, VCM) {
|
||||
std::string code = ""
|
||||
"@line x1 y1 z1\n"
|
||||
"# @tst ysfdrg\n"
|
||||
"@box {\n"
|
||||
" @rect texture \"$2\"\n"
|
||||
" @utro a 53.1\n"
|
||||
"}\n"
|
||||
;
|
||||
std::cout << code << std::endl;
|
||||
|
||||
try {
|
||||
auto document = xml::parse_vcm("<test>", code, "test");
|
||||
std::cout << xml::stringify(*document);
|
||||
} catch (const parsing_error& err) {
|
||||
std::cerr << err.errorLog() << std::endl;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user