Skip to content

Commit

Permalink
Merge pull request #17 from cachilders/feat-grid-error
Browse files Browse the repository at this point in the history
Grid Ops Fixes
  • Loading branch information
cachilders authored Feb 14, 2024
2 parents c331cf7 + f1a2152 commit d6a7846
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 39 deletions.
1 change: 1 addition & 0 deletions lib/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local actions = {
edit_step = 'edit_step',
disply_note = 'display_note',
play_note = 'play_note',
set_error_message = 'set_error_message',
set_observer_position = 'set_observer_position',
set_source_positions = 'set_source_positions',
toggle_sequence = 'toggle_sequence',
Expand Down
8 changes: 3 additions & 5 deletions lib/arrangement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ function Arrangement:press(k, z)
end
end

function Arrangement:turn(n, delta)
self:_ring_input_to_sequence(n, delta)
end

function Arrangement:twist(e, delta)
if e == 1 and not shift_depressed then
local mode = get_current_mode()
Expand Down Expand Up @@ -168,7 +164,9 @@ function Arrangement:_init_observers()
end

function Arrangement:_init_rings()
local rings = Rings:new()
local rings = Rings:new({
delta = function(n, delta) self:_ring_input_to_sequence(n, delta) end
})
for i = 1, self.sequences:size() do
local sequence = self.sequences:get_sequence(i)
rings:add(Ring:new({
Expand Down
2 changes: 2 additions & 0 deletions lib/arrangement/rings.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Rings = {
context = nil,
delta = nil,
host = nil,
rings = {}
}
Expand All @@ -13,6 +14,7 @@ end

function Rings:init(n)
self.host = arc.connect(n)
self.host.delta = self.delta
for _, ring in pairs(self.rings) do
ring:set('host', self.host)
end
Expand Down
43 changes: 24 additions & 19 deletions lib/chart.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ local Chart = {
host = nil,
lumen = 5,
page = 1,
pages = {},
plans = {}
pages = nil,
plans = nil
}

function Chart:new(options)
Expand All @@ -43,7 +43,7 @@ function Chart:hydrate(chart)
end

function Chart:init(n)
self.host = grid.connect(n)
self:set_grid(n)
self:_init_plans()
self:_init_pages()
end
Expand All @@ -56,6 +56,14 @@ function Chart:set(k, v)
self[k] = v
end

function Chart:set_grid(n)
self.host = grid.connect(n)
self.host.key = function(x, y, z) chart:press(x, y, z) end
if self.plans then
self:_init_pages()
end
end

function Chart:press(x, y, z)
self.pages[self.page]:press_to_page(x, y, z)
end
Expand All @@ -73,7 +81,6 @@ function Chart:step()
self:_step_count()
end


function Chart:affect(action, index, values)
if action == actions.emit_pulse then
local sequence = index
Expand All @@ -89,11 +96,21 @@ function Chart:affect(action, index, values)
end

function Chart:_init_pages()
local chart_height = self.host.rows
local chart_width = self.host.cols
self.page = 1
local rows = self.host.rows
local cols = self.host.cols
local chart_height = rows ~= 0 and rows or PANE_EDGE_LENGTH
local chart_width = cols ~= 0 and cols or PANE_EDGE_LENGTH
local page_count = 1
local pages = {}
local panes_per_page = 4
local function led(x, y, l)
if self.host.cols == PANE_EDGE_LENGTH then
-- Monobrite for 64s
l = 15
end
self.host:led(x, y, l)
end

if chart_height == PANE_EDGE_LENGTH then
if chart_width == PANE_EDGE_LENGTH then
Expand All @@ -118,7 +135,7 @@ function Chart:_init_pages()
offset = i - 1
end
local pane = Pane:new({pane = j, plan = self.plans[j + offset]})
pane:init(panes_per_page)
pane:init(panes_per_page, led)
table.insert(panes, pane)
end

Expand All @@ -131,33 +148,21 @@ function Chart:_init_pages()
end

function Chart:_init_plans()
local function led(x, y, l)
if self.host.cols == PANE_EDGE_LENGTH then
-- Monobrite for 64s
l = 15
end
self.host:led(x, y, l)
end

local plans = {
RadiationPlan:new({
led = led,
name = RADIATION_PLAN,
affect_arrangement = self.affect_arrangement,
affect_ensemble = self.affect_ensemble
}),
PathPlan:new({
led = led,
name = PATH_PLAN,
affect_ensemble = self.affect_ensemble
}),
CatPlan:new({
led = led,
name = CAT_PLAN,
affect_ensemble = self.affect_ensemble
}),
ReliefPlan:new({
led = led,
name = RELIEF_PLAN
})
}
Expand Down
8 changes: 6 additions & 2 deletions lib/chart/pane.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ function Pane:new(options)
return instance
end

function Pane:init(panes_per_page)
self.plan:init()
function Pane:init(panes_per_page, led)
if not self.plan:get('features') then
self.plan:init()
end
self.plan:set('led', led)
self:update_offsets(panes_per_page)
end

Expand All @@ -37,6 +40,7 @@ function Pane:update_offsets(panes_per_page)
local x_offset, y_offset = self:_determine_offsets(panes_per_page)
self.plan:set('x_offset', x_offset)
self.plan:set('y_offset', y_offset)
self.plan:update_symbol_offsets()
end

function Pane:get_offsets()
Expand Down
12 changes: 12 additions & 0 deletions lib/chart/plan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ function Plan:reset()
self:init()
end

function Plan:update_symbol_offsets()
for c = 1, PANE_EDGE_LENGTH do
for r = 1, PANE_EDGE_LENGTH do
local feature = self.features[c][r]
if feature then
feature:set('x_offset', self.x_offset)
feature:set('y_offset', self.y_offset)
end
end
end
end

function Plan:_add(x, y)
local symbol = {
led = self.led,
Expand Down
8 changes: 7 additions & 1 deletion lib/console.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local actions = include('lib/actions')
local ErrorScreen = include('lib/console/error_screen')
local InfoScreen = include('lib/console/info_screen')
local SequenceScreen = include('lib/console/sequence_screen')
local StepScreen = include('lib/console/step_screen')
Expand Down Expand Up @@ -56,7 +57,9 @@ function Console:refresh()
if self.dirty then
local mode = MODES[current_mode()]
screen.clear()
if mode == DEFAULT then
if mode == ERROR then
self.screens[ERROR]:draw()
elseif mode == DEFAULT then
local default_console_mode = DEFAULT_CONSOLE_MODES[self.default_mode]
if parameters.animations_enabled() and default_console_mode == ANIMATION then
local filepath = SPRITE_PATH..ANIMATION_SCENES[self.animation_scene]..'/'..self.animation_cell..'.png'
Expand Down Expand Up @@ -94,6 +97,8 @@ function Console:affect(action, index, values)
self.screens[SEQUENCE]:update(index, values)
elseif action == actions.edit_step then
self.screens[STEP]:update(index, values)
elseif action == actions.set_error_message then
self.screens[ERROR]:update(index)
end

self.dirty = true
Expand All @@ -116,6 +121,7 @@ end

function Console:_init_screens()
self.screens = {
[ERROR] = ErrorScreen:new(),
[INFO] = InfoScreen:new(),
[SEQUENCE] = SequenceScreen:new(),
[STEP] = StepScreen:new()
Expand Down
33 changes: 33 additions & 0 deletions lib/console/error_screen.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local console_constants = include('lib/console/constants')
local Screen = include('lib/console/screen')

local ERRORS = {
'Please connect a grid'
}
local X_RULE = console_constants.SCREEN_WIDTH / 2
local Y_RULE = console_constants.SCREEN_HEIGHT / 2

local ErrorScreen = {
error_index = 0
}

function ErrorScreen:new(options)
local instance = options or {}
setmetatable(self, {__index = Screen})
setmetatable(instance, self)
self.__index = self
return instance
end

function ErrorScreen:draw()
screen.font_face(console_constants.FONTS.BASIC.FACE)
screen.font_size(console_constants.FONTS.BASIC.SIZE)
screen.move(X_RULE, Y_RULE)
screen.text_center(ERRORS[self.error_index])
end

function ErrorScreen:update(i)
self.error_index = i
end

return ErrorScreen
28 changes: 16 additions & 12 deletions marcovaldo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
-- a spatial sequencer with cats

DEFAULT = 'default'
ERROR = 'error'
MODE_TIMEOUT_DELAY = 10
PLAN_COUNT = 4
PANE_EDGE_LENGTH = 8
SEQUENCE = 'sequence'
STEP = 'step'

MODES = {DEFAULT, SEQUENCE, STEP}
MODES = {DEFAULT, ERROR, SEQUENCE, STEP}

shift_depressed = false
current_mode = nil
Expand Down Expand Up @@ -93,8 +94,8 @@ end

function init_metaphors()
arrangement:init()
chart:init()
console:init()
chart:init()
ensemble:init()
end

Expand Down Expand Up @@ -122,6 +123,8 @@ function key(k, z)
if k == 2 and z == 0 and not shift_depressed then
if mode == SEQUENCE then
set_current_mode(DEFAULT)
elseif mode == ERROR then
set_current_mode(DEFAULT)
else
arrangement:press(k, z)
end
Expand All @@ -133,14 +136,6 @@ function key(k, z)
end
end

function arc.delta(n, delta)
arrangement:turn(n, delta)
end

function grid.key(x, y, z)
chart:press(x, y, z)
end

function default_mode_timeout_cancel()
if default_mode_timeout then
clock.cancel(default_mode_timeout)
Expand Down Expand Up @@ -172,9 +167,10 @@ function get_mode_index(mode)
end

function set_current_mode(mode)
if mode ~= DEFAULT and get_current_mode() == DEFAULT then
local edit_mode = mode ~= DEFAULT and mode ~= ERROR
if edit_mode and get_current_mode() == DEFAULT then
default_mode_timeout_new()
elseif mode ~= DEFAULT then
elseif edit_mode then
default_mode_timeout_extend()
elseif mode == DEFAULT then
clock.cancel(default_mode_timeout)
Expand Down Expand Up @@ -224,3 +220,11 @@ end
function refresh()
console:refresh()
end

function grid.add(added)
chart:set_grid(added.port)
end

function grid.remove(removed)
chart:set_grid()
end

0 comments on commit d6a7846

Please sign in to comment.