Skip to content

Instantly share code, notes, and snippets.

@brk3
Created June 9, 2013 19:17
Show Gist options
  • Save brk3/5744798 to your computer and use it in GitHub Desktop.
Save brk3/5744798 to your computer and use it in GitHub Desktop.
package com.bourke.roidrage;
import android.content.res.Resources;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.Log;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.metalev.multitouch.controller.MultiTouchController.PositionAndScale;
public class TextEntity extends ComicEntity {
private String mText;
/* drawText doesn't handle line breaks so we break it manually */
private ArrayList<String> mTextLines = new ArrayList<String>();
private transient Paint mPaint = new Paint();
private transient Rect mTextRect = new Rect();
private int mTextSize = 25;
private static final int TEXT_RECT_PADDING = 10;
private int mColor = 0xFF000000;
public TextEntity(String text, Resources res) {
super(res);
mText = text;
mPaint.setAntiAlias(true);
mPaint.setTextSize(mTextSize);
mPaint.setColor(mColor);
}
public TextEntity(Resources res, TextEntity e) {
super(res);
mText = e.getText();
mTextLines = e.mTextLines;
mPaint.setAntiAlias(true);
mPaint.setTextSize(e.mTextSize);
mPaint.setColor(e.mColor);
}
@Override
public void load(Context context, float startMidX, float startMidY) {
Resources res = context.getResources();
getMetrics(res);
mStartMidX = startMidX;
mStartMidY = startMidY;
mLockIconDrawable =
BitmapFactory.decodeResource(res, R.drawable.icon_lock);
Typeface typeface =
Typeface.createFromAsset(context.getAssets(),
"fonts/Courier_Bold.ttf");
mPaint.setTypeface(typeface);
// split any linebreaks and get bounding
mTextLines = new ArrayList<String>();
String lines[] = mText.split("\\n");
if (lines.length == 0) {
return;
}
for (String line : lines) {
mTextLines.add(line);
}
calcTextBounds();
float centerX;
float centerY;
float angle;
if (mFirstLoad) {
centerX = startMidX;
centerY = startMidY;
angle = 0.0f;
mFirstLoad = false;
} else {
mBoundingPaint = new Paint();
mBoundingPaint.setColor(Color.RED);
mBoundingPaint.setStyle(Paint.Style.STROKE);
mBoundingPaint.setStrokeWidth(3.0f);
centerX = mCenterX;
centerY = mCenterY;
angle = mAngle;
}
setPos(centerX, centerY, angle);
}
/**
* Set the position and scale of an image in screen coordinates
*/
@Override
public boolean setPos(PositionAndScale newImgPosAndScale) {
return setPos(newImgPosAndScale.getXOff(),
newImgPosAndScale.getYOff(),
newImgPosAndScale.getAngle());
}
private boolean setPos(float centerX, float centerY, float angle) {
float midWidth = (mWidth);// / 2);
float midHeight = (mHeight);// / 2);
mMinX = centerX - midWidth;
mMinY = centerY - midHeight;
mMaxX = centerX + midWidth;
mMaxY = centerY + midHeight;
mCenterX = centerX;
mCenterY = centerY;
mAngle = angle;
return true;
}
@Override
public void draw(Canvas canvas, boolean renderMode) {
canvas.save();
float dx = (mMaxX + mMinX) / 2;
float dy = (mMaxY + mMinY) / 2;
canvas.translate(dx, dy);
canvas.rotate(mAngle * 180.0f / (float) Math.PI);
canvas.translate(-dx, -dy);
int initial_dy = (int)dy;
for (String line : mTextLines) {
dy += mPaint.getFontSpacing();
canvas.drawText(line, dx, dy, mPaint);
}
// draw the bounding rect if been manipulated
if (mIsSelected) {
Rect boundRect = new Rect(
(int)dx - TEXT_RECT_PADDING,
(int)initial_dy - TEXT_RECT_PADDING,
(int)dx + mWidth + TEXT_RECT_PADDING,
(int)dy + TEXT_RECT_PADDING);
canvas.drawRect(boundRect, mBoundingPaint);
}
// draw a small lock icon at top right corner if entity is locked
if (mIsLocked && !renderMode) {
canvas.drawBitmap(mLockIconDrawable,
(int)mMaxX,
(int)mMaxY-mLockIconDrawable.getHeight(), mPaint);
}
canvas.restore();
}
@Override
public void drawScaled(float oldCanvasWidth, float oldCanvasHeight,
Canvas outCanvas) {
float canvasWidth = outCanvas.getWidth();
float canvasHeight = outCanvas.getHeight();
// calculate new values
float newPosX = (canvasWidth / oldCanvasWidth) * mCenterX;
float newPosY = (canvasHeight / oldCanvasHeight) * mCenterY;
float newScale = (canvasWidth / oldCanvasWidth) *
mPaint.getTextScaleX();
// store previous ones
float prevPosX = mCenterX;
float prevPosY = mCenterY;
float prevScale = mPaint.getTextScaleX();
setPos(newPosX, newPosY, mAngle);
mPaint.setTextSize(mTextSize*newScale);
draw(outCanvas, true);
mPaint.setTextSize(mTextSize*prevScale);
setPos(prevPosX, prevPosY, mAngle);
}
@Override
public void unload() {
}
@Override
public void mirror() {
for (int i=0; i<mTextLines.size(); i++) {
String curLineReversed = new StringBuffer(mTextLines.get(i))
.reverse().toString();
mTextLines.set(i, curLineReversed);
}
}
public void reMirror() {
}
public String getText() {
return mText;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeBoolean(mFirstLoad);
out.writeObject(mText);
out.writeInt(mTextSize);
out.writeFloat(mAngle);
out.writeInt(mColor);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
mFirstLoad = in.readBoolean();
mText = (String)in.readObject();
mTextSize = in.readInt();
mAngle = in.readFloat();
mColor = in.readInt();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(mTextSize);
mPaint.setColor(mColor);
}
public int getTextSize() {
return mTextSize;
}
public void setTextSize(int textSize) {
mTextSize = textSize;
mPaint.setTextSize(mTextSize);
calcTextBounds(); // recalculate text bounds to accomadate new size
}
public void setColor(int color) {
mColor = color;
mPaint.setColor(mColor);
}
public int getColor() {
return mColor;
}
public String longestSegment(ArrayList<String> lines){
String longest = lines.get(0);
for(String str : lines){
if(str.length() > longest.length()) {
longest = str;
}
}
return longest;
}
@Override
public boolean containsPoint(float touchX, float touchY) {
float dx = (mMaxX + mMinX) / 2;
float dy = (mMaxY + mMinY) / 2;
int initial_dy = (int)dy;
for (String line : mTextLines) {
dy += mPaint.getFontSpacing();
}
Rect boundRect = new Rect(
(int)dx - TEXT_RECT_PADDING,
(int)initial_dy - TEXT_RECT_PADDING,
(int)dx + mWidth + TEXT_RECT_PADDING,
(int)dy + TEXT_RECT_PADDING);
return boundRect.contains((int)touchX, (int)touchY);
}
@Override
public void setIsLatestSelected(boolean selected) {
// Disable this for TextEntity (no grab area)
}
private void calcTextBounds() {
mTextRect = new Rect();
// Calculate width based on widest line
String longestLine = longestSegment(mTextLines);
mPaint.getTextBounds(longestLine, 0, longestLine.length(), mTextRect);
mWidth = mTextRect.width() + TEXT_RECT_PADDING;
mHeight = (mTextRect.height() * mTextLines.size()) + TEXT_RECT_PADDING;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment