Skip to content

Commit

Permalink
Merge pull request #4820 from moberer/CATROID-581
Browse files Browse the repository at this point in the history
CATROID-581 Refactor handling of screen sizes in StageActivity
  • Loading branch information
JayTropper authored Mar 7, 2024
2 parents 23eb15e + 2ce7b08 commit 56a6e4e
Show file tree
Hide file tree
Showing 28 changed files with 410 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testNormalBehavior() {
PhysicsObject physicsObject = physicsWorld.getPhysicsObject(sprite);
physicsObject.setType(PhysicsObject.Type.DYNAMIC);

float setYValue = -ScreenValues.SCREEN_HEIGHT / 2.0f;
float setYValue = -ScreenValues.currentScreenResolution.getHeight() / 2.0f;
sprite.look.setYInUserInterfaceDimensionUnit(setYValue);
float setVelocityYValue = -(IfOnEdgeBouncePhysicsAction.THRESHOLD_VELOCITY_TO_ACTIVATE_BOUNCE - 1.0f);
physicsObject.setVelocity(physicsObject.getVelocity().x, setVelocityYValue);
Expand All @@ -106,7 +106,7 @@ public void testVelocityThresholdAtTopCollision() {
PhysicsObject physicsObject = physicsWorld.getPhysicsObject(sprite);
physicsObject.setType(PhysicsObject.Type.DYNAMIC);

float setYValue = ScreenValues.SCREEN_HEIGHT / 2.0f;
float setYValue = ScreenValues.currentScreenResolution.getHeight() / 2.0f;
sprite.look.setYInUserInterfaceDimensionUnit(setYValue);
float setVelocityYValue = IfOnEdgeBouncePhysicsAction.THRESHOLD_VELOCITY_TO_ACTIVATE_BOUNCE + 0.5f;
physicsObject.setVelocity(physicsObject.getVelocity().x, setVelocityYValue);
Expand Down Expand Up @@ -134,9 +134,11 @@ public void testSpriteOverlapsRightAndTopAxis() {
PhysicsObject physicsObject = physicsWorld.getPhysicsObject(sprite);
physicsObject.setType(PhysicsObject.Type.DYNAMIC);

float setXValue = ScreenValues.SCREEN_WIDTH / 2.0f - sprite.look.getLookData().getPixmap().getWidth() / 4.0f;
float setXValue =
ScreenValues.currentScreenResolution.getWidth() / 2.0f - sprite.look.getLookData().getPixmap().getWidth() / 4.0f;
sprite.look.setXInUserInterfaceDimensionUnit(setXValue);
float setYValue = ScreenValues.SCREEN_HEIGHT / 2.0f - sprite.look.getLookData().getPixmap().getHeight() / 4.0f;
float setYValue =
ScreenValues.currentScreenResolution.getHeight() / 2.0f - sprite.look.getLookData().getPixmap().getHeight() / 4.0f;
sprite.look.setYInUserInterfaceDimensionUnit(setYValue);

float setVelocityXValue = 400.0f;
Expand Down Expand Up @@ -198,9 +200,11 @@ public void testCollisionBroadcastOnIfOnEdgeBounce() throws Exception {
PhysicsObject physicsObject = physicsWorld.getPhysicsObject(sprite);
physicsObject.setType(PhysicsObject.Type.DYNAMIC);

float setXValue = ScreenValues.SCREEN_WIDTH / 2.0f - sprite.look.getLookData().getPixmap().getWidth() / 4.0f;
float setXValue =
ScreenValues.currentScreenResolution.getWidth() / 2.0f - sprite.look.getLookData().getPixmap().getWidth() / 4.0f;
sprite.look.setXInUserInterfaceDimensionUnit(setXValue);
float setYValue = ScreenValues.SCREEN_HEIGHT / 2.0f - sprite.look.getLookData().getPixmap().getHeight() / 4.0f;
float setYValue =
ScreenValues.currentScreenResolution.getHeight() / 2.0f - sprite.look.getLookData().getPixmap().getHeight() / 4.0f;
sprite.look.setYInUserInterfaceDimensionUnit(setYValue);

assertEquals(setXValue, sprite.look.getXInUserInterfaceDimensionUnit());
Expand Down Expand Up @@ -259,7 +263,8 @@ private Action setUpBounceWithSteps(float direction, float xValue, float yValue)
@Test
public void testOnEdgeBounceWithStepsRight() {
Action ifOnEdgeBouncePhysicsAction = setUpBounceWithSteps(90.0f,
ScreenValues.SCREEN_WIDTH / 2.0f, sprite.look.getYInUserInterfaceDimensionUnit());
ScreenValues.currentScreenResolution.getWidth() / 2.0f,
sprite.look.getYInUserInterfaceDimensionUnit());
float xValueBeforeAct = sprite.look.getXInUserInterfaceDimensionUnit();
float directionBefore = sprite.look.getMotionDirectionInUserInterfaceDimensionUnit();
ifOnEdgeBouncePhysicsAction.act(0.1f);
Expand All @@ -273,7 +278,8 @@ public void testOnEdgeBounceWithStepsRight() {
@Test
public void testOnEdgeBounceWithStepsLeft() {
Action ifOnEdgeBouncePhysicsAction = setUpBounceWithSteps(-90.0f,
-ScreenValues.SCREEN_WIDTH / 2.0f, sprite.look.getYInUserInterfaceDimensionUnit());
-ScreenValues.currentScreenResolution.getWidth() / 2.0f,
sprite.look.getYInUserInterfaceDimensionUnit());
float xValueBeforeAct = sprite.look.getXInUserInterfaceDimensionUnit();
float directionBefore = sprite.look.getMotionDirectionInUserInterfaceDimensionUnit();
ifOnEdgeBouncePhysicsAction.act(0.1f);
Expand All @@ -287,7 +293,8 @@ public void testOnEdgeBounceWithStepsLeft() {
@Test
public void testOnEdgeBounceWithStepsTop() {
Action ifOnEdgeBouncePhysicsAction = setUpBounceWithSteps(0,
sprite.look.getXInUserInterfaceDimensionUnit(), ScreenValues.SCREEN_HEIGHT / 2.0f);
sprite.look.getXInUserInterfaceDimensionUnit(),
ScreenValues.currentScreenResolution.getHeight() / 2.0f);
float yValueBeforeAct = sprite.look.getYInUserInterfaceDimensionUnit();
ifOnEdgeBouncePhysicsAction.act(0.1f);
float yValueAfterAct = sprite.look.getYInUserInterfaceDimensionUnit();
Expand All @@ -299,7 +306,8 @@ public void testOnEdgeBounceWithStepsTop() {
@Test
public void testOnEdgeBounceWithStepsBottom() {
Action ifOnEdgeBouncePhysicsAction = setUpBounceWithSteps(180,
sprite.look.getXInUserInterfaceDimensionUnit(), -ScreenValues.SCREEN_HEIGHT / 2.0f);
sprite.look.getXInUserInterfaceDimensionUnit(),
-ScreenValues.currentScreenResolution.getHeight() / 2.0f);
float yValueBeforeAct = sprite.look.getYInUserInterfaceDimensionUnit();
ifOnEdgeBouncePhysicsAction.act(0.1f);
float yValueAfterAct = sprite.look.getYInUserInterfaceDimensionUnit();
Expand All @@ -311,52 +319,56 @@ public void testOnEdgeBounceWithStepsBottom() {
@Test
public void testFlipRightEdgeWithStepsAndLeftRightRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_LEFT_RIGHT_ONLY, 90.0f,
ScreenValues.SCREEN_WIDTH / 2.0f, 0);
ScreenValues.currentScreenResolution.getWidth() / 2.0f, 0);
assertTrue(sprite.look.isFlipped());
}

@Test
public void testFlipLeftEdgeWithStepsAndLeftRightRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_LEFT_RIGHT_ONLY, -90.0f,
-ScreenValues.SCREEN_WIDTH / 2.0f, 0);
-ScreenValues.currentScreenResolution.getWidth() / 2.0f, 0);
assertFalse(sprite.look.isFlipped());
}

@Test
public void testFlipTopEdgeWithStepsAndLeftRightRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_LEFT_RIGHT_ONLY, 0, 0, ScreenValues.SCREEN_HEIGHT / 2.0f);
startActionWithRotationStyle(Look.ROTATION_STYLE_LEFT_RIGHT_ONLY, 0, 0,
ScreenValues.currentScreenResolution.getHeight() / 2.0f);
assertFalse(sprite.look.getLookData().getTextureRegion().isFlipY());
}

