-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathTextBreakExample.java
More file actions
162 lines (128 loc) · 5.61 KB
/
TextBreakExample.java
File metadata and controls
162 lines (128 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.andengine.examples;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleLayoutGameActivity;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga
*
* @author Nicolas Gramlich
* @since 11:54:51 - 03.04.2010
*/
public class TextBreakExample extends SimpleLayoutGameActivity implements TextWatcher {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private static final float AUTOWRAP_WIDTH = 720 - 50 - 50;
// ===========================================================
// Fields
// ===========================================================
private EditText mEditText;
private Font mFont;
private Text mText;
private Line mRight;
private Line mLeft;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected int getLayoutID() {
return R.layout.textbreakexample;
}
@Override
protected int getRenderSurfaceViewID() {
return R.id.textbreakexample_rendersurfaceview;
}
@Override
protected void onSetContentView() {
super.onSetContentView();
this.mEditText = (EditText)this.findViewById(R.id.textbreakexample_text);
this.mEditText.addTextChangedListener(this);
}
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, TextBreakExample.CAMERA_WIDTH, TextBreakExample.CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(TextBreakExample.CAMERA_WIDTH, TextBreakExample.CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, TextureOptions.BILINEAR, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 24);
this.mFont.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
this.mText = new Text(50, 40, this.mFont, "", 1000, new TextOptions(AutoWrap.LETTERS, AUTOWRAP_WIDTH, HorizontalAlign.CENTER, Text.LEADING_DEFAULT), vertexBufferObjectManager);
scene.attachChild(this.mText);
this.mLeft = new Line(0, 0, 0, TextBreakExample.CAMERA_HEIGHT, this.getVertexBufferObjectManager());
this.mRight = new Line(0, 0, 0, TextBreakExample.CAMERA_HEIGHT, this.getVertexBufferObjectManager());
this.mText.attachChild(this.mLeft);
this.mText.attachChild(this.mRight);
final Line leftBreakLine = new Line(0, 0, 0, TextBreakExample.CAMERA_HEIGHT, this.getVertexBufferObjectManager());
leftBreakLine.setLineWidth(2);
leftBreakLine.setColor(Color.RED);
this.mText.attachChild(leftBreakLine);
final Line rightBreakLine = new Line(AUTOWRAP_WIDTH, 0, AUTOWRAP_WIDTH, TextBreakExample.CAMERA_HEIGHT, this.getVertexBufferObjectManager());
rightBreakLine.setLineWidth(2);
rightBreakLine.setColor(Color.RED);
this.mText.attachChild(rightBreakLine);
this.updateText();
return scene;
}
@Override
public void afterTextChanged(final Editable pEditable) {
this.updateText();
}
private void updateText() {
final String string = this.mEditText.getText().toString();
this.mText.setText(string);
final float left = (this.mText.getWidth() * 0.5f) - (this.mText.getLineWidthMaximum() * 0.5f);
this.mLeft.setPosition(left, 0, left, TextBreakExample.CAMERA_HEIGHT);
final float right = (this.mText.getWidth() * 0.5f) + (this.mText.getLineWidthMaximum() * 0.5f);
this.mRight.setPosition(right, 0, right, TextBreakExample.CAMERA_HEIGHT);
}
@Override
public void beforeTextChanged(final CharSequence pCharSequence, final int pStart, final int pCount, final int pAfter) {
/* Nothing. */
}
@Override
public void onTextChanged(final CharSequence pCharSequence, final int pStart, final int pBefore, final int pCount) {
/* Nothing. */
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}