Skip to content

Commit 18a8d1e

Browse files
authored
rewrite editor script to use editor.transact api (#115)
1 parent d91e44f commit 18a8d1e

File tree

3 files changed

+195
-88
lines changed

3 files changed

+195
-88
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ Or point to the ZIP file of a [specific release](https://github.com/britzl/monar
2020
Using Monarch requires that screens are created in a certain way. Once you have one or more screens created you can start navigating between the screens.
2121

2222
## Editor Script
23-
Right click in on a`.gui` file in the outline and selected the menu item, it creates a `.collection` and a `.gui_script` with the same name as the `.gui` file. It adds the file with some basic setup done to them, adding the selected gui script to the created gui scene and in turns adds the gui scene to the newly created collection.
23+
Right click in on a`.gui`, `.gui_script` or `.collection` file in the outline and selected the menu item, it creates the other two file types with the same name as the selected file. It adds the file with some basic setup done to them, adding the selected gui script to the created gui scene and in turns adds the gui scene to the newly created collection.
24+
25+
You can also right click on directory to get the option to create a monarch scene from it, the directory should be empty (or only contain other monarch scene files).Use the option and it will create all three files inside of the directory.
2426

2527
<img src="/docs/editor_script.gif" width="200px">
2628

29+
You can use Monarchs editor script with your own custom templates. Create the template files you want, these are normal defold files. Then inside of your game.project file add a path to the files you created.
30+
31+
2732
## Creating screens
2833
Monarch screens are created in individual collections and either loaded through collection proxies or created through collection factories.
2934

Lines changed: 179 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
local M = {}
22

33
local collection_template
4-
local gui_script_content
5-
local gui_file_content
4+
local gui_script_template
5+
local gui_template
6+
7+
local function _log(msg)
8+
io.stdout:write("ERROR:MONARCH: " .. msg .. "\n")
9+
io.stdout:flush()
10+
end
611

712
local function ends_with(str, ending)
813
return ending == "" or str:sub(-#ending) == ending
@@ -13,48 +18,143 @@ local function file_exists(name)
1318
if f~=nil then io.close(f) return true else return false end
1419
end
1520

16-
local function get_filename(path)
21+
local function get_filename(path)
1722
local main, filename, extension = path:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
1823
return main, filename
1924
end
2025

21-
local function create_files(file_path)
22-
-- Construct paths
23-
local path = editor.get(file_path, "path")
24-
local main, filename = get_filename(path)
25-
local basename = filename:match("(.+)%..+")
26-
local target_collection_path = "." .. main .. basename .. ".collection"
27-
local target_gui_script_path = "." .. main .. basename .. ".gui_script"
28-
local target_gui_path = "." .. main .. basename .. ".gui"
29-
30-
-- Create the files if they don't exists
31-
if not file_exists(target_collection_path) then
32-
local collection_content = collection_template(path, basename)
33-
local collection = io.open(target_collection_path, "w")
34-
collection:write(collection_content)
35-
collection:close()
26+
local function get_template(game_project_path)
27+
-- The input file can be the "compiled" file. If they are they end with "c".
28+
-- If they end with "c" remove the last charater.
29+
local path = editor.get("/game.project", game_project_path)
30+
if path == nil then
31+
return
3632
end
37-
if not file_exists(target_gui_script_path) then
38-
local gui_script = io.open(target_gui_script_path, "w")
39-
gui_script:write(gui_script_content)
40-
gui_script:close()
33+
if ends_with(path, "c") then
34+
return path:sub(1, -2)
35+
end
36+
return path
37+
end
38+
39+
local function get_template_content(type_string)
40+
local default_template = {
41+
collection = collection_template,
42+
gui_script = gui_script_template,
43+
gui = gui_template,
44+
}
45+
local custom_template = {
46+
collection = get_template("monarch.collection_template"),
47+
gui_script = get_template("monarch.gui_script_template"),
48+
gui = get_template("monarch.gui_template"),
49+
}
50+
local template = custom_template[type_string]
51+
if template ~= nil then
52+
local file = io.open("." .. template, "rb")
53+
if file == nil then
54+
_log("Could not read " .. type_string .. " template '" .. template .. "'")
55+
return
56+
end
57+
local content = file:read("*a")
58+
file:close()
59+
return content
60+
end
61+
return default_template[type_string]
62+
end
63+
64+
local function add_monarch_go(collection_file, gui_file, name)
65+
return editor.tx.add(collection_file, "children", {
66+
type = "go",
67+
id = "monarch",
68+
components = {
69+
{
70+
type = "component-reference",
71+
id = name,
72+
path = gui_file,
73+
}
74+
}
75+
})
76+
end
77+
78+
local function create_collection(collection_file, gui_file, name)
79+
local _collection_content = get_template_content("collection")
80+
if _collection_content == nil then
81+
_log("Could not create colletion file at " .. gui)
82+
return
83+
end
84+
local collection = io.open("." .. collection_file, "w")
85+
if collection == nil then
86+
_log("Could not create colletion file at " .. collection_file)
87+
return
88+
end
89+
collection:write(_collection_content)
90+
collection:close()
91+
return add_monarch_go(collection_file, gui_file, name)
92+
end
4193

42-
-- Put the gui_script path into the gui file
43-
local gui_file = io.open("." .. path, "rb")
44-
local gui_text = gui_file:read("*a")
45-
gui_file:close()
94+
local function create_gui(gui_file, gui_script_file)
95+
local gui_content = get_template_content("gui")
96+
if gui_content == nil then
97+
_log("Could not create gui file at " .. gui)
98+
return
99+
end
100+
local gui = io.open("." .. gui_file, "w")
101+
if gui == nil then
102+
_log("Could not create gui file at " .. gui)
103+
return
104+
end
105+
gui:write(gui_content)
106+
gui:close()
46107

47-
gui_text = string.gsub(gui_text, 'script: "%.*"', [[script: "]] .. main .. basename .. ".gui_script" .. [["]])
108+
return editor.tx.set(gui_file, "script", gui_script_file)
109+
end
48110

49-
gui_file = io.open("." .. path, "w")
50-
gui_file:write(gui_text)
51-
gui_file:close()
111+
local function create_files(directory, name)
112+
-- Construct paths
113+
local target_collection_path = directory .. name .. ".collection"
114+
local target_gui_script_path = directory .. name .. ".gui_script"
115+
local target_gui_path = directory .. name .. ".gui"
116+
117+
if not file_exists("." .. target_gui_script_path) then
118+
local gui_script_template_content = get_template_content("gui_script")
119+
if gui_script_template_content == nil then
120+
return
121+
end
122+
local gui_script = io.open("." .. target_gui_script_path, "w")
123+
gui_script:write(gui_script_template_content)
124+
gui_script:close()
125+
end
126+
local transactions = {}
127+
128+
if file_exists("." .. target_gui_path) then
129+
-- If the gui file exists change the script.
130+
table.insert(transactions, editor.tx.set(target_gui_path, "script", target_gui_script_path)
131+
)
132+
else
133+
-- If the gui file doesn't exist create a new one.
134+
local transaction = create_gui(target_gui_path, target_gui_script_path)
135+
if transaction == nil then
136+
return
137+
end
138+
table.insert(transactions, transaction)
52139
end
53-
if not file_exists(target_gui_path) then
54-
local gui_content = gui_template(path)
55-
local gui = io.open(target_gui_path, "w")
56-
gui:write(gui_content)
57-
gui:close()
140+
if file_exists("." .. target_collection_path) then
141+
--- If the collection already exists we will add the monarch go to it.
142+
local transaction = add_monarch_go(target_collection_path, target_gui_path, name)
143+
if transaction == nil then
144+
return
145+
end
146+
table.insert(transactions, transaction)
147+
else
148+
-- If the collection doesn't exists we create a new one.
149+
local transaction = create_collection(target_collection_path, target_gui_path, name)
150+
if transaction == nil then
151+
return
152+
end
153+
table.insert(transactions, transaction)
154+
end
155+
if #transactions > 1 then
156+
editor.transact(transactions)
157+
editor.save()
58158
end
59159
end
60160

@@ -68,23 +168,54 @@ function M.get_commands()
68168
},
69169
active = function(opts)
70170
local path = editor.get(opts.selection, "path")
71-
return ends_with(path, ".gui")
171+
return ends_with(path, ".gui") or ends_with(path, ".gui_script") or ends_with(path, ".collection")
172+
end,
173+
run = function(opts)
174+
local path = editor.get(opts.selection, "path")
175+
local directory, filename = get_filename(path)
176+
177+
local basename = filename:match("(.+)%..+")
178+
create_files(directory, basename)
179+
end
180+
},
181+
{
182+
label="Create Monarch Scene From Directory",
183+
locations = {"Assets"},
184+
query = {
185+
selection = {type = "resource", cardinality = "one"}
186+
},
187+
active = function(opts)
188+
local _is_directory = editor.resource_attributes(editor.get(opts.selection, "path")).is_directory
189+
if not _is_directory then
190+
return false
191+
end
192+
local _children = editor.get(opts.selection, "children")
193+
if #_children >= 3 then
194+
return false
195+
end
196+
for i = 1, #_children do
197+
local _path = editor.get(_children[i], "path")
198+
if not (ends_with(_path, ".gui") or ends_with(_path, ".collection") or ends_with(_path, ".gui_script")) then
199+
return false
200+
end
201+
end
202+
return true
72203
end,
73204
run = function(opts)
74-
create_files(opts.selection)
205+
local path = editor.get(opts.selection, "path")
206+
local _, filename = get_filename(path)
207+
create_files(path .. "/", filename)
75208
end
76209
}
77210
}
78211
end
79212

