Skip to content

Commit dc27f2a

Browse files
ButaniumCGjupoulton
authored andcommitted
fixed camera not updating on first toggle switch
1 parent 8b47a4b commit dc27f2a

File tree

1 file changed

+59
-52
lines changed
  • engine/community-modules/camera/src/main/resources/view/camera-module

1 file changed

+59
-52
lines changed

engine/community-modules/camera/src/main/resources/view/camera-module/CameraModule.js

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export class CameraModule {
3030

3131
setActive(active) {
3232
this.viewerActive = active
33-
if (this.currentUpdateProgress !== undefined) {
34-
this.lastFrame = -2
35-
this.updateScene(this.previousUpdateData, this.currentUpdateFrame, this.currentUpdateProgress)
33+
this.lastFrame = -2
34+
if (this.previousUpdateData !== undefined) {
35+
this.updateScene(this.previousUpdateData, this.currentUpdateFrame, this.currentUpdateProgress || 1)
3636
}
3737
}
3838

@@ -46,14 +46,18 @@ export class CameraModule {
4646
y += root.currentState.y
4747
root = root.parent
4848
debug++
49-
if (debug > 10) {
50-
throw new Error("this is too long")
49+
if (debug > 500) {
50+
throw new Error("this is too long") // break point to be sure the program doesn't
51+
// loop infinitely, should never be triggered
5152
}
5253
}
5354
return {x, y}
5455
}
5556

5657
updateScene(previousData, currentData, progress) {
58+
this.currentUpdateFrame = currentData
59+
this.currentUpdateProgress = progress
60+
this.previousUpdateData = previousData
5761
const isActive = (currentData.registered.size !== 0) && (currentData.container.entity !== null)
5862
if (!(currentData.active && this.viewerActive)) {
5963
if (isActive) {
@@ -62,56 +66,59 @@ export class CameraModule {
6266
}
6367
return
6468
}
65-
this.currentUpdateFrame = currentData
66-
this.currentUpdateProgress = progress
67-
this.previousUpdateData = previousData
68-
if (this.lastFrame !== currentData.number) {
69-
if (isActive) {
70-
this.oldCameraState = {...this.currentCameraState}
71-
let maxX, minX, minY, maxY;
72-
let first = true;
73-
entityModule.entities.forEach(
74-
entity => {
75-
76-
if (currentData.registered.get(entity.id + "")) {
77-
const relativePos = this.getRelativePosFromContainer(entity, currentData.container.entity.id)
78-
if (first) {
79-
minX = maxX = relativePos.x
80-
minY = maxY = relativePos.y
81-
first = false
82-
} else {
83-
minX = Math.min(minX, relativePos.x)
84-
minY = Math.min(minY, relativePos.y)
85-
maxX = Math.max(maxX, relativePos.x)
86-
maxY = Math.max(maxY, relativePos.y)
87-
}
69+
if (!isActive) {
70+
const boundSize = {x: 1920, y: 1080}
71+
const position = {x: 0, y: 0}
72+
this.oldCameraState = {boundSize, position}
73+
}
8874

75+
if (this.lastFrame !== currentData.number && isActive) {
76+
this.oldCameraState = {...this.currentCameraState}
77+
let maxX, minX, minY, maxY;
78+
let first = true;
79+
entityModule.entities.forEach(
80+
entity => {
81+
82+
if (currentData.registered.get(entity.id + "")) {
83+
const relativePos = this.getRelativePosFromContainer(entity, currentData.container.entity.id)
84+
if (first) {
85+
minX = maxX = relativePos.x
86+
minY = maxY = relativePos.y
87+
first = false
88+
} else {
89+
minX = Math.min(minX, relativePos.x)
90+
minY = Math.min(minY, relativePos.y)
91+
maxX = Math.max(maxX, relativePos.x)
92+
maxY = Math.max(maxY, relativePos.y)
8993
}
94+
9095
}
91-
)
92-
const averagePoint = {x: (maxX + minX) / 2, y: (maxY + minY) / 2}
93-
const boundSize = {x: maxX - minX, y: maxY - minY}
94-
const containerState = currentData.container.entity.currentState
95-
const scale2 = Math.min(HEIGHT / (boundSize.y + currentData.cameraOffset), WIDTH / (boundSize.x + currentData.cameraOffset))
96-
const scale = {x: scale2 / containerState.scaleX, y: scale2 / containerState.scaleY}
97-
this.cameraEndScale = scale
98-
99-
const newX = ((currentData.container.sizeX / 2 - averagePoint.x) * scale2
100-
- (scale2 - 1) * currentData.container.sizeX / 2
101-
+ (WIDTH / 2 - (containerState.x + currentData.container.sizeX / 2))) / containerState.scaleX
102-
103-
const newY = ((currentData.container.sizeY / 2 - averagePoint.y) * scale2
104-
- (scale2 - 1) * currentData.container.sizeY / 2
105-
+ (HEIGHT / 2 - (containerState.y + currentData.container.sizeY / 2))) / containerState.scaleY
106-
107-
this.cameraEndPosition = {x: newX, y: newY}
108-
const position = averagePoint
109-
this.cameraCurve = (position.x - this.oldZoomState.position.x) ** 2 +
110-
(position.y - this.oldZoomState.position.y) ** 2 >= currentData.cameraOffset ** 2
111-
|| Math.max(Math.abs(boundSize.x - this.oldZoomState.boundSize.x),
112-
Math.abs(boundSize.y - this.oldZoomState.boundSize.y)) > currentData.cameraOffset ? easeOut : t => t
113-
this.oldZoomState = {boundSize, position}
114-
}
96+
}
97+
)
98+
const averagePoint = {x: (maxX + minX) / 2, y: (maxY + minY) / 2}
99+
const boundSize = {x: maxX - minX, y: maxY - minY}
100+
const containerState = currentData.container.entity.currentState
101+
const scale2 = Math.min(HEIGHT / (boundSize.y + currentData.cameraOffset), WIDTH / (boundSize.x + currentData.cameraOffset))
102+
const scale = {x: scale2 / containerState.scaleX, y: scale2 / containerState.scaleY}
103+
this.cameraEndScale = scale
104+
105+
const newX = ((currentData.container.sizeX / 2 - averagePoint.x) * scale2
106+
- (scale2 - 1) * currentData.container.sizeX / 2
107+
+ (WIDTH / 2 - (containerState.x + currentData.container.sizeX / 2))) / containerState.scaleX
108+
109+
const newY = ((currentData.container.sizeY / 2 - averagePoint.y) * scale2
110+
- (scale2 - 1) * currentData.container.sizeY / 2
111+
+ (HEIGHT / 2 - (containerState.y + currentData.container.sizeY / 2))) / containerState.scaleY
112+
113+
// currentData.container.entity.graphics.scale.x = currentData.container.entity.graphics.scale.y = 0.5
114+
this.cameraEndPosition = {x: newX, y: newY}
115+
//console.log(`frame ${currentData.number}, ${Math.round(progress*100)/100}%,container to x : ${newX}, y : ${newY}, scale : ${scale}`)
116+
const position = averagePoint
117+
this.cameraCurve = (position.x - this.oldZoomState.position.x) ** 2 +
118+
(position.y - this.oldZoomState.position.y) ** 2 >= currentData.cameraOffset ** 2
119+
|| Math.max(Math.abs(boundSize.x - this.oldZoomState.boundSize.x),
120+
Math.abs(boundSize.y - this.oldZoomState.boundSize.y)) > currentData.cameraOffset ? easeOut : t => t
121+
this.oldZoomState = {boundSize, position}
115122

116123
}
117124
const realProgress = Math.abs(currentData.number - this.lastFrame) > 1 ? 1 : progress

0 commit comments

Comments
 (0)