Skip to content

Commit

Permalink
Refactor window plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Jul 30, 2023
1 parent 008be68 commit e92a1e1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 56 deletions.
15 changes: 15 additions & 0 deletions src/common/plug_indexes/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ def getName(self) -> str:

def focus(self) -> None:
ui.showWindow(self.index)

# Hopefully this duplicate code can be fixed if I get a better answer to
# https://stackoverflow.com/q/75000973/6335363
MIXER: 'WindowIndex'
CHANNEL_RACK: 'WindowIndex'
PLAYLIST: 'WindowIndex'
PIANO_ROLL: 'WindowIndex'
BROWSER: 'WindowIndex'


WindowIndex.MIXER = WindowIndex(0)
WindowIndex.CHANNEL_RACK = WindowIndex(1)
WindowIndex.PLAYLIST = WindowIndex(2)
WindowIndex.PIANO_ROLL = WindowIndex(3)
WindowIndex.BROWSER = WindowIndex(4)
15 changes: 15 additions & 0 deletions src/common/tracks/mixer_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ class MixerTrack(AbstractTrack):
def __init__(self, index: int) -> None:
self.__index = index

def __eq__(self, __value: object) -> bool:
if isinstance(__value, MixerTrack):
return __value.index == self.__index
return NotImplemented

def __gt__(self, __value: object) -> bool:
if isinstance(__value, MixerTrack):
return self.index > __value.index
return NotImplemented

def __lt__(self, __value: object) -> bool:
if isinstance(__value, MixerTrack):
return self.index < __value.index
return NotImplemented

@property
def index(self) -> int:
return self.__index
Expand Down
4 changes: 4 additions & 0 deletions src/common/tracks/playlist_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class PlaylistTrack(AbstractTrack):
def __init__(self, index: int) -> None:
self.__index = index

@property
def index(self) -> int:
return self.__index

@property
def color(self) -> Color:
return Color.fromInteger(playlist.getTrackColor(self.__index))
Expand Down
2 changes: 2 additions & 0 deletions src/common/types/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@ def __eq__(self, other: object) -> bool:
else:
return NotImplemented

# Hopefully this duplicate code can be fixed if I get a better answer to
# https://stackoverflow.com/q/75000973/6335363
WHITE: 'Color'
GRAY: 'Color'
BLACK: 'Color'
Expand Down
5 changes: 3 additions & 2 deletions src/plugs/windows/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
more details.
"""
from common.extension_manager import ExtensionManager
from common.plug_indexes.window import WindowIndex
from devices import DeviceShadow
from plugs import WindowPlugin

INDEX = 4
INDEX = WindowIndex.BROWSER


class Browser(WindowPlugin):
Expand All @@ -27,7 +28,7 @@ def __init__(self, shadow: DeviceShadow) -> None:
super().__init__(shadow, [])

@classmethod
def getWindowId(cls) -> int:
def getWindowId(cls) -> WindowIndex:
return INDEX

@classmethod
Expand Down
67 changes: 36 additions & 31 deletions src/plugs/windows/mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import ui
import mixer
from common import getContext
from common.tracks.mixer_track import MixerTrack
from common.types import Color
from common.extension_manager import ExtensionManager
from common.plug_indexes.fl_index import (UnsafeIndex)
from common.plug_indexes.fl_index import WindowIndex
from common.util.api_fixes import (
getSelectedDockMixerTracks,
getMixerDockSides,
Expand Down Expand Up @@ -43,7 +44,7 @@
from plugs.mapping_strategies import MuteSoloStrategy
from plugs import WindowPlugin

INDEX = 0
INDEX = WindowIndex.MIXER
COLOR_DISABLED = Color.fromGrayscale(0.3, False)
COLOR_ARMED = Color.fromInteger(0xAF0000, 1.0, True)

Expand Down Expand Up @@ -129,29 +130,22 @@ def __init__(self, shadow: DeviceShadow) -> None:
).annotate("Show selected").colorize(Color.fromGrayscale(0.5))

# Create bindings for mute, solo and generic buttons
mutes_solos = MuteSoloStrategy(
lambda i: self._selection[i],
mixer.muteTrack,
mixer.isTrackMuted,
mixer.soloTrack,
mixer.isTrackSolo,
mixer.getTrackColor,
)
mutes_solos = MuteSoloStrategy(lambda i: self._selection[i])

# TODO: Bind master controls

# List of mapped channels
self._selection: list[int] = []
self._selection: list[MixerTrack] = []
# List of mapped channels, respecting the dock side
self._selection_docked: list[int] = []
self._selection_docked: list[MixerTrack] = []
# Dock side that we're mapping to
self._dock_side = 1
# Length of mapped channels
self._len = max(map(len, [self._faders, self._knobs]))
super().__init__(shadow, [mutes_solos])

@classmethod
def getWindowId(cls) -> int:
def getWindowId(cls) -> WindowIndex:
return INDEX

@classmethod
Expand All @@ -163,8 +157,14 @@ def updateSelected(self):
Update the list of selected tracks
"""
dock_side = mixer.getTrackDockSide(mixer.trackNumber())
selected = getSelectedDockMixerTracks()[dock_side]
dock_sides = getMixerDockSides()[dock_side]
selected = list(map(
MixerTrack,
getSelectedDockMixerTracks()[dock_side],
))
dock_sides = list(map(
MixerTrack,
getMixerDockSides()[dock_side],
))

