/**
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2014 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.html5player.client.formulaeditor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserVariablesContainer {
private List<UserVariable> projectVariables;
private Map<String, List<UserVariable>> spriteVariables;
public UserVariablesContainer() {
projectVariables = new ArrayList<UserVariable>(); //global variables
spriteVariables = new HashMap<String, List<UserVariable>>(); //local variables
}
public UserVariable getUserVariable(String userVariableName, String spriteName) {
UserVariable var;
//search in local variables
var = findUserVariable(userVariableName, getOrCreateVariableListForSprite(spriteName));
if (var == null) {
//search in global variables
var = findUserVariable(userVariableName, projectVariables);
}
return var;
}
public void addSpriteUserVariable(String spriteName, String userVariableName, Double userVariableValue) {
UserVariable userVariableToAdd = new UserVariable(userVariableName, userVariableValue);
List<UserVariable> varList = getOrCreateVariableListForSprite(spriteName);
varList.add(userVariableToAdd);
}
public void addProjectUserVariable(String userVariableName, Double userVariableValue) {
UserVariable userVariableToAdd = new UserVariable(userVariableName, userVariableValue);
projectVariables.add(userVariableToAdd);
}
public void deleteUserVariableByName(String spriteName, String userVariableName) {
UserVariable variableToDelete;
List<UserVariable> spriteVariables = getOrCreateVariableListForSprite(spriteName);
variableToDelete = findUserVariable(userVariableName, spriteVariables);
if (variableToDelete != null) {
spriteVariables.remove(variableToDelete);
}
variableToDelete = findUserVariable(userVariableName, projectVariables);
if (variableToDelete != null) {
projectVariables.remove(variableToDelete);
}
}
public List<UserVariable> getOrCreateVariableListForSprite(String spriteName) {
List<UserVariable> vars = spriteVariables.get(spriteName);
if (vars == null) {
vars = new ArrayList<UserVariable>();
spriteVariables.put(spriteName, vars);
}
return vars;
}
private UserVariable findUserVariable(String name, List<UserVariable> variables) {
if (variables == null) {
return null;
}
for (UserVariable variable : variables) {
if (variable.getName().equals(name)) {
return variable;
}
}
return null;
}
public void printProjectVariables() {
for (UserVariable uv : projectVariables) {
System.out.println("UVAR: " + uv.getName() + ": " + uv.getValue());
}
}
}