-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeSigScaler.nw.nwcuser.lua
162 lines (126 loc) · 3.95 KB
/
TimeSigScaler.nw.nwcuser.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
162
-- Version 0.9
assert((nwc.VERSIONDATE or '00000000') >= '20161230','This plugin requires version 2.75a')
--[[-----------------------------------------------------------------------------------------
TimeSigScaler.nw <http://nwsw.net/-f9533>
This object can be used to draw a scaled version of a prior, usually hidden, time signature.
This object should always be placed after the real time signature.
@Scale
This sets the scaling used in the synthesized replacement time signature.
--]]-----------------------------------------------------------------------------------------
if nwcut then
local userObjTypeName = arg[1]
local doAdd = false
local removeExtras = -1
local addObj = false
local changes = 0
nwcut.setlevel(0)
nwcut.writeline(nwcut.getprop('StartingLine'))
for item in nwcut.items() do
local t1 = nwcut.objtyp(item)
if t1 == 'User' then
local t2 = item:match('^|User|([^|]+)')
if t2 == userObjTypeName then
addObj = item
if doAdd then
doAdd = false
else
if removeExtras < 0 then
removeExtras = nwcut.askbox(('Should extra %s objects be removed?'):format(userObjTypeName))
end
if removeExtras == 1 then
changes = changes+1
item = false
end
end
end
end
if item and doAdd then
if not addObj then
local scale = nwcut.prompt('Scaling factor','#[50,250]',150)
addObj = ('|User|%s|Pos:0|Scale:%d'):format(userObjTypeName,scale)
end
changes = changes+1
nwcut.writeline(addObj)
doAdd = false
end
if t1 == 'TimeSig' then
local tsObj = nwcItem.new(item,1)
doAdd = true
tsObj:Set('Visibility','Never')
item = tostring(tsObj)
end
if item then
nwcut.writeline(item)
end
end
nwcut.writeline(nwcut.getprop('EndingLine'))
if changes < 1 then nwcut.warn('No changes\n') end
return
end
local userObjTypeName = ...
local object_spec = {
{ id='Scale', label='Scale (%)', type='int', default=150, min=50, max=300, step=10 },
}
local function do_spin(t,d)
t.Scale = t.Scale + d*10
end
local function drawUpDnArrow(x,y,h,w,t)
nwcdraw.setPen('solid',t)
nwcdraw.line(x-w/2,y-h/2,x-w/2,y+h/2)
nwcdraw.moveTo(x-w,y-h/2+1)
nwcdraw.lineBy(w/2,-1,w/2,1)
nwcdraw.moveTo(x-w,y+h/2-1)
nwcdraw.lineBy(w/2,1,w/2,-1)
end
local scanStopItems = {Note=1,Chord=1,RestChord=1,Rest=1,Bar=1,Clef=1,Key=1}
local spaceStopItems = {Spacer=1,RestMultiBar=1}
local function calcBoundarySpace(idx,dir,defaultSpace)
idx:reset()
while idx:find(dir) and not scanStopItems[idx:objType()] do
if spaceStopItems[idx:objType()] then return 0 end
end
return defaultSpace or 0.5
end
local function do_draw(t)
local idx = nwc.ntnidx
if not idx:find('prior', 'TimeSig') then
return nwc.toolbox.drawStaffSigLabel(UserObjTypeName)
end
local Scale = t.Scale*.01
if (nwcdraw.getTarget() == 'edit') and (idx:indexOffset() == -1) then
local afterW = calcBoundarySpace(idx,'next')
if not nwcdraw.isDrawing() then return afterW+0.6 end
drawUpDnArrow(-.1-afterW,0,3,0.5,500)
return
end
local NextTimeSig_Prop = idx:objProp('Signature') or '4/4'
local beforeW = calcBoundarySpace(idx,'prior',0.75)
local afterW = calcBoundarySpace(idx,'next')
nwcdraw.setFontClass('StaffSymbols')
nwcdraw.setFontSize(nwcdraw.getFontSize()*Scale)
local Beats,Value = NextTimeSig_Prop:match('(%d+)/(%d+)')
if NextTimeSig_Prop == 'Common' then
Beats,Value = 4,4
elseif NextTimeSig_Prop == 'AllaBreve' then
Beats,Value = 2,2
else
assert(Beats and Value,NextTimeSig_Prop)
end
local w1 = nwcdraw.calcTextSize(Beats)
local w2 = nwcdraw.calcTextSize(Value)
local w = math.max(w1,w2)
if not nwcdraw.isDrawing() then return w + beforeW + afterW end
local xmid = -w/2 - afterW
nwcdraw.alignText('middle','center')
nwcdraw.moveTo(xmid,4*Scale - 0.15)
nwcdraw.text(Beats)
nwcdraw.moveTo(xmid,-0.2*(Scale+1))
nwcdraw.text(Value)
end
return {
nwcut = {['Override All Time Signatures'] = 'FileText'},
spec = object_spec,
spin = do_spin,
width = do_draw,
draw = do_draw,
}