-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path3D text generator.lua
162 lines (141 loc) · 3.98 KB
/
3D text generator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Geometry / Mesh\n3D text generator...'}
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function sysCall_init()
simUI = require 'simUI'
textUtils = require 'textUtils'
sim.addLog(
sim.verbosity_scriptinfos,
"This tool allows to generate 3D text. Courtesy of 'Mechatronics Ninja'"
)
prevObj = -1
showDlg()
config = {}
config.color = {1, 1, 1}
config.text = "Hello\nWorld"
config.height = 0.1
config.centered = false
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_selChange(inData)
if #inData.sel == 1 then
initDlg(inData.sel[1])
else
prevObj = -1
end
end
function sysCall_beforeSimulation()
hideDlg()
end
function sysCall_afterSimulation()
showDlg()
end
function sysCall_cleanup()
hideDlg()
end
function sysCall_beforeInstanceSwitch()
hideDlg()
end
function sysCall_afterInstanceSwitch()
showDlg()
end
function showDlg()
if not ui then
local pos = 'position="-50,50" placement="relative"'
if uiPos then
pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"'
end
local xml =
'<ui title="3D text generator" activate="false" closeable="true" on-close="close_callback" ' ..
pos .. [[>
<label text="Text:"/>
<edit value="Hello\nWorld" on-change="text_callback" id="1" />
<label text="Height:"/>
<edit value="0.1" on-change="height_callback" id="2" />
<checkbox checked="false" text="Centered" on-change="centered_callback" id="3" />
<button text="Edit color" on-click="color_callback" id="4"/>
<button text="Generate new" on-click="generate_callback" id="5"/>
</ui>]]
ui = simUI.create(xml)
end
end
function initDlg(obj)
if obj >= 0 and obj ~= prevObj and ui then
local data = sim.readCustomTableData(obj, '__info__')
if data.type == '3dText' then
prevObj = obj
config = sim.readCustomTableData(obj, '__config__')
local txt = config.text:gsub("\n", "\\n")
simUI.setEditValue(ui, 1, txt)
simUI.setEditValue(ui, 2, string.format("%2f", config.height))
simUI.setCheckboxValue(ui, 3, not config.centered and 0 or 2)
end
end
end
function hideDlg()
if ui then
uiPos = {}
uiPos[1], uiPos[2] = simUI.getPosition(ui)
simUI.destroy(ui)
ui = nil
end
end
function height_callback(ui, id, v)
local nb = tonumber(v)
if nb then
if nb < 0.01 then nb = 0.01 end
if nb > 1 then nb = 1 end
config.height = nb
update()
end
end
function centered_callback(ui, id, v)
config.centered = (v ~= 0)
update()
end
function text_callback(ui, id, v)
v = v:gsub("\\n", "\n")
config.text = v
update()
end
function color_callback()
local c = simUI.colorDialog(config.color, "Text color", false, true)
if c then
config.color = c
update()
end
end
function generate_callback()
update(true)
end
function close_callback()
leaveNow = true
end
function update(generateNew)
local s = sim.getObjectSel()
local parentDummy
if #s == 1 then
local data = sim.readCustomTableData(s[1], '__info__')
if data.type == '3dText' then parentDummy = s[1] end
end
local doNothing
if generateNew then
parentDummy = nil
else
doNothing = (parentDummy == nil)
end
if not doNothing then
local h = textUtils.generateTextShape(
config.text, config.color, config.height, config.centered, nil, parentDummy
)
sim.writeCustomTableData(h, '__info__', {type = '3dText'})
sim.writeCustomTableData(h, '__config__', config)
sim.announceSceneContentChange()
end
end