Skip to content

Commit

Permalink
fix: make visual track mode behave better
Browse files Browse the repository at this point in the history
  • Loading branch information
gwatcha committed May 27, 2020
1 parent 7f44416 commit f28213f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 22 deletions.
6 changes: 3 additions & 3 deletions definitions/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ return {
PrevTab = 40862,
PrevTake = 40126,
PrevTrack = 40286,
UnselectAllButCurrentTrack = util.unselectAllButCurrentTrack,
PrevTransientInItem = {"SaveItemSelection", "SelectItemsUnderEditCursor", "MoveEditCursorToPrevTransientInSelectedItems", "RestoreItemSelection"},
ProjectEnd = util.moveToProjectEnd,
ProjectStart = util.moveToProjectStart,
ProjectTimeline = util.selectInnerProjectTimeline,
Quantize = {40009, midiCommand=true},
RecordConditional = {"TransportStop", "ToggleRecordConditional"},
RecordMacro = "RecordMacro",
RecordNormal = {"TransportStop", "ToggleRecordNormal"},
RecordOrStop = "_SWS_RECTOGGLE",
Record = {"SaveEditCursorPosition", "TimeSelectionStart", "RecordConditional", "RestoreEditCursorPosition", setTimeSelection=true},
Redo = 40030,
Expand All @@ -282,7 +282,7 @@ return {
ResetAllControlSurfaceDevices = 42348,
ResetAllMidiDevices = 41175,
ResetControlDevices = {"ResetAllMidiDevices", "ResetAllControlSurfaceDevices"},
Reset = {"Stop", "SetModeNormal", "UnselectAll"},
Reset = {"Stop", "SetModeNormal", "SetRecordModeNormal", "ResetSelection"},
ResetTrackToNormal = {"UnarmSelectedTracks", "UnarmAllEnvelopes", "SetAutomationModeTrimRead"},
RestoreEditCursorPosition = "_BR_RESTORE_CURSOR_POS_SLOT_16",
RestoreItemSelection = "_SWS_RESTALLSELITEMS1",
Expand Down Expand Up @@ -451,7 +451,7 @@ return {
UnfreezeTrack = 41644,
UnmuteAllTracks = 40339,
UnselectAllEvents = {40214, midiCommand=true},
UnselectAll = {"UnselectTracks", "UnselectItems", "UnselectEnvelopePoints", "UnselectAllEvents"},
ResetSelection = {"UnselectAllButCurrentTrack", "UnselectItems", "UnselectEnvelopePoints", "UnselectAllEvents"},
UnselectEnvelopePoints = 40331,
UnselectItems = 40289,
UnselectTracks = 40297,
Expand Down
51 changes: 43 additions & 8 deletions internal/command/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,62 @@ function runner.extendTimelineSelection(movement, args)
end
end

function runner.addToTrackSelection(selection_action, args)
function runner.extendTrackSelection(movement, args)
movement(table.unpack(args))
local end_pos = runner.getTrackPosition()
local pivot_i = state_functions.getVisualTrackPivotIndex()
log.user("end: " .. end_pos .. " pivot_i: " .. pivot_i)

runner.runAction("UnselectTracks")

local i = end_pos
while pivot_i ~= i do
local track = reaper.GetTrack(0, i)
reaper.SetTrackSelected(track, true)

if pivot_i > i then
i = i + 1
else
i = i - 1
end
end

local pivot_track = reaper.GetTrack(0, pivot_i)
reaper.SetTrackSelected(pivot_track, true)
end

-- reaper provides no function to get the current 'track cursor' position but it
-- is implicitly contained in which track is selected when we do and up down
-- motion
function runner.getTrackPosition()
local selected_tracks = {}
for i=0,reaper.CountSelectedTracks()-1 do
local track = reaper.GetSelectedTrack(0, i)
selected_tracks[i] = track
end

selection_action(table.unpack(args))
runner.runAction("UnselectTracks")
runner.runAction("NextTrack")
runner.runAction("PrevTrack")
local track_at_index = reaper.GetSelectedTrack(0, 0)
local index = reaper.GetMediaTrackInfo_Value(track_at_index, "IP_TRACKNUMBER") - 1

for _,previously_selected_track in pairs(selected_tracks) do
reaper.SetTrackSelected(previously_selected_track, true)
runner.runAction("UnselectTracks")
for _,track in ipairs(selected_tracks) do
reaper.SetTrackSelected(track, true)
end

return index
end

function runner.makeSelectionFromTrackMotion(track_motion, repetitions)
local initial_track = reaper.GetSelectedTrack(0, 0)
local first_index = reaper.GetMediaTrackInfo_Value(initial_track, "IP_TRACKNUMBER") - 1

local first_index = runner.getTrackPosition()
runner.runActionNTimes(track_motion, repetitions)

local end_track = reaper.GetSelectedTrack(0, 0)
if not end_track then
return
end

local second_index = reaper.GetMediaTrackInfo_Value(end_track, "IP_TRACKNUMBER") - 1

if first_index > second_index then
Expand Down
13 changes: 2 additions & 11 deletions internal/command/sequence_functions/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,20 @@ return {
runner.runAction(track_selector)
end
},
{
{ 'timeline_operator' },
function(timeline_operator)
runner.runAction(timeline_operator)
state_functions.setModeToNormal()
local first_track = reaper.GetSelectedTrack(0, 0)
reaper.SetOnlyTrackSelected(first_track)
end
},
{
{ 'track_motion' },
function(track_motion)
local args = {track_motion, 1}
local sel_function = runner.makeSelectionFromTrackMotion
runner.addToTrackSelection(sel_function, args)
runner.extendTrackSelection(sel_function, args)
end
},
{
{ 'number', 'track_motion' },
function(number, track_motion)
local args = {track_motion, number}
local sel_function = runner.makeSelectionFromTrackMotion
runner.addToTrackSelection(sel_function, args)
runner.extendTrackSelection(sel_function, args)
end
},
}
Expand Down
13 changes: 13 additions & 0 deletions internal/library/library.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ function library.setModeNormal()
end

function library.setModeVisualTrack()
local NextTrack = 40285
local PrevTrack = 40286

local first_track = reaper.GetSelectedTrack(0, 0)
if not first_track then
-- get the implicitly selected track
reaper.Main_OnCommand(NextTrack, 0)
reaper.Main_OnCommand(PrevTrack, 0)
end
first_track = reaper.GetSelectedTrack(0, 0)
reaper.SetOnlyTrackSelected(first_track)

local visual_track_pivot_i = reaper.GetMediaTrackInfo_Value(first_track, "IP_TRACKNUMBER") - 1

state_functions.setMode('visual_track')
state_functions.setVisualTrackPivotIndex(visual_track_pivot_i)
end

function library.setModeVisualTimeline()
Expand Down
1 change: 1 addition & 0 deletions internal/state_machine/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local constants = {
timeline_selection_side = "left",
last_searched_track_name = "^$",
last_track_name_search_direction_was_forward = true,
visual_track_pivot_i = 0,
last_command = {
context = "main",
mode = "normal",
Expand Down
12 changes: 12 additions & 0 deletions internal/state_machine/state_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ function state_functions.checkIfConsistentState(state)
return true
end

function state_functions.setVisualTrackPivotIndex(visual_track_pivot_i)
local state = state_interface.get()
state['visual_track_pivot_i'] = visual_track_pivot_i
state_interface.set(state)
end

function state_functions.getVisualTrackPivotIndex()
local state = state_interface.get()
local visual_track_pivot_i = state['visual_track_pivot_i']
return visual_track_pivot_i
end

function state_functions.setTimelineSelectionSide(left_or_right)
local state = state_interface.get()
state['timeline_selection_side'] = left_or_right
Expand Down
8 changes: 8 additions & 0 deletions internal/utils/reaper_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local format = require('utils.format')

local reaper_util = {}


function mergeItemPositionsLists(item_positions_list)
local merged_list = {}

Expand Down Expand Up @@ -299,4 +300,11 @@ function reaper_util.selectTrackByNumber()
end
end

function reaper_util.unselectAllButCurrentTrack()
local track = reaper.GetSelectedTrack(0, 0)
if track then
reaper.SetOnlyTrackSelected(track)
end
end

return reaper_util

0 comments on commit f28213f

Please sign in to comment.