forked from wentsa/Toast-LibGDX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToast.java
298 lines (258 loc) · 9.25 KB
/
Toast.java
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (c) 2017-present, Tomas Chalupnik (tchalupnik.cz)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.tchalupnik.libgdx;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Align;
/**
* Android-like toast implementation for LibGDX projects
*
* @author Tomas Chalupnik (tchalupnik.cz)
*/
public class Toast {
public enum Length {
SHORT(2),
LONG(3.5f);
private final float duration; // in seconds
Length(float duration) {
this.duration = duration;
}
}
private String msg;
private final BitmapFont font;
private float fadingDuration;
private final Color fontColor;
private SpriteBatch spriteBatch = new SpriteBatch();
private ShapeRenderer renderer = new ShapeRenderer();
private float opacity = 1f;
private int toastWidth;
private int toastHeight;
private float timeToLive;
private float positionX, positionY; // left bottom corner
private float fontX, fontY; // left top corner
private int fontWidth;
Toast(
String text,
Length length,
BitmapFont font,
Color backgroundColor,
float fadingDuration,
float maxRelativeWidth,
Color fontColor,
float positionY,
Integer customMargin
) {
this.msg = text;
this.font = font;
this.fadingDuration = fadingDuration;
this.positionY = positionY;
this.fontColor = fontColor;
this.timeToLive = length.duration;
renderer.setColor(backgroundColor);
// measure text box
GlyphLayout layoutSimple = new GlyphLayout();
layoutSimple.setText(this.font, text);
int lineHeight = (int) layoutSimple.height;
fontWidth = (int) (layoutSimple.width);
int fontHeight = (int) (layoutSimple.height);
int margin = customMargin == null ? lineHeight * 2 : customMargin;
float screenWidth = Gdx.graphics.getWidth();
float maxTextWidth = screenWidth * maxRelativeWidth;
if (fontWidth > maxTextWidth) {
BitmapFontCache cache = new BitmapFontCache(this.font, true);
GlyphLayout layout = cache.addText(text, 0, 0, maxTextWidth, Align.center, true);
fontWidth = (int) layout.width;
fontHeight = (int) layout.height;
}
toastHeight = fontHeight + 2 * margin;
toastWidth = fontWidth + 2 * margin;
positionX = (screenWidth / 2) - toastWidth / 2;
fontX = positionX + margin;
fontY = positionY + margin + fontHeight;
}
/**
* Displays toast<br/>
* Must be called at the end of {@link Game#render()}<br/>
* @param delta {@link Graphics#getDeltaTime()}
* @return activeness of the toast (true while being displayed, false otherwise)
*/
public boolean render(float delta) {
timeToLive -= delta;
if (timeToLive < 0) {
return false;
}
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.circle(positionX, positionY + toastHeight / 2, toastHeight / 2);
renderer.rect(positionX, positionY, toastWidth, toastHeight);
renderer.circle(positionX + toastWidth, positionY + toastHeight / 2, toastHeight / 2);
renderer.end();
spriteBatch.begin();
if (timeToLive > 0 && opacity > 0.15) {
if (timeToLive < fadingDuration) {
opacity = timeToLive / fadingDuration;
}
font.setColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a * opacity);
font.draw(spriteBatch, msg, fontX, fontY, fontWidth, Align.center, true);
}
spriteBatch.end();
return true;
}
/**
* Factory for creating toasts
*/
public static class ToastFactory {
private BitmapFont font;
private Color backgroundColor = new Color(55f / 256, 55f / 256, 55f / 256, 1);
private Color fontColor = new Color(1, 1, 1, 1);
private float positionY;
private float fadingDuration = 0.5f;
private float maxRelativeWidth = 0.65f;
private Integer customMargin;
private ToastFactory() {
float screenHeight = Gdx.graphics.getHeight();
float bottomGap = 100;
positionY = bottomGap + ((screenHeight - bottomGap) / 10);
}
/**
* Creates new toast
* @param text message
* @param length toast duration
* @return newly created toast
*/
public Toast create(String text, Length length) {
return new Toast(
text,
length,
font,
backgroundColor,
fadingDuration,
maxRelativeWidth,
fontColor,
positionY,
customMargin);
}
/**
* Builder for creating factory
*/
public static class Builder {
private boolean built = false;
private ToastFactory factory = new ToastFactory();
/**
* Specify font for toasts
* @param font font
* @return this
*/
public Builder font(BitmapFont font) {
check();
factory.font = font;
return this;
}
/**
* Specify background color for toasts.<br/>
* Note: Alpha channel is not supported (yet).<br/>
* Default: rgb(55,55,55)
* @param color background color
* @return this
*/
public Builder backgroundColor(Color color) {
check();
factory.backgroundColor = color;
return this;
}
/**
* Specify font color for toasts.<br/>
* Default: white
* @param color font color
* @return this
*/
public Builder fontColor(Color color) {
check();
factory.fontColor = color;
return this;
}
/**
* Specify vertical position for toasts<br/>
* Default: bottom part
* @param positionY vertical position of bottom left corner
* @return this
*/
public Builder positionY(float positionY) {
check();
factory.positionY = positionY;
return this;
}
/**
* Specify fading duration for toasts<br/>
* Default: 0.5s
* @param fadingDuration duration in seconds which it takes to disappear
* @return this
*/
public Builder fadingDuration(float fadingDuration) {
check();
if (fadingDuration < 0) {
throw new IllegalArgumentException("Duration must be non-negative number");
}
factory.fadingDuration = fadingDuration;
return this;
}
/**
* Specify max text width for toasts<br/>
* Default: 0.65
* @param maxTextRelativeWidth max text width relative to screen (Eg. 0.5 = max text width is equal to 50% of screen width)
* @return this
*/
public Builder maxTextRelativeWidth(float maxTextRelativeWidth) {
check();
factory.maxRelativeWidth = maxTextRelativeWidth;
return this;
}
/**
* Specify text margin for toasts<br/>
* Default: line height
* @param margin margin in px
* @return this
*/
public Builder margin(int margin) {
check();
factory.customMargin = margin;
return this;
}
/**
* Builds factory
* @return new factory
*/
public ToastFactory build() {
check();
if (factory.font == null) {
throw new IllegalStateException("Font is not set");
}
built = true;
return factory;
}
private void check() {
if (built) {
throw new IllegalStateException("Builder can be used only once");
}
}
}
}
}