Profile
- Name
- Set Band Length
- ID
- 103595
- Shared with
- Public
- Parent
- None
- Children
- Created on
- June 15, 2020 at 05:18 AM UTC
- Updated on
- June 15, 2020 at 05:18 AM UTC
- Description
Set all bands to a specified length.
Best for
Code
-- Function to draw the menu to input a length.
function setupMenu()
menuDialog = dialog.CreateDialog("Set Band Length")
menuDialog.length = dialog.AddTextbox("Length", 0)
menuDialog.set = dialog.AddButton("Set", 1)
menuDialog.cancel = dialog.AddButton("Cancel", 0)
return menuDialog
end
-- Function for showing a message if the length is not in the allowed range
function showOutOfRange()
rangeError = dialog.CreateDialog("Length out of range")
rangeError.text1 = dialog.AddLabel("Band length must be between 0 and 10000")
rangeError.OK = dialog.AddButton("OK", 1)
dialog.Show(rangeError)
end
-- Function for warning the user they did not enter a number
function showNAN()
nanError = dialog.CreateDialog("Invalid Input")
nanError.text1 = dialog.AddLabel("Value entered is not a number")
nanError.OK = dialog.AddButton("OK", 1)
dialog.Show(nanError)
end
-- Main function
function main()
-- Initialize length
local length = 0
-- Repeat until we break out of the loop, so that if user enters an invalid number, we can ask them to try again.
while true do
-- Show the length menu, initialize desired length in menu to the value of length
menu = setupMenu()
-- If the user pressed the OK Button
if dialog.Show(menu) == 1 then
-- Get the length the user entered. Convert to a number
length = tonumber(menu.length.value)
-- Check if value cannot be converted to a number. Warn user if it cannot.
if length == nil or length ~= length then
showNAN()
-- Check if number is outside range that band length can be. Warn user if it is
elseif length > 10000 or length < 0 then
showOutOfRange()
else
-- Loop through all the bands and set the length to the value the user requested
for i=1, band.GetCount() do
band.SetGoalLength(i, length)
end
-- Since we have now set the band length, exit our loop
break
end
-- If menu did not return 1, user must have cancelled, so break out of loop.
else
break
end
end
end
-- Run the main function
main()