Android Open Source - Sketcher History Helper






From Project

Back to project page Sketcher.

License

The source code is released under:

Apache License

If you think the Android project Sketcher listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.sketcher;
//from ww  w .ja  v  a  2 s  .c  om
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.HashMap;

import org.sketcher.style.StylesFactory;

import android.graphics.Bitmap;

public class HistoryHelper {
  private final Surface mSurface;

  private State mUndoState = new State();
  private State mRedoState = new State();

  private boolean isSwaped = false;

  public HistoryHelper(Surface surface) {
    mSurface = surface;
  }

  public void undo() {
    if (mRedoState.mBuffer == null || mUndoState.mBuffer == null) {
      return;
    }
    restoreState(mSurface.getBitmap(), isSwaped ? mRedoState : mUndoState);
    isSwaped = !isSwaped;
  }

  private void restoreState(Bitmap bitmap, State state) {
    Buffer byteBuffer = ByteBuffer.wrap(state.mBuffer);
    bitmap.copyPixelsFromBuffer(byteBuffer);
    StylesFactory.restoreState(state.stylesState);
  }

  public void saveState() {
    saveState(mSurface.getBitmap(), isSwaped ? mRedoState : mUndoState);
    isSwaped = !isSwaped;
  }

  private void saveState(Bitmap bitmap, State state) {
    state.mBuffer = new byte[bitmap.getRowBytes() * bitmap.getHeight()];
    Buffer byteBuffer = ByteBuffer.wrap(state.mBuffer);
    bitmap.copyPixelsToBuffer(byteBuffer);
    StylesFactory.saveState(state.stylesState);
  }

  private static class State {
    byte[] mBuffer = null;
    final HashMap<Integer, Object> stylesState = new HashMap<Integer, Object>();
  }
}




Java Source Code List

org.sketcher.AboutDialog.java
org.sketcher.Controller.java
org.sketcher.FileHelper.java
org.sketcher.HistoryHelper.java
org.sketcher.SketcherApplication.java
org.sketcher.Sketcher.java
org.sketcher.Style.java
org.sketcher.Surface.java
org.sketcher.colorpicker.AlphaPicker.java
org.sketcher.colorpicker.HuePicker.java
org.sketcher.colorpicker.PickerDialog.java
org.sketcher.colorpicker.Picker.java
org.sketcher.colorpicker.PreviewView.java
org.sketcher.colorpicker.SatValPicker.java
org.sketcher.colorpicker.Utils.java
org.sketcher.style.ChromeStyle.java
org.sketcher.style.CirclesStyle.java
org.sketcher.style.EraserStyle.java
org.sketcher.style.FurStyle.java
org.sketcher.style.GridStyle.java
org.sketcher.style.LongfurStyle.java
org.sketcher.style.RibbonStyle.java
org.sketcher.style.ShadedStyle.java
org.sketcher.style.SimpleStyle.java
org.sketcher.style.SketchyStyle.java
org.sketcher.style.SquaresStyle.java
org.sketcher.style.StylesFactory.java
org.sketcher.style.WebStyle.java