Cleaned up demo application code

This commit is contained in:
nucular 2014-10-28 16:55:42 +01:00
parent bc4ebf3728
commit e04f244640

230
main.lua
View File

@ -1,3 +1,6 @@
#/usr/bin/env love
-- love .
local sfxr = require("sfxr") local sfxr = require("sfxr")
-- Global stuff -- Global stuff
@ -168,22 +171,20 @@ function playSound()
end end
function createSeedBox() function createSeedBox()
local f = lf.Create("form") local f = lf.Create("form"):SetName("Random Seed")
f:SetName("Random Seed")
seed = lf.Create("numberbox") seed = lf.Create("numberbox")
seed:SetValue(math.floor(love.timer.getTime())) :SetValue(math.floor(love.timer.getTime()))
seed:SetMax(math.huge) :SetMax(math.huge)
seed:SetMin(-math.huge) :SetMin(-math.huge)
seed:SetWidth(100) :SetWidth(100)
f:AddItem(seed)
f:SetPos(5, 240) f:AddItem(seed):SetPos(5, 240)
end end
function createPresetGenerators() function createPresetGenerators()
local f = lf.Create("form") local f = lf.Create("form")
f:SetName("Preset Generators") :SetName("Preset Generators")
local generators = { local generators = {
{"Pickup/Coin", sound.randomPickup}, {"Pickup/Coin", sound.randomPickup},
{"Laser/Shoot", sound.randomLaser}, {"Laser/Shoot", sound.randomLaser},
@ -196,8 +197,8 @@ function createPresetGenerators()
for i, v in ipairs(generators) do for i, v in ipairs(generators) do
local b = lf.Create("button") local b = lf.Create("button")
b:SetText(v[1]) :SetText(v[1])
b:SetWidth(100) :SetWidth(100)
f:AddItem(b) f:AddItem(b)
b.OnClick = function(self) b.OnClick = function(self)
@ -208,18 +209,17 @@ function createPresetGenerators()
end end
end end
f:SetPos(5, 5) f:SetPos(5, 5):SetWidth(110)
f:SetWidth(110)
end end
function createRandomizers() function createRandomizers()
local f = lf.Create("form") local f = lf.Create("form"):SetName("Randomizers")
f:SetName("Randomizers")
local b = lf.Create("button") local b = lf.Create("button")
b:SetText("Mutate") :SetText("Mutate")
b:SetWidth(100) :SetWidth(100)
f:AddItem(b) f:AddItem(b)
b.OnClick = function(self) b.OnClick = function(self)
sound:mutate() sound:mutate()
updateParameters() updateParameters()
@ -227,9 +227,10 @@ function createRandomizers()
end end
local b = lf.Create("button") local b = lf.Create("button")
b:SetText("Randomize") :SetText("Randomize")
b:SetWidth(100) :SetWidth(100)
f:AddItem(b) f:AddItem(b)
b.OnClick = function(self) b.OnClick = function(self)
sound:randomize(seed:GetValue()) sound:randomize(seed:GetValue())
updateParameters() updateParameters()
@ -237,43 +238,47 @@ function createRandomizers()
playSound() playSound()
end end
f:SetPos(5, 515) f:SetPos(5, 515):SetSize(110, 80)
f:SetSize(110, 80)
end end
function createParameters() function createParameters()
local f = lf.Create("form") local f = lf.Create("form"):SetName("Parameters")
f:SetName("Parameters")
local l = lf.Create("list") local l = lf.Create("list")
l:SetSpacing(5) :SetSpacing(5)
l:SetPadding(5) :SetPadding(5)
l:SetSize(340, 565) :SetSize(340, 565)
f:AddItem(l) f:AddItem(l)
-- Waveforms -- Waveforms
l:AddItem(lf.Create("text"):SetPos(0, pheight):SetText("Wave Form")) l:AddItem(lf.Create("text"):SetPos(0, pheight):SetText("Wave Form"))
local m = lf.Create("multichoice") local m = lf.Create("multichoice")
m:AddChoice("Square") :AddChoice("Square")
m:AddChoice("Sawtooth") :AddChoice("Sawtooth")
m:AddChoice("Sine") :AddChoice("Sine")
m:AddChoice("Noise") :AddChoice("Noise")
m:SetChoice("Square") :SetChoice("Square")
m.OnChoiceSelected = function(o, c) m.OnChoiceSelected = function(o, c)
sound.wavetype = waveFormList[c] sound.wavetype = waveFormList[c]
end end
l:AddItem(m) l:AddItem(m)
guiparams.waveform = m guiparams.waveform = m
-- Repeat speed -- Repeat speed
local t = lf.Create("text"):SetPos(0, pheight) local t = lf.Create("text")
t:SetText("Repeat Speed 0") :SetText("Repeat Speed 0")
:SetPos(0, pheight)
local s = lf.Create("slider") local s = lf.Create("slider")
s:SetWidth(120) :SetWidth(120)
s:SetMinMax(0, 1) :SetMinMax(0, 1)
s:SetValue(sound.repeatspeed) :SetValue(sound.repeatspeed)
s.OnValueChanged = function(o) s.OnValueChanged = function(o)
local v = o:GetValue() local v = o:GetValue()
if v <= 0.02 and v >= -0.02 and v ~= 0 then if v <= 0.02 and v >= -0.02 and v ~= 0 then
@ -285,14 +290,13 @@ function createParameters()
t:SetText("Repeat Speed " .. tostring(math.floor(v * 100) / 100)) t:SetText("Repeat Speed " .. tostring(math.floor(v * 100) / 100))
end end
end end
l:AddItem(t)
l:AddItem(s) l:AddItem(t):AddItem(s)
guiparams.repeatspeed = {s, t} guiparams.repeatspeed = {s, t}
for i1, v1 in ipairs(guicategories) do for i1, v1 in ipairs(guicategories) do
local c = lf.Create("collapsiblecategory") local c = lf.Create("collapsiblecategory"):SetText(v1[1])
c:SetText(v1[1])
l:AddItem(c) l:AddItem(c)
local p = lf.Create("panel") local p = lf.Create("panel")
@ -303,12 +307,19 @@ function createParameters()
guiparams[v1[2]] = {} guiparams[v1[2]] = {}
for i2, v2 in ipairs(v1[3]) do for i2, v2 in ipairs(v1[3]) do
lf.Create("text", p):SetPos(0, pheight):SetText(v2[1]) lf.Create("text", p)
local t = lf.Create("text", p):SetPos(95, pheight):SetText("0") :SetText(v2[1])
:SetPos(0, pheight)
local s = lf.Create("slider", p):SetPos(130, pheight - 3):SetWidth(170) local t = lf.Create("text", p)
s:SetMinMax(v2[3], v2[4]) :SetText("0")
s:SetValue(sound[v1[2]][v2[2]]) :SetPos(95, pheight)
local s = lf.Create("slider", p)
:SetPos(130, pheight - 3)
:SetWidth(170)
:SetMinMax(v2[3], v2[4])
:SetValue(sound[v1[2]][v2[2]])
s.OnValueChanged = function(o) s.OnValueChanged = function(o)
local v = o:GetValue() local v = o:GetValue()
@ -330,17 +341,16 @@ function createParameters()
end end
f:SetPos(125, 5) f:SetPos(125, 5):SetSize(350, 590)
f:SetSize(350, 590)
end end
function createActionButtons() function createActionButtons()
local f = lf.Create("form") local f = lf.Create("form"):SetName("Actions")
f:SetName("Actions")
local b = lf.Create("button") local b = lf.Create("button")
b:SetText("Generate and Play") :SetText("Generate and Play")
b:SetWidth(140) :SetWidth(140)
b.OnClick = function(o) b.OnClick = function(o)
if not playing then if not playing then
playSound() playSound()
@ -348,27 +358,29 @@ function createActionButtons()
stopSound() stopSound()
end end
end end
playbutton = b playbutton = b
f:AddItem(b) f:AddItem(b)
local fr = lf.Create("frame") local fr = lf.Create("frame")
fr:SetName("File Picker") :SetName("File Picker")
fr:SetSize(400, 300) :SetSize(400, 300)
fr:Center() :Center()
fr:SetVisible(false) :SetVisible(false)
fr:SetModal(false) :SetModal(false)
local frl = lf.Create("columnlist", fr) local frl = lf.Create("columnlist", fr)
frl:SetPos(5, 30) :SetPos(5, 30)
frl:SetSize(390, 235) :SetSize(390, 235)
frl:AddColumn("Name") :AddColumn("Name")
local frt = lf.Create("textinput", fr) local frt = lf.Create("textinput", fr)
frt:SetPos(5, 270) :SetPos(5, 270)
frt:SetWidth(300) :SetWidth(300)
local frb = lf.Create("button", fr) local frb = lf.Create("button", fr)
frb:SetPos(315, 270) :SetPos(315, 270)
frl.OnRowSelected = function(p, row, data) frl.OnRowSelected = function(p, row, data)
frt:SetText(data[1]) frt:SetText(data[1])
@ -385,11 +397,13 @@ function createActionButtons()
fr:SetName("Save to ." .. type) fr:SetName("Save to ." .. type)
frt:SetText("sound." .. type) frt:SetText("sound." .. type)
frb:SetText("Save") frb:SetText("Save")
love.filesystem.getDirectoryItems("sounds", function(name) love.filesystem.getDirectoryItems("sounds", function(name)
if name:find(type, #type-#name+1, true) then if name:find(type, #type-#name+1, true) then
frl:AddRow(name) frl:AddRow(name)
end end
end) end)
frb.OnClick = function(o) frb.OnClick = function(o)
local name = frt:GetText() local name = frt:GetText()
if (#name > 0) then if (#name > 0) then
@ -401,8 +415,11 @@ function createActionButtons()
end end
end end
end end
frt.OnEnter = frb.OnClick frt.OnEnter = frb.OnClick
fr:SetVisible(true):SetModal(true):Center() fr:SetVisible(true)
:SetModal(true)
:Center()
end end
end end
@ -411,11 +428,13 @@ function createActionButtons()
fr:SetName("Load from ." .. type) fr:SetName("Load from ." .. type)
frt:SetText("sound." .. type) frt:SetText("sound." .. type)
frb:SetText("Load") frb:SetText("Load")
love.filesystem.getDirectoryItems("sounds", function(name) love.filesystem.getDirectoryItems("sounds", function(name)
if name:find(type, #type-#name+1, true) then if name:find(type, #type-#name+1, true) then
frl:AddRow(name) frl:AddRow(name)
end end
end) end)
frb.OnClick = function(o) frb.OnClick = function(o)
local name = frt:GetText() local name = frt:GetText()
if (#name > 0) then if (#name > 0) then
@ -427,44 +446,46 @@ function createActionButtons()
end end
end end
end end
frt.OnEnter = frb.OnClick frt.OnEnter = frb.OnClick
fr:SetVisible(true):SetModal(true):Center() fr:SetVisible(true)
:SetModal(true)
:Center()
end end
end end
local sb = lf.Create("button") local sb = lf.Create("button")
sb:SetText("Save Lua") :SetText("Save Lua")
sb:SetWidth(67) :SetWidth(67)
sb.OnClick = save("lua", function(f) sound:save(f, true) end) sb.OnClick = save("lua", function(f) sound:save(f, true) end)
f:AddItem(sb) f:AddItem(sb)
local lb = lf.Create("button") local lb = lf.Create("button")
lb:SetText("Load Lua") :SetText("Load Lua")
lb:SetWidth(67) :SetWidth(67)
lb.OnClick = load("lua", function(f) sound:load(f) end) sb.OnClick = load("lua", function(f) sound:load(f) end)
f:AddItem(lb) f:AddItem(lb)
local bsb = lf.Create("button") local bsb = lf.Create("button")
bsb:SetText("Save binary") :SetText("Save binary")
bsb:SetWidth(67) :SetWidth(67)
bsb.OnClick = save("sfs", function(f) sound:saveBinary(f) end) bsb.OnClick = save("sfs", function(f) sound:saveBinary(f) end)
f:AddItem(bsb) f:AddItem(bsb)
local blb = lf.Create("button") local blb = lf.Create("button")
blb:SetText("Load binary") :SetText("Load binary")
blb:SetWidth(67) :SetWidth(67)
blb.OnClick = load("sfs", function(f) sound:loadBinary(f) end) blb.OnClick = load("sfs", function(f) sound:loadBinary(f) end)
f:AddItem(blb) f:AddItem(blb)
local eb = lf.Create("button") local eb = lf.Create("button")
eb:SetText("Export WAV") :SetText("Export WAV")
eb:SetWidth(140) :SetWidth(140)
eb.OnClick = save("wav", function(f) sound:exportWAV(f) end) eb.OnClick = save("wav", function(f) sound:exportWAV(f) end)
f:AddItem(eb) f:AddItem(eb)
f:SetPos(485, 455) f:SetPos(485, 455):SetSize(150, 140)
f:SetSize(150, 140)
-- well ugh -- well ugh
lb:SetPos(78, 47) lb:SetPos(78, 47)
@ -475,9 +496,10 @@ end
function createOther() function createOther()
local f = lf.Create("form") local f = lf.Create("form")
f:SetName("Wave View") :SetName("Wave View")
f:SetPos(485, 5) :SetPos(485, 5)
f:SetSize(150, 170) :SetSize(150, 170)
local draw = function(o) local draw = function(o)
if source then if source then
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
@ -496,15 +518,16 @@ function createOther()
f.Draw = draw f.Draw = draw
local f = lf.Create("form") local f = lf.Create("form"):SetName("Volume")
f:SetName("Volume")
local t = lf.Create("text") local t = lf.Create("text"):SetText("Master 0.5")
t:SetText("Master 0.5")
f:AddItem(t) f:AddItem(t)
local s = lf.Create("slider") local s = lf.Create("slider")
s:SetMinMax(0, 1) :SetMinMax(0, 1)
s:SetSize(135, 20) :SetSize(135, 20)
s.OnValueChanged = function(o) s.OnValueChanged = function(o)
local v = o:GetValue() local v = o:GetValue()
if v <= 0.52 and v >= 0.48 and v ~= 0.5 then if v <= 0.52 and v >= 0.48 and v ~= 0.5 then
@ -514,15 +537,18 @@ function createOther()
sound.volume.master = v sound.volume.master = v
t:SetText("Master " .. tostring(math.floor(v * 100) / 100)) t:SetText("Master " .. tostring(math.floor(v * 100) / 100))
end end
s:SetValue(sound.volume.master) s:SetValue(sound.volume.master)
f:AddItem(s) f:AddItem(s)
local t = lf.Create("text")
t:SetText("Sound 0.5") local t = lf.Create("text"):SetText("Sound 0.5")
f:AddItem(t) f:AddItem(t)
local s = lf.Create("slider") local s = lf.Create("slider")
s:SetMinMax(0, 1) :SetMinMax(0, 1)
s:SetSize(135, 20) :SetSize(135, 20)
s.OnValueChanged = function(o) s.OnValueChanged = function(o)
local v = o:GetValue() local v = o:GetValue()
if v <= 0.52 and v >= 0.48 and v ~= 0.5 then if v <= 0.52 and v >= 0.48 and v ~= 0.5 then
@ -532,38 +558,33 @@ function createOther()
sound.volume.sound = v sound.volume.sound = v
t:SetText("Sound " .. tostring(math.floor(v * 100) / 100)) t:SetText("Sound " .. tostring(math.floor(v * 100) / 100))
end end
s:SetValue(sound.volume.sound) s:SetValue(sound.volume.sound)
f:AddItem(s) f:AddItem(s)
f:SetPos(485, 340)
f:SetWidth(150) f:SetPos(485, 340):SetWidth(150)
local f = lf.Create("form") local f = lf.Create("form"):SetName("Times / Duration")
f:SetName("Times / Duration")
local t = lf.Create("text") local t = lf.Create("text"):SetText("Generation: 0ms")
t:SetText("Generation: 0ms")
f:AddItem(t) f:AddItem(t)
statistics.generationtext = t statistics.generationtext = t
local t = lf.Create("text") local t = lf.Create("text"):SetText("Transfer: 0ms")
t:SetText("Transfer: 0ms")
f:AddItem(t) f:AddItem(t)
statistics.transfertext = t statistics.transfertext = t
local t = lf.Create("text") local t = lf.Create("text"):SetText("Wave View: 0ms")
t:SetText("Wave View: 0ms")
f:AddItem(t) f:AddItem(t)
statistics.waveviewtext = t statistics.waveviewtext = t
local t = lf.Create("text") local t = lf.Create("text"):SetText("Duration: 0ms")
t:SetText("Duration: 0ms")
f:AddItem(t) f:AddItem(t)
statistics.durationtext = t statistics.durationtext = t
f:SetPos(485, 185) f:SetPos(485, 185):SetWidth(150)
f:SetWidth(150)
end end
function updateParameters() function updateParameters()
@ -624,6 +645,7 @@ function love.load()
require("loveframes") require("loveframes")
lf = loveframes lf = loveframes
lf.util.SetActiveSkin("Orange") lf.util.SetActiveSkin("Orange")
love.graphics.setBackgroundColor(200, 200, 200) love.graphics.setBackgroundColor(200, 200, 200)
if not love.filesystem.isDirectory("sounds") then if not love.filesystem.isDirectory("sounds") then