Skip to content

Commit

Permalink
adden preset hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Diyan committed Mar 12, 2023
1 parent badcc39 commit 43dd736
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/UI/pages/hotkeysMainPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HotkeysMainPage extends StatelessWidget {
body: ListView(
children: [
ListTile(
title: const Text("Channel Hotkeys"),
title: const Text("Channel/Preset Hotkeys"),
trailing: const Icon(Icons.keyboard_arrow_right),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => HotkeysSetup(
Expand Down
21 changes: 21 additions & 0 deletions lib/UI/pages/hotkeysSetup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ class _HotkeysSetupState extends State<HotkeysSetup> {
widgets.add(buildWidget("Channel ${i + 1}", Icons.circle, colors[i],
HotkeyControl.ChannelByIndex, i, 0, false));
}

widgets.add(buildWidget("Previous Preset", Icons.keyboard_double_arrow_up,
null, HotkeyControl.PreviousPresetGlobal, 0, 0, false));
widgets.add(buildWidget("Next Preset", Icons.keyboard_double_arrow_down,
null, HotkeyControl.NextPresetGlobal, 0, 0, false));
widgets.add(buildWidget(
"Previous Preset in Category",
Icons.keyboard_arrow_up,
null,
HotkeyControl.PreviousPresetCategory,
0,
0,
false));
widgets.add(buildWidget(
"Next Preset in Category",
Icons.keyboard_arrow_down,
null,
HotkeyControl.NextPresetCategory,
0,
0,
false));
return widgets;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/bluetooth/ble_controllers/FlutterBluePlusController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class FlutterBluePlusController extends BLEController {
}

@override
void stopScanning() {
if (bleState == BleState.off) return;
flutterBlue.stopScan();
Future stopScanning() {
if (bleState == BleState.off) return Future.value(null);
return flutterBlue.stopScan();
}

@override
Expand All @@ -128,7 +128,7 @@ class FlutterBluePlusController extends BLEController {
}

_connectInProgress = true;
stopScanning();
await stopScanning();
setMidiSetupStatus(MidiSetupStatus.deviceConnecting);
try {
await ownDevice.connect(
Expand Down
63 changes: 63 additions & 0 deletions lib/bluetooth/devices/presets/presetsStorage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:uuid/uuid.dart';

import '../../../platform/platformUtils.dart';

enum PresetChangeDirection { previous, next }

class PresetsStorage extends ChangeNotifier {
static final PresetsStorage _storage = PresetsStorage._();
static const presetsFile = "presets.json";
Expand Down Expand Up @@ -150,6 +152,29 @@ class PresetsStorage extends ChangeNotifier {
return null;
}

dynamic findAdjacentPreset(
String uuid, bool acrossCategories, PresetChangeDirection direction) {
int catsCount = presetsData.length;
int catIndex = 0;

if (uuid.isEmpty) {
return _getAdjacentPreset(0, -1, direction, acrossCategories);
}
for (catIndex = 0; catIndex < catsCount; catIndex++) {
int pIndex = 0;
var cat = presetsData[catIndex];
var presets = cat["presets"];
int pCount = presets.length;
for (pIndex = 0; pIndex < pCount; pIndex++) {
if (presets[pIndex]?["uuid"] == uuid) {
return _getAdjacentPreset(
catIndex, pIndex, direction, acrossCategories);
}
}
}
return _getAdjacentPreset(0, -1, direction, acrossCategories);
}

Map<String, dynamic>? findCategoryOfPreset(Map<String, dynamic> preset) {
for (var cat in presetsData) {
for (var pr in cat["presets"]) {
Expand Down Expand Up @@ -410,6 +435,44 @@ class PresetsStorage extends ChangeNotifier {
if (_name != null) savePreset(presetData, _name, category);
}

dynamic _getAdjacentPreset(int catIndex, int pIndex,
PresetChangeDirection direction, bool acrossCategory) {
if (presetsData.length <= catIndex) return null;
var catLength = presetsData[catIndex]["presets"].length;
if (direction == PresetChangeDirection.previous) {
pIndex--;
} else {
pIndex++;
}

if (pIndex < 0) {
if (!acrossCategory) {
pIndex = catLength - 1;
} else {
do {
catIndex--;
if (catIndex < 0) {
catIndex = presetsData.length - 1;
}
} while (presetsData[catIndex]["presets"].length == 0);
pIndex = presetsData[catIndex]["presets"].length - 1;
}
} else if (pIndex >= catLength) {
if (!acrossCategory) {
pIndex = 0;
} else {
do {
catIndex++;
if (catIndex >= presetsData.length) {
catIndex = 0;
}
} while (presetsData[catIndex]["presets"].length == 0);
pIndex = 0;
}
}
return presetsData[catIndex]["presets"][pIndex];
}

String? _findFreeName(String name, String category) {
for (int i = 1; i < 1000; i++) {
RegExp regex = RegExp(r"\((\d+)\)$");
Expand Down
7 changes: 5 additions & 2 deletions lib/midi/ControllerConstants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class MidiConstants {
static const NoteOn = 0x90;
Expand Down Expand Up @@ -35,7 +34,11 @@ enum HotkeyControl {
JamTracksNextTrack,
JamTracksRewind,
JamTracksFF,
JamTracksABRepeat;
JamTracksABRepeat,
PreviousPresetGlobal,
NextPresetGlobal,
PreviousPresetCategory,
NextPresetCategory
}

extension HotkeyLabel on HotkeyControl {
Expand Down
30 changes: 30 additions & 0 deletions lib/midi/controllers/ControllerHotkey.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:mighty_plug_manager/audio/setlist_player/setlistPlayerState.dart';
import 'package:mighty_plug_manager/bluetooth/NuxDeviceControl.dart';
import 'package:mighty_plug_manager/bluetooth/devices/presets/presetsStorage.dart';
import 'package:mighty_plug_manager/bluetooth/devices/utilities/DelayTapTimer.dart';
import '../../bluetooth/devices/NuxDevice.dart';
import '../../bluetooth/devices/effects/MidiControllerHandles.dart';
Expand Down Expand Up @@ -183,6 +184,18 @@ class ControllerHotkey {
case HotkeyControl.JamTracksABRepeat:
SetlistPlayerState.instance().toggleABRepeat();
break;
case HotkeyControl.PreviousPresetGlobal:
_changeToAdjacentPreset(device, true, PresetChangeDirection.previous);
break;
case HotkeyControl.NextPresetGlobal:
_changeToAdjacentPreset(device, true, PresetChangeDirection.next);
break;
case HotkeyControl.PreviousPresetCategory:
_changeToAdjacentPreset(device, false, PresetChangeDirection.previous);
break;
case HotkeyControl.NextPresetCategory:
_changeToAdjacentPreset(device, false, PresetChangeDirection.next);
break;
default:
onHotkeyReceived(control);
}
Expand All @@ -194,6 +207,23 @@ class ControllerHotkey {
return (val / 127) * 100;
}

void _changeToAdjacentPreset(
NuxDevice device, bool acrossCategories, PresetChangeDirection dir) {
var uuid = NuxDeviceControl.instance().presetUUID;
for (int i = 0; i < 200; i++) {
var preset =
PresetsStorage().findAdjacentPreset(uuid, acrossCategories, dir);
if (preset != null &&
preset["uuid"] != uuid &&
device.isPresetSupported(preset)) {
device.presetFromJson(preset, null);
break;
} else if (preset != null) {
uuid = preset["uuid"];
}
}
}

void _modifyTempo(NuxDevice device, double amount) {
double newTempo = device.drumsTempo + amount;
device.setDrumsTempo(newTempo, true);
Expand Down

0 comments on commit 43dd736

Please sign in to comment.