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) {
Log.w(LogTag, "Trying to run inactive block function=" + this._block.getFunction_name());
return;
return null;
}
ProgramakerApi api = this._partGrid.getApi();
@ -386,6 +386,11 @@ public class ProgramakerCustomBlockPart implements Part {
continue;
}
Object queriedValue = entry.item2.query(lastValues[index]);
if (queriedValue != null) {
lastValues[index] = queriedValue;
}
if (lastValues[index] == null) {
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());
onExecutionCompleted(result);
return result;
}
private void onExecutionCompleted(ProgramakerFunctionCallResult result) {
@ -621,4 +628,31 @@ public class ProgramakerCustomBlockPart implements Part {
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(){
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
public int getX() {
return _xposition;

View File

@ -83,7 +83,7 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne
}
_connections.add(resultPoint);
wire.attachTo(resultPoint);
wire.attachTo(resultPoint, this);
_wires.add(wire);
}
else {
@ -141,7 +141,7 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne
_connections.add(inputConnector);
BooleanWire wire = new BooleanWire(this, _centerX, _centerY);
wire.attachTo(inputConnector);
wire.attachTo(inputConnector, this);
_wires.add(wire);
}
@ -155,4 +155,9 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne
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);
wire.attachTo(resultPoint);
wire.attachTo(resultPoint, this);
_wires.add(wire);
}
else {
@ -141,7 +141,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
_connections.add(inputConnector);
SignalWire wire = new SignalWire(this, _centerX, _centerY);
wire.attachTo(inputConnector);
wire.attachTo(inputConnector, this);
_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() {
return _centerX;
}

View File

@ -83,7 +83,7 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect
}
_connections.add(resultPoint);
wire.attachTo(resultPoint);
wire.attachTo(resultPoint, this);
_wires.add(wire);
}
else {
@ -141,7 +141,7 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect
_connections.add(inputConnector);
StringWire wire = new StringWire(this, _centerX, _centerY);
wire.attachTo(inputConnector);
wire.attachTo(inputConnector, this);
_wires.add(wire);
}
@ -155,4 +155,9 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect
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;
}
@Override
public Object query(Object lastValue) {
return null; // No relevant value (maybe time?)
}
private int getOutputConnectorCenterX() {
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 {
String id = data.getString("id");
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 {
String id = data.getString("id");
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 {
String id = data.getString("id");
int left = data.getInt("left");

View File

@ -5,8 +5,6 @@ import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import androidx.annotation.NonNull;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector;
@ -46,8 +44,6 @@ public class ConvertToString implements Part {
private int _bottom;
private List<InputConnector> inputConnectors;
private AnyInputConnector _toggleInputConnector;
@NonNull
private String _lastValue;
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;
_right = right;
_bottom = bottom;
_lastValue = "-";
_lastValue = null;
// Input connector
_toggleInputConnector = new AnyRoundInputConnector(
@ -121,7 +117,13 @@ public class ConvertToString implements Part {
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.GREEN);
textPaint.setTextSize(100);
canvas.drawText(_lastValue,
String value = "-";
if (_lastValue != null) {
value = _lastValue;
}
canvas.drawText(value,
_left + 10,
_bottom - 10,
textPaint);
@ -255,7 +257,7 @@ public class ConvertToString implements Part {
encoded = value.toString();
}
if (!_lastValue.equals(encoded)) {
if ((_lastValue != null) && !_lastValue.equals(encoded)) {
_lastValue = 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 {
String id = data.getString("id");
int left = data.getInt("left");

View File

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

View File

@ -1,13 +1,13 @@
package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
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);
}

View File

@ -1,13 +1,13 @@
package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal;
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);
}

View File

@ -1,13 +1,13 @@
package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
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);
}

View File

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