|
| 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 | + |
| 24 | +package org.catrobat.catroid.common.defaultprojectcreators; |
| 25 | + |
| 26 | +import android.content.Context; |
| 27 | +import android.graphics.BitmapFactory; |
| 28 | + |
| 29 | +import org.catrobat.catroid.R; |
| 30 | +import org.catrobat.catroid.common.LookData; |
| 31 | +import org.catrobat.catroid.content.Project; |
| 32 | +import org.catrobat.catroid.content.Scene; |
| 33 | +import org.catrobat.catroid.content.Script; |
| 34 | +import org.catrobat.catroid.content.Sprite; |
| 35 | +import org.catrobat.catroid.content.StartScript; |
| 36 | +import org.catrobat.catroid.content.bricks.MoveNStepsBrick; |
| 37 | +import org.catrobat.catroid.content.bricks.RepeatBrick; |
| 38 | +import org.catrobat.catroid.content.bricks.SetVariableBrick; |
| 39 | +import org.catrobat.catroid.content.bricks.TurnRightBrick; |
| 40 | +import org.catrobat.catroid.content.bricks.ZigZagStitchBrick; |
| 41 | +import org.catrobat.catroid.formulaeditor.Formula; |
| 42 | +import org.catrobat.catroid.formulaeditor.FormulaElement; |
| 43 | +import org.catrobat.catroid.formulaeditor.Operators; |
| 44 | +import org.catrobat.catroid.formulaeditor.UserVariable; |
| 45 | +import org.catrobat.catroid.io.ResourceImporter; |
| 46 | +import org.catrobat.catroid.io.XstreamSerializer; |
| 47 | +import org.catrobat.catroid.utils.ImageEditing; |
| 48 | + |
| 49 | +import java.io.File; |
| 50 | +import java.io.FileNotFoundException; |
| 51 | +import java.io.IOException; |
| 52 | + |
| 53 | +import static org.catrobat.catroid.common.Constants.DEFAULT_IMAGE_EXTENSION; |
| 54 | +import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME; |
| 55 | +import static org.catrobat.catroid.common.ScreenValues.SCREEN_HEIGHT; |
| 56 | +import static org.catrobat.catroid.common.ScreenValues.SCREEN_WIDTH; |
| 57 | +import static org.catrobat.catroid.stage.StageListener.SCREENSHOT_AUTOMATIC_FILE_NAME; |
| 58 | + |
| 59 | +public class DefaultExampleProject extends DefaultProjectCreator { |
| 60 | + public DefaultExampleProject() { |
| 61 | + defaultProjectNameResourceId = R.string.default_project_name; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Project createDefaultProject(String name, Context context, boolean landscapeMode) throws IOException { |
| 66 | + Project project = new Project(context, name, landscapeMode); |
| 67 | + |
| 68 | + if (project.getDirectory().exists()) { |
| 69 | + throw new IOException("Cannot create new project at " |
| 70 | + + project.getDirectory().getAbsolutePath() |
| 71 | + + ", directory already exists."); |
| 72 | + } |
| 73 | + |
| 74 | + XstreamSerializer.getInstance().saveProject(project); |
| 75 | + |
| 76 | + if (!project.getDirectory().isDirectory()) { |
| 77 | + throw new FileNotFoundException("Cannot create project at " + project.getDirectory().getAbsolutePath()); |
| 78 | + } |
| 79 | + |
| 80 | + int needleDrawableId; |
| 81 | + int backgroundDrawableId; |
| 82 | + int screenshotDrawableId; |
| 83 | + |
| 84 | + if (landscapeMode) { |
| 85 | + backgroundDrawableId = R.drawable.default_project_background_landscape; |
| 86 | + needleDrawableId = R.drawable.default_project_needle; |
| 87 | + screenshotDrawableId = R.drawable.default_project_screenshot_landscape; |
| 88 | + } else { |
| 89 | + backgroundDrawableId = R.drawable.default_project_background_portrait; |
| 90 | + needleDrawableId = R.drawable.default_project_needle; |
| 91 | + screenshotDrawableId = R.drawable.default_project_screenshot; |
| 92 | + } |
| 93 | + |
| 94 | + BitmapFactory.Options options = new BitmapFactory.Options(); |
| 95 | + options.inJustDecodeBounds = true; |
| 96 | + BitmapFactory.decodeResource(context.getResources(), backgroundDrawableId, options); |
| 97 | + |
| 98 | + backgroundImageScaleFactor = ImageEditing.calculateScaleFactor( |
| 99 | + options.outWidth, |
| 100 | + options.outHeight, |
| 101 | + SCREEN_WIDTH, |
| 102 | + SCREEN_HEIGHT); |
| 103 | + |
| 104 | + Scene scene = project.getDefaultScene(); |
| 105 | + |
| 106 | + File imageDir = new File(scene.getDirectory(), IMAGE_DIRECTORY_NAME); |
| 107 | + |
| 108 | + String imageFileName = "img" + DEFAULT_IMAGE_EXTENSION; |
| 109 | + |
| 110 | + File needleFile1 = ResourceImporter.createImageFileFromResourcesInDirectory(context.getResources(), |
| 111 | + needleDrawableId, |
| 112 | + imageDir, |
| 113 | + imageFileName, |
| 114 | + backgroundImageScaleFactor); |
| 115 | + |
| 116 | + ResourceImporter.createImageFileFromResourcesInDirectory(context.getResources(), |
| 117 | + screenshotDrawableId, |
| 118 | + scene.getDirectory(), |
| 119 | + SCREENSHOT_AUTOMATIC_FILE_NAME, |
| 120 | + 1); |
| 121 | + |
| 122 | + Sprite needle = new Sprite(context.getString(R.string.default_project_needle_name)); |
| 123 | + |
| 124 | + scene.addSprite(needle); |
| 125 | + |
| 126 | + needle.getLookList() |
| 127 | + .add(new LookData(context.getString(R.string.default_project_needle_name), needleFile1)); |
| 128 | + |
| 129 | + Script script = new StartScript(); |
| 130 | + |
| 131 | + UserVariable variableOuterLoop = new UserVariable(context.getString(R.string.default_project_outer_loop)); |
| 132 | + UserVariable variableInnerLoop = new UserVariable(context.getString(R.string.default_project_inner_loop)); |
| 133 | + |
| 134 | + needle.addUserVariable(variableInnerLoop); |
| 135 | + needle.addUserVariable(variableOuterLoop); |
| 136 | + |
| 137 | + script.addBrick(new SetVariableBrick(new Formula(8), variableInnerLoop)); |
| 138 | + script.addBrick(new SetVariableBrick(new Formula(8), variableOuterLoop)); |
| 139 | + script.addBrick(new ZigZagStitchBrick(new Formula(2), new Formula(10))); |
| 140 | + |
| 141 | + Formula repeatUntilFormulaOuterLoop = new Formula(1); |
| 142 | + repeatUntilFormulaOuterLoop.setRoot(new FormulaElement(FormulaElement.ElementType.USER_VARIABLE, variableOuterLoop.getName(), |
| 143 | + null)); |
| 144 | + |
| 145 | + RepeatBrick outerLoopRepeat = new RepeatBrick(repeatUntilFormulaOuterLoop); |
| 146 | + |
| 147 | + Formula repeatUntilFormulaInnerLoop = new Formula(1); |
| 148 | + repeatUntilFormulaInnerLoop.setRoot(new FormulaElement(FormulaElement.ElementType.USER_VARIABLE, |
| 149 | + variableInnerLoop.getName(), |
| 150 | + null)); |
| 151 | + |
| 152 | + RepeatBrick innerLoopRepeat = new RepeatBrick(repeatUntilFormulaInnerLoop); |
| 153 | + innerLoopRepeat.addBrick(new MoveNStepsBrick(new Formula(100))); |
| 154 | + |
| 155 | + FormulaElement innerLoopFormula = new FormulaElement(FormulaElement.ElementType.OPERATOR, |
| 156 | + Operators.DIVIDE.name(), null, |
| 157 | + new FormulaElement(FormulaElement.ElementType.NUMBER, String.valueOf(360), null), |
| 158 | + new FormulaElement(FormulaElement.ElementType.USER_VARIABLE, variableInnerLoop.getName(), |
| 159 | + null)); |
| 160 | + |
| 161 | + innerLoopRepeat.addBrick(new TurnRightBrick(new Formula(innerLoopFormula))); |
| 162 | + |
| 163 | + outerLoopRepeat.addBrick(innerLoopRepeat); |
| 164 | + FormulaElement outerLoopFormula = new FormulaElement(FormulaElement.ElementType.OPERATOR, |
| 165 | + Operators.DIVIDE.name(), null, |
| 166 | + new FormulaElement(FormulaElement.ElementType.NUMBER, String.valueOf(360), null), |
| 167 | + new FormulaElement(FormulaElement.ElementType.USER_VARIABLE, variableOuterLoop.getName(), |
| 168 | + null)); |
| 169 | + outerLoopRepeat.addBrick(new TurnRightBrick(new Formula(outerLoopFormula))); |
| 170 | + |
| 171 | + script.addBrick(outerLoopRepeat); |
| 172 | + needle.addScript(script); |
| 173 | + |
| 174 | + XstreamSerializer.getInstance().saveProject(project); |
| 175 | + return project; |
| 176 | + } |
| 177 | +} |
0 commit comments