sm64coopdx/mods/character-select-coop/o-api.lua

187 lines
6.1 KiB
Lua
Raw Normal View History

2023-12-16 18:25:28 +00:00
2024-02-20 21:10:45 +00:00
--- @class CharacterTable
--- @field public name string
--- @field public saveName string
--- @field public description table
--- @field public credit string
--- @field public color Color
--- @field public model ModelExtendedId|integer
--- @field public forceChar CharacterType
--- @field public lifeIcon TextureInfo
--- @field public camScale integer
2023-12-16 18:25:28 +00:00
local characterVoices = {}
2024-02-20 21:10:45 +00:00
local saveNameTable = {}
2023-12-16 18:25:28 +00:00
local E_MODEL_ARMATURE = smlua_model_util_get_id("armature_geo")
local table_insert = table.insert
local type = type
2024-02-20 21:10:45 +00:00
local function split_text_into_lines(text)
local words = {}
for word in text:gmatch("%S+") do
table.insert(words, word)
end
local lines = {}
local currentLine = ""
for i, word in ipairs(words) do
local measuredWidth = djui_hud_measure_text(currentLine .. " " .. word)*0.3
if measuredWidth <= 100 then
currentLine = currentLine .. " " .. word
else
table.insert(lines, currentLine)
currentLine = word
end
end
table.insert(lines, currentLine) -- add the last line
return lines
end
2023-12-16 18:25:28 +00:00
---------
-- API --
---------
---@param name string|nil Underscores turn into Spaces
2024-02-20 21:10:45 +00:00
---@param description table|string|nil {"string"}
2023-12-16 18:25:28 +00:00
---@param credit string|nil
---@param color Color|nil {r, g, b}
2024-02-20 21:10:45 +00:00
---@param modelInfo ModelExtendedId|integer|nil Use smlua_model_util_get_id()
2023-12-16 18:25:28 +00:00
---@param forceChar CharacterType|nil CT_MARIO, CT_LUIGI, CT_TOAD, CT_WALUIGI, CT_WARIO
---@param lifeIcon TextureInfo|nil Use get_texture_info()
2024-02-20 21:10:45 +00:00
---@param camScale integer|nil Zooms the camera based on a multiplier (Default 1.0)
2023-12-16 18:25:28 +00:00
---@return integer
2024-02-20 21:10:45 +00:00
local function character_add(name, description, credit, color, modelInfo, forceChar, lifeIcon, camScale)
if type(description) == "string" then
description = split_text_into_lines(description)
end
2023-12-16 18:25:28 +00:00
table_insert(characterTable, {
2024-02-20 21:10:45 +00:00
name = name and name or "Untitled",
saveName = name and string_space_to_underscore(name) or "Untitled",
2023-12-16 18:25:28 +00:00
description = description and description or {"No description has been provided"},
credit = credit and credit or "Unknown",
2023-12-23 16:40:20 +00:00
color = color and color or {r = 255, g = 255, b = 255},
2024-02-20 21:10:45 +00:00
model = modelInfo and modelInfo or E_MODEL_ARMATURE,
2023-12-16 18:25:28 +00:00
forceChar = forceChar and forceChar or CT_MARIO,
2024-02-20 21:10:45 +00:00
lifeIcon = lifeIcon and lifeIcon or nil,
camScale = camScale and camScale or 1
2023-12-16 18:25:28 +00:00
})
2024-02-20 21:10:45 +00:00
saveNameTable[#characterTable] = characterTable[#characterTable].saveName
2023-12-16 18:25:28 +00:00
return #characterTable
end
---@param charNum integer Use _G.charSelect.character_get_number_from_string() or _G.charSelect.character_add()'s return value
---@param name string|nil Underscores turn into Spaces
2024-02-20 21:10:45 +00:00
---@param description table|string|nil {"string"}
2023-12-16 18:25:28 +00:00
---@param credit string|nil
---@param color Color|nil {r, g, b}
2024-02-20 21:10:45 +00:00
---@param modelInfo ModelExtendedId|integer|nil Use smlua_model_util_get_id()
2023-12-16 18:25:28 +00:00
---@param forceChar CharacterType|nil CT_MARIO, CT_LUIGI, CT_TOAD, CT_WALUIGI, CT_WARIO
---@param lifeIcon TextureInfo|nil Use get_texture_info()
2024-02-20 21:10:45 +00:00
---@param camScale integer|nil Zooms the camera based on a multiplier (Default 1.0)
local function character_edit(charNum, name, description, credit, color, modelInfo, forceChar, lifeIcon, camScale)
if type(description) == "string" then
description = split_text_into_lines(description)
end
2023-12-16 18:25:28 +00:00
characterTable[charNum] = characterTable[charNum] and {
2024-02-20 21:10:45 +00:00
name = name and name or characterTable[charNum].name,
saveName = saveNameTable[charNum],
2023-12-16 18:25:28 +00:00
description = description and description or characterTable[charNum].description,
credit = credit and credit or characterTable[charNum].credit,
color = color and color or characterTable[charNum].color,
2024-02-20 21:10:45 +00:00
model = modelInfo and modelInfo or characterTable[charNum].model,
2023-12-16 18:25:28 +00:00
forceChar = forceChar and forceChar or characterTable[charNum].forceChar,
lifeIcon = lifeIcon and lifeIcon or characterTable[charNum].lifeIcon,
2024-02-20 21:10:45 +00:00
camScale = camScale and camScale or 1
2023-12-16 18:25:28 +00:00
} or nil
end
---@param modelInfo ModelExtendedId|integer
---@param clips table
2024-02-20 21:10:45 +00:00
local function character_add_voice(modelInfo, clips)
2023-12-16 18:25:28 +00:00
characterVoices[modelInfo] = clips
end
2024-02-20 21:10:45 +00:00
---@param modelInfo ModelExtendedId|integer
---@param caps table
local function character_add_caps(modelInfo, caps)
characterCaps[modelInfo] = caps
end
---@return CharacterTable
local function character_get_current_table()
2023-12-16 18:25:28 +00:00
return characterTable[currChar]
end
2024-02-20 21:10:45 +00:00
local function character_get_current_model_number()
2023-12-16 18:25:28 +00:00
return currChar
end
---@param name string
2024-02-20 21:10:45 +00:00
local function character_get_number_from_string(name)
2023-12-16 18:25:28 +00:00
for i = 2, #characterTable do
if characterTable[i].name == name or characterTable[i].name == string_space_to_underscore(name) then
return i
end
end
2024-02-20 21:10:45 +00:00
return nil
2023-12-16 18:25:28 +00:00
end
---@param m MarioState
2024-02-20 21:10:45 +00:00
local function character_get_voice(m)
2023-12-16 18:25:28 +00:00
return characterVoices[gPlayerSyncTable[m.playerIndex].modelId]
end
2024-02-20 21:10:45 +00:00
local function version_get()
2023-12-16 18:25:28 +00:00
return modVersion
end
2024-02-20 21:10:45 +00:00
local function is_menu_open()
2023-12-23 16:40:20 +00:00
return menuAndTransition
2023-12-16 18:25:28 +00:00
end
2024-02-20 21:10:45 +00:00
local function hook_allow_menu_open(func)
table_insert(allowMenu, func)
2023-12-16 18:25:28 +00:00
end
2024-02-20 21:10:45 +00:00
local function is_options_open()
2023-12-16 18:25:28 +00:00
return options
end
local controller = {
buttonDown = 0,
buttonPressed = 0,
extStickX = 0,
extStickY = 0,
rawStickX = 0,
rawStickY = 0,
stickMag = 0,
stickX = 0,
stickY = 0
}
---@param tableNum integer
2024-02-20 21:10:45 +00:00
local function get_status(tableNum)
2023-12-16 18:25:28 +00:00
return optionTable[tableNum].toggle
end
_G.charSelectExists = true -- Ace
_G.charSelect = {
character_add = character_add,
character_edit = character_edit,
character_add_voice = character_add_voice,
2024-02-20 21:10:45 +00:00
character_add_caps = character_add_caps,
2023-12-16 18:25:28 +00:00
character_get_current_table = character_get_current_table,
character_get_current_model_number = character_get_current_model_number,
character_get_number_from_string = character_get_number_from_string,
character_get_voice = character_get_voice,
version_get = version_get,
is_menu_open = is_menu_open,
hook_allow_menu_open = hook_allow_menu_open,
is_options_open = is_options_open,
get_status = get_status,
optionTableRef = optionTableRef,
controller = controller,
2023-12-18 22:01:33 +00:00
}