|
| 1 | +/* |
| 2 | + * Catroid: An on-device visual programming system for Android devices |
| 3 | + * Copyright (C) 2010-2018 The Catrobat Team |
| 4 | + * (<http://developer.catrobat.org/credits>) |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * An additional term exception under section 7 of the GNU Affero |
| 12 | + * General Public License, version 3, is available at |
| 13 | + * http://developer.catrobat.org/license_additional_term |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package org.catrobat.catroid.content.actions |
| 24 | + |
| 25 | +import android.util.Log |
| 26 | +import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction |
| 27 | +import org.catrobat.catroid.content.Sprite |
| 28 | +import org.catrobat.catroid.formulaeditor.Formula |
| 29 | +import org.catrobat.catroid.formulaeditor.InterpretationException |
| 30 | +import org.catrobat.catroid.formulaeditor.UserList |
| 31 | + |
| 32 | +class DeleteItemOfUserListAction : TemporalAction() { |
| 33 | + var sprite: Sprite? = null |
| 34 | + var formulaIndexToDelete: Formula? = null |
| 35 | + var userList: UserList? = null |
| 36 | + |
| 37 | + override fun update(percent: Float) { |
| 38 | + val listSize = userList?.value?.size.takeUnless { it == 0 } ?: return |
| 39 | + |
| 40 | + val indexToDelete = try { |
| 41 | + formulaIndexToDelete?.interpretInteger(sprite)?.minus(1) ?: 0 |
| 42 | + } catch (exception: InterpretationException) { |
| 43 | + Log.e(javaClass.simpleName, "Interpreting formula as integer failed", exception) |
| 44 | + 0 |
| 45 | + } |
| 46 | + |
| 47 | + if (indexToDelete in 0 until listSize) { |
| 48 | + userList!!.value.removeAt(indexToDelete) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments