mirror of
https://github.com/OpenMW/openmw.git
synced 2026-01-04 04:43:07 +00:00
Deduplicate event handling code
This commit is contained in:
parent
9af71e9575
commit
0df0ad9c1c
6 changed files with 51 additions and 66 deletions
|
|
@ -110,5 +110,38 @@ function aux_util.mapFilterSort(array, scoreFn)
|
|||
return sortedValues, sortedScores
|
||||
end
|
||||
|
||||
---
|
||||
-- Iterates over an array of event handlers, calling each in turn until one returns false.
|
||||
-- @function [parent=#util] callEventHandlers
|
||||
-- @param #table handlers An optional array of handlers to invoke
|
||||
-- @param #any ... Arguments to pass to each event handler
|
||||
-- @return boolean True if no further handlers should be called
|
||||
function aux_util.callEventHandlers(handlers, ...)
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](...) == false then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---
|
||||
-- Iterates over an array of event handler arrays, passing each to `aux_util.callEventHandlers` until the event is handled.
|
||||
-- @function [parent=#util] callMultipleEventHandlers
|
||||
-- @param #table handlers An array of event handler arrays
|
||||
-- @param #any ... Arguments to pass to each event handler
|
||||
-- @return boolean True if no further handlers should be called
|
||||
function aux_util.callMultipleEventHandlers(handlers, ...)
|
||||
for i = 1, #handlers do
|
||||
local stop = aux_util.callEventHandlers(handlers[i], ...)
|
||||
if stop then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return aux_util
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ local async = require('openmw.async')
|
|||
local core = require('openmw.core')
|
||||
local types = require('openmw.types')
|
||||
local world = require('openmw.world')
|
||||
local auxUtil = require('openmw_aux.util')
|
||||
|
||||
local EnableObject = async:registerTimerCallback('EnableObject', function(obj) obj.enabled = true end)
|
||||
|
||||
|
|
@ -38,21 +39,9 @@ local function onActivate(obj, actor)
|
|||
if obj.parentContainer then
|
||||
return
|
||||
end
|
||||
local handlers = handlersPerObject[obj.id]
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](obj, actor) == false then
|
||||
return -- skip other handlers
|
||||
end
|
||||
end
|
||||
end
|
||||
handlers = handlersPerType[obj.type]
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](obj, actor) == false then
|
||||
return -- skip other handlers
|
||||
end
|
||||
end
|
||||
local handled = auxUtil.callMultipleEventHandlers({ handlersPerObject[obj.id], handlersPerType[obj.type] }, obj, actor)
|
||||
if handled then
|
||||
return
|
||||
end
|
||||
types.Actor.activeEffects(actor):remove('invisibility')
|
||||
world._runStandardActivationAction(obj, actor)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ local self = require('openmw.self')
|
|||
local storage = require('openmw.storage')
|
||||
local types = require('openmw.types')
|
||||
local util = require('openmw.util')
|
||||
local auxUtil = require('openmw_aux.util')
|
||||
local Actor = types.Actor
|
||||
local Weapon = types.Weapon
|
||||
local Player = types.Player
|
||||
|
|
@ -270,10 +271,8 @@ local function spawnBloodEffect(position)
|
|||
end
|
||||
|
||||
local function onHit(data)
|
||||
for i = #onHitHandlers, 1, -1 do
|
||||
if onHitHandlers[i](data) == false then
|
||||
return -- skip other handlers
|
||||
end
|
||||
if auxUtil.callEventHandlers(onHitHandlers, data) then
|
||||
return
|
||||
end
|
||||
if data.successful and not godMode() then
|
||||
I.Combat.applyArmor(data)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,10 @@
|
|||
local anim = require('openmw.animation')
|
||||
local self = require('openmw.self')
|
||||
local auxUtil = require('openmw_aux.util')
|
||||
|
||||
local playBlendedHandlers = {}
|
||||
local function onPlayBlendedAnimation(groupname, options)
|
||||
for i = #playBlendedHandlers, 1, -1 do
|
||||
if playBlendedHandlers[i](groupname, options) == false then
|
||||
return
|
||||
end
|
||||
end
|
||||
local function onPlayBlendedAnimation(groupname, options)
|
||||
auxUtil.callEventHandlers(playBlendedHandlers, groupname, options)
|
||||
end
|
||||
|
||||
local function playBlendedAnimation(groupname, options)
|
||||
|
|
@ -20,22 +17,7 @@ end
|
|||
|
||||
local textKeyHandlers = {}
|
||||
local function onAnimationTextKey(groupname, key)
|
||||
local handlers = textKeyHandlers[groupname]
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](groupname, key) == false then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
handlers = textKeyHandlers['']
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](groupname, key) == false then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
auxUtil.callMultipleEventHandlers({ textKeyHandlers[groupname], textKeyHandlers[''] }, groupname, key)
|
||||
end
|
||||
|
||||
local initialized = false
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ local self = require('openmw.self')
|
|||
local I = require('openmw.interfaces')
|
||||
local types = require('openmw.types')
|
||||
local core = require('openmw.core')
|
||||
local auxUtil = require('openmw_aux.util')
|
||||
local NPC = require('openmw.types').NPC
|
||||
local Skill = core.stats.Skill
|
||||
|
||||
|
|
@ -104,11 +105,7 @@ local function skillUsed(skillid, options)
|
|||
end
|
||||
end
|
||||
|
||||
for i = #skillUsedHandlers, 1, -1 do
|
||||
if skillUsedHandlers[i](skillid, options) == false then
|
||||
return
|
||||
end
|
||||
end
|
||||
auxUtil.callEventHandlers(skillUsedHandlers, skillid, options)
|
||||
end
|
||||
|
||||
local function skillLevelUp(skillid, source)
|
||||
|
|
@ -144,11 +141,7 @@ local function skillLevelUp(skillid, source)
|
|||
options.levelUpSpecializationIncreaseValue = core.getGMST('iLevelupSpecialization')
|
||||
end
|
||||
|
||||
for i = #skillLevelUpHandlers, 1, -1 do
|
||||
if skillLevelUpHandlers[i](skillid, source, options) == false then
|
||||
return
|
||||
end
|
||||
end
|
||||
auxUtil.callEventHandlers(skillLevelUpHandlers, skillid, source, options)
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -1,26 +1,15 @@
|
|||
local types = require('openmw.types')
|
||||
local world = require('openmw.world')
|
||||
local auxUtil = require('openmw_aux.util')
|
||||
|
||||
local handlersPerObject = {}
|
||||
local handlersPerType = {}
|
||||
|
||||
local function useItem(obj, actor, force)
|
||||
local options = { force = force or false }
|
||||
local handlers = handlersPerObject[obj.id]
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](obj, actor, options) == false then
|
||||
return -- skip other handlers
|
||||
end
|
||||
end
|
||||
end
|
||||
handlers = handlersPerType[obj.type]
|
||||
if handlers then
|
||||
for i = #handlers, 1, -1 do
|
||||
if handlers[i](obj, actor, options) == false then
|
||||
return -- skip other handlers
|
||||
end
|
||||
end
|
||||
local handled = auxUtil.callMultipleEventHandlers({ handlersPerObject[obj.id], handlersPerType[obj.type] }, obj, actor, options)
|
||||
if handled then
|
||||
return
|
||||
end
|
||||
world._runStandardUseAction(obj, actor, options.force)
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue