1+ local pattern_time = require ' pattern_time'
12local constants = include (' animator/lib/constants' )
23local helpers = include (' animator/lib/helpers' )
34local GRID_LENGTH = constants .GRID_LENGTH
@@ -6,8 +7,9 @@ local CANVAS_HEIGHT = constants.CANVAS_HEIGHT
67local NAV_ROW = constants .GRID_NAV_ROW
78local GRID_LEVELS = constants .GRID_LEVELS
89local SNAPSHOT_NUM = 8
10+ local PATTERN_NUM = 4
911local g = grid .connect ()
10- local CLEAR_POSITION = SNAPSHOT_NUM + 1
12+ local CLEAR_POSITION = 15
1113local TOGGLE_VIEW_POSITION = constants .CANVAS_LENGTH
1214local DIV_START = GRID_LENGTH - 8
1315local INTERSECT_START = 2
@@ -23,12 +25,21 @@ function GRID.new(animator)
2325 animator = animator ,
2426 view = 1 ,
2527 selected = 1 ,
28+ patterns = {},
2629 }
2730 setmetatable (g , GRID )
2831 setmetatable (g , {__index = GRID })
2932
3033 g .viewKeyHandlers = {g .mainKeyHandler , g .optionsKeyHandler }
3134 g .viewRedraws = {g .redrawMain , g .redrawOptions }
35+
36+ for i = 1 ,PATTERN_NUM do
37+ g .patterns [i ] = pattern_time .new ()
38+ g .patterns [i ].process = function (e )
39+ g :handleBottomRowSelect (e .x , e .y )
40+ end
41+ end
42+
3243 return g
3344end
3445
4051function GRID :redrawMain ()
4152 g :all (0 )
4253 drawSteps (self .animator .stepLevels )
43- redrawBottomRow (self . snapshot )
54+ self : redrawBottomRow ()
4455 g :refresh ()
4556end
4657
@@ -75,17 +86,7 @@ function GRID:optionsKeyHandler(x, y, z)
7586 end
7687end
7788
78- function GRID :mainKeyDown (x , y , held )
79- if y == NAV_ROW and x == 13 then
80- return self .animator .pattern :rec_start ();
81- end
82- if y == NAV_ROW and x == 14 then
83- return self .animator .pattern :rec_stop ();
84- end
85- if y == NAV_ROW and x == 15 then
86- return self .animator .pattern :start ();
87- end
88-
89+ function GRID :mainKeyDown (x , y )
8990 if y <= CANVAS_HEIGHT then
9091 self :handleSequence (x , y )
9192 elseif y == NAV_ROW then
@@ -111,12 +112,48 @@ end
111112
112113function GRID :handleBottomRowSelect (x , y )
113114 local animator = self .animator
115+ local isClearHeld = self .held and self .held .y == NAV_ROW and self .held .x == CLEAR_POSITION
116+
117+ local function stopOtherPattern (current , patterns )
118+ for i = 1 ,PATTERN_NUM do
119+ local otherPat = patterns [i ]
120+ if i ~= current and (otherPat .rec == 1 or otherPat .play == 1 ) then
121+ otherPat :rec_stop ()
122+ otherPat :stop ()
123+ end
124+ end
125+ end
126+
114127 if x >= 1 and x <= SNAPSHOT_NUM then
115128 local e = {x = x , y = y }
116- local isClearHeld = self .held and self .held .y == NAV_ROW and self .held .x == CLEAR_POSITION
117129 animator .handleSelectSnapshot (x , isClearHeld )
118130 self .snapshot = x
119- self .animator .pattern :watch (e )
131+
132+ for i = 1 ,PATTERN_NUM do
133+ self .patterns [i ]:watch (e )
134+ end
135+
136+ animator .redraw ()
137+ elseif x >= 10 and x <= 13 then
138+ local i = x - 9
139+ local pattern = self .patterns [i ]
140+ if isClearHeld then
141+ pattern :rec_stop ()
142+ pattern :stop ()
143+ pattern :clear ();
144+ elseif pattern .rec == 1 then
145+ pattern :rec_stop ()
146+ pattern :stop ()
147+ pattern :start ();
148+ elseif pattern .count == 0 then
149+ stopOtherPattern (i , self .patterns )
150+ pattern :rec_start ()
151+ elseif pattern .play == 1 then
152+ pattern :stop ()
153+ else
154+ stopOtherPattern (i , self .patterns )
155+ pattern :start ()
156+ end
120157 animator .redraw ()
121158 elseif x == CLEAR_POSITION then
122159 self :setHeld (x , y )
@@ -213,6 +250,21 @@ function GRID:drawOptions(selected)
213250 end
214251end
215252
253+ function GRID :redrawBottomRow ()
254+ for i = 1 ,SNAPSHOT_NUM do
255+ if self .snapshot == i then
256+ g :led (i , NAV_ROW , GRID_LEVELS .HIGH )
257+ elseif self .animator .snapshots [i ] ~= nil then
258+ g :led (i , NAV_ROW , GRID_LEVELS .LOW_MED )
259+ else
260+ g :led (i , NAV_ROW , GRID_LEVELS .DIM )
261+ end
262+ end
263+ drawPatterns (self .patterns );
264+ g :led (CLEAR_POSITION , NAV_ROW , GRID_LEVELS .DIM )
265+ drawToggleViewPad ()
266+ end
267+
216268function drawSteps (levels )
217269 local findXY = helpers .findXY
218270 for pos ,level in pairs (levels ) do
@@ -221,13 +273,14 @@ function drawSteps(levels)
221273 end
222274end
223275
224- function redrawBottomRow (snapshot )
225- for i = 1 ,SNAPSHOT_NUM do
226- g :led (i , NAV_ROW , snapshot == i and GRID_LEVELS .HIGH or GRID_LEVELS .LOW_MED )
276+ function drawPatterns (patterns )
277+ for i = 1 ,PATTERN_NUM do
278+ local x = i + 9
279+ if patterns [i ].rec == 1 then g :led (x , NAV_ROW , GRID_LEVELS .HIGH )
280+ elseif patterns [i ].play == 1 then g :led (x , NAV_ROW , GRID_LEVELS .MED )
281+ elseif patterns [i ].count > 0 then g :led (x , NAV_ROW , GRID_LEVELS .LOW_MED )
282+ else g :led (x , NAV_ROW , GRID_LEVELS .DIM ) end
227283 end
228-
229- g :led (CLEAR_POSITION , NAV_ROW , GRID_LEVELS .DIM )
230- drawToggleViewPad ()
231284end
232285
233286function getNewLineSteps (a , b )
0 commit comments