-
Notifications
You must be signed in to change notification settings - Fork 96
/
Overlap2dRenderer.java
278 lines (224 loc) · 10.5 KB
/
Overlap2dRenderer.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
package com.uwsoft.editor.renderer.systems.render;
import box2dLight.RayHandler;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Affine2;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.physics.box2d.World;
import com.uwsoft.editor.renderer.commons.IExternalItemType;
import com.uwsoft.editor.renderer.components.*;
import com.uwsoft.editor.renderer.physics.PhysicsBodyLoader;
import com.uwsoft.editor.renderer.systems.render.logic.DrawableLogicMapper;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
public class Overlap2dRenderer extends IteratingSystem {
private final float TIME_STEP = 1f/60;
private ComponentMapper<ViewPortComponent> viewPortMapper = ComponentMapper.getFor(ViewPortComponent.class);
private ComponentMapper<CompositeTransformComponent> compositeTransformMapper = ComponentMapper.getFor(CompositeTransformComponent.class);
private ComponentMapper<NodeComponent> nodeMapper = ComponentMapper.getFor(NodeComponent.class);
private ComponentMapper<ParentNodeComponent> parentNodeMapper = ComponentMapper.getFor(ParentNodeComponent.class);
private ComponentMapper<TransformComponent> transformMapper = ComponentMapper.getFor(TransformComponent.class);
private ComponentMapper<MainItemComponent> mainItemComponentMapper = ComponentMapper.getFor(MainItemComponent.class);
private ComponentMapper<ShaderComponent> shaderComponentComponentMapper = ComponentMapper.getFor(ShaderComponent.class);
private DrawableLogicMapper drawableLogicMapper;
private RayHandler rayHandler;
// private World world;
//private Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
public static float timeRunning = 0;
public Batch batch;
public Overlap2dRenderer(Batch batch) {
super(Family.all(ViewPortComponent.class).get());
this.batch = batch;
drawableLogicMapper = new DrawableLogicMapper();
}
public void addDrawableType(IExternalItemType itemType) {
drawableLogicMapper.addDrawableToMap(itemType.getTypeId(), itemType.getDrawable());
}
@Override
public void processEntity(Entity entity, float deltaTime) {
timeRunning+=deltaTime;
ViewPortComponent ViewPortComponent = viewPortMapper.get(entity);
Camera camera = ViewPortComponent.viewPort.getCamera();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
drawRecursively(entity, 1f);
batch.end();
//TODO kinda not cool (this should be done in separate lights renderer maybe?
if (rayHandler != null) {
rayHandler.setCulling(false);
OrthographicCamera orthoCamera = (OrthographicCamera) camera;
camera.combined.scl(1f / PhysicsBodyLoader.getScale());
rayHandler.setCombinedMatrix(orthoCamera);
rayHandler.updateAndRender();
}
//debugRenderer.render(world, camera.combined);
//TODO Spine rendere thing
}
private void drawRecursively(Entity rootEntity, float parentAlpha) {
//currentComposite = rootEntity;
CompositeTransformComponent curCompositeTransformComponent = compositeTransformMapper.get(rootEntity);
TransformComponent transform = transformMapper.get(rootEntity);
ShaderComponent shaderComponent = shaderComponentComponentMapper.get(rootEntity);
boolean shaderExist = shaderComponent!=null && shaderComponent.getShader()!=null;
if(shaderExist){
batch.setShader(shaderComponent.getShader());
}
if (curCompositeTransformComponent.transform || transform.rotation != 0 || transform.scaleX !=1 || transform.scaleY !=1){
MainItemComponent childMainItemComponent = mainItemComponentMapper.get(rootEntity);
//System.out.println(" Name " +childMainItemComponent.itemIdentifier);
//System.out.println(curCompositeTransformComponent.computedTransform.toString());
computeTransform(rootEntity);
//System.out.println(curCompositeTransformComponent.computedTransform.toString());
applyTransform(rootEntity, batch);
}
TintComponent tintComponent = ComponentRetriever.get(rootEntity, TintComponent.class);
parentAlpha *= tintComponent.color.a;
drawChildren(rootEntity, batch, curCompositeTransformComponent, parentAlpha);
if (curCompositeTransformComponent.transform || transform.rotation != 0 || transform.scaleX !=1 || transform.scaleY !=1)
resetTransform(rootEntity, batch);
if(shaderExist){
batch.setShader(null);
}
}
private void drawChildren(Entity rootEntity, Batch batch, CompositeTransformComponent curCompositeTransformComponent, float parentAlpha) {
NodeComponent nodeComponent = nodeMapper.get(rootEntity);
Entity[] children = nodeComponent.children.begin();
TransformComponent transform = transformMapper.get(rootEntity);
if (curCompositeTransformComponent.transform || transform.rotation != 0 || transform.scaleX !=1 || transform.scaleY !=1) {
for (int i = 0, n = nodeComponent.children.size; i < n; i++) {
Entity child = children[i];
LayerMapComponent rootLayers = ComponentRetriever.get(rootEntity, LayerMapComponent.class);
ZIndexComponent childZIndexComponent = ComponentRetriever.get(child, ZIndexComponent.class);
if(!rootLayers.isVisible(childZIndexComponent.layerName)) {
continue;
}
MainItemComponent childMainItemComponent = mainItemComponentMapper.get(child);
if(!childMainItemComponent.visible){
continue;
}
int entityType = childMainItemComponent.entityType;
NodeComponent childNodeComponent = nodeMapper.get(child);
if(childNodeComponent ==null){
//Find logic from the mapper and draw it
drawableLogicMapper.getDrawable(entityType).draw(batch, child, parentAlpha);
}else{
//Step into Composite
drawRecursively(child, parentAlpha);
}
}
} else {
// No transform for this group, offset each child.
TransformComponent compositeTransform = transformMapper.get(rootEntity);
float offsetX = compositeTransform.x, offsetY = compositeTransform.y;
if(viewPortMapper.has(rootEntity)){
offsetX = 0;
offsetY = 0;
}
for (int i = 0, n = nodeComponent.children.size; i < n; i++) {
Entity child = children[i];
LayerMapComponent rootLayers = ComponentRetriever.get(rootEntity, LayerMapComponent.class);
ZIndexComponent childZIndexComponent = ComponentRetriever.get(child, ZIndexComponent.class);
if(!rootLayers.isVisible(childZIndexComponent.layerName)) {
continue;
}
MainItemComponent childMainItemComponent = mainItemComponentMapper.get(child);
if(!childMainItemComponent.visible){
continue;
}
TransformComponent childTransformComponent = transformMapper.get(child);
float cx = childTransformComponent.x, cy = childTransformComponent.y;
childTransformComponent.x = cx + offsetX;
childTransformComponent.y = cy + offsetY;
NodeComponent childNodeComponent = nodeMapper.get(child);
int entityType = mainItemComponentMapper.get(child).entityType;
if(childNodeComponent ==null){
//Find the logic from mapper and draw it
drawableLogicMapper.getDrawable(entityType).draw(batch, child, parentAlpha);
}else{
//Step into Composite
drawRecursively(child, parentAlpha);
}
childTransformComponent.x = cx;
childTransformComponent.y = cy;
}
}
nodeComponent.children.end();
}
/** Returns the transform for this group's coordinate system.
* @param rootEntity */
protected Matrix4 computeTransform (Entity rootEntity) {
CompositeTransformComponent curCompositeTransformComponent = compositeTransformMapper.get(rootEntity);
//NodeComponent nodeComponent = nodeMapper.get(rootEntity);
ParentNodeComponent parentNodeComponent = parentNodeMapper.get(rootEntity);
TransformComponent curTransform = transformMapper.get(rootEntity);
Affine2 worldTransform = curCompositeTransformComponent.worldTransform;
//TODO origin thing
float originX = 0;
float originY = 0;
float x = curTransform.x;
float y = curTransform.y;
float rotation = curTransform.rotation;
float scaleX = curTransform.scaleX;
float scaleY = curTransform.scaleY;
worldTransform.setToTrnRotScl(x + originX, y + originY, rotation, scaleX, scaleY);
if (originX != 0 || originY != 0) worldTransform.translate(-originX, -originY);
// Find the first parent that transforms.
CompositeTransformComponent parentTransformComponent = null;
//NodeComponent parentNodeComponent;
Entity parentEntity = null;
if(parentNodeComponent != null){
parentEntity = parentNodeComponent.parentEntity;
}
// if (parentEntity != null){
//
// }
// while (parentEntity != null) {
// parentNodeComponent = nodeMapper.get(parentEntity);
// if (parentTransformComponent.transform) break;
// System.out.println("Gand");
// parentEntity = parentNodeComponent.parentEntity;
// parentTransformComponent = compositeTransformMapper.get(parentEntity);
//
// }
if (parentEntity != null){
parentTransformComponent = compositeTransformMapper.get(parentEntity);
TransformComponent transform = transformMapper.get(parentEntity);
if(curCompositeTransformComponent.transform || transform.rotation != 0 || transform.scaleX !=1 || transform.scaleY !=1)
worldTransform.preMul(parentTransformComponent.worldTransform);
//MainItemComponent main = parentEntity.getComponent(MainItemComponent.class);
//System.out.println("NAME " + main.itemIdentifier);
}
curCompositeTransformComponent.computedTransform.set(worldTransform);
return curCompositeTransformComponent.computedTransform;
}
protected void applyTransform (Entity rootEntity, Batch batch) {
CompositeTransformComponent curCompositeTransformComponent = compositeTransformMapper.get(rootEntity);
curCompositeTransformComponent.oldTransform.set(batch.getTransformMatrix());
batch.setTransformMatrix(curCompositeTransformComponent.computedTransform);
}
protected void resetTransform (Entity rootEntity, Batch batch) {
CompositeTransformComponent curCompositeTransformComponent = compositeTransformMapper.get(rootEntity);
batch.setTransformMatrix(curCompositeTransformComponent.oldTransform);
}
public void setRayHandler(RayHandler rayHandler){
this.rayHandler = rayHandler;
}
// public void setBox2dWorld(World world) {
// this.world = world;
// }
//this method has been left to avoid any compatibility issue
//setPhysicsOn has been moved in PhysicsSystem class
//Physics is now totally decoupled from rendering
@Deprecated
public void setPhysicsOn(boolean isPhysicsOn) {
//empty
}
public Batch getBatch() {
return batch;
}
}