Skip to content

Add Undo/Redo fucntions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public abstract class ProgrammingActivityBase extends AppCompatActivity {
private final String TAG = "NxtProgrammingActivity";
private List<Button> mAddBlockButtons;
private Button mExecButton;
private UndoAndRedoButtonsManager mButtonsManager;
private ProgrammingSpaceManager mSpaceManager;
private boolean mIsConnected = false;

Expand Down Expand Up @@ -108,8 +109,11 @@ protected void onPostCreate(Bundle savedInstanceState) {
protected abstract Intent getIntentToExecute();

private void findViews() {
mButtonsManager = new UndoAndRedoButtonsManager((Button) findViewById(R.id.programming_undoButton),
(Button) findViewById(R.id.programming_redoButton));
mSpaceManager = new ProgrammingSpaceManager(this,
(BlockSpaceLayout) findViewById(R.id.programming_placingBlockSpaceLayout));
(BlockSpaceLayout) findViewById(R.id.programming_placingBlockSpaceLayout),
mButtonsManager);
mAddBlockButtons = new ArrayList<>(NKINDS);
mAddBlockButtons.add((Button) findViewById(R.id.programming_sequenceButton));
mAddBlockButtons.add((Button) findViewById(R.id.programming_repetitionButton));
Expand Down Expand Up @@ -160,6 +164,7 @@ protected void onActivityResult(
String blockName = data.getStringExtra("block_name");
ArrayList<BlockBase> blocks = BlockFactory.createBlocks(howToMake, blockName);
mSpaceManager.addBlocks(blocks);
mButtonsManager.checkButtonsWorkability();
}
break;

Expand Down Expand Up @@ -250,6 +255,7 @@ private void onDeleteAllBlocks() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSpaceManager.deleteAllBlocks();
mButtonsManager.checkButtonsWorkability();
}
})
.setNegativeButton(R.string.cancel, null)
Expand Down Expand Up @@ -348,4 +354,44 @@ public String getInputtedProgramName() {
return mEditText.getText().toString();
}
}

/**
* The manager of Undo and Redo buttons.
* This check the workability of buttons and
* enable or disable them.
*
* @author <a href="mailto:tatsuyaw0c@gmail.com">Tatsuya Iwanari</a>
*/
public class UndoAndRedoButtonsManager {
private Button mUndoButton;
private Button mRedoButton;

public UndoAndRedoButtonsManager(Button undoButton, Button redoButton) {
mUndoButton = undoButton;
mRedoButton = redoButton;

mUndoButton.setEnabled(false);
mUndoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSpaceManager.undo();
checkButtonsWorkability();
}
});

mRedoButton.setEnabled(false);
mRedoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSpaceManager.redo();
checkButtonsWorkability();
}
});
}

