@@ -88,6 +88,37 @@ local loopPattern = reaper.GetExtState("BruteSeq", "LoopPattern") == "1"
8888local loopSong = reaper .GetExtState (" BruteSeq" , " LoopSong" ) == " 1"
8989local ripple = reaper .GetExtState (" BruteSeq" , " Ripple" ) == " 1"
9090
91+ local currentPatternIndex = 1
92+
93+ local function updateCurrentPatternIndex ()
94+ sequencerTrack = getSequencerTrack ()
95+ local patternCount = reaper .CountTrackMediaItems (sequencerTrack )
96+ local itemIndexAtCursor = getItemIndexAtCursor (sequencerTrack )
97+ if followCursor and itemIndexAtCursor and itemIndexAtCursor ~= currentPatternIndex - 1 then
98+ currentPatternIndex = itemIndexAtCursor + 1
99+ else
100+ currentPatternIndex = math.min (currentPatternIndex , patternCount );
101+ end
102+ end
103+
104+ local function updateCursor (item , stepIndex )
105+ if followCursor then
106+ jumpToStep (item , stepIndex )
107+ end
108+ end
109+
110+ local function updateTimeSelection ()
111+ local sequencerTrack = getSequencerTrack ()
112+ if loopPattern then
113+ local currentPattern = getPattern (sequencerTrack , currentPatternIndex - 1 )
114+ if currentPattern then
115+ setTimeSelectionFromItem (currentPattern .item )
116+ end
117+ elseif loopSong then
118+ setTimeSelectionFromTrack (sequencerTrack )
119+ end
120+ end
121+
91122local function processPattern (currentPattern )
92123 -- Navigation Step Bar
93124 local currentStepTotal = getCurrentStep (currentPattern .item )
@@ -172,9 +203,10 @@ local function loop()
172203 local sequencerTrack = getSequencerTrack ()
173204 local patternCount = reaper .CountTrackMediaItems (sequencerTrack )
174205
175- currentPatternIndex = math.min (currentPatternIndex or 1 , patternCount );
176- local currentPattern = getPattern (sequencerTrack , currentPatternIndex - 1 )
206+ updateCurrentPatternIndex ()
177207
208+ local currentPattern = getPattern (sequencerTrack , currentPatternIndex - 1 )
209+ local command = nil
178210 if currentPattern then
179211 -- top bar
180212 reaper .ImGui_PushStyleVar (ctx , reaper .ImGui_StyleVar_FramePadding (), 4 , 4 )
@@ -189,10 +221,17 @@ local function loop()
189221 local y = reaper .ImGui_GetCursorPosY (ctx )
190222 reaper .ImGui_SetCursorPosY (ctx , y + 1 )
191223 reaper .ImGui_PushStyleVar (ctx , reaper .ImGui_StyleVar_FramePadding (), 4 , 2 )
192- addedPattern = reaper .ImGui_Button (ctx , " + " )
224+ removedPattern = reaper .ImGui_Button (ctx , " - " )
193225 reaper .ImGui_PopStyleVar (ctx )
226+ reaper .ImGui_SameLine (ctx )
194227
228+ local y = reaper .ImGui_GetCursorPosY (ctx )
229+ reaper .ImGui_SetCursorPosY (ctx , y + 1 )
230+ reaper .ImGui_PushStyleVar (ctx , reaper .ImGui_StyleVar_FramePadding (), 4 , 2 )
231+ addedPattern = reaper .ImGui_Button (ctx , " +" )
232+ reaper .ImGui_PopStyleVar (ctx )
195233 reaper .ImGui_SameLine (ctx )
234+
196235 reaper .ImGui_Text (ctx , ' Steps:' )
197236 reaper .ImGui_SameLine (ctx )
198237 changedSteps , currentPattern .steps = drawSlider (ctx , ' ##Length' , currentPattern .steps , 1 , 64 , 140 , 4 , 4 )
@@ -212,41 +251,46 @@ local function loop()
212251 changedRippleOption , ripple = reaper .ImGui_Checkbox (ctx , " Ripple" , ripple )
213252
214253 reaper .ImGui_PopStyleVar (ctx , 1 )
254+ reaper .ImGui_Separator (ctx )
215255
216- -- ensure loop consistency
217- loopSong = loopSong and not changedLoopPatternOption
218- loopPattern = loopPattern and not changedLoopSongOption
256+ if isMidi (currentPattern .item ) then
257+ processPattern (currentPattern )
258+ else
259+ reaper .ImGui_AlignTextToFramePadding (ctx )
260+ reaper .ImGui_Text (ctx , " Non MIDI items are not supported in the sequencer track" )
261+ end
219262
263+ -- Update logic
220264
221- if changedFollowOption then
265+ -- update options in reaper extended state
266+ if changedFollowOption or changedLoopPatternOption or changedLoopSongOption or changedRippleOption then
267+ -- ensure loop consistency
268+ loopSong = loopSong and not changedLoopPatternOption
269+ loopPattern = loopPattern and not changedLoopSongOption
222270 reaper .SetExtState (" BruteSeq" , " FollowCursor" , followCursor and " 1" or " 0" , true )
223- end
224-
225- if changedLoopPatternOption or changedLoopSongOption then
226271 reaper .SetExtState (" BruteSeq" , " LoopPattern" , loopPattern and " 1" or " 0" , true )
227272 reaper .SetExtState (" BruteSeq" , " LoopSong" , loopSong and " 1" or " 0" , true )
228- end
229-
230- if changedRippleOption then
231273 reaper .SetExtState (" BruteSeq" , " Ripple" , ripple and " 1" or " 0" , true )
232- end
233-
234- if addedPattern then
235- createPattern (sequencerTrack , currentPattern .steps > 0 and currentPattern .steps or 16 )
236- changedPattern = true
237- if followCursor then
238- currentPatternIndex = patternCount + 1
274+ if changedLoopPatternOption or changedLoopSongOption then
275+ updateTimeSelection ()
239276 end
240- end
241-
242- -- Jump to step
243- if changedPattern and followCursor then
277+ -- Delete pattern
278+ elseif removedPattern then
279+ removeItem ( currentPattern . item )
280+ updateCurrentPatternIndex ()
244281 currentPattern = getPattern (sequencerTrack , currentPatternIndex - 1 )
245- jumpToStep (currentPattern .item , 0 )
246- end
247-
282+ if currentPattern then
283+ updateCursor (currentPattern .item , 0 )
284+ end
285+ updateTimeSelection ()
286+ -- Add pattern
287+ elseif addedPattern then
288+ newItem = createItem (sequencerTrack , currentPattern .steps > 0 and currentPattern .steps or 16 )
289+ currentPatternIndex = patternCount + 1
290+ updateCursor (newItem , 0 )
291+ updateTimeSelection ()
248292 -- Resize pattern
249- if changedSteps or changedTimes then
293+ elseif changedSteps or changedTimes then
250294 reaper .Undo_BeginBlock ()
251295 local originalLength = reaper .GetMediaItemInfo_Value (currentPattern .item , ' D_LENGTH' )
252296 if changedSteps then
@@ -256,41 +300,20 @@ local function loop()
256300 if ripple then
257301 rippleFollowingItems (sequencerTrack , currentPattern .item , originalLength )
258302 end
303+ updateTimeSelection ()
259304 reaper .Undo_EndBlock (' Resize pattern' ,- 1 )
260- end
261-
262- -- Change time selection
263- if changedPattern or changedLoopPatternOption or changedLoopSongOption or changedSteps or changedTimes then
305+ elseif changedPattern then
264306 currentPattern = getPattern (sequencerTrack , currentPatternIndex - 1 )
265- if loopPattern then
266- setTimeSelectionFromItem (currentPattern .item )
267- elseif loopSong then
268- setTimeSelectionFromTrack (sequencerTrack )
269- end
270- end
271-
272- -- Follow cursor
273- local itemIndexAtCursor = getItemIndexAtCursor (sequencerTrack )
274- if followCursor and itemIndexAtCursor and itemIndexAtCursor ~= currentPatternIndex - 1 then
275- currentPatternIndex = itemIndexAtCursor + 1
276- currentPattern = getPattern (sequencerTrack , currentPatternIndex - 1 )
277- end
278-
279- reaper .ImGui_Separator (ctx )
280-
281- if isMidi (currentPattern .item ) then
282- processPattern (currentPattern )
283- else
284- reaper .ImGui_AlignTextToFramePadding (ctx )
285- reaper .ImGui_Text (ctx , " Non MIDI items are not supported in the sequencer track" )
307+ updateCursor (currentPattern .item , 0 )
308+ updateTimeSelection ()
286309 end
287- else
310+ else -- if currentPattern
288311 reaper .ImGui_SameLine (ctx )
289312 reaper .ImGui_AlignTextToFramePadding (ctx )
290313 reaper .ImGui_Text (ctx , " Add a pattern to start" )
291314 reaper .ImGui_SameLine (ctx )
292315 if reaper .ImGui_Button (ctx , " Add Pattern" ) then
293- createPattern (sequencerTrack , 16 )
316+ createItem (sequencerTrack , 16 )
294317 currentPatternIndex = 1
295318 end
296319 end
0 commit comments