80-
gui_template = function(gui_script)
81-
return [[script: "]].. gui_script .. [["
82-
material: "/builtins/materials/gui.material"
83-
adjust_reference: ADJUST_REFERENCE_PARENT
84-
]]
85-
end
213+
gui_template = [[
214+
material: "/builtins/materials/gui.material"
215+
adjust_reference: ADJUST_REFERENCE_PARENT
216+
]]
86217

87-
gui_script_content = [[local monarch = require "monarch.monarch"
218+
gui_script_template = [[local monarch = require "monarch.monarch"
88219

89220
function init(self)
90221
msg.post(".", "acquire_input_focus")
@@ -106,49 +237,10 @@ function on_reload(self)
106237
end
107238
]]
108239

109-
110-
collection_template = function(gui_script, name)
111-
return [[name: "]].. name .. [["
112-
scale_along_z: 0
113-
embedded_instances {
114-
id: "go"
115-
data: "components {\n"
116-
" id: \"monarch\"\n"
117-
" component: \"]].. gui_script .. [[\"\n"
118-
" position {\n"
119-
" x: 0.0\n"
120-
" y: 0.0\n"
121-
" z: 0.0\n"
122-
" }\n"
123-
" rotation {\n"
124-
" x: 0.0\n"
125-
" y: 0.0\n"
126-
" z: 0.0\n"
127-
" w: 1.0\n"
128-
" }\n"
129-
"}\n"
130-
""
131-
position {
132-
x: 0.0
133-
y: 0.0
134-
z: 0.0
135-
}
136-
rotation {
137-
x: 0.0
138-
y: 0.0
139-
z: 0.0
140-
w: 1.0
141-
}
142-
scale3 {
143-
x: 1.0
144-
y: 1.0
145-
z: 1.0
146-
}
147-
}
148-
]]
149-
150-
end
151-
240+
collection_template = [[
241+
name: "m"
242+
scale_along_z: 0
243+
]]
152244

153245

154246
return M

monarch/ext.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[monarch]
2+
3+
collection_template.type = resource
4+
collection_template.filter = collection
5+
6+
gui_script_template.type = resource
7+
gui_script_template.filter = gui_script
8+
9+
gui_template.type = resource
10+
gui_template.filter = gui

0 commit comments

Comments
 (0)