if len(selected) == 0:
# No selection, we need to generate one
Expand Down Expand Up @@ -194,7 +194,7 @@ def updateSelected(self):
or index > self._selection_docked[-1]
):
self._selection = [dock_sides[i] for i in range(first, last)]
self._selection_docked = list(range(first, last))
self._selection_docked = list(map(MixerTrack, range(first, last)))
self._dock_side = dock_side
self.displayRect()

Expand All @@ -204,7 +204,7 @@ def displayRect(self):
"""
first = self._selection_docked[0]
ui.miDisplayDockRect(
first + 1,
first.index + 1,
len(self._selection),
self._dock_side,
2000,
Expand Down Expand Up @@ -254,7 +254,7 @@ def fader(
) -> bool:
"""Faders -> volume"""
index = self._selection[control.getControl().coordinate[1]]
mixer.setTrackVolume(index, snapFaders(
mixer.setTrackVolume(index.index, snapFaders(
control.value, control.getControl()))
return True

Expand Down Expand Up @@ -283,10 +283,12 @@ def updateColors(self):
self._fader_master.annotation = name
# For each selected track
for n, i in enumerate(self._selection):
c = Color.fromInteger(mixer.getTrackColor(i))
name = mixer.getTrackName(i)
vol = mixer.getTrackVolume(i)
pan = mixer.getTrackPan(i)
c = i.color
name = i.name
# FIXME: Refactor to make volume and pan properties of the
# MixerTrack type
vol = mixer.getTrackVolume(i.index)
pan = mixer.getTrackPan(i.index)
# Only apply to controls that are within range
if len(self._faders) > n:
self._faders[n].color = c
Expand All @@ -299,13 +301,15 @@ def updateColors(self):
self._knobs[n].value = unsnapKnobs(pan)
# Select buttons
if len(self._selects) > n:
if mixer.isTrackSelected(i):
# FIXME: Also make this a property
if mixer.isTrackSelected(i.index):
self._selects[n].color = c
else:
self._selects[n].color = COLOR_DISABLED
# Arm buttons
if len(self._arms) > n:
if mixer.isTrackArmed(i):
# FIXME: Also make this a property
if mixer.isTrackArmed(i.index):
self._arms[n].color = COLOR_ARMED
else:
self._arms[n].color = COLOR_DISABLED
Expand All @@ -317,7 +321,8 @@ def knob(
) -> bool:
"""Knobs -> panning"""
index = self._selection[control.getControl().coordinate[1]]
mixer.setTrackPan(index, snapKnobs(control.value))
# FIXME: Use properties
mixer.setTrackPan(index.index, snapKnobs(control.value))
return True

def masterKnob(
Expand All @@ -336,24 +341,24 @@ def masterKnob(
def arm(
self,
control: ControlShadowEvent,
index: UnsafeIndex,
index: WindowIndex,
*args: Any
) -> bool:
"""Arm track"""
index = self._selection[control.getControl().coordinate[1]]
mixer.armTrack(index)
track = self._selection[control.getControl().coordinate[1]]
mixer.armTrack(track.index)
return True

@filterButtonLift()
def select(
self,
control: ControlShadowEvent,
index: UnsafeIndex,
index: WindowIndex,
*args: Any
) -> bool:
"""Select track"""
index = self._selection[control.getControl().coordinate[1]]
mixer.selectTrack(index)
track = self._selection[control.getControl().coordinate[1]]
mixer.selectTrack(track.index)
return True

@filterButtonLift()
Expand Down
7 changes: 3 additions & 4 deletions src/plugs/windows/piano_roll.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import transport
import ui
from common.extension_manager import ExtensionManager
from common.plug_indexes import WindowIndex
from common.types import Color
from devices import DeviceShadow
from plugs import WindowPlugin
from plugs.event_filters import filterButtonLift
from control_surfaces import ToolSelector, ControlShadowEvent

INDEX = 3


TOOL_COLORS = [
Color.fromInteger(0xffc43f), # Pencil
Expand Down Expand Up @@ -52,8 +51,8 @@ def __init__(self, shadow: DeviceShadow) -> None:
super().__init__(shadow, [])

@classmethod
def getWindowId(cls) -> int:
return INDEX
def getWindowId(cls) -> WindowIndex:
return WindowIndex.PIANO_ROLL

@classmethod
def create(cls, shadow: DeviceShadow) -> 'WindowPlugin':
Expand Down
30 changes: 11 additions & 19 deletions src/plugs/windows/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import transport
import general
from common import getContext
from common.tracks import PlaylistTrack
from common.types import Color
from common.extension_manager import ExtensionManager
from common.plug_indexes.fl_index import UnsafeIndex
from common.plug_indexes import WindowIndex, FlIndex
from common.util.api_fixes import getFirstPlaylistSelection
from control_surfaces import consts
from control_surfaces import ControlShadowEvent
Expand All @@ -40,8 +41,6 @@
from plugs.event_filters import filterButtonLift
from plugs.mapping_strategies import MuteSoloStrategy

INDEX = 2

TOOL_COLORS = [
Color.fromInteger(0xffc43f), # Pencil
Color.fromInteger(0x7bcefd), # Paint
Expand All @@ -61,11 +60,11 @@ def getNumDrumCols() -> int:
return getContext().getDevice().getDrumPadSize()[1]


def getSelection(i: int):
def getSelection(i: int) -> PlaylistTrack:
selection = getFirstPlaylistSelection()
if i >= getNumDrumCols():
raise IndexError()
return selection + i
return PlaylistTrack(selection + i)


class Playlist(WindowPlugin):
Expand All @@ -82,14 +81,7 @@ def __init__(self, shadow: DeviceShadow) -> None:
target_num=len(TOOL_COLORS),
)\
.colorize(TOOL_COLORS)
mute_solo = MuteSoloStrategy(
getSelection,
playlist.muteTrack,
playlist.isTrackMuted,
playlist.soloTrack,
playlist.isTrackSolo,
playlist.getTrackColor,
)
mute_solo = MuteSoloStrategy(getSelection)

# Navigation mappings
# FIXME: This is super yucky, come up with a better system for it
Expand Down Expand Up @@ -152,8 +144,8 @@ def __init__(self, shadow: DeviceShadow) -> None:
super().__init__(shadow, [mute_solo])

@classmethod
def getWindowId(cls) -> int:
return INDEX
def getWindowId(cls) -> WindowIndex:
return WindowIndex.PLAYLIST

@classmethod
def create(cls, shadow: DeviceShadow) -> 'WindowPlugin':
Expand All @@ -162,7 +154,7 @@ def create(cls, shadow: DeviceShadow) -> 'WindowPlugin':
def jogWheel(
self,
control: ControlShadowEvent,
index: UnsafeIndex,
index: FlIndex,
*args: Any
) -> bool:
if control.value == consts.JOG_NEXT:
Expand All @@ -184,12 +176,12 @@ def jogWheel(
track = playlist.trackCount() - 1
playlist.deselectAll()
playlist.selectTrack(track)
ui.scrollWindow(INDEX, track)
ui.scrollWindow(WindowIndex.PLAYLIST.index, track)
elif isinstance(control.getControl(), StandardJogWheel):
# Need to account for ticks being zero-indexed and bars being
# 1-indexed
bar = int(transport.getSongPos(3)) + increment - 1
ui.scrollWindow(INDEX, bar, 1)
ui.scrollWindow(WindowIndex.PLAYLIST.index, bar, 1)
# TODO: Make this work with time signature markers
transport.setSongPos(bar * general.getRecPPB(), 2)
return True
Expand All @@ -201,7 +193,7 @@ def eSelectTool(
window,
idx: int,
) -> bool:
# FIXME: This uses keyboard shortcuts which are extremely unreliable
# HACK: This uses keyboard shortcuts which are extremely unreliable
if idx < len(TOOL_COLORS):
# If we're already in a menu, close it
if ui.isInPopupMenu():
Expand Down

0 comments on commit e92a1e1

Please sign in to comment.