Merge pull request #527 from MihailRis/vcm-format
new simple text 3d models format
This commit is contained in:
commit
ead46158d9
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 id="editorRoot" orientation="horizontal" split-pos="0.3">
|
||||||
<splitbox split-pos="0.75">
|
<splitbox split-pos="0.75">
|
||||||
<panel interval="2" color="0" padding="2">
|
<iframe src="core:files_panel"></iframe>
|
||||||
<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>
|
|
||||||
<panel id="problemsLog"
|
<panel id="problemsLog"
|
||||||
color="#00000030"
|
color="#00000030"
|
||||||
padding="5,15,5,15">
|
padding="5,15,5,15">
|
||||||
@ -14,46 +8,49 @@
|
|||||||
</panel>
|
</panel>
|
||||||
</splitbox>
|
</splitbox>
|
||||||
<splitbox id="editorContainer" split-pos="0.8">
|
<splitbox id="editorContainer" split-pos="0.8">
|
||||||
<container color="#00000080"
|
<splitbox id="codePanel" orientation="horizontal">
|
||||||
onclick="document.editor.focused = true document.editor.caret = -1">
|
<container color="#00000080" cursor="text"
|
||||||
<container size-func="-1,30" color="#00000020">
|
onclick="document.editor.focused = true document.editor.caret = -1">
|
||||||
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
<container size-func="-1,30" color="#00000020">
|
||||||
interactive="true" onclick="unlock_access()"
|
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
||||||
color="#FFFFFF80" size="16" pos="4,6"
|
interactive="true" onclick="unlock_access()"
|
||||||
hover-color="#1080FF"></image>
|
color="#FFFFFF80" size="16" pos="4,6"
|
||||||
<panel orientation="horizontal" gravity="top-right"
|
hover-color="#1080FF"></image>
|
||||||
size="60,16" padding="8" interval="8" color="0">
|
<panel orientation="horizontal" gravity="top-right"
|
||||||
<image id="saveIcon" src="gui/save" tooltip="@Save"
|
size="60,16" padding="8" interval="8" color="0">
|
||||||
enabled="false" interactive="true"
|
<image id="saveIcon" src="gui/save" tooltip="@Save"
|
||||||
hover-color="#1080FF"
|
enabled="false" interactive="true"
|
||||||
onclick="save_current_file()"
|
hover-color="#1080FF"
|
||||||
color="#FFFFFF80" size="16"></image>
|
onclick="save_current_file()"
|
||||||
<image id="infoIcon" src="gui/info" tooltip="@editor.info.tooltip"
|
color="#FFFFFF80" size="16"></image>
|
||||||
enabled="true" interactive="true"
|
<image id="infoIcon" src="gui/info" tooltip="@editor.info.tooltip"
|
||||||
color="#FFFFFF80" size="16"></image>
|
enabled="true" interactive="true"
|
||||||
<image id="syncIcon" src="gui/play" tooltip="@Run"
|
color="#FFFFFF80" size="16"></image>
|
||||||
enabled="true" interactive="true"
|
<image id="syncIcon" src="gui/play" tooltip="@Run"
|
||||||
hover-color="#1080FF"
|
enabled="true" interactive="true"
|
||||||
onclick="run_current_file()"
|
hover-color="#1080FF"
|
||||||
color="#FFFFFF80" size="16"></image>
|
onclick="run_current_file()"
|
||||||
</panel>
|
color="#FFFFFF80" size="16"></image>
|
||||||
<label id="title" pos="26,8"></label>
|
</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>
|
</container>
|
||||||
<textbox
|
<modelviewer id="modelviewer" visible="false" center="0.5,0.5,0.5" cam-rotation="45,-45,0"/>
|
||||||
id='editor'
|
</splitbox>
|
||||||
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>
|
|
||||||
<splitbox orientation="horizontal" split-pos="0.4">
|
<splitbox orientation="horizontal" split-pos="0.4">
|
||||||
<panel id="traceback" padding="4" color="#000000A0">
|
<panel id="traceback" padding="4" color="#000000A0">
|
||||||
</panel>
|
</panel>
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
local writeables = {}
|
local writeables = {}
|
||||||
local registry
|
local registry
|
||||||
local filenames
|
|
||||||
|
|
||||||
local current_file = {
|
local current_file = {
|
||||||
filename = "",
|
filename = "",
|
||||||
@ -49,6 +48,10 @@ events.on("core:error", function (msg, traceback)
|
|||||||
table.insert(errors_all, full)
|
table.insert(errors_all, full)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
events.on("core:open_in_editor", function(filename, linenum)
|
||||||
|
open_file_in_editor(filename, linenum)
|
||||||
|
end)
|
||||||
|
|
||||||
local function find_mutable(filename)
|
local function find_mutable(filename)
|
||||||
local packid = file.prefix(filename)
|
local packid = file.prefix(filename)
|
||||||
if packid == "core" then
|
if packid == "core" then
|
||||||
@ -80,16 +83,6 @@ local function refresh_file_title()
|
|||||||
..(edited and ' *' or '')
|
..(edited and ' *' or '')
|
||||||
end
|
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)
|
function on_control_combination(keycode)
|
||||||
if keycode == input.keycode("s") then
|
if keycode == input.keycode("s") then
|
||||||
save_current_file()
|
save_current_file()
|
||||||
@ -111,10 +104,29 @@ function unlock_access()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function reload_model(filename, name)
|
||||||
|
assets.parse_model("xml", document.editor.text, name)
|
||||||
|
end
|
||||||
|
|
||||||
function run_current_file()
|
function run_current_file()
|
||||||
if not current_file.filename then
|
if not current_file.filename then
|
||||||
return
|
return
|
||||||
end
|
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)
|
local chunk, err = loadstring(document.editor.text, current_file.filename)
|
||||||
clear_output()
|
clear_output()
|
||||||
if not chunk then
|
if not chunk then
|
||||||
@ -126,9 +138,7 @@ function run_current_file()
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
end
|
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()
|
save_current_file()
|
||||||
|
|
||||||
local func = function()
|
local func = function()
|
||||||
@ -198,6 +208,7 @@ events.on("core:open_traceback", function(traceback_b64)
|
|||||||
tb_list.size = srcsize
|
tb_list.size = srcsize
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
--- Save the current file in the code editor if has writeable path.
|
||||||
function save_current_file()
|
function save_current_file()
|
||||||
if not current_file.mutable then
|
if not current_file.mutable then
|
||||||
return
|
return
|
||||||
@ -209,7 +220,22 @@ function save_current_file()
|
|||||||
document.editor.edited = false
|
document.editor.edited = false
|
||||||
end
|
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)
|
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 editor = document.editor
|
||||||
local source = file.read(filename):gsub('\t', ' ')
|
local source = file.read(filename):gsub('\t', ' ')
|
||||||
editor.scroll = 0
|
editor.scroll = 0
|
||||||
@ -229,43 +255,11 @@ function open_file_in_editor(filename, line, mutable)
|
|||||||
document.saveIcon.enabled = current_file.modified
|
document.saveIcon.enabled = current_file.modified
|
||||||
end
|
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)
|
function on_open(mode)
|
||||||
registry = require "core:internal/scripts_registry"
|
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_traceback()
|
||||||
clear_output()
|
clear_output()
|
||||||
|
|
||||||
filenames = registry.filenames
|
|
||||||
table.sort(filenames)
|
|
||||||
build_files_list(filenames)
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -22,8 +22,7 @@
|
|||||||
markup="md"
|
markup="md"
|
||||||
></textbox>
|
></textbox>
|
||||||
</container>
|
</container>
|
||||||
<iframe id="editorRoot" pos="0,30" size-func="-1,gui.get_viewport()[2]-30"
|
<iframe id="editorRoot" pos="0,30" size-func="-1,gui.get_viewport()[2]-30">
|
||||||
src="core:code_editor">
|
|
||||||
</iframe>
|
</iframe>
|
||||||
<textbox id='prompt'
|
<textbox id='prompt'
|
||||||
consumer='submit'
|
consumer='submit'
|
||||||
|
|||||||
@ -104,8 +104,10 @@ function set_mode(mode)
|
|||||||
|
|
||||||
if mode == 'debug' then
|
if mode == 'debug' then
|
||||||
document.root.color = {16, 18, 20, 220}
|
document.root.color = {16, 18, 20, 220}
|
||||||
|
document.editorRoot.src = "core:code_editor"
|
||||||
else
|
else
|
||||||
document.root.color = {0, 0, 0, 128}
|
document.root.color = {0, 0, 0, 128}
|
||||||
|
document.editorRoot.src = ""
|
||||||
end
|
end
|
||||||
|
|
||||||
document.prompt.visible = show_prompt
|
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'
|
<label hover-color='#30A0FF'
|
||||||
pos="20,2"
|
pos="20,2"
|
||||||
interactive="true"
|
interactive="true"
|
||||||
onclick='open_file_in_editor("%{filename}")'
|
onclick='%{open_func}("%{filename}")'
|
||||||
markup='md'
|
markup='md'
|
||||||
tooltip='%{unit}'
|
tooltip='%{unit}'
|
||||||
sizefunc="-1,-1">
|
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 +1,6 @@
|
|||||||
local export = {
|
local export = {
|
||||||
filenames = {},
|
filenames = {},
|
||||||
classification = {}
|
registry = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
local function collect_components(dirname, dest)
|
local function collect_components(dirname, dest)
|
||||||
@ -9,7 +9,7 @@ local function collect_components(dirname, dest)
|
|||||||
for i, filename in ipairs(files) do
|
for i, filename in ipairs(files) do
|
||||||
if file.ext(filename) == "lua" then
|
if file.ext(filename) == "lua" then
|
||||||
table.insert(dest, filename)
|
table.insert(dest, filename)
|
||||||
export.classification[filename] = {
|
export.registry[filename] = {
|
||||||
type="component",
|
type="component",
|
||||||
unit=file.prefix(filename)..":"..file.stem(filename)
|
unit=file.prefix(filename)..":"..file.stem(filename)
|
||||||
}
|
}
|
||||||
@ -33,13 +33,13 @@ local function collect_scripts(dirname, dest, ismodule)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function load_scripts_list()
|
local function load_scripts_list(packs)
|
||||||
local packs = pack.get_installed()
|
local registry = export.registry
|
||||||
for _, packid in ipairs(packs) do
|
for _, packid in ipairs(packs) do
|
||||||
collect_scripts(packid..":modules", export.filenames, true)
|
collect_scripts(packid..":modules", export.filenames, true)
|
||||||
end
|
end
|
||||||
for _, filename in ipairs(export.filenames) do
|
for _, filename in ipairs(export.filenames) do
|
||||||
export.classification[filename] = {
|
registry[filename] = {
|
||||||
type="module",
|
type="module",
|
||||||
unit=file.join(file.parent(file.prefix(filename)..":"..
|
unit=file.join(file.parent(file.prefix(filename)..":"..
|
||||||
filename:sub(filename:find("/")+1)),
|
filename:sub(filename:find("/")+1)),
|
||||||
@ -51,27 +51,39 @@ local function load_scripts_list()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function export.build_classification()
|
local function load_models_list(packs)
|
||||||
local classification = {}
|
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
|
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
|
end
|
||||||
for id, props in pairs(item.properties) do
|
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
|
end
|
||||||
local packs = pack.get_installed()
|
local packs = pack.get_installed()
|
||||||
for _, packid in ipairs(packs) do
|
for _, packid in ipairs(packs) do
|
||||||
classification[packid..":scripts/world.lua"] = {type="world", unit=packid}
|
registry[packid..":scripts/world.lua"] = {type="world", unit=packid}
|
||||||
classification[packid..":scripts/hud.lua"] = {type="hud", unit=packid}
|
registry[packid..":scripts/hud.lua"] = {type="hud", unit=packid}
|
||||||
end
|
end
|
||||||
export.classification = classification
|
export.registry = registry
|
||||||
export.filenames = {}
|
export.filenames = {}
|
||||||
|
|
||||||
load_scripts_list()
|
load_scripts_list(packs)
|
||||||
|
load_models_list(packs)
|
||||||
end
|
end
|
||||||
|
|
||||||
function export.get_info(filename)
|
function export.get_info(filename)
|
||||||
return export.classification[filename]
|
return export.registry[filename]
|
||||||
end
|
end
|
||||||
|
|
||||||
return export
|
return export
|
||||||
|
|||||||
@ -63,4 +63,4 @@ cache_names(block)
|
|||||||
cache_names(item)
|
cache_names(item)
|
||||||
|
|
||||||
local scripts_registry = require "core:internal/scripts_registry"
|
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)
|
table.insert(__post_runnables, runnable)
|
||||||
end
|
end
|
||||||
|
|
||||||
assets = {}
|
|
||||||
assets.load_texture = core.__load_texture
|
|
||||||
|
|
||||||
-- --------- Deprecated functions ------ --
|
-- --------- Deprecated functions ------ --
|
||||||
local function wrap_deprecated(func, name, alternatives)
|
local function wrap_deprecated(func, name, alternatives)
|
||||||
return function (...)
|
return function (...)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#include "assetload_funcs.hpp"
|
#include "assetload_funcs.hpp"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -10,6 +11,7 @@
|
|||||||
#include "coders/imageio.hpp"
|
#include "coders/imageio.hpp"
|
||||||
#include "coders/json.hpp"
|
#include "coders/json.hpp"
|
||||||
#include "coders/obj.hpp"
|
#include "coders/obj.hpp"
|
||||||
|
#include "coders/vcm.hpp"
|
||||||
#include "coders/vec3.hpp"
|
#include "coders/vec3.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
@ -300,7 +302,8 @@ assetload::postfunc assetload::sound(
|
|||||||
|
|
||||||
static void request_textures(AssetsLoader* loader, const model::Model& model) {
|
static void request_textures(AssetsLoader* loader, const model::Model& model) {
|
||||||
for (auto& mesh : model.meshes) {
|
for (auto& mesh : model.meshes) {
|
||||||
if (mesh.texture.find('$') == std::string::npos) {
|
if (mesh.texture.find('$') == std::string::npos &&
|
||||||
|
mesh.texture.find(':') == std::string::npos) {
|
||||||
auto filename = TEXTURES_FOLDER + "/" + mesh.texture;
|
auto filename = TEXTURES_FOLDER + "/" + mesh.texture;
|
||||||
loader->add(
|
loader->add(
|
||||||
AssetType::TEXTURE, filename, mesh.texture, nullptr
|
AssetType::TEXTURE, filename, mesh.texture, nullptr
|
||||||
@ -337,9 +340,40 @@ assetload::postfunc assetload::model(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
path = paths.find(file + ".obj");
|
path = paths.find(file + ".obj");
|
||||||
|
if (io::exists(path)) {
|
||||||
|
auto text = io::read_string(path);
|
||||||
|
try {
|
||||||
|
auto model = obj::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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (path.empty()) {
|
||||||
|
throw std::runtime_error("could not to find model " + util::quote(file));
|
||||||
|
}
|
||||||
|
|
||||||
auto text = io::read_string(path);
|
auto text = io::read_string(path);
|
||||||
try {
|
try {
|
||||||
auto model = obj::parse(path.string(), text).release();
|
auto model = vcm::parse(path.string(), text).release();
|
||||||
return [=](Assets* assets) {
|
return [=](Assets* assets) {
|
||||||
request_textures(loader, *model);
|
request_textures(loader, *model);
|
||||||
assets->store(std::unique_ptr<model::Model>(model), name);
|
assets->store(std::unique_ptr<model::Model>(model), name);
|
||||||
|
|||||||
@ -9,7 +9,11 @@ util::TextureRegion util::get_texture_region(
|
|||||||
) {
|
) {
|
||||||
size_t sep = name.find(':');
|
size_t sep = name.find(':');
|
||||||
if (sep == std::string::npos) {
|
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 {
|
} else {
|
||||||
auto atlas = assets.get<Atlas>(name.substr(0, sep));
|
auto atlas = assets.get<Atlas>(name.substr(0, sep));
|
||||||
if (atlas) {
|
if (atlas) {
|
||||||
|
|||||||
161
src/coders/vcm.cpp
Normal file
161
src/coders/vcm.cpp
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
#include "vcm.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "xml.hpp"
|
||||||
|
#include "util/stringutil.hpp"
|
||||||
|
#include "graphics/commons/Model.hpp"
|
||||||
|
#include "io/io.hpp"
|
||||||
|
|
||||||
|
using namespace vcm;
|
||||||
|
using namespace xml;
|
||||||
|
|
||||||
|
static const std::unordered_map<std::string, int> side_indices {
|
||||||
|
{"north", 0},
|
||||||
|
{"south", 1},
|
||||||
|
{"top", 2},
|
||||||
|
{"bottom", 3},
|
||||||
|
{"west", 4},
|
||||||
|
{"east", 5},
|
||||||
|
};
|
||||||
|
|
||||||
|
static void perform_rect(const xmlelement& root, model::Model& model) {
|
||||||
|
auto from = root.attr("from").asVec3();
|
||||||
|
auto right = root.attr("right").asVec3();
|
||||||
|
auto up = root.attr("up").asVec3();
|
||||||
|
|
||||||
|
right *= -1;
|
||||||
|
from -= right;
|
||||||
|
|
||||||
|
UVRegion region {};
|
||||||
|
if (root.has("region")) {
|
||||||
|
region.set(root.attr("region").asVec4());
|
||||||
|
} else {
|
||||||
|
region.scale(glm::length(right), glm::length(up));
|
||||||
|
}
|
||||||
|
if (root.has("region-scale")) {
|
||||||
|
region.scale(root.attr("region-scale").asVec2());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto flip = root.attr("flip", "").getText();
|
||||||
|
if (flip == "h") {
|
||||||
|
std::swap(region.u1, region.u2);
|
||||||
|
right *= -1;
|
||||||
|
from -= right;
|
||||||
|
} else if (flip == "v") {
|
||||||
|
std::swap(region.v1, region.v2);
|
||||||
|
up *= -1;
|
||||||
|
from -= up;
|
||||||
|
}
|
||||||
|
std::string texture = root.attr("texture", "$0").getText();
|
||||||
|
auto& mesh = model.addMesh(texture);
|
||||||
|
|
||||||
|
auto normal = glm::cross(glm::normalize(right), glm::normalize(up));
|
||||||
|
mesh.addRect(
|
||||||
|
from + right * 0.5f + up * 0.5f,
|
||||||
|
right * 0.5f,
|
||||||
|
up * 0.5f,
|
||||||
|
normal,
|
||||||
|
region
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void perform_box(const xmlelement& root, model::Model& model) {
|
||||||
|
auto from = root.attr("from").asVec3();
|
||||||
|
auto to = root.attr("to").asVec3();
|
||||||
|
|
||||||
|
UVRegion regions[6] {};
|
||||||
|
regions[0].scale(to.x - from.x, to.y - from.y);
|
||||||
|
regions[1].scale(from.x - to.x, to.y - from.y);
|
||||||
|
regions[2].scale(to.x - from.x, to.z - from.z);
|
||||||
|
regions[3].scale(from.x - to.x, to.z - from.z);
|
||||||
|
regions[4].scale(to.z - from.z, to.y - from.y);
|
||||||
|
regions[5].scale(from.z - to.z, to.y - from.y);
|
||||||
|
|
||||||
|
auto center = (from + to) * 0.5f;
|
||||||
|
auto halfsize = (to - from) * 0.5f;
|
||||||
|
|
||||||
|
std::string texfaces[6] {"$0","$1","$2","$3","$4","$5"};
|
||||||
|
|
||||||
|
for (const auto& elem : root.getElements()) {
|
||||||
|
if (elem->getTag() == "part") {
|
||||||
|
// todo: replace by expression parsing
|
||||||
|
auto tags = util::split(elem->attr("tags").getText(), ',');
|
||||||
|
for (auto& tag : tags) {
|
||||||
|
util::trim(tag);
|
||||||
|
const auto& found = side_indices.find(tag);
|
||||||
|
if (found == side_indices.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int idx = found->second;
|
||||||
|
if (elem->has("texture")) {
|
||||||
|
texfaces[idx] = elem->attr("texture").getText();
|
||||||
|
}
|
||||||
|
if (elem->has("region")) {
|
||||||
|
regions[idx].set(elem->attr("region").asVec4());
|
||||||
|
}
|
||||||
|
if (elem->has("region-scale")) {
|
||||||
|
regions[idx].scale(elem->attr("region-scale").asVec2());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool deleted[6] {};
|
||||||
|
if (root.has("delete")) {
|
||||||
|
// todo: replace by expression parsing
|
||||||
|
auto names = util::split(root.attr("delete").getText(), ',');
|
||||||
|
for (auto& name : names) {
|
||||||
|
util::trim(name);
|
||||||
|
const auto& found = side_indices.find(name);
|
||||||
|
if (found != side_indices.end()) {
|
||||||
|
deleted[found->second] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
if (deleted[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
bool enabled[6] {};
|
||||||
|
enabled[i] = true;
|
||||||
|
auto& mesh = model.addMesh(texfaces[i]);
|
||||||
|
mesh.addBox(center, halfsize, regions, enabled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::unique_ptr<model::Model> load_model(const xmlelement& root) {
|
||||||
|
model::Model model;
|
||||||
|
|
||||||
|
for (const auto& elem : root.getElements()) {
|
||||||
|
auto tag = elem->getTag();
|
||||||
|
|
||||||
|
if (tag == "rect") {
|
||||||
|
perform_rect(*elem, model);
|
||||||
|
} else if (tag == "box") {
|
||||||
|
perform_box(*elem, model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_unique<model::Model>(std::move(model));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<model::Model> vcm::parse(
|
||||||
|
std::string_view file, std::string_view src
|
||||||
|
) {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/coders/vcm.hpp
Normal file
12
src/coders/vcm.hpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace model {
|
||||||
|
struct Model;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace vcm {
|
||||||
|
std::unique_ptr<model::Model> parse(std::string_view file, std::string_view src);
|
||||||
|
}
|
||||||
@ -355,6 +355,83 @@ std::unique_ptr<Document> xml::parse(
|
|||||||
return parser.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(
|
inline void newline(
|
||||||
std::stringstream& ss, bool nice, const std::string& indentStr, int indent
|
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::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;
|
using xmlelement = Node;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,17 +37,15 @@ void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
|
|||||||
}
|
}
|
||||||
if (def.model.type == BlockModelType::CUSTOM) {
|
if (def.model.type == BlockModelType::CUSTOM) {
|
||||||
auto model = assets.require<model::Model>(def.model.name);
|
auto model = assets.require<model::Model>(def.model.name);
|
||||||
// temporary dirty fix tbh
|
|
||||||
if (def.model.name.find(':') == std::string::npos) {
|
for (auto& mesh : model.meshes) {
|
||||||
for (auto& mesh : model.meshes) {
|
size_t pos = mesh.texture.find(':');
|
||||||
size_t pos = mesh.texture.find(':');
|
if (pos == std::string::npos) {
|
||||||
if (pos == std::string::npos) {
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
|
||||||
if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
|
for (auto& vertex : mesh.vertices) {
|
||||||
for (auto& vertex : mesh.vertices) {
|
vertex.uv = region->apply(vertex.uv);
|
||||||
vertex.uv = region->apply(vertex.uv);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,22 @@ void Mesh::addPlane(
|
|||||||
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
|
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Mesh::addRect(
|
||||||
|
const glm::vec3& pos,
|
||||||
|
const glm::vec3& right,
|
||||||
|
const glm::vec3& up,
|
||||||
|
const glm::vec3& norm,
|
||||||
|
const UVRegion& uv
|
||||||
|
) {
|
||||||
|
vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm});
|
||||||
|
vertices.push_back({pos+right-up, {uv.u2, uv.v1}, norm});
|
||||||
|
vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm});
|
||||||
|
|
||||||
|
vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm});
|
||||||
|
vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm});
|
||||||
|
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
|
||||||
|
}
|
||||||
|
|
||||||
void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
|
void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
|
||||||
addPlane(pos+Z*size, X*size, Y*size, Z);
|
addPlane(pos+Z*size, X*size, Y*size, Z);
|
||||||
addPlane(pos-Z*size, -X*size, Y*size, -Z);
|
addPlane(pos-Z*size, -X*size, Y*size, -Z);
|
||||||
@ -51,16 +67,23 @@ void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::addBox(
|
void Mesh::addBox(
|
||||||
const glm::vec3& pos, const glm::vec3& size, const UVRegion (&uvs)[6]
|
const glm::vec3& pos,
|
||||||
|
const glm::vec3& size,
|
||||||
|
const UVRegion (&uvs)[6],
|
||||||
|
const bool enabledSides[6]
|
||||||
) {
|
) {
|
||||||
addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]);
|
if (enabledSides[0]) // north
|
||||||
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
|
addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]);
|
||||||
|
if (enabledSides[1]) // south
|
||||||
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]);
|
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
|
||||||
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]);
|
if (enabledSides[2]) // top
|
||||||
|
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1));
|
||||||
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
|
if (enabledSides[3]) // bottom
|
||||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]);
|
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] * glm::vec2(-1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::scale(const glm::vec3& size) {
|
void Mesh::scale(const glm::vec3& size) {
|
||||||
|
|||||||
@ -31,11 +31,19 @@ namespace model {
|
|||||||
const glm::vec3& norm,
|
const glm::vec3& norm,
|
||||||
const UVRegion& region
|
const UVRegion& region
|
||||||
);
|
);
|
||||||
|
void addRect(
|
||||||
|
const glm::vec3& pos,
|
||||||
|
const glm::vec3& right,
|
||||||
|
const glm::vec3& up,
|
||||||
|
const glm::vec3& norm,
|
||||||
|
const UVRegion& region
|
||||||
|
);
|
||||||
void addBox(const glm::vec3& pos, const glm::vec3& size);
|
void addBox(const glm::vec3& pos, const glm::vec3& size);
|
||||||
void addBox(
|
void addBox(
|
||||||
const glm::vec3& pos,
|
const glm::vec3& pos,
|
||||||
const glm::vec3& size,
|
const glm::vec3& size,
|
||||||
const UVRegion (&texfaces)[6]
|
const UVRegion (&texfaces)[6],
|
||||||
|
const bool enabledSides[6]
|
||||||
);
|
);
|
||||||
void scale(const glm::vec3& size);
|
void scale(const glm::vec3& size);
|
||||||
};
|
};
|
||||||
@ -47,6 +55,11 @@ namespace model {
|
|||||||
/// @param texture texture name
|
/// @param texture texture name
|
||||||
/// @return writeable Mesh
|
/// @return writeable Mesh
|
||||||
Mesh& addMesh(const std::string& texture) {
|
Mesh& addMesh(const std::string& texture) {
|
||||||
|
for (auto& mesh : meshes) {
|
||||||
|
if (mesh.texture == texture) {
|
||||||
|
return mesh;
|
||||||
|
}
|
||||||
|
}
|
||||||
meshes.push_back({texture, {}});
|
meshes.push_back({texture, {}});
|
||||||
return meshes[meshes.size()-1];
|
return meshes[meshes.size()-1];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,11 @@
|
|||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
#include "maths/UVRegion.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)
|
Batch3D::Batch3D(size_t capacity)
|
||||||
: capacity(capacity) {
|
: capacity(capacity) {
|
||||||
|
|
||||||
@ -31,20 +36,28 @@ void Batch3D::begin(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Batch3D::vertex(
|
void Batch3D::vertex(
|
||||||
float x, float y, float z, float u, float v,
|
float x,
|
||||||
float r, float g, float b, float a
|
float y,
|
||||||
|
float z,
|
||||||
|
float u,
|
||||||
|
float v,
|
||||||
|
float r,
|
||||||
|
float g,
|
||||||
|
float b,
|
||||||
|
float a
|
||||||
) {
|
) {
|
||||||
buffer[index].position = {x, y, z};
|
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};
|
buffer[index].color = {r, g, b, a};
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
void Batch3D::vertex(
|
void Batch3D::vertex(
|
||||||
glm::vec3 coord, float u, float v,
|
glm::vec3 coord, float u, float v, float r, float g, float b, float a
|
||||||
float r, float g, float b, float a
|
|
||||||
) {
|
) {
|
||||||
buffer[index].position = coord;
|
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};
|
buffer[index].color = {r, g, b, a};
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@ -54,7 +67,9 @@ void Batch3D::vertex(
|
|||||||
float r, float g, float b, float a
|
float r, float g, float b, float a
|
||||||
) {
|
) {
|
||||||
buffer[index].position = point;
|
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};
|
buffer[index].color = {r, g, b, a};
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@ -86,14 +101,18 @@ void Batch3D::face(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Batch3D::texture(const Texture* new_texture){
|
void Batch3D::texture(const Texture* new_texture){
|
||||||
if (currentTexture == new_texture)
|
if (currentTexture == new_texture) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
flush();
|
flush();
|
||||||
currentTexture = new_texture;
|
currentTexture = new_texture;
|
||||||
if (new_texture == nullptr)
|
if (new_texture == nullptr) {
|
||||||
blank->bind();
|
blank->bind();
|
||||||
else
|
region = blank->getUVRegion();
|
||||||
|
} else {
|
||||||
new_texture->bind();
|
new_texture->bind();
|
||||||
|
region = currentTexture->getUVRegion();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch3D::sprite(
|
void Batch3D::sprite(
|
||||||
@ -240,6 +259,16 @@ void Batch3D::blockCube(
|
|||||||
cube((1.0f - size) * -0.5f, size, texfaces, tint, shading);
|
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(
|
void Batch3D::vertex(
|
||||||
const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint
|
const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
#include "commons.hpp"
|
#include "commons.hpp"
|
||||||
#include "MeshData.hpp"
|
#include "MeshData.hpp"
|
||||||
|
#include "maths/UVRegion.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@ -32,8 +33,8 @@ class Batch3D : public Flushable {
|
|||||||
std::unique_ptr<Texture> blank;
|
std::unique_ptr<Texture> blank;
|
||||||
size_t index;
|
size_t index;
|
||||||
glm::vec4 tint {1.0f};
|
glm::vec4 tint {1.0f};
|
||||||
|
|
||||||
const Texture* currentTexture;
|
const Texture* currentTexture;
|
||||||
|
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
|
||||||
|
|
||||||
void vertex(
|
void vertex(
|
||||||
float x, float y, float z,
|
float x, float y, float z,
|
||||||
@ -102,6 +103,8 @@ public:
|
|||||||
const glm::vec4& tint,
|
const glm::vec4& tint,
|
||||||
bool shading = true
|
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 vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec4& tint);
|
||||||
void point(const glm::vec3& pos, const glm::vec4& tint);
|
void point(const glm::vec3& pos, const glm::vec4& tint);
|
||||||
void flush() override;
|
void flush() override;
|
||||||
|
|||||||
@ -34,7 +34,7 @@ public:
|
|||||||
Batch2D* getBatch2D() const;
|
Batch2D* getBatch2D() const;
|
||||||
|
|
||||||
const glm::uvec2& getViewport() 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 setViewport(const glm::uvec2& viewport);
|
||||||
void setFramebuffer(Framebuffer* fbo);
|
void setFramebuffer(Framebuffer* fbo);
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
GLSLExtension* Shader::preprocessor = new GLSLExtension();
|
GLSLExtension* Shader::preprocessor = new GLSLExtension();
|
||||||
|
Shader* Shader::used = nullptr;
|
||||||
|
|
||||||
Shader::Shader(uint id) : id(id){
|
Shader::Shader(uint id) : id(id){
|
||||||
}
|
}
|
||||||
@ -25,6 +26,7 @@ Shader::~Shader(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Shader::use(){
|
void Shader::use(){
|
||||||
|
used = this;
|
||||||
glUseProgram(id);
|
glUseProgram(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,3 +133,7 @@ std::unique_ptr<Shader> Shader::create(
|
|||||||
}
|
}
|
||||||
return std::make_unique<Shader>(id);
|
return std::make_unique<Shader>(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Shader& Shader::getUsed() {
|
||||||
|
return *used;
|
||||||
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
class GLSLExtension;
|
class GLSLExtension;
|
||||||
|
|
||||||
class Shader {
|
class Shader {
|
||||||
|
static Shader* used;
|
||||||
uint id;
|
uint id;
|
||||||
std::unordered_map<std::string, uint> uniformLocations;
|
std::unordered_map<std::string, uint> uniformLocations;
|
||||||
|
|
||||||
@ -43,4 +44,6 @@ public:
|
|||||||
const std::string& vertexSource,
|
const std::string& vertexSource,
|
||||||
const std::string& fragmentSource
|
const std::string& fragmentSource
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static Shader& getUsed();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -50,16 +50,31 @@ static inline UVRegion get_region_for(
|
|||||||
|
|
||||||
void ModelsGenerator::prepare(Content& content, Assets& assets) {
|
void ModelsGenerator::prepare(Content& content, Assets& assets) {
|
||||||
for (auto& [name, def] : content.blocks.getDefs()) {
|
for (auto& [name, def] : content.blocks.getDefs()) {
|
||||||
if (def->model.type == BlockModelType::CUSTOM && def->model.name.empty()) {
|
if (def->model.type == BlockModelType::CUSTOM) {
|
||||||
assets.store(
|
if (def->model.name.empty()) {
|
||||||
std::make_unique<model::Model>(
|
assets.store(
|
||||||
loadCustomBlockModel(
|
std::make_unique<model::Model>(
|
||||||
def->model.customRaw, assets, !def->shadeless
|
loadCustomBlockModel(
|
||||||
)
|
def->model.customRaw, assets, !def->shadeless
|
||||||
),
|
)
|
||||||
name + ".model"
|
),
|
||||||
);
|
name + ".model"
|
||||||
def->model.name = def->name + ".model";
|
);
|
||||||
|
def->model.name = def->name + ".model";
|
||||||
|
} else {
|
||||||
|
auto srcModel = assets.get<model::Model>(def->model.name);
|
||||||
|
if (srcModel) {
|
||||||
|
auto model = std::make_unique<model::Model>(*srcModel);
|
||||||
|
for (auto& mesh : model->meshes) {
|
||||||
|
if (mesh.texture.length() && mesh.texture[0] == '$') {
|
||||||
|
int index = std::stoll(mesh.texture.substr(1));
|
||||||
|
mesh.texture = "blocks:" + def->textureFaces[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
def->model.name = name + ".model";
|
||||||
|
assets.store(std::move(model), def->model.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto& [name, def] : content.items.getDefs()) {
|
for (auto& [name, def] : content.items.getDefs()) {
|
||||||
@ -83,7 +98,7 @@ model::Model ModelsGenerator::fromCustom(
|
|||||||
for (size_t i = 0; i < modelBoxes.size(); i++) {
|
for (size_t i = 0; i < modelBoxes.size(); i++) {
|
||||||
auto& mesh = model.addMesh("blocks:");
|
auto& mesh = model.addMesh("blocks:");
|
||||||
mesh.lighting = lighting;
|
mesh.lighting = lighting;
|
||||||
const UVRegion boxtexfaces[6] = {
|
UVRegion boxtexfaces[6] = {
|
||||||
get_region_for(modelTextures[i * 6 + 5], assets),
|
get_region_for(modelTextures[i * 6 + 5], assets),
|
||||||
get_region_for(modelTextures[i * 6 + 4], assets),
|
get_region_for(modelTextures[i * 6 + 4], assets),
|
||||||
get_region_for(modelTextures[i * 6 + 3], assets),
|
get_region_for(modelTextures[i * 6 + 3], assets),
|
||||||
@ -91,8 +106,15 @@ model::Model ModelsGenerator::fromCustom(
|
|||||||
get_region_for(modelTextures[i * 6 + 1], assets),
|
get_region_for(modelTextures[i * 6 + 1], assets),
|
||||||
get_region_for(modelTextures[i * 6 + 0], 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(
|
mesh.addBox(
|
||||||
modelBoxes[i].center(), modelBoxes[i].size() * 0.5f, boxtexfaces
|
modelBoxes[i].center(),
|
||||||
|
modelBoxes[i].size() * 0.5f,
|
||||||
|
boxtexfaces,
|
||||||
|
enabled
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < points.size() / 4; i++) {
|
for (size_t i = 0; i < points.size() / 4; i++) {
|
||||||
|
|||||||
@ -36,6 +36,7 @@ void InlineFrame::setDocument(const std::shared_ptr<UiDocument>& document) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InlineFrame::act(float delta) {
|
void InlineFrame::act(float delta) {
|
||||||
|
Container::act(delta);
|
||||||
if (document || src.empty()) {
|
if (document || src.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
152
src/graphics/ui/elements/ModelViewer.cpp
Normal file
152
src/graphics/ui/elements/ModelViewer.cpp
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
#include <array>
|
||||||
|
#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 "SplitBox.hpp"
|
||||||
|
|
||||||
|
#include "util/stack_vector.hpp"
|
||||||
|
|
||||||
using namespace gui;
|
using namespace gui;
|
||||||
|
|
||||||
SplitBox::SplitBox(GUI& gui, const glm::vec2& size, float splitPos, Orientation orientation)
|
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() {
|
void SplitBox::refresh() {
|
||||||
Container::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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 size = getSize();
|
glm::vec2 size = getSize();
|
||||||
if (nodes.size() == 1) {
|
if (visibleNodes.size() == 1) {
|
||||||
auto node = nodes.at(0);
|
auto node = visibleNodes.at(0);
|
||||||
node->setPos(glm::vec2());
|
node->setPos(glm::vec2());
|
||||||
node->setSize(size);
|
node->setSize(size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto nodeA = nodes.at(0);
|
auto nodeA = visibleNodes.at(0);
|
||||||
auto nodeB = nodes.at(1);
|
auto nodeB = visibleNodes.at(1);
|
||||||
|
|
||||||
float sepRadius = interval / 2.0f;
|
float sepRadius = interval / 2.0f;
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,7 @@
|
|||||||
#include "elements/Panel.hpp"
|
#include "elements/Panel.hpp"
|
||||||
#include "elements/TextBox.hpp"
|
#include "elements/TextBox.hpp"
|
||||||
#include "elements/TrackBar.hpp"
|
#include "elements/TrackBar.hpp"
|
||||||
|
#include "elements/ModelViewer.hpp"
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "frontend/locale.hpp"
|
#include "frontend/locale.hpp"
|
||||||
#include "frontend/menu.hpp"
|
#include "frontend/menu.hpp"
|
||||||
@ -368,6 +369,23 @@ static std::shared_ptr<UINode> read_split_box(
|
|||||||
return splitBox;
|
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(
|
static std::shared_ptr<UINode> read_panel(
|
||||||
UiXmlReader& reader, const xml::xmlelement& element
|
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("trackbar", read_track_bar);
|
||||||
add("container", read_container);
|
add("container", read_container);
|
||||||
add("bindbox", read_input_bind_box);
|
add("bindbox", read_input_bind_box);
|
||||||
|
add("modelviewer", read_model_viewer);
|
||||||
add("inventory", read_inventory);
|
add("inventory", read_inventory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -192,6 +192,9 @@ std::string EnginePaths::createWriteableDevice(const std::string& name) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (name == "core") {
|
||||||
|
folder = "res:";
|
||||||
|
}
|
||||||
if (folder.emptyOrInvalid()) {
|
if (folder.emptyOrInvalid()) {
|
||||||
throw std::runtime_error("pack not found");
|
throw std::runtime_error("pack not found");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
extern const luaL_Reg applib[];
|
extern const luaL_Reg applib[];
|
||||||
|
extern const luaL_Reg assetslib[];
|
||||||
extern const luaL_Reg audiolib[];
|
extern const luaL_Reg audiolib[];
|
||||||
extern const luaL_Reg base64lib[];
|
extern const luaL_Reg base64lib[];
|
||||||
extern const luaL_Reg bjsonlib[];
|
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}
|
||||||
|
};
|
||||||
@ -1,13 +1,12 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
#include "coders/png.hpp"
|
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "assets/Assets.hpp"
|
#include "assets/Assets.hpp"
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
#include "content/ContentControl.hpp"
|
#include "content/ContentControl.hpp"
|
||||||
#include "debug/Logger.hpp"
|
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "io/engine_paths.hpp"
|
#include "io/engine_paths.hpp"
|
||||||
#include "io/io.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");
|
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) {
|
static int l_open_folder(lua::State* L) {
|
||||||
platform::open_folder(io::resolve(lua::require_string(L, 1)));
|
platform::open_folder(io::resolve(lua::require_string(L, 1)));
|
||||||
return 0;
|
return 0;
|
||||||
@ -322,6 +286,5 @@ const luaL_Reg corelib[] = {
|
|||||||
{"open_folder", lua::wrap<l_open_folder>},
|
{"open_folder", lua::wrap<l_open_folder>},
|
||||||
{"quit", lua::wrap<l_quit>},
|
{"quit", lua::wrap<l_quit>},
|
||||||
{"capture_output", lua::wrap<l_capture_output>},
|
{"capture_output", lua::wrap<l_capture_output>},
|
||||||
{"__load_texture", lua::wrap<l_load_texture>},
|
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include "graphics/ui/elements/TextBox.hpp"
|
#include "graphics/ui/elements/TextBox.hpp"
|
||||||
#include "graphics/ui/elements/TrackBar.hpp"
|
#include "graphics/ui/elements/TrackBar.hpp"
|
||||||
#include "graphics/ui/elements/InlineFrame.hpp"
|
#include "graphics/ui/elements/InlineFrame.hpp"
|
||||||
|
#include "graphics/ui/elements/ModelViewer.hpp"
|
||||||
#include "graphics/ui/gui_util.hpp"
|
#include "graphics/ui/gui_util.hpp"
|
||||||
#include "graphics/ui/markdown.hpp"
|
#include "graphics/ui/markdown.hpp"
|
||||||
#include "graphics/core/Font.hpp"
|
#include "graphics/core/Font.hpp"
|
||||||
@ -114,6 +115,16 @@ static int l_node_destruct(lua::State* L) {
|
|||||||
return 0;
|
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) {
|
static int l_node_reposition(lua::State* L) {
|
||||||
auto docnode = get_document_node(L);
|
auto docnode = get_document_node(L);
|
||||||
docnode.node->reposition();
|
docnode.node->reposition();
|
||||||
@ -341,6 +352,8 @@ static int p_get_src(UINode* node, lua::State* L) {
|
|||||||
return lua::pushstring(L, image->getTexture());
|
return lua::pushstring(L, image->getTexture());
|
||||||
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
||||||
return lua::pushstring(L, iframe->getSrc());
|
return lua::pushstring(L, iframe->getSrc());
|
||||||
|
} else if (auto modelviewer = dynamic_cast<ModelViewer*>(node)) {
|
||||||
|
return lua::pushstring(L, modelviewer->getModel());
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -408,6 +421,13 @@ static int p_get_destruct(UINode*, lua::State* L) {
|
|||||||
return lua::pushcfunction(L, lua::wrap<l_node_destruct>);
|
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) {
|
static int p_get_reposition(UINode*, lua::State* L) {
|
||||||
return lua::pushcfunction(L, lua::wrap<l_node_reposition>);
|
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) {
|
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 docname = lua::require_string(L, 1);
|
||||||
auto element = lua::require_string(L, 2);
|
auto element = lua::require_string(L, 2);
|
||||||
if (lua::isnumber(L, 3)) {
|
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});
|
const auto& id = request_node_id(DocumentNode {docnode.document, node});
|
||||||
return push_document_node(L, id);
|
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);
|
auto attr = lua::require_string(L, 3);
|
||||||
|
|
||||||
static const std::unordered_map<
|
static const std::unordered_map<
|
||||||
@ -530,6 +559,7 @@ static int l_gui_getattr(lua::State* L) {
|
|||||||
{"moveInto", p_move_into},
|
{"moveInto", p_move_into},
|
||||||
{"add", p_get_add},
|
{"add", p_get_add},
|
||||||
{"destruct", p_get_destruct},
|
{"destruct", p_get_destruct},
|
||||||
|
{"refresh", p_refresh},
|
||||||
{"reposition", p_get_reposition},
|
{"reposition", p_get_reposition},
|
||||||
{"clear", p_get_clear},
|
{"clear", p_get_clear},
|
||||||
{"setInterval", p_set_interval},
|
{"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));
|
image->setTexture(lua::require_string(L, idx));
|
||||||
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
||||||
iframe->setSrc(lua::require_string(L, idx));
|
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) {
|
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) {
|
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 docname = lua::require_string(L, 1);
|
||||||
auto element = lua::require_string(L, 2);
|
auto element = lua::require_string(L, 2);
|
||||||
auto attr = lua::require_string(L, 3);
|
auto attr = lua::require_string(L, 3);
|
||||||
|
|||||||
@ -64,6 +64,7 @@ static void create_libs(State* L, StateType stateType) {
|
|||||||
openlib(L, "__vc_app", applib);
|
openlib(L, "__vc_app", applib);
|
||||||
}
|
}
|
||||||
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
|
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
|
||||||
|
openlib(L, "assets", assetslib);
|
||||||
openlib(L, "audio", audiolib);
|
openlib(L, "audio", audiolib);
|
||||||
openlib(L, "console", consolelib);
|
openlib(L, "console", consolelib);
|
||||||
openlib(L, "core", corelib);
|
openlib(L, "core", corelib);
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <glm/vec2.hpp>
|
#include <glm/vec2.hpp>
|
||||||
|
#include <glm/vec4.hpp>
|
||||||
|
|
||||||
struct UVRegion {
|
struct UVRegion {
|
||||||
float u1;
|
float u1;
|
||||||
@ -42,8 +43,8 @@ struct UVRegion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void scale(float x, float y) {
|
void scale(float x, float y) {
|
||||||
float w = getWidth();
|
float w = u2 - u1;
|
||||||
float h = getHeight();
|
float h = v2 - v1;
|
||||||
float cx = (u1 + u2) * 0.5f;
|
float cx = (u1 + u2) * 0.5f;
|
||||||
float cy = (v1 + v2) * 0.5f;
|
float cy = (v1 + v2) * 0.5f;
|
||||||
u1 = cx - w * 0.5f * x;
|
u1 = cx - w * 0.5f * x;
|
||||||
@ -51,4 +52,21 @@ struct UVRegion {
|
|||||||
u2 = cx + w * 0.5f * x;
|
u2 = cx + w * 0.5f * x;
|
||||||
v2 = cy + h * 0.5f * y;
|
v2 = cy + h * 0.5f * y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scale(const glm::vec2& vec) {
|
||||||
|
scale(vec.x, vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set(const glm::vec4& vec) {
|
||||||
|
u1 = vec.x;
|
||||||
|
v1 = vec.y;
|
||||||
|
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