Skip to content

Commit 9e62769

Browse files
committed
Converted the extension to pure lua
1 parent 5c6310e commit 9e62769

4 files changed

Lines changed: 84 additions & 168 deletions

File tree

editor-script-atlas/atlas.editor_script

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
--local utils = require "editor.utils"
2-
31
local M = {}
42

3+
local function image_template(image)
4+
return [[images {
5+
image: "]] .. image .. [["
6+
}
7+
]]
8+
end
9+
10+
local function path_segments(path)
11+
return string.match(path, "(.-)([^\\/]-%.?([^%.\\/]*))$")
12+
end
13+
514
local function unpack(t, i)
615
i = i or 1
716
if t[i] ~= nil then
@@ -13,72 +22,95 @@ local function ends_with(str, ending)
1322
return ending == "" or str:sub(-#ending) == ending
1423
end
1524

16-
-- Need to source the bash_profile ourselves to add the users
17-
-- environment variables such as his extended PATH
18-
local function command_workaround()
19-
if editor.platform == "x86_64-darwin" then
20-
return "./editor-script-atlas/scripts/extend_path.sh"
25+
local function is_add_active(opts)
26+
local atlas = false
27+
for _, id in pairs(opts.selection) do
28+
local path = editor.get(id, "path")
29+
if ends_with(path, ".atlas") then
30+
if atlas == true then
31+
return false
32+
end
33+
atlas = true
34+
elseif ends_with(path, ".png") then
35+
else
36+
return false
37+
end
2138
end
22-
return "python"
39+
return atlas and #opts.selection > 1
2340
end
2441

42+
local function is_new_active(opts)
43+
local atlas = false
44+
for _, id in pairs(opts.selection) do
45+
local path = editor.get(id, "path")
46+
if ends_with(path, ".png") then
47+
else
48+
return false
49+
end
50+
end
51+
return true
52+
end
53+
54+
local function add_to_atlas(opts)
55+
local atlas
56+
local new_images_string = ""
57+
for _, id in pairs(opts.selection) do
58+
local path = editor.get(id, "path")
59+
if ends_with(path, ".png") then
60+
new_images_string = new_images_string .. image_template(path)
61+
else
62+
atlas = path
63+
end
64+
end
65+
66+
-- Get the text
67+
local atlas_file = io.open("." .. atlas, "r")
68+
local atlas_text = atlas_file:read("*a")
69+
atlas_file:close()
70+
71+
atlas_file = io.open("." .. atlas, "w")
72+
atlas_file:write(new_images_string .. atlas_text)
73+
atlas_file:close()
74+
end
75+
76+
77+
local function new_atlas(opts)
78+
local base_path = path_segments(editor.get(opts.selection[1], "path"))
79+
local atlas = base_path .. "ATLAS" .. os.date("%Y%m%d%H%M%S") .. ".atlas"
80+
local atlas_string = ""
81+
for _, id in pairs(opts.selection) do
82+
local path = editor.get(id, "path")
83+
atlas_string = atlas_string .. image_template(path)
84+
end
85+
atlas_string = atlas_string .. [[margin: 0
86+
extrude_borders: 2
87+
inner_padding: 0]]
88+
89+
local atlas_file = io.open("." .. atlas, "w")
90+
atlas_file:write(atlas_string)
91+
atlas_file:close()
92+
93+
end
2594

2695
function M.get_commands()
2796
return {
2897
{
29-
label="Add To Atlas...",
98+
label="Add to Atlas...",
3099
locations = {"Assets"},
31100
query = {
32101
selection = {type = "resource", cardinality = "many"}
33102
},
34-
active = function(opts)
35-
local atlas = false
36-
for _, id in pairs(opts.selection) do
37-
local path = editor.get(id, "path")
38-
if ends_with(path, ".atlas") then
39-
if atlas == true then
40-
return false
41-
end
42-
atlas = true
43-
elseif ends_with(path, ".png") then
44-
else
45-
return false
46-
end
47-
end
48-
return atlas and #opts.selection > 1
49-
end,
50-
run = function(opts)
51-
local paths = {}
52-
for _, id in pairs(opts.selection) do
53-
table.insert(paths, '"' .. editor.get(id, "path") .. '"')
54-
end
55-
return {
56-
{
57-
action = "shell",
58-
command = {command_workaround(), "-u", "-c", editor.get("/editor-script-atlas/scripts/add_to_atlas.py", "text"), unpack(paths)}
59-
}
60-
}
61-
end
103+
active = is_add_active,
104+
run = add_to_atlas
62105
},
63106
{
64-
label="Sort Atlas",
107+
label="Create New Atlas...",
65108
locations = {"Assets"},
66109
query = {
67-
selection = {type = "resource", cardinality = "one"}
110+
selection = {type = "resource", cardinality = "many"}
68111
},
69-
active = function(opts)
70-
local path = editor.get(opts.selection, "path")
71-
return ends_with(path, ".atlas")
72-
end,
73-
run = function(opts)
74-
local path = editor.get(opts.selection, "path")
75-
return {
76-
{
77-
action = "shell",
78-
command = {command_workaround(), "-u", "-c", editor.get("/editor-script-atlas/scripts/sort_atlas.py", "text"), path}
79-
}
80-
}
81-
end
112+
active = is_new_active,
113+
run = new_atlas
82114
}
83115
}
84116
end

editor-script-atlas/scripts/add_to_atlas.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

editor-script-atlas/scripts/extend_path.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

editor-script-atlas/scripts/sort_atlas.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)