Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "note off" support for buttons #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/src/main/assets/template1.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contents: [
text: "button",
rect: [213, 68, 513, 368],
width: 300,
oscButtonPressed: "button 1"
oscButtonPressed: "button 1 $1"
},
{
type:"button",
Expand All @@ -22,7 +22,7 @@ contents: [
text: "button",
rect: [562, 68, 862, 368],
width: 300,
oscButtonPressed: "button 1"
oscButtonPressed: "button 2 $1"
},
{
type:"button",
Expand All @@ -34,7 +34,7 @@ contents: [
text: "button",
rect: [213, 407, 513, 707],
width: 300,
oscButtonPressed: "button 1"
oscButtonPressed: "button 3 $1"
},
{
type:"button",
Expand All @@ -46,6 +46,6 @@ contents: [
text: "button",
rect: [562, 407, 862, 707],
width: 300,
oscButtonPressed: "button 1"
oscButtonPressed: "button 4 $1"
}
]}
]}
6 changes: 3 additions & 3 deletions app/src/main/assets/template2.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contents: [
text: "button",
rect: [348, 516, 448, 616],
width: 100,
oscButtonPressed: "button 1"
oscButtonPressed: "button 1 $1"
},
{
type:"button",
Expand All @@ -64,7 +64,7 @@ contents: [
text: "button",
rect: [986, 517, 1086, 617],
width: 100,
oscButtonPressed: "button 1"
oscButtonPressed: "button 2 $1"
},
{
type:"vslider",
Expand Down Expand Up @@ -92,4 +92,4 @@ contents: [
minValue: 0.0,
OSCValueChanged: "/hslider $1"
}
]}
]}
4 changes: 2 additions & 2 deletions app/src/main/assets/template3.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ contents: [
text: "button",
rect: [781, 139, 881, 239],
width: 100,
oscButtonPressed: "button 1"
oscButtonPressed: "button 1 $1"
},
{
type:"hslider",
Expand All @@ -119,4 +119,4 @@ contents: [
minValue: 0.0,
OSCValueChanged: "/hslider $1"
}
]}
]}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class OSCButtonView extends OSCControlView {
private static final int BORDER_SIZE = 3;

private SimpleDoubleTapDetector mDoubleTapDetector;

public OSCButtonView(Context context, OSCViewGroup parent, OSCButtonParameters params) {
super(context, parent);

Expand Down Expand Up @@ -118,10 +118,11 @@ public boolean onTouchEvent(MotionEvent event) {
else {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
this.mFingerDown = true;
fireOSCMessage();
fireOSCMessage(true);
}
else if(event.getAction() == MotionEvent.ACTION_UP) {
this.mFingerDown = false;
fireOSCMessage(false);
}

invalidate(0, 0, this.mParams.getWidth(), this.mParams.getHeight());
Expand Down Expand Up @@ -217,7 +218,15 @@ public void updateDimensions(int width, int height) {
}

private String oscMessage;
private List<Object> oscArgs;
// Message arguments. If $1 is present in the OSC arguments, these
// two lists contain 1 and 0 in its place, for note on and note off,
// respectively.
private List<Object> oscArgsOn;
private List<Object> oscArgsOff;
// To mimic the behaviour of previous versions, the "note off" event will
// only fire if the OSC parameters contain a '$1'.
private boolean enableNoteOffEvent = false;


public void updateOSCPressed(String value) {
if(!value.equals(this.getParameters().getOSCButtonPressed())) {
Expand All @@ -228,17 +237,42 @@ public void updateOSCPressed(String value) {

private void initialOSCParse() {
String[] oscParts = this.getParameters().getOSCButtonPressed().split(" ");
this.oscArgs = new ArrayList<Object>();
List<Object> oscArgs = new ArrayList<Object>();

// index of the $1 parameter, if present
int paramIdx = -1;
this.enableNoteOffEvent = false;

for(int i = 1; i < oscParts.length; i += 1) {
this.oscArgs.add(Utilities.simpleParse(oscParts[i]));
oscArgs.add(Utilities.simpleParse(oscParts[i]));

if (oscParts[i].equals("$1")) {
paramIdx = i-1;
}
}

// To avoid memory allocation within the critical path (the touch event->osc),
// cache the on- and off messages separately. (Not sure whether this is actually necessary?)
this.oscArgsOn = new ArrayList<Object>(oscArgs);

this.oscArgsOff = new ArrayList<Object>(oscArgs);

if (paramIdx != -1) {
this.oscArgsOn.set(paramIdx, 1);
this.oscArgsOff.set(paramIdx, 0);
this.enableNoteOffEvent = true;
}

this.oscMessage = oscParts[0];
}

private void fireOSCMessage() {
private void fireOSCMessage(boolean buttonActive) {
try {
OSCWrapper.getInstance().sendOSC(this.oscMessage, this.oscArgs);
if (buttonActive) {
OSCWrapper.getInstance().sendOSC(this.oscMessage, this.oscArgsOn);
} else if (this.enableNoteOffEvent) {
OSCWrapper.getInstance().sendOSC(this.oscMessage, this.oscArgsOff);
}
}
catch(Exception exp) {}
}
Expand Down Expand Up @@ -275,7 +309,7 @@ public static OSCButtonParameters getDefaultParameters() {
params.setTop(100);
params.setRight(200);
params.setBottom(200);
params.setOSCButtonPressed("button 1");
params.setOSCButtonPressed("button 1 $1");

return params;
}
Expand Down