-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CATROID-1551 REFACTOR SetVariableAction to Kotlin
Converted the java file SetVariableAction.java to Kotlin.
- Loading branch information
1 parent
94e9755
commit 8574863
Showing
2 changed files
with
91 additions
and
102 deletions.
There are no files selected for viewing
102 changes: 0 additions & 102 deletions
102
catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.java
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
catroid/src/main/java/org/catrobat/catroid/content/actions/SetVariableAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Catroid: An on-device visual programming system for Android devices | ||
* Copyright (C) 2010-2022 The Catrobat Team | ||
* (<http://developer.catrobat.org/credits>) | ||
* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
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 | ||
} | ||
} |