Skip to content

Instantly share code, notes, and snippets.

@celediel
Last active October 8, 2020 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celediel/dca7854133fd8547243f28f3f0bd7408 to your computer and use it in GitHub Desktop.
Save celediel/dca7854133fd8547243f28f3f0bd7408 to your computer and use it in GitHub Desktop.
Activate With A Click
local this = {}
-- look, I made an enum in lua
this.click = {left = 0, right = 1, middle = 2, four = 3, five = 4, six = 5, seven = 6, eight = 7}
this.modName = "Activate with a Click"
this.author = "Celediel"
this.modInfo =
[[Activate/interact with a click of the mouse, because simply mapping activate to a mouse button doesn't work when activating with objects that are made interactive by an MWSE Lua script.
Fixes interactivity with objects such as wells and kegs in Ashfall, and tameable guars in The Guar Whisperer.
I don't know why it happens but I wrote this script to "fix" it.
Requires Activate to be bound to a keyboard key for obvious reasons.
]]
this.version = "1.2.1"
this.configString = string.gsub(this.modName, "%s+", "")
return this
local common = require("celediel.Activate With A Click.common")
local defaultConfig = {clickActivate = true, activateMouseButton = common.click.right, disableWithWeapon = false}
local config = mwse.loadConfig(common.configString, defaultConfig)
-- mwse.log("[%s] Loaded config:", common.modName)
-- mwse.log(json.encode(config, {indent = true}))
return config
-- {{{ variables and config and such
local common = require("celediel.Activate With A Click.common")
local config = require("celediel.Activate With A Click.config")
local activateKey = nil
-- https://mwse.readthedocs.io/en/latest/mwscript/references.html#control-types
local activateKeyScanCode = 5
-- }}}
-- {{{ helper functions
local function getActivateKey()
local inputMaps = tes3.worldController.inputController.inputMaps
-- offset +1 between control type and inputMaps array (thanks cpassuel)
if (inputMaps[activateKeyScanCode + 1].device == 0) then
return inputMaps[activateKeyScanCode + 1].code
else
return nil
end
end
-- }}}
-- {{{ event functions
local function onMouseButtonDown(e)
-- not in menus
if tes3.menuMode() then return end
-- not if weapon readied if configured to do so
if tes3.mobilePlayer and tes3.mobilePlayer.weaponDrawn and config.disableWithWeapon then return end
if e.button == config.activateMouseButton and config.clickActivate and activateKey then tes3.tapKey(activateKey) end
end
-- refresh keybinding when leaving menu (thanks again cpassuel)
local function onMenuExit() activateKey = getActivateKey() end
local function onInitialized()
activateKey = getActivateKey()
event.register("mouseButtonDown", onMouseButtonDown)
event.register("menuExit", onMenuExit)
if activateKey then
mwse.log("[%s] Successfully initialized", common.modName)
else
mwse.log("[%s] activate key not bound to keyboard, mod will not work until activate is bound to keyboard",
common.modName)
end
end
event.register("initialized", onInitialized)
-- }}}
-- {{{ MCM
event.register("modConfigReady", function() mwse.mcm.register(require("celediel.Activate With A Click.mcm")) end)
-- }}}
-- vim:fdm=marker
local common = require("celediel.Activate With A Click.common")
local config = require("celediel.Activate With A Click.config")
local function createTableVar(id) return mwse.mcm.createTableVariable {id = id, table = config} end
local template = mwse.mcm.createTemplate(common.modName)
template:saveOnClose(common.configString, config)
local page = template:createSideBarPage({
label = "Sidebar Page???",
description = string.format("%s v%s by %s\n\n%s", common.modName, common.version, common.author, common.modInfo)
})
local category = page:createCategory(common.modName)
category:createYesNoButton({
label = "Activate with a click?",
description = "Does what it says!",
variable = createTableVar("clickActivate")
})
category:createYesNoButton({
label = "Disable when weapon is out?",
description = "Don't activate with a click when weapon is readied.",
variable = createTableVar("disableWithWeapon")
})
category:createDropdown({
label = "Select which mouse button activates",
options = {
{label = "Left", value = common.click.left},
{label = "Right", value = common.click.right},
{label = "Middle", value = common.click.middle},
{label = "Four", value = common.click.four},
{label = "Five", value = common.click.five},
{label = "Six", value = common.click.six},
{label = "Seven", value = common.click.seven},
{label = "Eight", value = common.click.eight}
},
variable = createTableVar("activateMouseButton")
})
return template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment