Skip to content

Commit

Permalink
Merge branch 'surinder83singh-juggaar'
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Dec 12, 2016
2 parents cad7935 + c108160 commit 189c173
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public abstract class AbstractBlocklyActivity extends AppCompatActivity {
protected WorkspaceFragment mWorkspaceFragment;
protected ToolboxFragment mToolboxFragment;
protected TrashFragment mTrashFragment;
protected ZoomBehavior mZoomBehavior;

// These two may be null if {@link #onCreateAppNavigationDrawer} returns null.
protected View mNavigationDrawer;
Expand Down Expand Up @@ -322,6 +323,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

mWorkspaceHelper = new WorkspaceHelper(this);
mZoomBehavior = new ZoomBehavior(this);
mBlockViewFactory = onCreateBlockViewFactory(mWorkspaceHelper);

BlocklyController.Builder builder = new BlocklyController.Builder(this)
Expand All @@ -336,22 +338,12 @@ protected void onCreate(Bundle savedInstanceState) {
.setToolboxFragment(mToolboxFragment, mDrawerLayout);
mController = builder.build();

TypedArray a = getTheme().obtainStyledAttributes(
null,
R.styleable.BlocklyWorkspaceTheme,
0, 0);
boolean showZoomButtons = true;
try {
showZoomButtons = a.getBoolean(R.styleable.BlocklyWorkspaceTheme_showZoomButtons, true);
} finally {
a.recycle();
}

onConfigureTrashIcon();
onConfigureZoomInButton();
onConfigureZoomOutButton();
onConfigureCenterViewButton();
setZoomButtonVisibility(showZoomButtons);
onConfigureZoomBehavior();
mWorkspaceFragment.setZoomBehavior(mZoomBehavior);

boolean loadedPriorInstance = checkAllowRestoreBlocklyState(savedInstanceState)
&& mController.onRestoreSnapshot(savedInstanceState);
Expand Down Expand Up @@ -702,16 +694,16 @@ public void onClick(View v) {
* {@link R.id#blockly_zoom_out_button}
* from the view hierarchy according
* <p/>
* @param visible
*/
protected void setZoomButtonVisibility(boolean visible){
protected void onConfigureZoomBehavior(){
View button = findViewById(R.id.blockly_zoom_in_button);
int buttonVisibility = mZoomBehavior.isButtonEnabled()? View.VISIBLE : View.GONE;
if (button != null) {
button.setVisibility(visible ? View.VISIBLE : View.GONE);
button.setVisibility(buttonVisibility);
}
button = findViewById(R.id.blockly_zoom_out_button);
if (button != null) {
button.setVisibility(visible ? View.VISIBLE : View.GONE);
button.setVisibility(buttonVisibility);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

package com.google.blockly.android;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -45,54 +42,17 @@
* android:id="@+id/blockly_workspace"
* android:layout_width="match_parent"
* android:layout_height="match_parent"
* <b>blockly:scrollable</b>="true"
* /&gt;
* </pre></blockquote>
*/
public class WorkspaceFragment extends Fragment {
private static final String TAG = "WorkspaceFragment";

public static final boolean DEFAULT_SCROLLABLE = true;
public static final boolean DEFAULT_SCALABLE = true;

public static final String ARG_SCROLLABLE = "WorkspaceFragment_scrollable";
public static final String ARG_SCALABLE = "WorkspaceFragment_scalable";

private BlocklyController mController;
private Workspace mWorkspace;
private VirtualWorkspaceView mVirtualWorkspaceView;
private WorkspaceView mWorkspaceView;

private boolean mScrollable = true;
private boolean mScalable = true;

@Override
public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
super.onInflate(context, attrs, savedInstanceState);

TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.WorkspaceFragment,
0, 0);
try {
//noinspection ResourceType
mScrollable =
a.getBoolean(R.styleable.WorkspaceFragment_scrollable, DEFAULT_SCROLLABLE);
mScalable =
a.getBoolean(R.styleable.WorkspaceFragment_scalable, DEFAULT_SCALABLE);
} finally {
a.recycle();
}

// Store values in arguments, so fragment resume works (no inflation during resume).
Bundle args = getArguments();
if (args == null) {
setArguments(args = new Bundle());
}
args.putBoolean(ARG_SCROLLABLE, mScrollable);
args.putBoolean(ARG_SCALABLE, mScalable);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -102,10 +62,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mVirtualWorkspaceView =
(VirtualWorkspaceView) rootView.findViewById(R.id.virtual_workspace);
mWorkspaceView = (WorkspaceView) rootView.findViewById(R.id.workspace);

mVirtualWorkspaceView.setScrollable(mScrollable);
mVirtualWorkspaceView.setScalable(mScalable);

return rootView;
}

Expand All @@ -125,14 +81,9 @@ public void setController(BlocklyController controller) {
mController.initWorkspaceView(mWorkspaceView);
}

public boolean getScrollable() {
return mScrollable;
}

public void setScrollable(boolean scrollable) {
mScrollable = scrollable;
public void setZoomBehavior(ZoomBehavior zoomBehavior){
if (mVirtualWorkspaceView != null) {
mVirtualWorkspaceView.setScrollable(mScrollable);
mVirtualWorkspaceView.setZoomBehavior(zoomBehavior);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.google.blockly.android;

import android.content.Context;
import android.content.res.TypedArray;

/**
* Created by Surinder Singh ([email protected]) on 09/12/16.
*/
public final class ZoomBehavior {

//no scrollbar, no zoom, no buttons
public static final int BEHAVIOR_FIXED = 1;
//only scrollable, no buttons, no zoom
public static final int BEHAVIOR_SCROLL_ONLY = 2;
//scrollable, zoomable with buttons, zoom-in/out buttons
public static final int BEHAVIOR_BUTTONS_ONLY = 3;
//scrollable, zoomable, no buttons
public static final int BEHAVIOR_ZOOM_ONLY = 4;
//scrollable, zoomable, zoom-in/out buttons
public static final int BEHAVIOR_ZOOM_AND_BUTTONS = 5;

public static final int DEFAULT_BEHAVIOR = BEHAVIOR_ZOOM_AND_BUTTONS;

private int mBehavior = DEFAULT_BEHAVIOR;


public ZoomBehavior(Context context) {
TypedArray a = context.getTheme().obtainStyledAttributes(
null,
R.styleable.BlocklyWorkspaceTheme,
0, 0);
try {
mBehavior =
a.getInt(R.styleable.BlocklyWorkspaceTheme_zoomBehavior, DEFAULT_BEHAVIOR);
} finally {
a.recycle();
}
}

/**
* @return true if zoom-in/out buttons are enabled
*/
public boolean isButtonEnabled(){
return (mBehavior == BEHAVIOR_BUTTONS_ONLY || mBehavior == BEHAVIOR_ZOOM_AND_BUTTONS);
}

/**
* @return true if workspace is scrollable
*/
public boolean isScrollEnabled(){
return mBehavior > BEHAVIOR_FIXED;
}

/**
* @return true if workspace scalable using touch/pinch events
*/
public boolean isPinchZoomEnabled(){
return mBehavior > BEHAVIOR_BUTTONS_ONLY;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ protected FieldView buildFieldView(Field field) {
case Field.TYPE_ANGLE: {
BasicFieldAngleView fieldAngleView = new BasicFieldAngleView(mContext);
fieldAngleView.setField((FieldAngle) field);
fieldAngleView.setWorkspaceHelper(mHelper);
return fieldAngleView;
}
case Field.TYPE_CHECKBOX: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.view.inputmethod.InputMethodManager;

import com.google.blockly.android.R;
import com.google.blockly.android.WorkspaceFragment;
import com.google.blockly.android.ZoomBehavior;

/**
* Virtual view of a {@link WorkspaceView}.
Expand All @@ -52,7 +54,7 @@ public class VirtualWorkspaceView extends NonPropagatingViewGroup {
private static final int INIT_ZOOM_SCALES_INDEX = 2;

protected boolean mScrollable = true;
protected boolean mScalable;
protected boolean mScalable = true;

private final ViewPoint mPanningStart = new ViewPoint();

Expand Down Expand Up @@ -107,6 +109,7 @@ public void onFinishInflate() {
setHorizontalScrollBarEnabled(mScrollable);
setVerticalScrollBarEnabled(mScrollable);

mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureListener());
mTapGestureDetector = new GestureDetector(getContext(), new TapGestureListener());
mGridRenderer.updateGridBitmap(mViewScale);
mImeManager = (InputMethodManager) getContext()
Expand Down Expand Up @@ -159,13 +162,18 @@ public boolean isScrollable() {
return mScrollable;
}

public void setZoomBehavior(ZoomBehavior zoomBehavior){
setScrollable(zoomBehavior.isScrollEnabled());
setScalable(zoomBehavior.isPinchZoomEnabled());
}

/**
* Configures whether the user can scroll the workspace by dragging. If scrolling is disabled,
* the workspace will reset to 0,0 in the top right hand corner.
*
* @param scrollable Allow scrolling if true. Otherwise, disable it.
*/
public void setScrollable(boolean scrollable) {
protected void setScrollable(boolean scrollable) {
if (scrollable == mScrollable) {
return;
}
Expand All @@ -182,15 +190,12 @@ public void setScrollable(boolean scrollable) {
*
* @param scalable Allow scalability if true. Otherwise, disable it.
*/
public void setScalable(boolean scalable){
protected void setScalable(boolean scalable){
if(mScalable == scalable){
return;
}
mScalable = scalable;
if(scalable) {
mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureListener());
}else{
mScaleGestureDetector = null;
if(!scalable) {
resetView();
}
}
Expand Down Expand Up @@ -237,7 +242,7 @@ public boolean dispatchTouchEvent(MotionEvent event) {

@Override
public boolean onTouchEvent(MotionEvent event) {
if(mScaleGestureDetector != null) {
if(mScalable && mScaleGestureDetector != null) {
mScaleGestureDetector.onTouchEvent(event);
if (mScaleGestureDetector.isInProgress()) {
// If the scale gesture detector is handling a scale-and-pan gesture, then exit here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.util.AttributeSet;
import android.widget.TextView;

import com.google.blockly.android.ui.WorkspaceHelper;
import com.google.blockly.model.Field;
import com.google.blockly.model.FieldAngle;

Expand All @@ -29,21 +30,16 @@
*/
public class BasicFieldAngleView extends TextView implements FieldView {
private static final char DEGREE_SYMBOL = '\u00B0';
protected WorkspaceHelper mHelper;

protected Field.Observer mFieldObserver = new Field.Observer() {
@Override
public void onValueChanged(Field angleField, String oldValue, String newValue) {
assert (angleField == mAngleField);

CharSequence curDisplayText = getText();
int len = curDisplayText.length();

// Trim the degree symbol
if (len > 0 && curDisplayText.charAt(len - 1) == DEGREE_SYMBOL) {
curDisplayText = curDisplayText.subSequence(0, len - 1);
}
String curDisplayText = removeSymbol(getText().toString());
if (!newValue.contentEquals(curDisplayText)) {
setText(newValue + DEGREE_SYMBOL);
setValue(newValue);
}
}
};
Expand All @@ -65,6 +61,41 @@ public BasicFieldAngleView(Context context, AttributeSet attrs, int defStyleAttr
initTextWatcher();
}

public void setWorkspaceHelper(WorkspaceHelper helper){
mHelper = helper;
setValue(getCleanValue());
}

public String getCleanValue(){
if(mAngleField!=null){
return Float.toString(mAngleField.getAngle());
}
return removeSymbol(getText().toString());
}

private String removeSymbol(String string){
int len = string.length();

// Trim the degree symbol
if (len > 0){
if(string.charAt(len - 1) == DEGREE_SYMBOL) {
string = string.substring(0, len - 1);
}else if(string.charAt(0) == DEGREE_SYMBOL){
string = string.substring(1);
}
}

return string;
}

private void setValue(String value){
if(mHelper!=null && mHelper.useRtl()) {
setText(DEGREE_SYMBOL + value);
}else{
setText(value + DEGREE_SYMBOL);
}
}

private void initTextWatcher() {
addTextChangedListener(new TextWatcher() {
@Override
Expand Down Expand Up @@ -96,8 +127,7 @@ public void setField(Field field) {
}
mAngleField = angleField;
if (mAngleField != null) {
// TODO(#438): Degree symbol on the left in RTL modes
setText(Float.toString(mAngleField.getAngle()) + DEGREE_SYMBOL);
setValue(Float.toString(mAngleField.getAngle()));
mAngleField.registerObserver(mFieldObserver);
} else {
setText("");
Expand Down
Loading

0 comments on commit 189c173

Please sign in to comment.