Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit e4e38f7

Browse files
committed
Refactored StageLayout.
1 parent dd68720 commit e4e38f7

14 files changed

+413
-441
lines changed
Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.badlogic.gdx.scenes.scene2d.ui;
22

3+
34
import com.badlogic.gdx.checksum.CRC32;
45
import com.badlogic.gdx.json.AnnotatedJsonObject;
56
import com.badlogic.gdx.json.annotations.JsonSerializable;
67
import com.badlogic.gdx.json.annotations.JsonSerialize;
78
import com.badlogic.gdx.scenes.scene2d.Actor;
89
import com.badlogic.gdx.scenes.scene2d.Touchable;
9-
10-
import java.util.function.Consumer;
11-
import java.util.function.Function;
10+
import com.badlogic.gdx.utils.GdxRuntimeException;
1211

1312
@JsonSerializable(dynamic = true)
1413
public abstract class ActorLayout<T extends Actor> implements AnnotatedJsonObject {
@@ -20,71 +19,51 @@ public abstract class ActorLayout<T extends Actor> implements AnnotatedJsonObjec
2019
public String style = "default";
2120

2221
@JsonSerialize
23-
public ContainerLayout layout;
22+
public BaseLayout layout;
2423

2524
@JsonSerialize
26-
public String[] group = {};
25+
public boolean visible = true;
26+
27+
ActorLayout(Class<T> actorClass) {
28+
this.actorClass = actorClass;
29+
}
2730

2831
@Override
2932
public void onJsonWrite() {
3033
/* not implemented */
3134
}
3235

33-
int nameId;
36+
public int nameId;
37+
Class<T> actorClass;
3438

3539
@Override
3640
public void onJsonRead() {
3741

3842
if (layout == null) {
39-
layout = new ContainerLayout();
43+
layout = new BaseLayout();
44+
}
45+
46+
if (name == null) {
47+
throw new GdxRuntimeException("ActorLayout name is null. It needs a name field!");
4048
}
4149

4250
nameId = CRC32.calculateString(name).hashCode();
4351
}
4452

45-
protected final Container<T> create(Skin skin,
46-
Container<?> envelope,
47-
Function<String, ActorLayout<?>> layouts,
48-
Consumer<Actor> registry,
49-
StageLayoutListener listener) {
53+
protected Actor create(Skin skin, StageLayoutListener listener) {
5054

5155
// create actor, using layout data
56+
T actor = createActor(skin, listener);
5257

53-
T actor = createActor(skin, style, layouts, registry, listener);
54-
55-
// set name and save to registry for later lookups
56-
58+
// set name for later lookups
5759
actor.setName(name);
58-
registry.accept(actor);
59-
60-
// wrap in container
6160

62-
return createContainer(actor, envelope);
63-
}
64-
65-
private Container<T> createContainer(T actor, Container<?> envelope) {
66-
67-
Container<T> container = new StageLayoutContainer<T>(actor, envelope);
68-
69-
if (layout.width > 0) {
70-
container.width(layout.width);
71-
}
72-
73-
if (layout.height > 0) {
74-
container.height(layout.height);
75-
}
76-
77-
if (!layout.touchable) {
78-
container.setTouchable(Touchable.disabled);
79-
}
61+
actor.setTouchable(layout.touchable ? Touchable.enabled : Touchable.disabled);
62+
actor.setVisible(visible);
8063

81-
return container;
64+
return actor;
8265
}
8366

84-
protected abstract T createActor(Skin skin,
85-
String styleName,
86-
Function<String, ActorLayout<?>> layouts,
87-
Consumer<Actor> registry,
88-
StageLayoutListener listener);
67+
protected abstract T createActor(Skin skin, StageLayoutListener listener);
8968

90-
}
69+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.badlogic.gdx.scenes.scene2d.ui;
2+
3+
import com.badlogic.gdx.json.annotations.JsonSerializable;
4+
import com.badlogic.gdx.json.annotations.JsonSerialize;
5+
import com.badlogic.gdx.math.MathUtils;
6+
import com.badlogic.gdx.math.Vector2;
7+
import com.badlogic.gdx.scenes.scene2d.*;
8+
import com.badlogic.gdx.utils.viewport.Viewport;
9+
10+
@JsonSerializable
11+
public class BaseLayout {
12+
13+
@JsonSerialize
14+
public int x = 0;
15+
16+
@JsonSerialize
17+
public int y = 0;
18+
19+
@JsonSerialize
20+
public int width = 0;
21+
22+
@JsonSerialize
23+
public int height = 0;
24+
25+
@JsonSerialize
26+
public Align anchor = Align.Center;
27+
28+
@JsonSerialize
29+
public boolean touchable = true;
30+
31+
public enum Align {
32+
Center,
33+
Left,
34+
Right,
35+
Bottom,
36+
Top,
37+
BottomLeft,
38+
BottomRight,
39+
TopLeft,
40+
TopRight
41+
}
42+
43+
<T extends Actor> void resize(Stage stage, Actor actor, boolean root) {
44+
45+
int parentWidth, parentHeight;
46+
47+
if (root) {
48+
Viewport viewport = stage.getViewport();
49+
50+
parentWidth = viewport.getScreenWidth();
51+
parentHeight = viewport.getScreenHeight();
52+
53+
} else {
54+
Actor parent = actor.getParent();
55+
56+
parentWidth = MathUtils.floor(parent.getWidth());
57+
parentHeight = MathUtils.floor(parent.getHeight());
58+
}
59+
60+
int actorWidth = MathUtils.floor(actor.getWidth());
61+
int actorHeight = MathUtils.floor(actor.getHeight());
62+
63+
/** Dynamic sizing of actor if we want percentage of parent (indicated by negative width) */
64+
if(width < 0){
65+
actorWidth = MathUtils.clamp(-width, 1, 100) * parentWidth / 100;
66+
67+
actor.setWidth(actorWidth);
68+
}
69+
if(height < 0){
70+
actorHeight = MathUtils.clamp(-height, 1, 100) * parentHeight / 100;
71+
72+
actor.setHeight(actorHeight);
73+
}
74+
75+
int px = 0, py = 0;
76+
77+
switch (anchor) {
78+
79+
case Center:
80+
px = parentWidth / 2 - actorWidth / 2;
81+
py = parentHeight / 2 - actorHeight / 2;
82+
break;
83+
84+
case Left:
85+
px = 0;
86+
py = parentHeight / 2 - actorHeight / 2;
87+
break;
88+
89+
case Right:
90+
px = parentWidth - actorWidth;
91+
py = parentHeight / 2 - actorHeight / 2;
92+
break;
93+
94+
case Bottom:
95+
px = parentWidth / 2 - actorWidth / 2;
96+
py = 0;
97+
break;
98+
99+
case Top:
100+
px = parentWidth / 2 - actorWidth / 2;
101+
py = parentHeight - actorHeight;
102+
break;
103+
104+
case BottomLeft:
105+
px = 0;
106+
py = 0;
107+
break;
108+
109+
case BottomRight:
110+
px = parentWidth - actorWidth;
111+
py = 0;
112+
break;
113+
114+
case TopLeft:
115+
px = 0;
116+
py = parentHeight - actorHeight;
117+
break;
118+
119+
case TopRight:
120+
px = parentWidth - actorWidth;
121+
py = parentHeight - actorHeight;
122+
break;
123+
}
124+
125+
px += x;
126+
py += y;
127+
128+
actor.setPosition(px, py);
129+
}
130+
131+
}
Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
11
package com.badlogic.gdx.scenes.scene2d.ui;
22

3-
import com.badlogic.gdx.scenes.scene2d.*;
3+
import com.badlogic.gdx.scenes.scene2d.InputEvent;
44
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
55

6-
import java.util.function.Consumer;
7-
import java.util.function.Function;
8-
96
@SuppressWarnings("unused")
107
public class ButtonLayout extends ActorLayout<Button> {
118

12-
@Override
13-
protected Button createActor(Skin skin,
14-
String styleName,
15-
Function<String, ActorLayout<?>> layouts,
16-
Consumer<Actor> registry,
17-
StageLayoutListener listener) {
18-
19-
Button button = new Button(skin, styleName);
20-
21-
button.addListener(new ClickListener() {
22-
23-
@Override
24-
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
25-
if (super.touchDown(event, x, y, pointer, button)) {
26-
listener.onButtonDown(nameId);
27-
return true;
28-
}
29-
return false;
30-
}
9+
public ButtonLayout() {
10+
super(Button.class);
11+
}
3112

32-
@Override
33-
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
34-
super.touchUp(event, x, y, pointer, button);
35-
listener.onButtonUp(nameId);
36-
}
13+
@Override
14+
protected Button createActor(Skin skin, StageLayoutListener listener) {
3715

38-
@Override
39-
public boolean mouseMoved(InputEvent event, float x, float y) {
40-
listener.onMouseOver(nameId);
41-
return super.mouseMoved(event, x, y);
42-
}
43-
});
16+
Button button = new Button(skin, style);
17+
button.addListener(new StageLayoutClickListener(listener, nameId));
4418

4519
return button;
4620
}
4721

22+
public static class StageLayoutClickListener extends ClickListener {
23+
24+
private StageLayoutListener listener;
25+
private int nameId;
26+
27+
public StageLayoutClickListener(StageLayoutListener listener, int nameId) {
28+
this.listener = listener;
29+
this.nameId = nameId;
30+
}
31+
32+
@Override
33+
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
34+
if (super.touchDown(event, x, y, pointer, button)) {
35+
listener.onButtonDown(nameId);
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
@Override
42+
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
43+
super.touchUp(event, x, y, pointer, button);
44+
listener.onButtonUp(nameId);
45+
}
46+
47+
@Override
48+
public boolean mouseMoved(InputEvent event, float x, float y) {
49+
listener.onMouseOver(nameId);
50+
return super.mouseMoved(event, x, y);
51+
}
52+
}
53+
4854
}

0 commit comments

Comments
 (0)