Skip to content

Commit

Permalink
chore: Allow encoder assignment configs to define an `encoderValueNam…
Browse files Browse the repository at this point in the history
…e` to display in place of the Cubase-builtin name
  • Loading branch information
bjoluc committed Mar 31, 2024
1 parent 8008fd0 commit e93589b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// organize-imports-ignore
import "core-js/actual/array/iterator";
import "core-js/actual/array/from";
import "core-js/actual/array/includes";
import "core-js/actual/array/reverse";
import "core-js/actual/array/flat-map";
import "core-js/actual/string/pad-start";
Expand Down
4 changes: 4 additions & 0 deletions src/mapping/encoders/EncoderMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LedButton } from "/decorators/surface-elements/LedButton";
import { ChannelSurfaceElements, ControlSectionSurfaceElements } from "/device-configs";
import { Device, MainDevice } from "/devices";
import { SegmentDisplayManager } from "/midi/managers/SegmentDisplayManager";
import { ChannelTextManager } from "/midi/managers/lcd/ChannelTextManager";
import { GlobalState } from "/state";
import { ContextVariable } from "/util";

Expand All @@ -29,6 +30,7 @@ export type EncoderMappingConfig = Array<{

export class EncoderMapper {
private readonly channelElements: ChannelSurfaceElements[];
private readonly channelTextManagers: ChannelTextManager[];

/** An array containing the control buttons of each main device */
private readonly deviceButtons: ControlSectionSurfaceElements["buttons"][];
Expand All @@ -47,6 +49,7 @@ export class EncoderMapper {
private readonly globalState: GlobalState,
) {
this.channelElements = devices.flatMap((device) => device.channelElements);
this.channelTextManagers = devices.flatMap((device) => device.lcdManager.channelTextManagers);
this.mainDevices = devices.filter((device) => device instanceof MainDevice) as MainDevice[];
this.deviceButtons = this.mainDevices.map(
(device) => (device as MainDevice).controlSectionElements.buttons,
Expand Down Expand Up @@ -96,6 +99,7 @@ export class EncoderMapper {
this.deviceButtons,
this.channelElements,
this.mixerBankChannels,
this.channelTextManagers,
this.segmentDisplayManager,
this.globalState,
);
Expand Down
22 changes: 18 additions & 4 deletions src/mapping/encoders/EncoderPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import { LedButton } from "/decorators/surface-elements/LedButton";
import { EncoderDisplayMode, LedPushEncoder } from "/decorators/surface-elements/LedPushEncoder";
import { ChannelSurfaceElements, ControlSectionButtons } from "/device-configs";
import { SegmentDisplayManager } from "/midi/managers/SegmentDisplayManager";
import { ChannelTextManager } from "/midi/managers/lcd/ChannelTextManager";
import { GlobalState } from "/state";
import { ContextVariable } from "/util";

export interface EncoderAssignmentConfig {
encoderValue?: MR_HostValue;

/**
* A custom string to display as the encoder's title instead of the one provided by Cubase
*/
encoderValueName?: string;

displayMode: EncoderDisplayMode;
pushToggleValue?: MR_HostValue;

Expand Down Expand Up @@ -67,6 +74,7 @@ export class EncoderPage implements EncoderPageConfig {
private readonly deviceButtons: ControlSectionButtons[],
private readonly channelElements: ChannelSurfaceElements[],
private readonly mixerBankChannels: MR_MixerBankChannel[],
private readonly channelTextManagers: ChannelTextManager[],
private readonly segmentDisplayManager: SegmentDisplayManager,
private readonly globalState: GlobalState,
) {
Expand Down Expand Up @@ -259,10 +267,16 @@ export class EncoderPage implements EncoderPageConfig {
this.setActivatorButtonLeds(context, 1);

for (const [encoderIndex, { encoder }] of this.channelElements.entries()) {
encoder.displayMode.set(
context,
this.assignments[encoderIndex]?.displayMode ?? EncoderDisplayMode.SingleDot,
);
const assignment = this.assignments[encoderIndex] as EncoderAssignmentConfig | undefined;
encoder.displayMode.set(context, assignment?.displayMode ?? EncoderDisplayMode.SingleDot);
if (assignment?.encoderValueName) {
this.channelTextManagers[encoderIndex].setParameterNameOverride(
context,
assignment.encoderValueName,
);
} else {
this.channelTextManagers[encoderIndex].clearParameterNameOverride(context);
}
}

this.globalState.isValueDisplayModeActive.set(context, false);
Expand Down
4 changes: 2 additions & 2 deletions src/mapping/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export function makeHostMapping(
return channel;
});

bindEncoders(page, devices, mixerBankChannels, segmentDisplayManager, globalState);

for (const device of devices) {
if (device instanceof MainDevice) {
const controlSectionElements = device.controlSectionElements;
Expand Down Expand Up @@ -97,6 +95,8 @@ export function makeHostMapping(
}
}

bindEncoders(page, devices, mixerBankChannels, segmentDisplayManager, globalState);

lifecycleCallbacks.addActivationCallback((context) => {
globalState.areMotorsActive.set(context, true);
});
Expand Down
2 changes: 1 addition & 1 deletion src/midi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function bindChannelElements(device: Device, globalState: GlobalState) {
}

// Scribble Strip
const channelTextManager = device.lcdManager.getChannelTextManager(channelIndex);
const channelTextManager = device.lcdManager.channelTextManagers[channelIndex];

channel.encoder.mOnEncoderValueTitleChange.addCallback((context, _title1, title2) => {
channelTextManager.setParameterName(context, title2);
Expand Down
18 changes: 17 additions & 1 deletion src/midi/managers/lcd/ChannelTextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class ChannelTextManager {
private uniqueManagerId = ChannelTextManager.nextManagerId++;

private parameterName = new ContextVariable("");
private parameterNameOverride = new ContextVariable<string | undefined>(undefined);
private parameterValue = new ContextVariable("");
private channelName = new ContextVariable("");
private isLocalValueModeActive = new ContextVariable(false);
Expand Down Expand Up @@ -175,7 +176,7 @@ export class ChannelTextManager {
this.isLocalValueModeActive.get(context) ||
this.globalState.isValueDisplayModeActive.get(context)
? this.parameterValue.get(context)
: this.parameterName.get(context),
: this.parameterNameOverride.get(context) ?? this.parameterName.get(context),
);
}

Expand Down Expand Up @@ -215,6 +216,21 @@ export class ChannelTextManager {
this.updateNameValueDisplay(context);
}

/**
* Sets a parameter name string that replaces the one set via `setParameterName()` until
* `clearParameterNameOverride()` is invoked.
*/
setParameterNameOverride(context: MR_ActiveDevice, name: string) {
this.parameterNameOverride.set(
context,
ChannelTextManager.centerString(ChannelTextManager.abbreviateString(name)),
);
}

clearParameterNameOverride(context: MR_ActiveDevice) {
this.parameterNameOverride.set(context, undefined);
}

setParameterValue(context: MR_ActiveDevice, value: string) {
value = ChannelTextManager.translateParameterValue(value);

Expand Down
8 changes: 2 additions & 6 deletions src/midi/managers/lcd/LcdManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export class LcdManager {
return Array(length + 1).join(" ");
}

private channels: ChannelTextManager[];
channelTextManagers: ChannelTextManager[];

constructor(
private device: Device,
globalState: GlobalState,
timerUtils: TimerUtils,
) {
this.channels = createElements(
this.channelTextManagers = createElements(
8,
(channelIndex) =>
new ChannelTextManager(
Expand Down Expand Up @@ -51,10 +51,6 @@ export class LcdManager {
this.sendText(context, row * 56 + (channelIndex % 8) * 7, text);
}

getChannelTextManager(channelIndex: number) {
return this.channels[channelIndex];
}

clearDisplays(context: MR_ActiveDevice) {
this.sendText(context, 0, LcdManager.makeSpaces(112));
}
Expand Down

0 comments on commit e93589b

Please sign in to comment.