From 8574863dfc74a790cef7ac62f0a5882291bdc806 Mon Sep 17 00:00:00 2001 From: Daniel Hevesy Date: Wed, 26 Apr 2023 15:57:51 +0200 Subject: [PATCH] CATROID-1551 REFACTOR SetVariableAction to Kotlin Converted the java file SetVariableAction.java to Kotlin. --- .../content/actions/SetVariableAction.java | 102 ------------------ .../content/actions/SetVariableAction.kt | 91 ++++++++++++++++ 2 files changed, 91 insertions(+), 102 deletions(-) delete mode 100644 catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.java create mode 100644 catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.kt diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.java deleted file mode 100644 index 1f3ad23b549..00000000000 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team - * () - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * An additional term exception under section 7 of the GNU Affero - * General Public License, version 3, is available at - * http://developer.catrobat.org/license_additional_term - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.catrobat.catroid.content.actions; - -import android.util.Log; - -import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; - -import org.catrobat.catroid.ProjectManager; -import org.catrobat.catroid.common.ServiceProvider; -import org.catrobat.catroid.content.Scope; -import org.catrobat.catroid.devices.multiplayer.MultiplayerInterface; -import org.catrobat.catroid.formulaeditor.Formula; -import org.catrobat.catroid.formulaeditor.FormulaElement; -import org.catrobat.catroid.formulaeditor.UserVariable; - -import static org.catrobat.catroid.bluetooth.base.BluetoothDevice.MULTIPLAYER; -import static org.catrobat.catroid.common.CatroidService.BLUETOOTH_DEVICE_SERVICE; -import static org.catrobat.catroid.common.Constants.TEXT_FROM_CAMERA_SENSOR_HASHCODE; -import static org.catrobat.catroid.formulaeditor.common.Conversions.convertArgumentToDouble; - -public class SetVariableAction extends TemporalAction { - - private Scope scope; - private Formula changeVariable; - private UserVariable userVariable; - - @Override - protected void update(float percent) { - if (userVariable == null) { - return; - } - Object value = changeVariable == null ? Double.valueOf(0d) - : changeVariable.interpretObject(scope); - - if (changeVariable != null && changeVariable.getRoot().isBoolean(scope)) { - value = (Double) value != 0; - } - - boolean isFirstLevelStringTree = false; - if (changeVariable != null && changeVariable.getRoot().getElementType() == FormulaElement.ElementType.STRING) { - isFirstLevelStringTree = true; - } - - try { - if (!isFirstLevelStringTree && value instanceof String && userVariable.hashCode() != TEXT_FROM_CAMERA_SENSOR_HASHCODE - && convertArgumentToDouble(value) != null) { - value = convertArgumentToDouble(value); - } - } catch (NumberFormatException numberFormatException) { - Log.d(getClass().getSimpleName(), "Couldn't parse String", numberFormatException); - } - userVariable.setValue(value); - - UserVariable multiplayerVariable = ProjectManager.getInstance().getCurrentProject().getMultiplayerVariable(userVariable.getName()); - if (multiplayerVariable != null) { - MultiplayerInterface multiplayerDevice = getMultiplayerDevice(); - if (multiplayerDevice != null) { - multiplayerDevice.sendChangedMultiplayerVariables(userVariable); - } - } - } - - public MultiplayerInterface getMultiplayerDevice() { - return ServiceProvider.getService(BLUETOOTH_DEVICE_SERVICE).getDevice(MULTIPLAYER); - } - - public void setUserVariable(UserVariable userVariable) { - if (userVariable == null) { - return; - } - this.userVariable = userVariable; - } - - public void setChangeVariable(Formula changeVariable) { - this.changeVariable = changeVariable; - } - - public void setScope(Scope scope) { - this.scope = scope; - } -} diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.kt b/catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.kt new file mode 100644 index 00000000000..ec0619e1895 --- /dev/null +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.kt @@ -0,0 +1,91 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2022 The Catrobat Team + * () + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * An additional term exception under section 7 of the GNU Affero + * General Public License, version 3, is available at + * http://developer.catrobat.org/license_additional_term + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.catrobat.catroid.content.actions + +import android.util.Log +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction +import org.catrobat.catroid.ProjectManager +import org.catrobat.catroid.bluetooth.base.BluetoothDevice +import org.catrobat.catroid.common.CatroidService +import org.catrobat.catroid.common.Constants +import org.catrobat.catroid.common.ServiceProvider +import org.catrobat.catroid.content.Scope +import org.catrobat.catroid.devices.multiplayer.MultiplayerInterface +import org.catrobat.catroid.formulaeditor.Formula +import org.catrobat.catroid.formulaeditor.FormulaElement +import org.catrobat.catroid.formulaeditor.UserVariable +import org.catrobat.catroid.formulaeditor.common.Conversions.convertArgumentToDouble + +class SetVariableAction : TemporalAction() { + private var scope: Scope? = null + private var changeVariable: Formula? = null + private var userVariable: UserVariable? = null + override fun update(percent: Float) { + if (userVariable == null) { + return + } + + var value = changeVariable?.interpretObject(scope) ?: java.lang.Double.valueOf(0.0) + if (changeVariable != null && changeVariable?.root?.isBoolean(scope) == true) { + value = value as Double != 0.0 + } + + val isFirstLevelStringTree = changeVariable != null && + changeVariable?.root?.elementType == FormulaElement.ElementType.STRING + val isConvertible = !isFirstLevelStringTree && + userVariable.hashCode() != Constants.TEXT_FROM_CAMERA_SENSOR_HASHCODE && + checkValueForDoubleConversion(value) + try { + value = if (isConvertible) convertArgumentToDouble(value) else value + } catch (numberFormatException: NumberFormatException) { + Log.d(javaClass.simpleName, "Couldn't parse String", numberFormatException) + } + + userVariable?.value = value + handleMultiplayerVariable() + } + + val multiplayerDevice: MultiplayerInterface? + get() = ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE).getDevice( + BluetoothDevice.MULTIPLAYER + ) + + private fun handleMultiplayerVariable() { + ProjectManager.getInstance().currentProject.getMultiplayerVariable(userVariable?.name) ?: return + multiplayerDevice?.sendChangedMultiplayerVariables(userVariable) + } + private fun checkValueForDoubleConversion(value: Any?): Boolean = + value is String && convertArgumentToDouble(value) != null + + fun setUserVariable(userVariable: UserVariable?) { + this.userVariable = userVariable ?: return + } + + fun setChangeVariable(changeVariable: Formula?) { + this.changeVariable = changeVariable + } + + fun setScope(scope: Scope?) { + this.scope = scope + } +}