/* * Catroid: An on-device visual programming system for Android devices * Copyright (C) 2010-2016 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.bricks; import android.content.Context; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Spinner; import android.widget.TextView; import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; import org.catrobat.catroid.R; import org.catrobat.catroid.common.BrickValues; import org.catrobat.catroid.content.Scene; import org.catrobat.catroid.content.Sprite; import org.catrobat.catroid.formulaeditor.Formula; import org.catrobat.catroid.ui.fragment.FormulaEditorFragment; import java.util.List; public class LegoEv3MotorTurnAngleBrick extends FormulaBrick { private static final long serialVersionUID = 1L; private transient View prototypeView; private String motor; private transient Motor motorEnum; private transient TextView editSpeed; public static enum Motor { MOTOR_A, MOTOR_B, MOTOR_C, MOTOR_D, MOTOR_B_C, MOTOR_ALL } public LegoEv3MotorTurnAngleBrick(Motor motor, int degrees) { this.motorEnum = motor; this.motor = motorEnum.name(); initializeBrickFields(new Formula(degrees)); } public LegoEv3MotorTurnAngleBrick(Motor motor, Formula degreesFormula) { this.motorEnum = motor; this.motor = motorEnum.name(); initializeBrickFields(degreesFormula); } protected Object readResolve() { if (motor != null) { motorEnum = Motor.valueOf(motor); } return this; } private void initializeBrickFields(Formula degreesFormula) { addAllowedBrickField(BrickField.LEGO_EV3_DEGREES); setFormulaWithBrickField(BrickField.LEGO_EV3_DEGREES, degreesFormula); } @Override public int getRequiredResources() { return BLUETOOTH_LEGO_EV3 | getFormulaWithBrickField(BrickField.LEGO_EV3_DEGREES).getRequiredResources(); } @Override public View getPrototypeView(Context context) { prototypeView = View.inflate(context, R.layout.brick_ev3_motor_turn_angle, null); TextView textX = (TextView) prototypeView.findViewById(R.id.ev3_motor_turn_angle_edit_text); textX.setText(String.valueOf(BrickValues.LEGO_ANGLE)); Spinner legoSpinner = (Spinner) prototypeView.findViewById(R.id.lego_ev3_motor_turn_angle_spinner); legoSpinner.setFocusableInTouchMode(false); legoSpinner.setFocusable(false); legoSpinner.setEnabled(false); ArrayAdapter<CharSequence> motorAdapter = ArrayAdapter.createFromResource(context, R.array.ev3_motor_chooser, android.R.layout.simple_spinner_item); motorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); legoSpinner.setAdapter(motorAdapter); legoSpinner.setSelection(motorEnum.ordinal()); legoSpinner.setGravity(Gravity.CENTER); return prototypeView; } @Override public Brick clone() { return new LegoEv3MotorTurnAngleBrick(motorEnum, getFormulaWithBrickField(BrickField.LEGO_EV3_DEGREES).clone()); } @Override public void showFormulaEditorToEditFormula(View view) { FormulaEditorFragment.showFragment(view, this, BrickField.LEGO_EV3_DEGREES); } @Override public View getView(final Context context, int brickId, BaseAdapter baseAdapter) { if (animationState) { return view; } if (view == null) { alphaValue = 255; } view = View.inflate(context, R.layout.brick_ev3_motor_turn_angle, null); view = BrickViewProvider.setAlphaOnView(view, alphaValue); setCheckboxView(R.id.brick_ev3_motor_turn_checkbox); editSpeed = (TextView) view.findViewById(R.id.ev3_motor_turn_angle_edit_text); getFormulaWithBrickField(BrickField.LEGO_EV3_DEGREES).setTextFieldId(R.id.ev3_motor_turn_angle_edit_text); getFormulaWithBrickField(BrickField.LEGO_EV3_DEGREES).refreshTextField(view); editSpeed.setOnClickListener(this); ArrayAdapter<CharSequence> motorAdapter = ArrayAdapter.createFromResource(context, R.array.ev3_motor_chooser, android.R.layout.simple_spinner_item); motorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner motorSpinner = (Spinner) view.findViewById(R.id.lego_ev3_motor_turn_angle_spinner); motorSpinner.setAdapter(motorAdapter); motorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { motorEnum = Motor.values()[position]; motor = motorEnum.name(); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); motorSpinner.setSelection(motorEnum.ordinal()); motorSpinner.setGravity(Gravity.CENTER); return view; } @Override public List<SequenceAction> addActionToSequence(Sprite sprite, SequenceAction sequence) { sequence.addAction(sprite.getActionFactory().createLegoEv3MotorTurnAngleAction(sprite, motorEnum, getFormulaWithBrickField(BrickField.LEGO_EV3_DEGREES))); return null; } @Override public void updateReferenceAfterMerge(Scene into, Scene from) { } }