@Test
public void testFlipBottomEdgeWithStepsAndLeftRightRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_LEFT_RIGHT_ONLY, 180, 0, ScreenValues.SCREEN_HEIGHT / 2.0f);
startActionWithRotationStyle(Look.ROTATION_STYLE_LEFT_RIGHT_ONLY, 180, 0,
ScreenValues.currentScreenResolution.getHeight() / 2.0f);
assertFalse(sprite.look.getLookData().getTextureRegion().isFlipY());
}

@Test
public void testFlipRightEdgeWithStepsAndNoRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_NONE, 90.0f,
ScreenValues.SCREEN_WIDTH / 2.0f, 0);
ScreenValues.currentScreenResolution.getWidth() / 2.0f, 0);
assertFalse(sprite.look.isFlipped());
}

@Test
public void testFlipLeftEdgeWithStepsAndNoRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_NONE, -90.0f,
-ScreenValues.SCREEN_WIDTH / 2.0f, 0);
-ScreenValues.currentScreenResolution.getWidth() / 2.0f, 0);
assertFalse(sprite.look.isFlipped());
}

@Test
public void testFlipTopEdgeWithStepsAndNoRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_NONE, 0, 0, ScreenValues.SCREEN_HEIGHT / 2.0f);
startActionWithRotationStyle(Look.ROTATION_STYLE_NONE, 0, 0,
ScreenValues.currentScreenResolution.getHeight() / 2.0f);
assertFalse(sprite.look.getLookData().getTextureRegion().isFlipY());
}

