Skip to content

Commit 1ee689b

Browse files
committed
add Scene and SceneManager
1 parent 599d42f commit 1ee689b

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed
Binary file not shown.
Binary file not shown.

graphics/src/main/java/com/ndsl/graphics/display/Display.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.ndsl.graphics.display.fps.FPSLimiter;
1313
import com.ndsl.graphics.display.key.KeyInputHandler;
1414
import com.ndsl.graphics.display.mouse.MouseInputHandler;
15+
import com.ndsl.graphics.display.scene.SceneManager;
1516
import com.ndsl.graphics.pos.Pos;
1617
import com.ndsl.graphics.pos.Rect;
1718
import org.jetbrains.annotations.Nullable;
@@ -32,6 +33,8 @@ public class Display extends JFrame {
3233
public MouseInputHandler mouseInputHandler;
3334
public Debugger debugger;
3435

36+
public SceneManager sceneManager=new SceneManager();
37+
3538
@Deprecated
3639
public AudioInput audioInput=new AudioInput(0);
3740

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.ndsl.graphics.display.scene;
2+
3+
import com.ndsl.graphics.display.Display;
4+
import com.ndsl.graphics.display.drawable.Drawable;
5+
import com.ndsl.graphics.display.drawable.GUIBase;
6+
import com.ndsl.graphics.display.drawable.ui.UIBase;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class Scene {
13+
public String id;
14+
public Scene(String id) {
15+
this.id=id;
16+
}
17+
18+
@Override
19+
public boolean equals(Object obj) {
20+
if(obj instanceof Scene) {
21+
return ((Scene)obj).id==this.id;
22+
}
23+
return false;
24+
}
25+
26+
27+
public List<Drawable> drawableList=new ArrayList<>();
28+
public List<GUIBase> guiList=new ArrayList<>();
29+
public List<UIBase> uiList=new ArrayList<>();
30+
31+
public Scene add(Object o){
32+
if(o instanceof UIBase){
33+
uiList.add((UIBase)o);
34+
}else if(o instanceof GUIBase){
35+
guiList.add((GUIBase)o);
36+
}else if(o instanceof Drawable){
37+
drawableList.add((Drawable)o);
38+
}
39+
return this;
40+
}
41+
42+
public Scene remove(Object o) {
43+
if(o instanceof UIBase){
44+
uiList.remove((UIBase)o);
45+
}else if(o instanceof GUIBase){
46+
guiList.remove((GUIBase)o);
47+
}else if(o instanceof Drawable){
48+
drawableList.remove((Drawable)o);
49+
}
50+
return this;
51+
}
52+
53+
public boolean isExistDrawable(String id){
54+
for(Drawable drawable:drawableList){
55+
if(drawable.getID()==null) continue;
56+
if(drawable.getID().equals(id)){
57+
return true;
58+
}
59+
}
60+
return false;
61+
}
62+
63+
public boolean isExistGui(String id){
64+
for(GUIBase guiBase:guiList){
65+
if(guiBase.getID()==null) continue;
66+
if(guiBase.getID().equals(id)){
67+
return true;
68+
}
69+
}
70+
return false;
71+
}
72+
73+
public boolean isExistGUI(String id){
74+
for(GUIBase guiBase:guiList){
75+
if(guiBase.getID()==null) continue;
76+
if(guiBase.getID().equals(id)){
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
83+
@Nullable
84+
public Drawable getDrawableWithID(String id){
85+
for(Drawable drawable:drawableList){
86+
if(drawable.getID()==null) continue;
87+
if(drawable.getID().equals(id)){
88+
return drawable;
89+
}
90+
}
91+
System.out.println("Not Found in Scene:"+id);
92+
return null;
93+
}
94+
95+
@Nullable
96+
public UIBase getUIWithID(String id){
97+
for(UIBase uiBase:uiList){
98+
if(uiBase.getID()==null) continue;
99+
if(uiBase.getID().equals(id)){
100+
return uiBase;
101+
}
102+
}
103+
System.out.println("Not Found in Scene:"+id);
104+
return null;
105+
}
106+
107+
@Nullable
108+
public GUIBase getGUIWithID(String id){
109+
for(GUIBase guiBase:guiList){
110+
if(guiBase.getID()==null) continue;
111+
if(guiBase.getID().equals(id)){
112+
return guiBase;
113+
}
114+
}
115+
System.out.println("Not Found in Scene:"+id);
116+
return null;
117+
}
118+
119+
120+
/**
121+
* for Display
122+
*/
123+
private void clearDisplay(Display display) {
124+
display.uiList.clear();
125+
display.guiList.clear();
126+
display.drawableList.clear();
127+
}
128+
129+
private void addAllToDisplay(Display display){
130+
for(Drawable draw:drawableList){
131+
display.addDrawable(draw);
132+
}
133+
for(UIBase ui:uiList){
134+
display.addUI(ui);
135+
}
136+
for(GUIBase guiBase:guiList){
137+
display.addGui(guiBase);
138+
}
139+
}
140+
141+
public void copyToDisplay(Display display){
142+
clearDisplay(display);
143+
addAllToDisplay(display);
144+
}
145+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.ndsl.graphics.display.scene;
2+
3+
import com.ndsl.graphics.display.Display;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class SceneManager {
10+
public List<Scene> scenes=new ArrayList<>();
11+
12+
public SceneManager addScene(Scene scene) {
13+
if(isExist(scene.id)) scenes.remove(scene);
14+
scenes.add(scene);
15+
return this;
16+
}
17+
18+
public boolean isExist(String id){
19+
for(Scene scene: scenes){
20+
if(scene.id.equals(id)){
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
public SceneManager remove(Scene scene){
28+
return remove(scene.id);
29+
}
30+
31+
public SceneManager remove(String id){
32+
for(Scene scene: scenes){
33+
if(scene.id.equals(id)){
34+
scene.remove(scene);
35+
break;
36+
}
37+
}
38+
return this;
39+
}
40+
41+
@Nullable
42+
public Scene getScene(String id){
43+
for(Scene scene: scenes){
44+
if(scene.id.equals(id)){
45+
return scene;
46+
}
47+
}
48+
return null;
49+
}
50+
51+
public void setScene(String id, Display display){
52+
if(isExist(id)){
53+
setScene(getScene(id),display);
54+
}else{
55+
System.out.println("Scene:"+id+" is not found");
56+
}
57+
}
58+
59+
public void setScene(Scene scene,Display display){
60+
scene.copyToDisplay(display);
61+
}
62+
}

0 commit comments

Comments
 (0)