public void checkButtonsWorkability() {
mUndoButton.setEnabled(mSpaceManager.canUndo());
mRedoButton.setEnabled(mSpaceManager.canRedo());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.programming.visual.event;


import com.pileproject.drive.programming.visual.block.BlockBase;
import com.pileproject.drive.programming.visual.layout.BlockSpaceLayout;

/**
* EventBase for the addition of block
*
* @author <a href="mailto:tatsuyaw0c@gmail.com">Tatsuya Iwanari</a>
* @version 1.0 4-June-2013
*/
public class AddEvent extends EventBase {

public AddEvent(int elementCount, int index) {
super(elementCount, index);
}

@Override
public EventBase undo(BlockSpaceLayout layout, int elementCount) {
// Get block name
String blockName = ((BlockBase) layout.getChildAt(mIndex)).getClass().getName();
layout.removeViewAt(mIndex); // Remove it

// Create a new DeleteNumDiff for Redo
EventBase diffForRedo = new DeleteEvent(elementCount, mIndex, blockName);
return diffForRedo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.programming.visual.event;

import com.pileproject.drive.programming.visual.block.BlockBase;
import com.pileproject.drive.programming.visual.block.NumTextHolder;
import com.pileproject.drive.programming.visual.layout.BlockSpaceLayout;

/**
* EventBase for the number change of TextView attached to block
*
* @author <a href="mailto:tatsuyaw0c@gmail.com">Tatsuya Iwanari</a>
* @version 1.0 4-June-2013
*/
public class ChangeNumberEvent extends EventBase {
private int mOldNum;

public ChangeNumberEvent(int elementCount, int index, int oldNum) {
super(elementCount, index);

mOldNum = oldNum;
}

@Override
public EventBase undo(BlockSpaceLayout layout, int elementCount) {
BlockBase block = (BlockBase) layout.getChildAt(mIndex);

// Get current number of TextView
int curNum = ((NumTextHolder) block).getNum();
((NumTextHolder) block).setNum(mOldNum); // Set old number to TextView

// Create a new ChangeNumberEvent for Redo
EventBase diffForRedo = new ChangeNumberEvent(elementCount, mIndex, curNum);

return diffForRedo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.programming.visual.event;

import android.view.ViewGroup;

import com.pileproject.drive.programming.visual.block.BlockBase;
import com.pileproject.drive.programming.visual.block.BlockFactory;
import com.pileproject.drive.programming.visual.layout.BlockSpaceLayout;

/**
* EventBase for the delete of a block
*
* @author <a href="mailto:tatsuyaw0c@gmail.com">Tatsuya Iwanari</a>
* @version 1.0 4-June-2013
*/
public class DeleteEvent extends EventBase {
private String mBlockName;

public DeleteEvent(int elementCount, int index, String name) {
super(elementCount, index);

mBlockName = name;
}

@Override
public EventBase undo(BlockSpaceLayout layout, int elementCount) {
// Recreate block
BlockBase block = BlockFactory.createBlocks(BlockFactory.UNDO, mBlockName).get(0);

// Attach it to layout
layout.addView(block, mIndex,
new BlockSpaceLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

// Create a new AddNumDiff for Redo
EventBase diffForRedo = new AddEvent(elementCount, mIndex);
return diffForRedo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.programming.visual.event;


import com.pileproject.drive.programming.visual.layout.BlockSpaceLayout;

/**
* Base class of EventBase for undo and redo
*
* @author <a href="mailto:tatsuyaw0c@gmail.com">Tatsuya Iwanari</a>
* @version 1.0 4-June-2013
*/
public abstract class EventBase {
protected final int mElementCount;
protected final int mIndex;

protected EventBase(int elementCount, int index) {
mElementCount = elementCount;
mIndex = index;
}

public int getElementCount() {
return mElementCount;
}

public int getIndex() {
return mIndex;
}

/**
* Undo method
*
* @param layout Layout that has blocks
* @param elementCount Number that shows how many UNDOs should be done at once
* @return
*/
public abstract EventBase undo(BlockSpaceLayout layout, int elementCount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2011-2015 PILE Project, Inc. <dev@pileproject.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pileproject.drive.programming.visual.event;

import android.view.View;

import com.pileproject.drive.programming.visual.layout.BlockSpaceLayout;


/**
* EventBase for the move of block
*
* @author <a href="mailto:tatsuyaw0c@gmail.com">Tatsuya Iwanari</a>
* @version 1.0 4-June-2013
*/
public class MoveEvent extends EventBase {
private int mOldX, mOldY;

public MoveEvent(int elementCount, int index, int oldX, int oldY) {
super(elementCount, index);
mOldX = oldX;
mOldY = oldY;
}

@Override
public EventBase undo(BlockSpaceLayout layout, int elementCount) {
View view = layout.getChildAt(mIndex); // Get view

// Get current position of View and move
int curX = view.getLeft();
int curY = view.getTop();
view.layout(mOldX, mOldY, mOldX + view.getWidth(), mOldY + view.getHeight());

// Create New MoveEvent for Redo
EventBase diffForRedo = new MoveEvent(elementCount, mIndex, curX, curY);

return diffForRedo;
}
}
Loading