Master Actions

mail@pastecode.io avatar
unknown
lua
16 days ago
26 kB
3
Indexable
Never
local editingMenu
local focusTest

local focusOn = {
    local EditingMenu = false,
    local Group = nil,
    local Action = nil,
    local Animation = nil,
    local Prop = nil,
    local Pfx = nil,
    local Sound = nil,
    local function ResetAll()
        EditingMenu = false
        Group = nil
        Action = nil
        Animation = nil
        Prop = nil
        Pfx = nil
        Sound = nil
    end
}

local function CreateEtitingMenu(actionGroups)
    if editingMenu then
        menu.delete(editingMenu)
    end

    local focus = nil

    editingMenu = menu.list(menu.my_root(), "Edit Actions", {}, "Editar os grupos e as ações")
    if focusOn.EditingMenu then
        focus = editingMenu
        focusOn.ResetAll()
    end

    menu.divider(editingMenu, "Editando...")

    for groupName, actions in pairs(actionGroups) do

        
        local groupMenu = menu.list(editingMenu, groupName, {}, "Editar grupo: " .. groupName)
        if not focusTest then
            focusTest = groupMenu
        end
        menu.divider(groupMenu, "Editando o grupo: " .. groupName)

        if focusOn.Group == groupName then
            focus = groupMenu
            --focusOn.ResetAll()
        end
        
        menu.text_input(groupMenu, "Nome do grupo", {"nameofgroup" .. groupName}, "O nome do grupo", function(value)
            util.toast("Nome do grupo: " .. value)
            data.actions[value] = actionGroups[groupName]
            data.actions[groupName] = nil
            data.SaveData()
            focusOn.Group = value
            CreateEtitingMenu(data.actions)
            CreateGroupsAndActions(data.actions)
        end, groupName)
        menu.action(groupMenu, "Deletar", {}, "Deleta o grupo \'" .. groupName .. "\', e todas as suas ações", function()
            --Confirmar a exclusão
            util.toast("Deletando grupo: " .. groupName)
            data.actions[groupName] = nil
            data.SaveData()
            focusOn.EditingMenu = true
            CreateEtitingMenu(data.actions)
            CreateGroupsAndActions(data.actions)
        end)
        menu.action(groupMenu, "Adicionar", {}, "Adiciona uma nova ação ao grupo", function()
            --ir até o menu da nova ação já editando o nome
            util.toast("Adicionando ação")
            table.insert(data.actions[groupName], {name = "Default " .. #actions + 1})
            data.SaveData()
            focusOn.Action = "Default " .. #actions
            CreateEtitingMenu(data.actions)
            CreateGroupsAndActions(data.actions)
        end)
        
        menu.divider(groupMenu, "Lista de ações")
        
        for actionIndex, action in ipairs(actions) do
            local actionMenu = menu.list(groupMenu, action.name, {}, "Editar ação: " .. action.name)
            if focusOn.Action == groupName .. action.name then
                focus = actionMenu
                --focusOn.ResetAll()
            end

            menu.divider(actionMenu, "Editando a ação: " .. action.name)
            
            menu.text_input(actionMenu, "Nome da ação", {"nameofaction" .. groupName .. action.name}, "O nome da ação", function(value)
                util.toast("Nome da ação: " .. value)
                data.actions[groupName][actionIndex].name = value
                data.SaveData()
                focusOn.Action = groupName .. value
                CreateEtitingMenu(data.actions)
                CreateGroupsAndActions(data.actions)
            end, action.name)

            menu.action(groupMenu, "Deletar", {}, "Deleta a ação \'" .. action.name .. "\', e todos os comportamentos", function()
                --Confirmar a exclusão
                util.toast("Deletando a ação: " .. action.name)
                data.actions[groupName][actionIndex] = nil
                data.SaveData()
                focusOn.Group = groupName
                CreateEtitingMenu(data.actions)
                CreateGroupsAndActions(data.actions)
            end)
            
            local animationsMenu = menu.list(actionMenu, "Animações (" .. #(action.animations or {}) .. ")", {}, "Animações dessa ação")
            local propsMenu = menu.list(actionMenu, "Props (" .. #(action.props or {}) .. ")"", {}, "Pops dessa ação")
            local pfxsMenu = menu.list(actionMenu, "Pfxs (" .. #(action.pfxs or {}) .. ")"", {}, "Pfxs dessa ação")
            local soundsMenu = menu.list(actionMenu, "Sounds (" .. #(action.sounds or {}) .. ")"", {}, "Sons dessa ação")
            
            
            menu.action(animationsMenu, "Adicionar", {}, "Adicionar uma nova animação", function ()
                data.actions[groupName][actionIndex].animations = action.animations or {}
                table.insert(data.actions[groupName][actionIndex].animations, #action, {dict = "anim@mp_player_intupperthumbs_up", anim = "idle_a", flag = 49})
                data.SaveData()
                focusOn.Animation = groupName .. action.. #data.action[groupName][actionIndex].animations
                CreateEtitingMenu(data.actions)
                CreateGroupsAndActions(data.actions)
            end)
            
            menu.action(propsMenu, "Adicionar", {}, "Adicionar um novo prop", function()
                data.actions[groupName][actionIndex].props = action.props or {}
                table.insert(data.actions[groupName][actionIndex].props, #action, {prop = 0, attached = true, boneID = 0})
                data.SaveData()
                focusOn.Prop = groupName .. action.. #data.action[groupName][actionIndex].props
                CreateEtitingMenu(data.actions)
                CreateGroupsAndActions(data.actions)
            end)
            
            menu.action(pfxsMenu, "Adicionar", {}, "Adicionar um novo pfx", function()
                data.actions[groupName][actionIndex].pfxs = action.pfxs or {}
                table.insert(data.actions[groupName][actionIndex].pfxs, #action, {effectAsset = "", pfxName = ""})
                data.SaveData()
                focusOn.Pfx = groupName .. action.. #data.action[groupName][actionIndex].pfxs
                CreateEtitingMenu(data.actions)
                CreateGroupsAndActions(data.actions)
            end)
            
            menu.action(soundsMenu, "Adicionar", {}, "Adicionar uma novo som", function()
                data.actions[groupName][actionIndex].sounds = action.sounds or {}
                table.insert(data.actions[groupName][actionIndex].sounds, #action, {sound = ""})
                data.SaveData()
                focusOn.Sound = groupName .. action.. #data.action[groupName][actionIndex].sounds
                CreateEtitingMenu(data.actions)
                CreateGroupsAndActions(data.actions)
            end)
            
            if action.animation then
                for index, animation in ipairs(action.animation) do
                    local animationMenu = menu.list(animationsMenu, "Animação" .. index, {}, "")

                    if focusOn.Animation == groupMenu .. action.name .. index then
                        focus = animationMenu
                    end
                    
                    menu.text_input(animationMenu, "Dictionary", {"dict" .. groupName .. action.name .. index}, "", function(value)
                        data.action[groupMenu][actionIndex].animations[index].dict = value
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, animation.dict)
                    
                    menu.text_input(animationMenu, "Animation", {"anim" .. groupName .. action.name .. index}, "", function(value)
                        data.action[groupMenu][actionIndex].animations[index].anim = value
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, animation.anim)

                    local animFlags = {
                        FLAG_NORMAL = 0,
                        FLAG_REPEAT = 1,
                        FLAG_STOP_LAST_FRAME = 2,
                        FLAG_UPPERBODY = 16,
                        FLAG_ENABLE_PLAYER_CONTROL = 32,
                        FLAG_CANCELABLE = 120
                    }

                    local function boolsToFlag(repeatAnim, stopLastFrame, upperBody, enablePlayerControl, cancelable)
                        flag = 0
                        flag = flag | (repeatAnim and animFlags.FLAG_REPEAT or 0)
                        flag = flag | (stopLastFrame and animFlags.FLAG_STOP_LAST_FRAME or 0)
                        flag = flag | (upperBody and animFlags.FLAG_UPPERBODY or 0)
                        flag = flag | (enablePlayerControl and animFlags.FLAG_ENABLE_PLAYER_CONTROL or 0)
                        flag = flag | (cancelable and animFlags.FLAG_CANCELABLE or 0)
                        return flag
                    end

                    local function flagToBools(flag)
                        return 
                        (flag & animFlags.FLAG_REPEAT) ~= 0,
                        (flag & animFlags.FLAG_STOP_LAST_FRAME) ~= 0,
                        (flag & animFlags.FLAG_UPPERBODY) ~= 0,
                        (flag & animFlags.FLAG_ENABLE_PLAYER_CONTROL) ~= 0,
                        (flag & animFlags.FLAG_CANCELABLE) ~= 0,
                    end
                    
                    local looped, stopLastFrame, upperBody, enablePlayerControl, cancelable = flagToBools(animation.flag)
                    
                    menu.toggle(animationMenu, "Looped", {}, "Se a animação deve ficar repetindo em loop", function(value)
                        looped = value
                        data.actions[groupName][actionIndex].animations[index].flag = boolsToFlag(looped, stopLastFrame, upperBody, enablePlayerControl, cancelable)
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, looped)

                    menu.toggle(animationMenu, "Stop last frame", {}, "", function(value)
                        stopLastFrame = value
                        data.actions[groupName][actionIndex].animations[index].flag = boolsToFlag(looped, stopLastFrame, upperBody, enablePlayerControl, cancelable)
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, stopLastFrame)

                    menu.toggle(animationMenu, "upper body", {}, "", function(value)
                        upperBody = value
                        data.actions[groupName][actionIndex].animations[index].flag = boolsToFlag(looped, stopLastFrame, upperBody, enablePlayerControl, cancelable)
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, upperBody)

                    menu.toggle(animationMenu, "enable player controll", {}, "", function(value)
                        enablePlayerControl = value
                        data.actions[groupName][actionIndex].animations[index].flag = boolsToFlag(looped, stopLastFrame, upperBody, enablePlayerControl, cancelable)
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, enablePlayerControl)
                    
                    menu.toggle(animationMenu, "cancelable", {}, "", function(value)
                        cancelable = value
                        data.actions[groupName][actionIndex].animations[index].flag = boolsToFlag(looped, stopLastFrame, upperBody, enablePlayerControl, cancelable)
                        data.SaveData()
                        focusOn.Animation = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, cancelable)
                    
                    menu.slider(animationMenu, "Duration", {}, "Duração da animação \n-1 - looped \n<1 Lenta \n>1 Rapida", -1, 100000, 1, function()
                    
                    end)

                    menu.action(groupMenu, "Deletar", {}, "Deleta a animação", function()
                        --Confirmar a exclusão
                        util.toast("Deletando animação")
                        data.actions[groupName][actionIndex].animations[index] = nil
                        if #data.actions[groupName][actionIndex].animations == 0 then
                            data.actions[groupName][actionIndex].animations = nil
                        end
                        data.SaveData()
                        focusOn.Action = actionMenu
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)
                end
            end
            
            if action.props then
                for index, prop in ipairs(action.props) do
                    local propMenu = menu.list(propsMenu, "Props " .. index, {}, "")

                    if focusOn.Prop == groupMenu .. action.name .. index then
                        focus = propMenu
                    end

                    menu.text_input(animationMenu, "Prop", {"prop" .. groupName .. action.name .. index}, "", function(value)
                        data.action[groupMenu][actionIndex].props[index].prop = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, prop.prop)

                    menu.toggle(animationMenu, "Attached", {}, "Se o objeto irá ficar grudado no ped", function(value)
                        data.actions[groupName][actionIndex].props[index].attached = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end, prop.attached)

                    menu.slider(animationMenu, "Position X", {}, "Posição x do prop", -100000, 100000, 1, function(value)
                        data.actions[groupName][actionIndex].props[index].posX = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)

                    menu.slider(animationMenu, "Position Y", {}, "Posição y do prop", -100000, 100000, 1, function(value)
                        data.actions[groupName][actionIndex].props[index].posY = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)

                    menu.slider(animationMenu, "Position Z", {}, "Posição z do prop", -100000, 100000, 1, function(value)
                        data.actions[groupName][actionIndex].props[index].posZ = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)

                    menu.slider(animationMenu, "Rotação X", {}, "Rotação x do prop", -100000, 100000, 1, function(value)
                        data.actions[groupName][actionIndex].props[index].rotX = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)

                    menu.slider(animationMenu, "Rotação Y", {}, "Rotação y do prop", -100000, 100000, 1, function(value)
                        data.actions[groupName][actionIndex].props[index].rotY = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)

                    menu.slider(animationMenu, "Rotação Z", {}, "Rotação z do prop", -100000, 100000, 1, function(value)
                        data.actions[groupName][actionIndex].props[index].rotZ = value
                        data.SaveData()
                        focusOn.Prop = groupName .. action.. #index
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)
                    
                    menu.action(groupMenu, "Deletar", {}, "Deleta o prop", function()
                        --Confirmar a exclusão
                        util.toast("Deletando prop")
                        data.actions[groupName][actionIndex].props[index] = nil
                        if #data.actions[groupName][actionIndex].props == 0 then
                            data.actions[groupName][actionIndex].props = nil
                        end
                        data.SaveData()
                        focusOn.Action = actionMenu
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)
                end
            end
            
            if action.pfxs then
                for index, pfx in ipairs(action.pfxs) do
                    local pfxMenu = menu.list(pfxsMenu, "Pfx " .. index, {}, "")

                    if focusOn.Pfx == groupMenu .. action.name .. index then
                        focus = pfxMenu
                    end

                    menu.action(groupMenu, "Deletar", {}, "Deleta o pfx", function()
                        --Confirmar a exclusão
                        util.toast("Deletando pfx")
                        data.actions[groupName][actionIndex].pfxs[index] = nil
                        if #data.actions[groupName][actionIndex].pfxs == 0 then
                            data.actions[groupName][actionIndex].pfxs = nil
                        end
                        data.SaveData()
                        focusOn.Action = actionMenu
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)
                end
            end
            
            if action.sounds then
                for index, sound in ipairs(action.sounds) do
                    local soundMenu = menu.list(pfxsMenu, "Sounds " .. index, {}, "")

                    if focusOn.Sound == groupMenu .. action.name .. index then
                        focus = soundMenu
                    end

                    menu.action(groupMenu, "Deletar", {}, "Deleta o som", function()
                        --Confirmar a exclusão
                        util.toast("Deletando som")
                        data.actions[groupName][actionIndex].sounds[index] = nil
                        if #data.actions[groupName][actionIndex].sounds == 0 then
                            data.actions[groupName][actionIndex].sounds = nil
                        end
                        data.SaveData()
                        focusOn.Action = actionMenu
                        CreateEtitingMenu(data.actions)
                        CreateGroupsAndActions(data.actions)
                    end)
                end
            end
        end
    end

    if focus then
        focusOn.ResetAll()
        menu.focus(focus)
    end
end

local function CreateNewActionSubMenu()
    local boneNames = {"Head", "Neck", "Spine", "Pelvis", "Left Arm", "Right Arm", "Left Leg", "Right Leg", "Left Foot", "Right Foot"}
    local boneIDs = {31086, 39317, 24818, 11816, 18905, 57005, 46078, 28252, 2108, 65245}

    local newGroupMenu = menu.list(menu.my_root(), "Add New Group", {}, "Create a new group of actions")
    local actionMenu = menu.list(newGroupMenu, "Add New Action", {}, "Create a new action")
    
    local newAction = {name = "", animations = {}, props = {}, pfxs = {}, sounds = {}}
    local newGroupName = ""

    menu.text_input(newGroupMenu, "Group Name", {"group_name"}, "Enter the name of the group", function(value)
        newGroupName = value
    end)

    menu.text_input(actionMenu, "Action Name", {"action_name"}, "Enter the name of the action", function(value)
        newAction.name = value
    end)

    local animationMenu = menu.list(actionMenu, "Add Animation", {}, "Add a new animation to the action")
    menu.text_input(animationMenu, "Dict", {"dict"}, "Enter animation dictionary", function(value)
        table.insert(newAction.animations, {dict = value, anim = "", flag = 0})
    end)
    menu.text_input(animationMenu, "Anim", {"anim"}, "Enter animation name", function(value)
        newAction.animations[#newAction.animations].anim = value
    end)
    menu.slider(animationMenu, "Flag", {}, "Enter animation flag", 0, 49, 0, 1, function(value)
        newAction.animations[#newAction.animations].flag = value
    end)

    local propMenu = menu.list(actionMenu, "Add Prop", {}, "Add a new prop to the action")
    menu.text_input(propMenu, "Prop Hash", {"propHash"}, "Enter prop hash", function(value)
        table.insert(newAction.props, {prop = tonumber(value), attached = true, posX = 0, posY = 0, posZ = 0, rotX = 0, rotY = 0, rotZ = 0, boneID = 0})
    end)
    menu.slider_text(propMenu, "Bone", {}, "Select bone to attach prop", boneNames, function(index)
        newAction.props[#newAction.props].boneID = boneIDs[index]
    end)

    local pfxMenu = menu.list(actionMenu, "Add Pfx", {}, "Add a new pfx to the action")
    menu.text_input(pfxMenu, "Effect Asset", {"effect_asset"}, "Enter effect asset", function(value)
        table.insert(newAction.pfxs, {effectAsset = value, pfxName = "", effectDelay = 0, attached = true, attachedToObject = false, posX = 0, posY = 0, posZ = 0, rotX = 0, rotY = 0, rotZ = 0, scale = 1.0, boneID = 0})
    end)
    menu.text_input(pfxMenu, "Pfx Name", {"pfx_name"}, "Enter pfx name", function(value)
        newAction.pfxs[#newAction.pfxs].pfxName = value
    end)
    menu.slider(pfxMenu, "Effect Delay", {}, "Enter effect delay", 0, 10000, 0, 100, function(value)
        newAction.pfxs[#newAction.pfxs].effectDelay = value
    end)
    menu.slider_text(pfxMenu, "Bone", {"bone"}, "Select bone to attach pfx", boneNames, function(index)
        newAction.pfxs[#newAction.pfxs].boneID = boneIDs[index]
    end)

    local soundMenu = menu.list(actionMenu, "Add Sound", {}, "Add a new sound to the action")
    menu.text_input(soundMenu, "Sound Name", {"sound_name"}, "Enter sound name", function(value)
        table.insert(newAction.sounds, {soundName = value, soundSet = ""})
    end)
    menu.text_input(soundMenu, "Sound Set", {"sound_set"}, "Enter sound set", function(value)
        newAction.sounds[#newAction.sounds].soundSet = value
    end)

    menu.action(actionMenu, "Save Action", {}, "Save the new action", function()
        if not data.actions[newGroupName] then
            data.actions[newGroupName] = {}
        end
        table.insert(data.actions[newGroupName], newAction)
        util.toast("Action saved to group: " .. newGroupName)
        data.SaveData()
        Cleanup()
        CreateGroupsAndActions(data.actions)
    end)
end

--- Start Menu

menu.action(menu.my_root(), "Stop Actions", {"stopactions"}, "Stop all actions and cleanup", function()
    Cleanup(true)
end)

menu.action(menu.my_root(), "Reload save data", {}, "Reload all groups and actions, and read file again", function()
    data.LoadData()
    CreateGroupsAndActions(data.actions)
end)

menu.action(menu.my_root(), "Focus Test", {}, "", function()
    menu.focus(focusTest)
end)

--CreateNewActionSubMenu()
CreateEtitingMenu(data.actions)
CreateGroupsAndActions(data.actions)
Leave a Comment