Skip to content

Commit

Permalink
Making sample manager testable
Browse files Browse the repository at this point in the history
  • Loading branch information
ZILtoid1991 committed Jul 20, 2023
1 parent 4a2fb87 commit c9609bc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
19 changes: 19 additions & 0 deletions test1/editorevents.d
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,23 @@ public class RemoveSample : UndoableEvent {
public void undo() {
mcfg.addWaveFromBackup(modID, backup);
}
}
public class RenameSample : UndoableEvent {
ModuleConfig mcfg;
string modID;
int sampleID;
string newName;
string oldName;
public this (ModuleConfig mcfg, string modID, int sampleID, string newName) {
this.mcfg = mcfg;
this.modID = modID;
this.sampleID = sampleID;
this.newName = newName;
}
public void redo() {
oldName = mcfg.renameWave(modID, sampleID, newName);
}
public void undo() {
mcfg.renameWave(modID, sampleID, oldName);
}
}
10 changes: 7 additions & 3 deletions test1/modulerouter.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ModuleRouter : Window {
Button button_addMod;
Button button_remMod;
Button button_preset;
Button button_setup;
Button button_sampleman;
Button button_audNode;
Button button_midiNode;
Button button_remNode;
Expand All @@ -28,7 +28,7 @@ public class ModuleRouter : Window {
button_addMod = new Button("Add module..."d, "button_addMod", Box(535, 20, 635, 40));
button_remMod = new Button("Remove module"d, "button_remMod", Box(535, 45, 635, 65));
button_preset = new Button("Presets..."d, "button_preset", Box(535, 70, 635, 90));
button_setup = new Button("Settings..."d, "button_setup", Box(535, 95, 635, 115));
button_sampleman = new Button("Samples..."d, "button_samples", Box(535, 95, 635, 115));
button_audNode = new Button("Add audio node"d, "button_audNode", Box(535, 180, 635, 200));
//button_midiNode = new Button("Add MIDI node"d, "button1", Box(535, 205, 635, 225));
button_remNode = new Button("Remove node"d, "button_remNode", Box(535, 205, 635, 225));
Expand All @@ -45,7 +45,7 @@ public class ModuleRouter : Window {
addElement(listView_routing);
addElement(button_addMod);
addElement(button_remMod);
addElement(button_setup);
addElement(button_sampleman);
addElement(button_preset);
addElement(button_audNode);
//addElement(button_midiNode);
Expand Down Expand Up @@ -101,6 +101,10 @@ public class ModuleRouter : Window {
[TextInputFieldType.ASCIIText, TextInputFieldType.ASCIIText]);
listView_routing.refresh();
}
private void button_sampleman_onClick(Event e) {
import test1.sampleman;
adk.wh.addWindow(new SampleMan(adk));
}
private void listView_modules_onTextEdit(Event e) {
//CellEditEvent ce = cast(CellEditEvent)e;
ListViewItem item = cast(ListViewItem)e.aux;
Expand Down
72 changes: 54 additions & 18 deletions test1/sampleman.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WaveformViewer : WindowElement {
}
public void setWaveform(const(ubyte)[] src, WaveFormat fmt) {
DecoderWorkpad wp;
switch (fmt.format) { //Hope this branching won't impact performance too much
switch (fmt.format) {
case AudioFormat.PCM:
if (fmt.bitsPerSample == 8) {
waveform.length = src.length;
Expand Down Expand Up @@ -51,21 +51,23 @@ public class WaveformViewer : WindowElement {
StyleSheet ss = getStyleSheet();
parent.clearArea(position);
parent.drawBox(position, 24);
real ratio = position.width / cast(real)waveform.length;
const int divident = ushort.max / position.height();
{
Point o = Point(0, position.height / 2);
parent.drawLine(position.cornerUL + o, position.cornerUR + o, 23);
}
for (int i ; i < position.width ; i++) {
Point p =
Point(i + position.left, (waveform[cast(size_t)floor(i * ratio)]/divident) + position.top + (position.height / 2));
parent.drawLine(p, p, ss.getColor("text"));
}
if (waveform.length) {
real ratio = position.width / cast(real)waveform.length;
const int divident = ushort.max / position.height();
{
Point o = Point(0, position.height / 2);
parent.drawLine(position.cornerUL + o, position.cornerUR + o, 23);
}
for (int i ; i < position.width ; i++) {
Point p =
Point(i + position.left, (waveform[cast(size_t)floor(i * ratio)]/divident) + position.top + (position.height / 2));
parent.drawLine(p, p, ss.getColor("text"));
}

if (isFocused) {
const int textPadding = ss.drawParameters["horizTextPadding"];
parent.drawBoxPattern(position - textPadding, ss.pattern["blackDottedLine"]);
if (isFocused) {
const int textPadding = ss.drawParameters["horizTextPadding"];
parent.drawBoxPattern(position - textPadding, ss.pattern["blackDottedLine"]);
}
}
if (state == ElementState.Disabled) {
parent.bitBLTPattern(position, ss.getImage("ElementDisabledPtrn"));
Expand Down Expand Up @@ -94,13 +96,13 @@ public class SampleMan : Window {

string moduleName;
//TextBox textBox0;
public this(AudioDevKit adk){
public this(AudioDevKit adk) {
moduleName = adk.selectedModID;
this.adk = adk;

super(Box(0, 0, 520, 322), "Sample manager ["d ~ moduleName.to!dstring ~ "]"d);
listView_sampleList = new ListView(new ListViewHeader(16, [40 ,250], ["ID" ,"file source"]), null, "listView0",
Box(5, 20, 335, 185));
listView_sampleList = new ListView(new ListViewHeader(16, [40, 120, 250], ["ID" ,"name" ,"file source"]), null,
"listView0", Box(5, 20, 335, 185));
button_load = new Button("Load"d, "button0", Box(340, 20, 440, 40));
button_slice = new Button("Slice"d, "button1", Box(340, 45, 440, 65));
button_remove = new Button("Remove"d, "button0", Box(340, 70, 440, 90));
Expand All @@ -119,9 +121,40 @@ public class SampleMan : Window {
addElement(label_format);
addElement(label_slmpR);
addElement(label_len);

button_load.onMouseLClick = &button_load_onClick;
button_slice.onMouseLClick = &button_slice_onClick;
button_remove.onMouseLClick = &button_remove_onClick;
listView_sampleList.onItemSelect = &listView_sampleList_onSelect;
listView_sampleList.onTextInput = &listView_sampleList_onTextEdit;
listView_sampleList.editEnable = true;

refreshSampleList();
}
protected void refreshSampleList() {
waveFileData = adk.mcfg.getWaveFileList(moduleName);
listView_sampleList.clear();
foreach (WaveFileData key; waveFileData) {
listView_sampleList ~= new ListViewItem(16, [to!dstring(key.id), to!dstring(key.name), to!dstring(key.path)],
[TextInputFieldType.None, TextInputFieldType.ASCIIText, TextInputFieldType.None]);
}
listView_sampleList.refresh();
}
protected void listView_sampleList_onSelect(Event ev) {
if (listView_sampleList.value >= 0) {
import pixelperfectengine.audio.base.modulebase;
ModuleConfig mcfg = adk.mcfg;
AudioModule am = mcfg.getModule(moduleName);
wfv.setWaveform(am.getWaveformData(listView_sampleList.value), am.getWaveformDataFormat(listView_sampleList.value));
wfv.draw();
}
}
protected void listView_sampleList_onTextEdit(Event ev) {
CellEditEvent cev = cast(CellEditEvent)ev;
if (listView_sampleList.value >= 0) {
WaveFileData wfd = waveFileData[listView_sampleList.value];
adk.eventStack.addToTop(new RenameSample(adk.mcfg, moduleName, wfd.id, to!string(cev.text.toDString)));
}
}
protected void button_load_onClick(Event ev) {
import pixelperfectengine.concrete.dialogs.filedialog;
Expand All @@ -144,6 +177,7 @@ public class SampleMan : Window {
if (key.id == sampleID) return;
}
adk.eventStack.addToTop(new AddSampleFile(adk.mcfg, moduleName, sampleID, path));
refreshSampleList();
} catch (Exception e) {

}
Expand All @@ -156,12 +190,14 @@ public class SampleMan : Window {
const int src = waveFileData[listView_sampleList.value].id;
const int len = end - begin;
adk.eventStack.addToTop(new AddSampleSlice(adk.mcfg, moduleName, src, id, begin, len));
refreshSampleList();
}
}
protected void button_remove_onClick(Event ev) {
if (listView_sampleList.value >= 0) {
const int selID = waveFileData[listView_sampleList.value].id;
adk.eventStack.addToTop(new RemoveSample(adk.mcfg, moduleName, selID));
refreshSampleList();
}
}
}
Expand Down

0 comments on commit c9609bc

Please sign in to comment.