drag button wizard step
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
package org.solovyev.android.calculator.wizard;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.view.drag.DirectionDragButton;
|
||||
import org.solovyev.android.view.drag.DragButton;
|
||||
import org.solovyev.android.view.drag.DragDirection;
|
||||
import org.solovyev.android.view.drag.SimpleOnDragListener;
|
||||
import org.solovyev.common.math.Point2d;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockFragment;
|
||||
|
||||
public class DragButtonWizardStep extends SherlockFragment {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final String ACTION = "action";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
private DirectionDragButton dragButton;
|
||||
|
||||
@Nullable
|
||||
private TextView actionTextView;
|
||||
|
||||
private DragButtonAction action = DragButtonAction.center;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
action = (DragButtonAction) savedInstanceState.getSerializable(ACTION);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.cpp_wizard_step_drag_button, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View root, Bundle savedInstanceState) {
|
||||
super.onViewCreated(root, savedInstanceState);
|
||||
|
||||
dragButton = (DirectionDragButton) root.findViewById(R.id.wizard_dragbutton);
|
||||
dragButton.setOnClickListener(new DragButtonOnClickListener());
|
||||
dragButton.setOnDragListener(new SimpleOnDragListener(new DragButtonProcessor(), SimpleOnDragListener.getDefaultPreferences(getActivity())));
|
||||
actionTextView = (TextView) root.findViewById(R.id.wizard_dragbutton_action_textview);
|
||||
|
||||
actionTextView.setText(action.actionTextResId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
outState.putSerializable(ACTION, action);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC/INNER
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static enum DragButtonAction {
|
||||
center(R.string.cpp_wizard_dragbutton_action_center, null),
|
||||
up(R.string.cpp_wizard_dragbutton_action_up, DragDirection.up),
|
||||
left(R.string.cpp_wizard_dragbutton_action_left, DragDirection.left),
|
||||
down(R.string.cpp_wizard_dragbutton_action_down, DragDirection.down);
|
||||
|
||||
private final int actionTextResId;
|
||||
|
||||
@Nullable
|
||||
private final DragDirection dragDirection;
|
||||
|
||||
DragButtonAction(int actionTextResId, @Nullable DragDirection dragDirection) {
|
||||
this.actionTextResId = actionTextResId;
|
||||
this.dragDirection = dragDirection;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
DragButtonAction getNextAction() {
|
||||
final DragButtonAction[] values = values();
|
||||
final int position = Arrays.binarySearch(values, this);
|
||||
if (position < values.length - 1) {
|
||||
return values[position + 1];
|
||||
} else {
|
||||
return values[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DragButtonOnClickListener implements View.OnClickListener {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(action == DragButtonAction.center) {
|
||||
setNextAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setNextAction() {
|
||||
setAction(action.getNextAction());
|
||||
}
|
||||
|
||||
private class DragButtonProcessor implements SimpleOnDragListener.DragProcessor {
|
||||
@Override
|
||||
public boolean processDragEvent(@Nonnull DragDirection dragDirection,
|
||||
@Nonnull DragButton dragButton,
|
||||
@Nonnull Point2d startPoint2d,
|
||||
@Nonnull MotionEvent motionEvent) {
|
||||
if(action.dragDirection == dragDirection) {
|
||||
setNextAction();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void setAction(DragButtonAction action) {
|
||||
if (this.action != action) {
|
||||
this.action = action;
|
||||
if (actionTextView != null) {
|
||||
actionTextView.setText(this.action.actionTextResId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,7 +9,6 @@ import android.support.v4.app.Fragment;
|
||||
import org.solovyev.android.App;
|
||||
import org.solovyev.android.Views;
|
||||
import org.solovyev.android.calculator.CalculatorApplication;
|
||||
import org.solovyev.android.calculator.CalculatorLocator;
|
||||
import org.solovyev.android.calculator.CalculatorPreferences;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -133,6 +132,24 @@ enum WizardStep {
|
||||
bundle.putSerializable(ONSCREEN_CALCULATOR_ENABLED, CalculatorPreferences.OnscreenCalculator.showAppIcon.getPreference(preferences));
|
||||
return bundle;
|
||||
}
|
||||
},
|
||||
|
||||
drag_button_step(DragButtonWizardStep.class) {
|
||||
@Override
|
||||
boolean onNext(@Nonnull Fragment fragment) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean onPrev(@Nonnull Fragment fragment) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
Bundle getFragmentArgs() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@Nonnull
|
||||
|
Reference in New Issue
Block a user