Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaked colony rendering screen behavior, added panning limits. #1107

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 80 additions & 22 deletions src/hu/openig/screen/items/PlanetScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ public static class Mine {
boolean zoomDirection;
/** Zoom to normal. */
boolean zoomNormal;
/** Center the view after changing the planet. */
boolean centerView = true;
/** The weather overlay. */
WeatherOverlay weatherOverlay;
/** The weather sound is running? */
Expand All @@ -319,6 +317,10 @@ public static class Mine {
double previousSimulationTime = 0;
/** Ratio of the frame time since last simulation and simulation time since last simulation step. */
double simFrameRatio = 0;
/** The ratio of the X coordinates of the center of the rendering screen and the center of the planet surface. */
double xViewRatio = 0;
/** The ratio of the Y coordinates of the center of the rendering screen and the center of the planet surface. */
double yViewRatio = 0;

@Override
public void onFinish() {
Expand All @@ -330,19 +332,19 @@ public boolean keyboard(KeyEvent e) {
boolean rep = false;
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
render.offsetY += 28;
render.incrementOffsetY(28);
rep = true;
break;
case KeyEvent.VK_DOWN:
render.offsetY -= 28;
render.incrementOffsetY(-28);
rep = true;
break;
case KeyEvent.VK_LEFT:
render.offsetX += 54;
render.incrementOffsetX(54);
rep = true;
break;
case KeyEvent.VK_RIGHT:
render.offsetX -= 54;
render.incrementOffsetX(-54);
rep = true;
break;
case KeyEvent.VK_ESCAPE:
Expand Down Expand Up @@ -578,7 +580,7 @@ public void invoke() {
}
});
if (surface() != null) {
centerScreen();
setViewLocation();
}
focused = render;
moveSelect = false;
Expand All @@ -592,7 +594,6 @@ public void invoke() {
public void onLeave() {

cancelWeatherSound();

placementMode = false;
pavementMode = false;
buildingsPanel.build.down = false;
Expand Down Expand Up @@ -843,6 +844,43 @@ public Location getLocationAt(int mx, int my) {
}
return null;
}

/** Increment the rendering screen's X offset by the give amount
* @param offset the offset to increment by
*/
public void incrementOffsetX(int offset) {
setOffsetX(offsetX + offset);
}
/** Set the rendering screen's X offset to the given new offset. Only update the offset
* if with the new offset the current rendering screen's center is still within the planet surface's
* area.
* @param newOffset the offset to increment by
*/
public void setOffsetX(int newOffset) {
double testViewRatio = (newOffset - width/2)/render.scale / surface().boundingRectangle.width;
if (testViewRatio <= 0 && testViewRatio >= -1) {
offsetX = newOffset;
xViewRatio = (offsetX - width/2)/scale / surface().boundingRectangle.width;
}
}
/** Increment the rendering screen's Y offset by the give amount
* @param offset the offset to increment by
*/
public void incrementOffsetY(int offset) {
setOffsetY(offsetY + offset);
}
/** Set the rendering screen's Y offset to the given new offset. Only update the offset
* if with the new offset the current rendering screen's center is still within the planet surface's
* area.
* @param newOffset the offset to increment by
*/
public void setOffsetY(int newOffset) {
double testViewRatio = (newOffset - height/2)/render.scale / surface().boundingRectangle.height;
if (testViewRatio <= 0 && testViewRatio >= -1) {
offsetY = newOffset;
yViewRatio = (offsetY - height / 2) / render.scale / surface().boundingRectangle.height;
}
}
/**
* Retrieve the gun at the given location.
* @param mx the mouse X
Expand Down Expand Up @@ -944,8 +982,8 @@ public boolean mouse(UIMouse e) {
lastY = e.y;
doDragMode(true);
}
offsetX += e.x - lastX;
offsetY += e.y - lastY;
incrementOffsetX(e.x - lastX);
incrementOffsetY(e.y - lastY);

lastX = e.x;
lastY = e.y;
Expand Down Expand Up @@ -1174,22 +1212,22 @@ public boolean mouse(UIMouse e) {
} else {
doZoomOut();
}
offsetX = (int)(e.x - scale * mx);
offsetY = (int)(e.y - scale * my);
setOffsetX((int)(e.x - scale * mx));
setOffsetY((int)(e.y - scale * my));
rep = true;
} else
if (e.has(Modifier.SHIFT)) {
if (e.z < 0) {
offsetX += 54;
incrementOffsetX(54);
} else {
offsetX -= 54;
incrementOffsetX(-54);
}
rep = true;
} else {
if (e.z < 0) {
offsetY += 28 * 3;
incrementOffsetY(28 * 3);
} else {
offsetY -= 28 * 3;
incrementOffsetY(-28 * 3);
}
rep = true;
}
Expand Down Expand Up @@ -3580,7 +3618,6 @@ public void invoke() {
public void invoke() {
buttonSound(SoundType.CLICK_HIGH_2);
player().movePrevPlanet();
centerView = true;
}
};
next = new UIImageButton(commons.starmap().forwards);
Expand All @@ -3590,7 +3627,6 @@ public void invoke() {
public void invoke() {
buttonSound(SoundType.CLICK_HIGH_2);
player().moveNextPlanet();
centerView = true;
}
};

Expand Down Expand Up @@ -3718,6 +3754,21 @@ public void onResize() {
startBattle.location(sidebarNavigation.x - startBattle.width, sidebarNavigation.y + sidebarNavigation.height - startBattle.height);

render.bounds(window.x, window.y, window.width, window.height);
// When resizing fix the location of the rendering screen so it does not end up far outside the planet surface area
double testViewRatioX = (render.offsetX - width/2)/render.scale / surface().boundingRectangle.width;
double testViewRatioY = (render.offsetY - height/2)/render.scale / surface().boundingRectangle.height;
if (testViewRatioX > 0) {
render.offsetX = width/2;
}
if (testViewRatioX < -1) {
render.offsetX = (int) -(surface().boundingRectangle.width * render.scale - width/2);
}
if (testViewRatioY > 0) {
render.offsetY = height/2;
}
if (testViewRatioY < -1) {
render.offsetY = (int) -(surface().boundingRectangle.height * render.scale - height/2);
}

leftFill.bounds(sidebarBuildings.x, sidebarBuildings.y + sidebarBuildings.height,

Expand Down Expand Up @@ -7235,10 +7286,7 @@ public PlanetStatistics update(PlanetSurface surface) {
if (battle == null) {
clearGroundBattle();
}
if (centerView) {
centerView = false;
centerScreen();
}
setViewLocation();
}
// check if the AI has removed any building while we were looking at its planet
if (currentBuilding != null) {
Expand Down Expand Up @@ -7331,6 +7379,16 @@ void centerScreen() {
render.offsetX = -(int)((surface().boundingRectangle.width * render.scale - width) / 2);
render.offsetY = -(int)((surface().boundingRectangle.height * render.scale - height) / 2);
}

/** Set the render screen's location, if no previous screen location ratio is available then center the screen. */
void setViewLocation() {
if (xViewRatio != 0 && yViewRatio != 0) {
render.offsetX = (int) (xViewRatio * planet().surface.boundingRectangle.width * render.scale + width/2);
render.offsetY = (int) (yViewRatio * planet().surface.boundingRectangle.height * render.scale + height/2);
} else {
centerScreen();
}
}
/**
* Retreat from the current attack.
*/
Expand Down