@Test
public void testFlipBottomEdgeWithStepsAndNoRotationStyle() {
startActionWithRotationStyle(Look.ROTATION_STYLE_NONE, 180, 0, ScreenValues.SCREEN_HEIGHT / 2.0f);
startActionWithRotationStyle(Look.ROTATION_STYLE_NONE, 180, 0,
ScreenValues.currentScreenResolution.getHeight() / 2.0f);
assertFalse(sprite.look.getLookData().getTextureRegion().isFlipY());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ private float bounce(float bounceFactor) {
physicsWorld.step(0.3f);
}

float y = physicsWorld.getPhysicsObject(sprite).getY() + (ScreenValues.SCREEN_HEIGHT / 2);
float y =
physicsWorld.getPhysicsObject(sprite).getY() + (ScreenValues.currentScreenResolution.getHeight() / 2);
physicsWorld.step(0.3f);

while (y < (physicsWorld.getPhysicsObject(sprite).getY() + (ScreenValues.SCREEN_HEIGHT / 2))) {
y = physicsWorld.getPhysicsObject(sprite).getY() + (ScreenValues.SCREEN_HEIGHT / 2);
while (y < (physicsWorld.getPhysicsObject(sprite).getY() + (ScreenValues.currentScreenResolution.getHeight() / 2))) {
y =
physicsWorld.getPhysicsObject(sprite).getY() + (ScreenValues.currentScreenResolution.getHeight() / 2);
physicsWorld.step(0.3f);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class ScreenshotSaverTest(private val name: String, private val fileName: String
@Before
fun setUp() {
ScreenValues.setToDefaultScreenSize()
val height = ScreenValues.SCREEN_HEIGHT
val width = ScreenValues.SCREEN_WIDTH
val height = ScreenValues.currentScreenResolution.height
val width = ScreenValues.currentScreenResolution.width
dummyData = ByteArray(NUMBER_OF_COLORS * width * height)
val stageActivity = activityTestRule.activity
val folder = stageActivity.cacheDir.absolutePath + "/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.catrobat.catroid.content.bricks.Brick;
import org.catrobat.catroid.content.bricks.HideBrick;
import org.catrobat.catroid.io.StorageOperations;
import org.catrobat.catroid.utils.Resolution;
import org.catrobat.catroid.utils.Utils;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -62,11 +63,10 @@ public class DefaultProjectComparatorTest {

@Before
public void setUp() throws IOException {
screenWidthBuffer = ScreenValues.SCREEN_WIDTH;
screenHeightBuffer = ScreenValues.SCREEN_HEIGHT;
screenWidthBuffer = ScreenValues.currentScreenResolution.getWidth();
screenHeightBuffer = ScreenValues.currentScreenResolution.getHeight();

ScreenValues.SCREEN_WIDTH = 480;
ScreenValues.SCREEN_HEIGHT = 800;
ScreenValues.currentScreenResolution = new Resolution(480, 800);

if (projectDir.isDirectory()) {
StorageOperations.deleteDir(projectDir);
Expand All @@ -78,8 +78,8 @@ public void setUp() throws IOException {

@After
public void tearDown() throws Exception {
ScreenValues.SCREEN_WIDTH = screenWidthBuffer;
ScreenValues.SCREEN_HEIGHT = screenHeightBuffer;
ScreenValues.currentScreenResolution = new Resolution(screenWidthBuffer,
screenHeightBuffer);

if (projectDir.isDirectory()) {
StorageOperations.deleteDir(projectDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.catrobat.catroid.uiespresso.util.UiTestUtils;
import org.catrobat.catroid.uiespresso.util.matchers.StageMatchers;
import org.catrobat.catroid.uiespresso.util.rules.BaseActivityTestRule;
import org.catrobat.catroid.utils.Resolution;
import org.catrobat.catroid.utils.ScreenValueHandler;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -114,8 +115,7 @@ public void testRequestScreenshotShouldSaveStageContentToFile() {
}

private void createProjectWithBlueSprite(String projectName) throws IOException {
ScreenValues.SCREEN_HEIGHT = PROJECT_HEIGHT;
ScreenValues.SCREEN_WIDTH = PROJECT_WIDTH;
ScreenValues.currentScreenResolution = new Resolution(PROJECT_WIDTH, PROJECT_HEIGHT);

Project project = UiTestUtils.createDefaultTestProject(projectName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.catrobat.catroid.uiespresso.util.UiTestUtils;
import org.catrobat.catroid.uiespresso.util.matchers.StageMatchers;
import org.catrobat.catroid.uiespresso.util.rules.BaseActivityTestRule;
import org.catrobat.catroid.utils.Resolution;
import org.catrobat.catroid.utils.ScreenValueHandler;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -83,8 +84,7 @@ public void checkForBlueSpriteColor() {
}

public Project createProjectWithBlueSprite(String projectName) throws IOException {
ScreenValues.SCREEN_HEIGHT = PROJECT_HEIGHT;
ScreenValues.SCREEN_WIDTH = PROJECT_WIDTH;
ScreenValues.currentScreenResolution = new Resolution(PROJECT_WIDTH, PROJECT_HEIGHT);

Project project = UiTestUtils.createDefaultTestProject(projectName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.catrobat.catroid.R;
import org.catrobat.catroid.common.LookData;
import org.catrobat.catroid.common.ScreenValues;
import org.catrobat.catroid.content.Project;
import org.catrobat.catroid.content.Scene;
import org.catrobat.catroid.content.Script;
Expand All @@ -52,8 +53,6 @@

import static org.catrobat.catroid.common.Constants.DEFAULT_IMAGE_EXTENSION;
import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME;
import static org.catrobat.catroid.common.ScreenValues.SCREEN_HEIGHT;
import static org.catrobat.catroid.common.ScreenValues.SCREEN_WIDTH;
import static org.catrobat.catroid.common.Constants.SCREENSHOT_AUTOMATIC_FILE_NAME;

public class DefaultExampleProject extends DefaultProjectCreator {
Expand Down Expand Up @@ -98,8 +97,8 @@ public Project createDefaultProject(String name, Context context, boolean landsc
backgroundImageScaleFactor = ImageEditing.calculateScaleFactor(
options.outWidth,
options.outHeight,
SCREEN_WIDTH,
SCREEN_HEIGHT);
ScreenValues.currentScreenResolution.getWidth(),
ScreenValues.currentScreenResolution.getHeight());

Scene scene = project.getDefaultScene();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,17 @@ object VisualDetectionHandler {
val relativeY = x / imageWidth
coordinatesFromRelativePosition(
1 - relativeX,
ScreenValues.SCREEN_WIDTH / aspectRatio,
ScreenValues.currentScreenResolution.width / aspectRatio,
if (frontCamera) relativeY else 1 - relativeY,
ScreenValues.SCREEN_WIDTH.toDouble()
ScreenValues.currentScreenResolution.width.toDouble()
)
} else {
val relativeX = x / imageHeight
coordinatesFromRelativePosition(
if (frontCamera) 1 - relativeX else relativeX,
ScreenValues.SCREEN_HEIGHT / aspectRatio,
ScreenValues.currentScreenResolution.height / aspectRatio,
1 - y / imageWidth,
ScreenValues.SCREEN_HEIGHT.toDouble()
ScreenValues.currentScreenResolution.height.toDouble()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,26 @@
package org.catrobat.catroid.common;

import org.catrobat.catroid.content.Project;
import org.catrobat.catroid.utils.Resolution;

public final class ScreenValues {

private static final int DEFAULT_SCREEN_WIDTH = 1280;
private static final int DEFAULT_SCREEN_HEIGHT = 768;

// CHECKSTYLE DISABLE StaticVariableNameCheck FOR 2 LINES
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;

public static final int CAST_SCREEN_WIDTH = 1280;
public static final int CAST_SCREEN_HEIGHT = 720;
private static final Resolution DEFAULT_SCREEN_RESOLUTION = new Resolution(1280, 768);
public static final Resolution CAST_SCREEN_RESOLUTION = new Resolution(1280, 720);
public static Resolution currentScreenResolution;

private ScreenValues() {
throw new AssertionError();
}

public static float getAspectRatio() {
if (SCREEN_WIDTH == 0 || SCREEN_HEIGHT == 0) {
setToDefaultScreenSize();
}
return (float) SCREEN_WIDTH / (float) SCREEN_HEIGHT;
}

public static void setToDefaultScreenSize() {
SCREEN_WIDTH = DEFAULT_SCREEN_WIDTH;
SCREEN_HEIGHT = DEFAULT_SCREEN_HEIGHT;
}

public static int getScreenHeightForProject(Project project) {
if (project.isCastProject()) {
return CAST_SCREEN_HEIGHT;
}

return SCREEN_HEIGHT;
currentScreenResolution = DEFAULT_SCREEN_RESOLUTION;
}

public static int getScreenWidthForProject(Project project) {
public static Resolution getResolutionForProject(Project project) {
if (project.isCastProject()) {
return CAST_SCREEN_WIDTH;
return CAST_SCREEN_RESOLUTION;
}

return SCREEN_WIDTH;
return currentScreenResolution;
}
}
Loading

0 comments on commit 56a6e4e

Please sign in to comment.