Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement event handling to state #4051

Open
ESV-Sweetplum opened this issue Apr 12, 2024 · 0 comments
Open

Implement event handling to state #4051

ESV-Sweetplum opened this issue Apr 12, 2024 · 0 comments

Comments

@ESV-Sweetplum
Copy link

ESV-Sweetplum commented Apr 12, 2024

Is your feature request related to a problem? Please describe.

In my plugin, placing a group of timing lines and scroll velocities will add data into a table, which is then saved in a bookmark for future use. The purpose of this is to easily be able to delete or modify all of the placed timing lines and notes. However, problems arise when the user does an undo action; this does not undo any of the code, leading to a faulty tabular entry.

Describe the solution you'd like

The syntax I'm thinking is similar to how web APIs are constructed:

state.on("undo", undoFunction)

function undoFunction()
-- execute code here
end

This syntax is very versatile and allows new features to be implemented very easily. Some ideas I have for emitted events and potential syntaxes would be the following:

state.on("load", loadFunction)
state.on("exit", exitFunction)
state.on("redo", redoFunction)
state.on("placeHitObject", function (hitObject) doSomething() end) 
state.on("removeHitObject"), function (hitObject) doSomething() end)

Describe alternatives you've considered

Of course, it's possible to create keybind wrappers using imgui.IsKeyPressed() and imgui.IsKeyDown(), like the following example to detect undos:

if (imgui.IsKeyDown(LeftControl) and imgui.IsKeyPressed(keys.Z)) then
  -- execute undo action
end

However, there are a couple issues with this:

  • Other actions are possibly rebindable, and plugins are not exposed to the user's keybind settings
  • IsKeyPressed() is quite unreliable with many plugin tabs open, as conflicts between the editor and the plugin happen very often. SV makers can testify that sometimes, it takes 2-3 key presses to execute an action that should only take one.
  • Keybind wrappers are very limited, as there is no existing support for detecting clicks on a non-ImGui element.

Alternatively, we could detect current state and previously saved state and detect changes. In my AFFINE example, lineOffsets are actually saved. Technically, something like this could be possible:

function draw()
    imgui.Begin("Window")

    local previousLineCount = state.GetValue("lineCount") or 0
    local lineCount = #map.TimingPoints

    local desiredLineDifference = -5 -- Assume that the previous action of placing lines placed 5 of them.  Thus, we are looking for when 5 lines are deleted.

    if (lineCount - previousLineCount == desiredLineDifference) then 
         -- Since the number of lines that were deleted was equal to however 
         -- many lines was placed, assume an undo occurred
    end

    state.SetValue("lineCount", lineCount)

    imgui.End()
end

Hopefully it's easy to see why this isn't viable at a large scale.

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant