Add mechanism for synchronously pulling getter block values.

This commit is contained in:
Sergio Martínez Portela 2020-05-28 18:31:48 +02:00
parent f456b75030
commit 1cbb712eee
17 changed files with 126 additions and 22 deletions

View File

@ -370,10 +370,10 @@ public class ProgramakerCustomBlockPart implements Part {
} }
} }
private void runBlockOperation() { private ProgramakerFunctionCallResult runBlockOperation() {
if (!this.active) { if (!this.active) {
Log.w(LogTag, "Trying to run inactive block function=" + this._block.getFunction_name()); Log.w(LogTag, "Trying to run inactive block function=" + this._block.getFunction_name());
return; return null;
} }
ProgramakerApi api = this._partGrid.getApi(); ProgramakerApi api = this._partGrid.getApi();
@ -386,6 +386,11 @@ public class ProgramakerCustomBlockPart implements Part {
continue; continue;
} }
Object queriedValue = entry.item2.query(lastValues[index]);
if (queriedValue != null) {
lastValues[index] = queriedValue;
}
if (lastValues[index] == null) { if (lastValues[index] == null) {
arguments.add(null); // TODO: Get default value from block definition arguments.add(null); // TODO: Get default value from block definition
} }
@ -398,6 +403,8 @@ public class ProgramakerCustomBlockPart implements Part {
Log.i(LogTag, "Execution result="+result.getResult()); Log.i(LogTag, "Execution result="+result.getResult());
onExecutionCompleted(result); onExecutionCompleted(result);
return result;
} }
private void onExecutionCompleted(ProgramakerFunctionCallResult result) { private void onExecutionCompleted(ProgramakerFunctionCallResult result) {
@ -621,4 +628,31 @@ public class ProgramakerCustomBlockPart implements Part {
input.unlink(); input.unlink();
} }
} }
@Override
public Object query(Object lastValue) {
String blockType = _block.getBlock_type();
if ((blockType == null) || (!blockType.equals("getter"))) {
// Only relevant for getters
return null;
}
String token = this.prepareStart();
if (token != null) {
try {
_partGrid.update();
ProgramakerFunctionCallResult result = runBlockOperation();
if (result != null) {
return result.getResult();
}
}
finally {
this.freeBlock(token);
}
}
return null;
}
} }

View File

@ -229,6 +229,11 @@ public class RoundButton implements Part {
} }
@Override
public Object query(Object lastValue) {
return null; // Only pulse output, so no relevant data
}
public RoundOutputConnector getPressedOutputConnector(){ public RoundOutputConnector getPressedOutputConnector(){
return _pressedOutputConnector; return _pressedOutputConnector;
} }

View File

@ -55,6 +55,17 @@ public class AnyRoundInputConnector extends AnyInputConnector {
} }
} }
public Object query(Object lastValue) {
for (Wire linked : _attachments) {
Object returned = linked.query(lastValue);
if (returned != null) {
return returned;
}
}
return null;
}
@Override @Override
public int getX() { public int getX() {
return _xposition; return _xposition;

View File

@ -83,7 +83,7 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne
} }
_connections.add(resultPoint); _connections.add(resultPoint);
wire.attachTo(resultPoint); wire.attachTo(resultPoint, this);
_wires.add(wire); _wires.add(wire);
} }
else { else {
@ -141,7 +141,7 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne
_connections.add(inputConnector); _connections.add(inputConnector);
BooleanWire wire = new BooleanWire(this, _centerX, _centerY); BooleanWire wire = new BooleanWire(this, _centerX, _centerY);
wire.attachTo(inputConnector); wire.attachTo(inputConnector, this);
_wires.add(wire); _wires.add(wire);
} }
@ -155,4 +155,9 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne
wire.send(signal); wire.send(signal);
} }
} }
@Override
public Object query(Object lastValue) {
return _part.query(lastValue);
}
} }

View File

@ -83,7 +83,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
} }
_connections.add(resultPoint); _connections.add(resultPoint);
wire.attachTo(resultPoint); wire.attachTo(resultPoint, this);
_wires.add(wire); _wires.add(wire);
} }
else { else {
@ -141,7 +141,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
_connections.add(inputConnector); _connections.add(inputConnector);
SignalWire wire = new SignalWire(this, _centerX, _centerY); SignalWire wire = new SignalWire(this, _centerX, _centerY);
wire.attachTo(inputConnector); wire.attachTo(inputConnector, this);
_wires.add(wire); _wires.add(wire);
} }
@ -156,6 +156,11 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
} }
} }
@Override
public Object query(Object lastValue) {
return _part.query(lastValue);
}
public float getX() { public float getX() {
return _centerX; return _centerX;
} }

View File

@ -83,7 +83,7 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect
} }
_connections.add(resultPoint); _connections.add(resultPoint);
wire.attachTo(resultPoint); wire.attachTo(resultPoint, this);
_wires.add(wire); _wires.add(wire);
} }
else { else {
@ -141,7 +141,7 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect
_connections.add(inputConnector); _connections.add(inputConnector);
StringWire wire = new StringWire(this, _centerX, _centerY); StringWire wire = new StringWire(this, _centerX, _centerY);
wire.attachTo(inputConnector); wire.attachTo(inputConnector, this);
_wires.add(wire); _wires.add(wire);
} }
@ -155,4 +155,9 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect
wire.send(signal); wire.send(signal);
} }
} }
@Override
public Object query(Object lastValue) {
return _part.query(lastValue);
}
} }

View File

@ -297,6 +297,11 @@ public class Ticker implements Part {
_thread = null; _thread = null;
} }
@Override
public Object query(Object lastValue) {
return null; // No relevant value (maybe time?)
}
private int getOutputConnectorCenterX() { private int getOutputConnectorCenterX() {
return (_left + _right) / 2; return (_left + _right) / 2;

View File

@ -288,6 +288,11 @@ public class Toggle implements Part {
} }
@Override
public Object query(Object lastValue) {
return _activated;
}
public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid partGrid, JSONObject data) throws JSONException { public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
String id = data.getString("id"); String id = data.getString("id");
int left = data.getInt("left"); int left = data.getInt("left");

View File

@ -220,6 +220,11 @@ public class ColorBox implements Part {
} }
@Override
public Object query(Object lastValue) {
return null;
}
public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
String id = data.getString("id"); String id = data.getString("id");
int left = data.getInt("left"); int left = data.getInt("left");

View File

@ -154,6 +154,11 @@ public class Placeholder implements Part {
} }
@Override
public Object query(Object lastValue) {
return null;
}
public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
String id = data.getString("id"); String id = data.getString("id");
int left = data.getInt("left"); int left = data.getInt("left");

View File

@ -5,8 +5,6 @@ import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import com.codigoparallevar.minicards.PartInstantiator; import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector; import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector;
@ -46,8 +44,6 @@ public class ConvertToString implements Part {
private int _bottom; private int _bottom;
private List<InputConnector> inputConnectors; private List<InputConnector> inputConnectors;
private AnyInputConnector _toggleInputConnector; private AnyInputConnector _toggleInputConnector;
@NonNull
private String _lastValue; private String _lastValue;
private ConvertToString(String id, PartGrid partGrid, int left, int top, int right, int bottom) { private ConvertToString(String id, PartGrid partGrid, int left, int top, int right, int bottom) {
@ -57,7 +53,7 @@ public class ConvertToString implements Part {
_top = top; _top = top;
_right = right; _right = right;
_bottom = bottom; _bottom = bottom;
_lastValue = "-"; _lastValue = null;
// Input connector // Input connector
_toggleInputConnector = new AnyRoundInputConnector( _toggleInputConnector = new AnyRoundInputConnector(
@ -121,7 +117,13 @@ public class ConvertToString implements Part {
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.GREEN); textPaint.setColor(Color.GREEN);
textPaint.setTextSize(100); textPaint.setTextSize(100);
canvas.drawText(_lastValue,
String value = "-";
if (_lastValue != null) {
value = _lastValue;
}
canvas.drawText(value,
_left + 10, _left + 10,
_bottom - 10, _bottom - 10,
textPaint); textPaint);
@ -255,7 +257,7 @@ public class ConvertToString implements Part {
encoded = value.toString(); encoded = value.toString();
} }
if (!_lastValue.equals(encoded)) { if ((_lastValue != null) && !_lastValue.equals(encoded)) {
_lastValue = encoded; _lastValue = encoded;
_stringOutputConnector.send(new StringSignal(encoded)); _stringOutputConnector.send(new StringSignal(encoded));
@ -297,6 +299,11 @@ public class ConvertToString implements Part {
} }
@Override
public Object query(Object lastValue) {
return _lastValue;
}
public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid partGrid, JSONObject data) throws JSONException { public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
String id = data.getString("id"); String id = data.getString("id");
int left = data.getInt("left"); int left = data.getInt("left");

View File

@ -32,4 +32,6 @@ public interface Part extends Selectable, Moveable, Drawable {
void resume(); void resume();
void pause(); void pause();
Object query(Object lastValue);
} }

View File

@ -1,13 +1,13 @@
package com.codigoparallevar.minicards.types.connectors.Wiring; package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal; import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
public class BooleanWire extends Wire<BooleanSignal, BooleanInputConnector> implements Moveable, Drawable { public class BooleanWire extends Wire<BooleanSignal, BooleanInputConnector> implements Moveable, Drawable {
public BooleanWire(Dropper dropper, int xInit, int yInit) { public BooleanWire(OutputConnector dropper, int xInit, int yInit) {
super(dropper, xInit, yInit); super(dropper, xInit, yInit);
} }

View File

@ -1,13 +1,13 @@
package com.codigoparallevar.minicards.types.connectors.Wiring; package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal; import com.codigoparallevar.minicards.types.wireData.Signal;
public class SignalWire extends Wire<Signal, SignalInputConnector> implements Moveable, Drawable { public class SignalWire extends Wire<Signal, SignalInputConnector> implements Moveable, Drawable {
public SignalWire(Dropper dropper, int xInit, int yInit) { public SignalWire(OutputConnector dropper, int xInit, int yInit) {
super(dropper, xInit, yInit); super(dropper, xInit, yInit);
} }

View File

@ -1,13 +1,13 @@
package com.codigoparallevar.minicards.types.connectors.Wiring; package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal; import com.codigoparallevar.minicards.types.wireData.StringSignal;
public class StringWire extends Wire<StringSignal, StringInputConnector> implements Moveable, Drawable { public class StringWire extends Wire<StringSignal, StringInputConnector> implements Moveable, Drawable {
public StringWire(Dropper dropper, int xInit, int yInit) { public StringWire(OutputConnector dropper, int xInit, int yInit) {
super(dropper, xInit, yInit); super(dropper, xInit, yInit);
} }

View File

@ -9,6 +9,7 @@ import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.types.wireData.WireDataType;
public class Wire<T extends WireDataType, InputConnectorType extends InputConnector<T, InputConnectorType>> public class Wire<T extends WireDataType, InputConnectorType extends InputConnector<T, InputConnectorType>>
@ -21,6 +22,7 @@ public class Wire<T extends WireDataType, InputConnectorType extends InputConnec
private int _yend; private int _yend;
private final static int _pathRunWay = 100; private final static int _pathRunWay = 100;
protected InputConnectorType _attachedTo = null; protected InputConnectorType _attachedTo = null;
protected OutputConnector _attachedFrom = null;
public Wire(Dropper dropper, int xInit, int yInit) { public Wire(Dropper dropper, int xInit, int yInit) {
_dropper = dropper; _dropper = dropper;
@ -74,8 +76,10 @@ public class Wire<T extends WireDataType, InputConnectorType extends InputConnec
return _yend; return _yend;
} }
public void attachTo(InputConnectorType resultPoint) { public void attachTo(InputConnectorType resultPoint, OutputConnector output) {
_attachedTo = resultPoint; _attachedTo = resultPoint;
_attachedFrom = output;
_xend = resultPoint.getX(); _xend = resultPoint.getX();
_yend = resultPoint.getY(); _yend = resultPoint.getY();
resultPoint.getAttachment(this); resultPoint.getAttachment(this);
@ -89,4 +93,8 @@ public class Wire<T extends WireDataType, InputConnectorType extends InputConnec
_attachedTo = null; _attachedTo = null;
_dropper.unlinkWire(this); _dropper.unlinkWire(this);
} }
public Object query(Object lastValue) {
return this._attachedFrom.query(lastValue);
}
} }

View File

@ -23,4 +23,6 @@ public interface OutputConnector<T extends WireDataType,
void connectTo(T1 inputConnector); void connectTo(T1 inputConnector);
void send(T data); void send(T data);
Object query(Object lastValue);
} }