-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDemonstration.java
384 lines (342 loc) · 15.9 KB
/
Demonstration.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package com.payne.games.piemenu.testMenu.otherTests;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Array;
import com.payne.games.piemenu.AnimatedPieMenu;
import com.payne.games.piemenu.AnimatedPieWidget;
import com.payne.games.piemenu.PieMenu;
import com.payne.games.piemenu.PieWidget.PieWidgetStyle;
import com.payne.games.piemenu.testMenu.core.BaseScreen;
import com.payne.games.piemenu.testMenu.core.TestsMenu;
public class Demonstration extends BaseScreen {
private AnimatedPieMenu dragPie;
private PieMenu permaPie;
private PieMenu rightMousePie;
private PieMenu middleMousePie;
private PieMenu.PieMenuStyle midStyle1;
private PieMenu.PieMenuStyle midStyle2;
private AnimatedPieWidget widget;
private float midStyle1InnerRadius = 24f / 80;
private float midStyle2InnerRadius = 27f / 80;
private Color backgroundColor = new Color(1, 1, 1, .2f);
private int dragPieAmount = 0;
private int permaPieAmount = 0;
private int widgetAmount = 0;
private final int INITIAL_CHILDREN_AMOUNT = 5;
public Demonstration(TestsMenu game) {
super(game);
}
@Override
public void show() {
setScreenColor(.25f, .25f, .45f, 1);
/* Setting up the Stage. */
Table root = new Table();
root.setFillParent(true);
game.stage.addActor(root);
// game.stage.setDebugAll(true);
/* Controls and instructions. */
root.add(new Label("R: restart\n" +
"S: toggle the permanent pie\n" +
"C: change style of Middle-click Pie\n" +
"L: less items in certain menus\n" +
"M: more items in certain menus\n" +
"Middle-click / Right-click: try it out!\n", game.skin)).colspan(2).padTop(25);
root.row().padBottom(150).uniform();
/* Adding the demo widgets. */
setUpButtonDragPieMenu(root);
setUpRightMousePieMenu();
setUpMiddleMousePieMenu();
setUpPieWidget(root);
setUpPermaPieMenu();
}
private void setUpPieWidget(Table root) {
/* Setting up and creating the widget. */
PieWidgetStyle style = new PieWidgetStyle();
style.backgroundColor = new Color(1, 1, 1, 1);
style.sliceColor = new Color(.4f, .4f, .4f, 1);
style.alternateSliceColor = new Color(.6f, 0, 0, 1);
widget = new AnimatedPieWidget(game.skin.getRegion("white"), style, 100, .5f, 0, 180);
/* Populating the widget. */
for (int i = 0; i < INITIAL_CHILDREN_AMOUNT; i++) {
Label label = new Label(Integer.toString(widgetAmount++), game.skin);
widget.addActor(label);
}
/* Setting up the demo-button. */
final TextButton textButton = new TextButton("Toggle Widget", game.skin);
textButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
widget.toggleVisibility(.9f);
widget.setPosition(textButton.getX(Align.center),
textButton.getTop() + 15, Align.center);
}
});
root.add(textButton).expand().bottom();
/* Including the Widget in the Stage. */
game.stage.addActor(widget);
widget.setVisible(false);
}
private void setUpButtonDragPieMenu(Table root) {
/* Setting up and creating the widget. */
PieMenu.PieMenuStyle style = new PieMenu.PieMenuStyle();
style.backgroundColor = new Color(1, 1, 1, .3f);
style.selectedColor = new Color(.7f, .3f, .5f, 1);
style.sliceColor = new Color(0, .7f, 0, 1);
style.alternateSliceColor = new Color(.7f, 0, 0, 1);
dragPie = new AnimatedPieMenu(game.skin.getRegion("white"), style, 130, 50f / 130, 180, 320);
/* Customizing the behavior. */
dragPie.setInfiniteSelectionRange(true);
/* Populating the widget. */
for (int i = 0; i < INITIAL_CHILDREN_AMOUNT; i++) {
Label label = new Label(Integer.toString(dragPieAmount++), game.skin);
dragPie.addActor(label);
}
/* Setting up the demo-button. */
final TextButton textButton = new TextButton("Drag Pie", game.skin);
textButton.addListener(new ClickListener() {
/*
In our particular case, we want to NOT use a ChangeListener because
else the user would have to release his mouse-click before seeing
the menu, which goes against our current goal of obtaining a
"drag-selection" menu.
*/
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
dragPie.resetSelection();
dragPie.centerOnActor(textButton);
dragPie.animateOpening(.4f);
transferInteraction(game.stage, dragPie);
return true;
}
});
root.add(textButton).expand().bottom();
/* Adding a selection-listener. */
dragPie.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
dragPie.transitionToClosing(.4f);
int index = dragPie.getSelectedIndex();
if (!dragPie.isValidIndex(index)) {
textButton.setText("Drag Pie");
return;
}
Actor child = dragPie.getChild(index);
textButton.setText(((Label) child).getText().toString());
}
});
/* Including the Widget in the Stage. */
game.stage.addActor(dragPie);
dragPie.setVisible(false);
}
/**
* To be used to get the user to transition directly into
* {@link InputListener#touchDragged(InputEvent, float, float, int)}
* as if he had triggered
* {@link InputListener#touchDown(InputEvent, float, float, int, int)}.<br/>
* I am not certain this is the recommended way of doing this, but for the
* purposes of this demonstration, it works!
*
* @param stage the stage.
* @param widget the PieMenu on which to transfer the interaction.
*/
private void transferInteraction(Stage stage, PieMenu widget) {
if (widget == null) throw new IllegalArgumentException("widget cannot be null.");
if (widget.getPieMenuListener() == null) throw new IllegalArgumentException("inputListener cannot be null.");
stage.addTouchFocus(widget.getPieMenuListener(), widget, widget, 0, widget.getSelectionButton());
}
private void setUpRightMousePieMenu() {
/* Setting up and creating the widget. */
PieMenu.PieMenuStyle style = new PieMenu.PieMenuStyle();
style.separatorWidth = 2;
style.circumferenceWidth = 2;
style.backgroundColor = new Color(1, 1, 1, .1f);
style.separatorColor = new Color(.1f, .1f, .1f, 1);
style.downColor = new Color(.5f, .5f, .5f, 1);
style.sliceColor = new Color(.33f, .33f, .33f, 1);
rightMousePie = new PieMenu(game.skin.getRegion("white"), style, 80, 0, 90);
/* Customizing the behavior. */
rightMousePie.setInfiniteSelectionRange(true);
rightMousePie.setSelectionButton(Input.Buttons.RIGHT);
/* Setting up listeners. */
rightMousePie.addListener(new PieMenu.PieMenuCallbacks() {
@Override
public void onHighlightChange(int highlightedIndex) {
switch (highlightedIndex) {
case 0:
screenColorRed = .25f;
screenColorBlue = .45f;
screenColorGreen = .25f;
break;
case 1:
screenColorRed = .45f;
screenColorBlue = .25f;
screenColorGreen = .25f;
break;
case 2:
screenColorRed = .25f;
screenColorBlue = .25f;
screenColorGreen = .45f;
break;
default:
screenColorRed = .75f;
screenColorBlue = .75f;
screenColorGreen = .75f;
break;
}
}
});
rightMousePie.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println("ChangeListener - selected index: " + rightMousePie.getSelectedIndex());
rightMousePie.setVisible(false);
rightMousePie.remove();
}
});
/* Populating the widget. */
Label blue = new Label("blue", game.skin);
rightMousePie.addActor(blue);
Label red = new Label("red", game.skin);
rightMousePie.addActor(red);
Label green = new Label("green", game.skin);
rightMousePie.addActor(green);
}
private void setUpMiddleMousePieMenu() {
// todo: eventually should look similar to https://dribbble.com/shots/647272-Circle-Menu-PSD
/* Setting up and creating the widget. */
midStyle1 = new PieMenu.PieMenuStyle();
midStyle1.selectedColor = new Color(1, .5f, .5f, .5f);
midStyle1.background = new TextureRegionDrawable(getTextureAutoDisposable("rael_pie.png"));
middleMousePie = new PieMenu(game.skin.getRegion("white"), midStyle1, 80, midStyle1InnerRadius, 30) {
/* Since we are using Images, we want to resize them to fit within each sector. */
@Override
public void modifyActor(Actor actor, float degreesPerChild, float actorDistanceFromCenter) {
float size = getEstimatedRadiusAt(degreesPerChild, actorDistanceFromCenter);
size *= 1.26f; // adjusting the returned value to our likes
actor.setSize(size, size);
}
};
/* Customizing the behavior. */
middleMousePie.setInfiniteSelectionRange(true);
/* Adding a selection-listener. */
middleMousePie.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println("ChangeListener - selected index: " + middleMousePie.getSelectedIndex());
middleMousePie.setVisible(false);
middleMousePie.remove();
}
});
/* Populating the widget. */
Array<Image> imgs = new Array<>();
imgs.add(new Image(getTextureAutoDisposable("heart-drop.png")));
imgs.add(new Image(getTextureAutoDisposable("beer-stein.png")));
imgs.add(new Image(getTextureAutoDisposable("coffee-mug.png")));
imgs.add(new Image(getTextureAutoDisposable("gooey-daemon.png")));
imgs.add(new Image(getTextureAutoDisposable("jeweled-chalice.png")));
imgs.add(new Image(getTextureAutoDisposable("coffee-mug.png")));
for (int i = 0; i < imgs.size; i++)
middleMousePie.addActor(imgs.get(i));
/* Creating an alternate skin, just for showing off */
midStyle2 = new PieMenu.PieMenuStyle();
midStyle2.separatorWidth = 2;
midStyle2.selectedColor = new Color(1, .5f, .5f, .5f);
midStyle2.separatorColor = new Color(.1f, .1f, .1f, .5f);
midStyle2.sliceColor = new Color(.73f, .33f, .33f, .1f);
midStyle2.background = new TextureRegionDrawable(getTextureAutoDisposable("disc.png"));
}
private void setUpPermaPieMenu() {
/* Setting up and creating the widget. */
PieMenu.PieMenuStyle style = new PieMenu.PieMenuStyle();
style.circumferenceWidth = 2;
style.backgroundColor = backgroundColor;
style.downColor = new Color(.5f, .5f, .5f, 1);
style.sliceColor = new Color(.33f, .33f, .33f, 1);
style.alternateSliceColor = new Color(.25f, .25f, .25f, 1);
style.circumferenceColor = new Color(0, 0, 0, 1);
permaPie = new PieMenu(game.skin.getRegion("white"), style, 80, 20f / 80, 0, 180);
/* Adding a selection-listener. */
permaPie.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
float alpha = MathUtils.map(0, permaPie.getAmountOfChildren() - 1, 0, 1, permaPie.getSelectedIndex());
widget.getStyle().backgroundColor.set(backgroundColor.r, backgroundColor.g, backgroundColor.b, alpha);
}
});
/* Populating the widget. */
for (int i = 0; i < INITIAL_CHILDREN_AMOUNT; i++) {
Label label = new Label(Integer.toString(permaPieAmount++), game.skin);
permaPie.addActor(label);
}
/* Customizing the behavior. */
permaPie.setDefaultIndex(2);
/* Including the Widget at some absolute coordinate in the World. */
permaPie.setPosition(Gdx.graphics.getWidth() / 2f, 0, Align.center);
game.stage.addActor(permaPie);
}
@Override
public void updateInputPost(float delta) {
/* Debugging and interactions. */
if (Gdx.input.isKeyJustPressed(Input.Keys.M)) {
dragPie.addActor(new Label(Integer.toString(dragPieAmount++), game.skin));
permaPie.addActor(new Label(Integer.toString(permaPieAmount++), game.skin));
widget.addActor(new Label(Integer.toString(widgetAmount++), game.skin));
}
if (Gdx.input.isKeyJustPressed(Input.Keys.R)) {
dragPieAmount = 0;
permaPieAmount = 0;
widgetAmount = 0;
dispose();
show();
}
if (Gdx.input.isKeyJustPressed(Input.Keys.L)) {
if (dragPie.getAmountOfChildren() == 0)
return;
dragPie.removeActor(dragPie.getChild(dragPie.getAmountOfChildren() - 1));
permaPie.removeActor(permaPie.getChild(permaPie.getAmountOfChildren() - 1));
widget.removeActor(widget.getChild(widget.getAmountOfChildren() - 1));
}
if (Gdx.input.isKeyJustPressed(Input.Keys.S)) {
permaPie.setVisible(!permaPie.isVisible());
}
if (Gdx.input.isButtonJustPressed(rightMousePie.getSelectionButton())) {
game.stage.addActor(rightMousePie);
rightMousePie.centerOnMouse();
rightMousePie.setVisible(true);
transferInteraction(game.stage, rightMousePie);
}
if (Gdx.input.isButtonJustPressed(Input.Buttons.MIDDLE)) {
game.stage.addActor(middleMousePie);
middleMousePie.centerOnMouse();
middleMousePie.resetSelection();
middleMousePie.setVisible(true);
}
if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
middleMousePie.setStyle(
middleMousePie.getStyle() == midStyle1
? midStyle2 : midStyle1);
middleMousePie.setInnerRadiusPercent(
middleMousePie.getStyle() == midStyle1
? midStyle1InnerRadius : midStyle2InnerRadius);
}
}
@Override
public void resize(int width, int height) {
super.resize(width, height);
permaPie.setPosition(Gdx.graphics.getWidth() / 2f, 0, Align.center);
}
}