354 lines
10 KiB
Java
354 lines
10 KiB
Java
package com.codigoparallevar.minicards.parts.strings;
|
|
|
|
import android.graphics.Color;
|
|
import android.graphics.Paint;
|
|
import android.graphics.Rect;
|
|
import android.support.annotation.NonNull;
|
|
import android.util.Log;
|
|
|
|
import com.codigoparallevar.minicards.PartInstantiator;
|
|
import com.codigoparallevar.minicards.ScrolledCanvas;
|
|
import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector;
|
|
import com.codigoparallevar.minicards.parts.connectors.StringRoundOutputConnector;
|
|
import com.codigoparallevar.minicards.types.Moveable;
|
|
import com.codigoparallevar.minicards.types.Part;
|
|
import com.codigoparallevar.minicards.types.PartConnection;
|
|
import com.codigoparallevar.minicards.types.PartGrid;
|
|
import com.codigoparallevar.minicards.types.Tuple2;
|
|
import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector;
|
|
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
|
|
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
|
|
import com.codigoparallevar.minicards.types.wireData.StringSignal;
|
|
import com.codigoparallevar.minicards.types.wireData.WireDataType;
|
|
import com.codigoparallevar.minicards.utils.Serializations;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
public class ConvertToString implements Part {
|
|
private static final String TOGGLE_INPUT_CONNECTOR_ID = "any_input_connector";
|
|
private static final int DEFAULT_SIDE_SIZE = 100;
|
|
private final String _id;
|
|
private final PartGrid _partGrid;
|
|
private final StringRoundOutputConnector _stringOutputConnector;
|
|
private final LinkedList<OutputConnector> _outputConnectors;
|
|
private int _left;
|
|
private int _top;
|
|
private int _right;
|
|
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) {
|
|
_id = id;
|
|
_partGrid = partGrid;
|
|
_left = left;
|
|
_top = top;
|
|
_right = right;
|
|
_bottom = bottom;
|
|
_lastValue = "-";
|
|
|
|
// Input connector
|
|
_toggleInputConnector = new AnyRoundInputConnector(
|
|
this,
|
|
getInputConnectorCenterX(),
|
|
getInputConnectorCenterY(),
|
|
getInputConnectRadius());
|
|
inputConnectors = new LinkedList<>();
|
|
inputConnectors.add(_toggleInputConnector);
|
|
|
|
// Output connector
|
|
// Create connectors
|
|
_stringOutputConnector = new StringRoundOutputConnector(
|
|
this,
|
|
_partGrid,
|
|
getOutputConnectorCenterX(), getOutputConnectorCenterY(),
|
|
getOutputConnectRadius());
|
|
|
|
_outputConnectors = new LinkedList<>();
|
|
_outputConnectors.add(_stringOutputConnector);
|
|
}
|
|
|
|
public ConvertToString(PartGrid partGrid, int left, int top, int right, int bottom) {
|
|
this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom);
|
|
}
|
|
|
|
@Override
|
|
public int get_left() {
|
|
return _left;
|
|
}
|
|
|
|
@Override
|
|
public int get_right() {
|
|
return _right;
|
|
}
|
|
|
|
@Override
|
|
public int get_top() {
|
|
return _top;
|
|
}
|
|
|
|
@Override
|
|
public int get_bottom() {
|
|
return _bottom;
|
|
}
|
|
|
|
@Override
|
|
public void draw(ScrolledCanvas canvas, boolean devMode) {
|
|
|
|
if (devMode) {
|
|
drawConnector(canvas);
|
|
drawWires(canvas, devMode);
|
|
|
|
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
paint.setColor(Color.WHITE);
|
|
canvas.drawRect(
|
|
new Rect(_left, _top,
|
|
_right, _bottom),
|
|
paint);
|
|
|
|
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
textPaint.setColor(Color.GREEN);
|
|
textPaint.setTextSize(100);
|
|
canvas.drawText(_lastValue,
|
|
_left,
|
|
_top - 5,
|
|
textPaint);
|
|
}
|
|
}
|
|
|
|
private void drawConnector(ScrolledCanvas canvas) {
|
|
Paint inputConnectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
inputConnectorPaint.setColor(Color.YELLOW);
|
|
|
|
canvas.drawCircle(
|
|
getInputConnectorCenterX(), getInputConnectorCenterY(),
|
|
getInputConnectRadius(),
|
|
inputConnectorPaint);
|
|
|
|
Paint outputConnectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
outputConnectorPaint.setColor(Color.RED);
|
|
|
|
canvas.drawCircle(_right, getOutputConnectorCenterY(),
|
|
getOutputConnectRadius(),
|
|
outputConnectorPaint);
|
|
}
|
|
|
|
private void drawWires(ScrolledCanvas canvas, boolean devMode) {
|
|
for (OutputConnector outputConnector : _outputConnectors){
|
|
outputConnector.drawWires(canvas, devMode);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void moveEnd(int x, int y) {
|
|
final int width = _right - _left;
|
|
final int height = _bottom - _top;
|
|
|
|
_left = x - width / 2;
|
|
_right = _left + width;
|
|
|
|
_top = y - height / 2;
|
|
_bottom = _top + height;
|
|
|
|
_toggleInputConnector.updatePosition(
|
|
getInputConnectorCenterX(),
|
|
getInputConnectorCenterY());
|
|
|
|
_stringOutputConnector.updatePosition(
|
|
getOutputConnectorCenterX(),
|
|
getOutputConnectorCenterY());
|
|
}
|
|
|
|
@Override
|
|
public void drop(int x, int y) {
|
|
moveEnd(x, y);
|
|
}
|
|
|
|
@Override
|
|
public void touched() {
|
|
Log.d("Placeholder", "Placeholder touched");
|
|
}
|
|
|
|
@Override
|
|
public List<InputConnector> getInputConnectors() {
|
|
return inputConnectors;
|
|
}
|
|
|
|
@Override
|
|
public List<OutputConnector> getOutputConnectors() {
|
|
return _outputConnectors;
|
|
}
|
|
|
|
public AnyInputConnector getToggleInputConnector() {
|
|
return _toggleInputConnector;
|
|
}
|
|
|
|
public OutputConnector getToggleOutputConnector() {
|
|
return _stringOutputConnector;
|
|
}
|
|
|
|
|
|
@Override
|
|
public JSONObject serialize() throws JSONException {
|
|
JSONObject serialized = new JSONObject();
|
|
|
|
serialized.put("id", _id);
|
|
serialized.put("left", _left);
|
|
serialized.put("top", _top);
|
|
serialized.put("right", _right);
|
|
serialized.put("bottom", _bottom);
|
|
|
|
serialized.put("on_string_output_connector",
|
|
Serializations.serialize(serializeConnectionEndpoints()));
|
|
|
|
return serialized;
|
|
}
|
|
|
|
private List<Map<String, String>> serializeConnectionEndpoints() {
|
|
List<Map<String, String>> serializedData = new LinkedList<>();
|
|
|
|
for (Tuple2<String, String> endpoint : _stringOutputConnector.getConnectionEndpoints()){
|
|
serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2));
|
|
}
|
|
|
|
return serializedData;
|
|
}
|
|
|
|
@Override
|
|
public void send(InputConnector roundInputConnector, WireDataType signal) {
|
|
String encoded = "null";
|
|
Object value = signal.get();
|
|
|
|
if (value != null){
|
|
encoded = value.toString();
|
|
}
|
|
|
|
if (!_lastValue.equals(encoded)) {
|
|
_lastValue = encoded;
|
|
|
|
_stringOutputConnector.send(new StringSignal(encoded));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String get_id() {
|
|
return _id;
|
|
}
|
|
|
|
@Override
|
|
public InputConnector getConnectorWithId(String inputConnectorId) {
|
|
switch (inputConnectorId){
|
|
case ConvertToString.TOGGLE_INPUT_CONNECTOR_ID:
|
|
return _toggleInputConnector;
|
|
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getConnectorId(InputConnector inputConnector) {
|
|
if (inputConnector == _toggleInputConnector){
|
|
return ConvertToString.TOGGLE_INPUT_CONNECTOR_ID;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void resume() {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void pause() {
|
|
|
|
}
|
|
|
|
public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
|
|
String id = data.getString("id");
|
|
int left = data.getInt("left");
|
|
int top = data.getInt("top");
|
|
int right = data.getInt("right");
|
|
int bottom = data.getInt("bottom");
|
|
|
|
ConvertToString toggle = new ConvertToString(id, partGrid, left, top, right, bottom);
|
|
|
|
List<PartConnection> connections = new LinkedList<>();
|
|
|
|
JSONArray connectorOuts = data.getJSONArray("on_string_output_connector");
|
|
for (int i = 0; i < connectorOuts.length(); i++) {
|
|
connections.add(PartConnection.deserialize(
|
|
toggle._stringOutputConnector,
|
|
connectorOuts.getJSONObject(i)));
|
|
}
|
|
|
|
return new Tuple2<Part, List<PartConnection>>(toggle, connections);
|
|
}
|
|
|
|
@Override
|
|
public boolean containsPoint(int x, int y) {
|
|
return (x >= get_left()) && (x <= get_right())
|
|
&& (y >= get_top()) && (y <= get_bottom());
|
|
}
|
|
|
|
@Override
|
|
public Moveable getMoveable() {
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public void unlink() {
|
|
for (InputConnector input : getInputConnectors()) {
|
|
input.unlink();
|
|
}
|
|
}
|
|
|
|
public int getInputConnectorCenterX() {
|
|
return get_left();
|
|
}
|
|
|
|
private int getInputConnectRadius() {
|
|
return (get_right() - get_left()) / 2;
|
|
}
|
|
|
|
public int getInputConnectorCenterY() {
|
|
return (get_top() + get_bottom()) / 2;
|
|
}
|
|
|
|
private int getOutputConnectorCenterX() {
|
|
return _right;
|
|
}
|
|
|
|
private int getOutputConnectorCenterY() {
|
|
return (_top + _bottom) / 2;
|
|
}
|
|
|
|
private int getOutputConnectRadius() {
|
|
return (_right - _left) / 2;
|
|
}
|
|
|
|
|
|
public static PartInstantiator getInstantiator() {
|
|
final int halfSideSize = DEFAULT_SIDE_SIZE / 2;
|
|
return new PartInstantiator() {
|
|
@Override
|
|
protected Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center) {
|
|
return new ConvertToString(grid,
|
|
center.item1 - halfSideSize, center.item2 - halfSideSize,
|
|
center.item1 + halfSideSize, center.item2 + halfSideSize);
|
|
}
|
|
};
|
|
}
|
|
}
|