Skip to content

Commit 0f77763

Browse files
committed
added function to get the value for a toggle command
1 parent 28499ec commit 0f77763

File tree

3 files changed

+76
-110
lines changed

3 files changed

+76
-110
lines changed

src/actions/common.ts

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,15 @@ export function createCommonActions(self: InstanceBaseExt<WingConfig>): Companio
110110
callback: async (event) => {
111111
const sel = event.options.sel as string
112112
const cmd = ActionUtil.getScribblelightCommand(sel, getNodeNumber(event, 'sel'))
113-
const val = ActionUtil.getNumber(event, 'led')
114-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
115-
if (val < 2) {
116-
send(cmd, val)
117-
} else {
118-
send(cmd, Number(!currentVal))
119-
}
113+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'led'), state)
114+
send(cmd, val)
120115
},
121116
subscribe: (event) => {
122-
const sel = event.options.sel as string
123-
const cmd = ActionUtil.getScribblelightCommand(sel, getNodeNumber(event, 'sel'))
124-
ensureLoaded(cmd)
117+
if (event.options.led ?? 0 > 2) {
118+
const sel = event.options.sel as string
119+
const cmd = ActionUtil.getScribblelightCommand(sel, getNodeNumber(event, 'sel'))
120+
ensureLoaded(cmd)
121+
}
125122
},
126123
},
127124
[CommonActions.SetScribbleLightColor]: {
@@ -261,18 +258,15 @@ export function createCommonActions(self: InstanceBaseExt<WingConfig>): Companio
261258
callback: async (event) => {
262259
const sel = event.options.sel as string
263260
const cmd = ActionUtil.getMuteCommand(sel, getNodeNumber(event, 'sel'))
264-
const val = ActionUtil.getNumber(event, 'mute')
265-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
266-
if (val < 2) {
267-
send(cmd, val)
268-
} else {
269-
send(cmd, Number(!currentVal))
270-
}
261+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'mute'), state)
262+
send(cmd, val)
271263
},
272264
subscribe: (event) => {
273-
const sel = event.options.sel as string
274-
const cmd = ActionUtil.getMuteCommand(sel, getNodeNumber(event, 'sel'))
275-
ensureLoaded(cmd)
265+
if (event.options.sel ?? 0 > 2) {
266+
const sel = event.options.sel as string
267+
const cmd = ActionUtil.getMuteCommand(sel, getNodeNumber(event, 'sel'))
268+
ensureLoaded(cmd)
269+
}
276270
},
277271
},
278272
////////////////////////////////////////////////////////////////
@@ -474,18 +468,15 @@ export function createCommonActions(self: InstanceBaseExt<WingConfig>): Companio
474468
callback: async (event) => {
475469
const sel = event.options.sel as string
476470
const cmd = ActionUtil.getSoloCommand(sel, getNodeNumber(event, 'sel'))
477-
const val = ActionUtil.getNumber(event, 'solo')
478-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
479-
if (val < 2) {
480-
send(cmd, val)
481-
} else {
482-
send(cmd, Number(!currentVal))
483-
}
471+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'solo'), state)
472+
send(cmd, val)
484473
},
485474
subscribe: (event) => {
486-
const sel = event.options.sel as string
487-
const cmd = ActionUtil.getSoloCommand(sel, getNodeNumber(event, 'sel'))
488-
ensureLoaded(cmd)
475+
if (event.options.sel ?? 0 > 2) {
476+
const sel = event.options.sel as string
477+
const cmd = ActionUtil.getSoloCommand(sel, getNodeNumber(event, 'sel'))
478+
ensureLoaded(cmd)
479+
}
489480
},
490481
},
491482
[CommonActions.ClearSolo]: {
@@ -758,23 +749,16 @@ export function createCommonActions(self: InstanceBaseExt<WingConfig>): Companio
758749
callback: async (event) => {
759750
const sel = event.options.src as string
760751
const cmd = ActionUtil.getSendMuteCommand(sel, getNodeNumber(event, 'src'), getNodeNumber(event, 'dest'))
761-
const val = ActionUtil.getNumber(event, 'mute')
762-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
763752
// The Send mutes need to be sent inverted becauxe it is an 'on' command
764-
if (val >= 2) {
765-
send(cmd, Number(!currentVal))
766-
} else {
767-
if (val < 1) {
768-
send(cmd, 1)
769-
} else {
770-
send(cmd, 0)
771-
}
772-
}
753+
const val = Number(!ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'mute'), state))
754+
send(cmd, val)
773755
},
774756
subscribe: (event) => {
775-
const sel = event.options.sel as string
776-
const cmd = ActionUtil.getSendMuteCommand(sel, getNodeNumber(event, 'src'), getNodeNumber(event, 'dest'))
777-
ensureLoaded(cmd)
757+
if (event.options.sel ?? 0 > 2) {
758+
const sel = event.options.sel as string
759+
const cmd = ActionUtil.getSendMuteCommand(sel, getNodeNumber(event, 'src'), getNodeNumber(event, 'dest'))
760+
ensureLoaded(cmd)
761+
}
778762
},
779763
},
780764
////////////////////////////////////////////////////////////////
@@ -897,23 +881,16 @@ export function createCommonActions(self: InstanceBaseExt<WingConfig>): Companio
897881
callback: async (event) => {
898882
const sel = event.options.src as string
899883
const cmd = ActionUtil.getMainSendMuteCommand(sel, getNodeNumber(event, 'src'), getNodeNumber(event, 'dest'))
900-
const val = ActionUtil.getNumber(event, 'mute')
901-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
902-
// The Main Send mutes need to be sent inverted becauxe it is an 'on' command
903-
if (val >= 2) {
904-
send(cmd, Number(!currentVal))
905-
} else {
906-
if (val < 1) {
907-
send(cmd, 1)
908-
} else {
909-
send(cmd, 0)
910-
}
911-
}
884+
// The Send mutes need to be sent inverted becauxe it is an 'on' command
885+
const val = Number(!ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'mute'), state))
886+
send(cmd, val)
912887
},
913888
subscribe: (event) => {
914-
const sel = event.options.sel as string
915-
const cmd = ActionUtil.getMainSendMuteCommand(sel, getNodeNumber(event, 'src'), getNodeNumber(event, 'dest'))
916-
ensureLoaded(cmd)
889+
if (event.options.mute ?? 0 > 2) {
890+
const sel = event.options.sel as string
891+
const cmd = ActionUtil.getMainSendMuteCommand(sel, getNodeNumber(event, 'src'), getNodeNumber(event, 'dest'))
892+
ensureLoaded(cmd)
893+
}
917894
},
918895
},
919896
[CommonActions.SetMainSendFader]: {

src/actions/config.ts

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { InstanceBaseExt } from '../types.js'
99
import { WingConfig } from '../config.js'
1010
import * as ActionUtil from './utils.js'
1111
import { ConfigurationCommands } from '../commands/config.js'
12-
import { StateUtil } from '../state/index.js'
1312
import { getIdLabelPair } from '../choices/utils.js'
1413
import { getTalkbackOptions, getTalkbackModeOptions, getTalkbackIndividualOptions } from '../choices/config.js'
1514

@@ -44,17 +43,14 @@ export function createConfigurationActions(self: InstanceBaseExt<WingConfig>): C
4443
options: [GetMuteDropdown('mute')],
4544
callback: async (event) => {
4645
const cmd = ConfigurationCommands.SoloMute()
47-
const val = ActionUtil.getNumber(event, 'mute')
48-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
49-
if (val >= 2) {
50-
send(cmd, Number(!currentVal))
51-
} else {
52-
send(cmd, val)
53-
}
46+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'mute'), state)
47+
send(cmd, val)
5448
},
55-
subscribe: (_) => {
56-
const cmd = ConfigurationCommands.SoloMute()
57-
ensureLoaded(cmd)
49+
subscribe: (event) => {
50+
if (event.options.mute ?? 0 > 2) {
51+
const cmd = ConfigurationCommands.SoloMute()
52+
ensureLoaded(cmd)
53+
}
5854
},
5955
},
6056
[CommonActions.SetSoloDim]: {
@@ -70,17 +66,14 @@ export function createConfigurationActions(self: InstanceBaseExt<WingConfig>): C
7066
],
7167
callback: async (event) => {
7268
const cmd = ConfigurationCommands.SoloDim()
73-
const val = ActionUtil.getNumber(event, 'dim')
74-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
75-
if (val >= 2) {
76-
send(cmd, Number(!currentVal))
77-
} else {
78-
send(cmd, val)
79-
}
69+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'dim'), state)
70+
send(cmd, val)
8071
},
81-
subscribe: (_) => {
82-
const cmd = ConfigurationCommands.SoloDim()
83-
ensureLoaded(cmd)
72+
subscribe: (event) => {
73+
if (event.options.dim ?? 0 > 2) {
74+
const cmd = ConfigurationCommands.SoloDim()
75+
ensureLoaded(cmd)
76+
}
8477
},
8578
},
8679
[CommonActions.SetSoloMono]: {
@@ -96,17 +89,14 @@ export function createConfigurationActions(self: InstanceBaseExt<WingConfig>): C
9689
],
9790
callback: async (event) => {
9891
const cmd = ConfigurationCommands.SoloMono()
99-
const val = ActionUtil.getNumber(event, 'mono')
100-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
101-
if (val >= 2) {
102-
send(cmd, Number(!currentVal))
103-
} else {
104-
send(cmd, val)
105-
}
92+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'mono'), state)
93+
send(cmd, val)
10694
},
107-
subscribe: (_) => {
108-
const cmd = ConfigurationCommands.SoloMono()
109-
ensureLoaded(cmd)
95+
subscribe: (event) => {
96+
if (event.options.mono ?? 0 > 2) {
97+
const cmd = ConfigurationCommands.SoloMono()
98+
ensureLoaded(cmd)
99+
}
110100
},
111101
},
112102
[CommonActions.SetSoloLRSwap]: {
@@ -122,17 +112,14 @@ export function createConfigurationActions(self: InstanceBaseExt<WingConfig>): C
122112
],
123113
callback: async (event) => {
124114
const cmd = ConfigurationCommands.SoloLRSwap()
125-
const val = ActionUtil.getNumber(event, 'swap')
126-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
127-
if (val >= 2) {
128-
send(cmd, Number(!currentVal))
129-
} else {
130-
send(cmd, val)
131-
}
115+
const val = ActionUtil.getSetOrToggleValue(cmd, ActionUtil.getNumber(event, 'swap'), state)
116+
send(cmd, val)
132117
},
133-
subscribe: (_) => {
134-
const cmd = ConfigurationCommands.SoloLRSwap()
135-
ensureLoaded(cmd)
118+
subscribe: (event) => {
119+
if (event.options.swap ?? 0 > 2) {
120+
const cmd = ConfigurationCommands.SoloLRSwap()
121+
ensureLoaded(cmd)
122+
}
136123
},
137124
},
138125
////////////////////////////////////////////////////////////////
@@ -210,17 +197,11 @@ export function createConfigurationActions(self: InstanceBaseExt<WingConfig>): C
210197
const talkback = event.options.tb as string
211198
const destination = event.options.dest as string
212199
const cmd = ActionUtil.getTalkbackAssignCommand(talkback, destination)
213-
const val = event.options.assign as number
214-
if (val < 2) {
215-
send(cmd, val)
216-
} else {
217-
const currentVal = StateUtil.getBooleanFromState(cmd, state)
218-
send(cmd, Number(!currentVal))
219-
}
200+
const val = ActionUtil.getSetOrToggleValue(cmd, event.options.assign as number, state)
201+
send(cmd, val)
220202
},
221203
subscribe: (event) => {
222-
const val = event.options.mode as number
223-
if (val >= 2) {
204+
if (event.options.mode ?? 0 > 2) {
224205
const talkback = event.options.tb as string
225206
const destination = event.options.dest as string
226207
const cmd = ActionUtil.getTalkbackAssignCommand(talkback, destination)

src/actions/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { WingTransitions } from '../transitions.js'
2-
import { WingState } from '../state/index.js'
2+
import { StateUtil, WingState } from '../state/index.js'
33
import { CompanionActionInfo, CompanionFeedbackInfo } from '@companion-module/base'
44
import { Easing } from '../easings.js'
55
import * as StateUtils from '../state/utils.js'
@@ -316,3 +316,11 @@ export function getTalkbackAssignCommand(talkback: string, destination: string):
316316
}
317317
return cmd
318318
}
319+
320+
export function getSetOrToggleValue(cmd: string, val: number, state: WingState): number {
321+
if (val >= 2) {
322+
const currentVal = StateUtil.getBooleanFromState(cmd, state)
323+
return Number(!currentVal)
324+
}
325+
return val
326+
}

0 commit comments

Comments
 (0)