-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextObject.pde
192 lines (171 loc) · 5.04 KB
/
TextObject.pde
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
private class TextObject extends GameObject {
private PFont font = font0;
private int alignX = LEFT;
private int alignY = TOP;
private String string;
private float textSize;
private boolean hasSize;
private boolean isNumberMode;
private NumType numType;
private int formatLeft, formatRight;
private TextObject() {
string = "";
}
private TextObject(String string) {
this.string = string;
}
private void numberMode(NumType numType) {
numberMode(numType, 0, 0);
}
private void numberMode(NumType numType, int formatLeft) {
numberMode(numType, formatLeft, 0);
}
private void numberMode(NumType numType, int formatLeft, int formatRight) {
isNumberMode = true;
this.numType = numType;
this.formatLeft = formatLeft;
this.formatRight = formatRight;
}
private void stringMode() {
isNumberMode = false;
}
@Override
public TextObject clone(){
TextObject object = new TextObject();
try {
object = (TextObject)super.clone();
object.states = new HashMap<String, State>(this.states);
for(Entry<String, State> entry : object.states.entrySet()) {
object.addState(entry.getKey(), entry.getValue());
}
// object.subStates = new ArrayList<State>(this.subStates);
} catch (Exception e){
e.printStackTrace();
}
return object;
}
@Override
protected void adjustParameter() {
if(isFreeRef) {
return;
}
if(textSize == 0.0f || scale == 0.0f) {
posRX = 0.0f;
posRY = 0.0f;
return;
}
textFont(font);
textSize(textSize * scale);
switch(alignX) {
case LEFT:
posRX = posX + textWidth(string) / 2.0f;
break;
case CENTER:
posRX = posX;
break;
case RIGHT:
posRX = posX - textWidth(string) / 2.0f;
break;
}
switch(alignY) {
case TOP:
posRY = posY + textAscent() / 2.0f;
break;
case CENTER:
posRY = posY;
break;
case BASELINE:
posRY = posY - textAscent() / 2.0f;
break;
case BOTTOM:
posRY = posY - (textAscent() + textDescent()) / 2.0f;
break;
}
}
/*
private void removeSize() {
hasSize = false;
}
private boolean hasSize() {
return hasSize;
}
*/
private void setText(String string) {
this.string = string;
adjustParameter();
}
private String getText() {
return string;
}
protected final void setAlign(int alignX, int alignY) {
this.alignX = alignX;
this.alignY = alignY;
adjustParameter();
}
private void setFont(PFont font) {
this.font = font;
adjustParameter();
}
private void setTextSize(float textSize) {
if(textSize < 0.0f) {
println("Error:textSize can't support under 0.0f");
this.textSize = 0.0f;
}
this.textSize = textSize;
adjustParameter();
}
@Override
protected void concreteDraw() {
if(textSize == 0.0f || scale == 0.0f) {
return;
}
textFont(font);
textSize(textSize * scale);
float diffX = textWidth(string) * (scale - 1.0f) / 2.0f;
float diffY = textAscent() * (scale - 1.0f) / 2.0f;
float realPosX = 0.0f;
float realPosY = 0.0f;
switch(alignX) {
case LEFT:
realPosX = (posX - diffX) * DISPLAY_SCALE + WIDTH_MARGIN;
break;
case CENTER:
realPosX = posX * DISPLAY_SCALE + WIDTH_MARGIN;
break;
case RIGHT:
realPosX = (posX + diffX) * DISPLAY_SCALE + WIDTH_MARGIN;
break;
}
switch(alignY) {
case TOP:
realPosY = (posY - diffX) * DISPLAY_SCALE + HEIGHT_MARGIN;
break;
case CENTER:
realPosY = posY * DISPLAY_SCALE + HEIGHT_MARGIN;
break;
case BASELINE:
realPosY = (posY + diffY) * DISPLAY_SCALE + HEIGHT_MARGIN;
break;
case BOTTOM:
diffY = (textAscent() + textDescent()) * (scale - 1.0f) / 2.0f;
realPosY = posY * DISPLAY_SCALE + diffY + HEIGHT_MARGIN;
break;
}
textSize(textSize * scale * DISPLAY_SCALE);
fill(colors, alpha);
textAlign(alignX, alignY);
if(isNumberMode) {
switch(numType) {
case INTEGER:
text(nf((int)number, formatLeft, formatRight), realPosX, realPosY);
break;
case FLOAT:
text(nf(number, formatLeft, formatRight), realPosX, realPosY);
break;
}
//text(string, realPosX, realPosY, realSizeX , realSizeY);
} else {
text(string, realPosX, realPosY);
}
}
}