From bca5540f6376bb4cc4d06149bb94884c55e521a2 Mon Sep 17 00:00:00 2001 From: MJ Date: Thu, 26 Mar 2020 22:38:32 +0100 Subject: [PATCH] Moved LetterboxingViewport from app to graphics. #183 --- CHANGELOG.md | 2 ++ app/README.md | 14 ----------- graphics/README.md | 24 +++++++++++++++++++ .../src/main/kotlin/ktx/graphics/camera.kt | 14 +++++------ .../test/kotlin/ktx/graphics/cameraTest.kt | 2 +- 5 files changed, 34 insertions(+), 22 deletions(-) rename app/src/main/kotlin/ktx/app/letterboxingViewport.kt => graphics/src/main/kotlin/ktx/graphics/camera.kt (88%) rename app/src/test/kotlin/ktx/app/letterboxingViewportTest.kt => graphics/src/test/kotlin/ktx/graphics/cameraTest.kt (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 810e42a9..6380f566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,10 @@ - `profile` inlined function allows to profile an operation with the LibGDX `PerformanceCounter`. - `PerformanceCounter.profile` inlined extension method eases usage of `PerformanceCounter` API. - `PerformanceCounter.prettyPrint` allows to print basic performance data after profiling. +- **[CHANGE]** (`ktx-app`) `LetterboxingViewport` moved from `ktx-app` to `ktx-graphics`. - **[FEATURE]** (`ktx-ashley`) Added `Entity.contains` (`in` operator) that checks if an `Entity` has a `Component`. - **[FEATURE]** (`ktx-async`) Added `RenderingScope` factory function for custom scopes using rendering thread dispatcher. +- **[FEATURE]** (`ktx-graphics`) Added `LetterboxingViewport` from `ktx-app`. - **[FEATURE]** (`ktx-graphics`) Added `takeScreenshot` utility function that allows to save a screenshot of the application. - **[FEATURE]** (`ktx-math`) Added `lerp` and `interpolate` extension functions for `Float` ranges. - **[FEATURE]** (`ktx-preferences`) Added a new KTX module: Preferences API extensions. diff --git a/app/README.md b/app/README.md index 921cd996..4c2044c1 100644 --- a/app/README.md +++ b/app/README.md @@ -33,10 +33,6 @@ being a class like `com.badlogic.gdx.InputAdapter`. - `clearScreen` is an inlined utility function that hides the OpenGL calls, allowing to clear the screen with a chosen color. -- `LetterboxingViewport` combines `ScreenViewport` and `FitViewport` behavior: it targets a specific aspect ratio and -applies letterboxing like `FitViewport`, but it does not scale rendered objects when resized, keeping them in fixed size -similarly to `ScreenViewport`. Thanks to customizable target PPI value, it is ideal for GUIs and can easily support -different screen sizes. - `emptyScreen` provides no-op implementations of `Screen`. #### Profiling @@ -125,16 +121,6 @@ class MyInputListener : KtxInputAdapter { } ``` -Creating and customizing a new `LetterboxingViewport`: - -```Kotlin -import ktx.app.LetterboxingViewport - -val viewport: Viewport = LetterboxingViewport(targetPpiX = 96f, targetPpiY = 96f, aspectRatio = 4f / 3f) -// Updating viewport on resize: -viewport.update(Gdx.graphics.width, Gdx.graphics.height, true) -``` - Profiling an operation: ```Kotlin diff --git a/graphics/README.md b/graphics/README.md index 41b915b8..add877fe 100644 --- a/graphics/README.md +++ b/graphics/README.md @@ -44,6 +44,13 @@ original `ShapeRenderer` methods and perform the same actions. The methods inclu A `use` inlined extension method is also available for `ShapeRenderer`, ensuring you won't have unbalanced `begin()` and `end()` calls. +#### Cameras and viewports + +- `LetterboxingViewport` combines `ScreenViewport` and `FitViewport` behavior: it targets a specific aspect ratio and +applies letterboxing like `FitViewport`, but it does not scale rendered objects when resized, keeping them in fixed size +similarly to `ScreenViewport`. Thanks to customizable target PPI value, it is ideal for GUIs and can easily support +different screen sizes. + ### Usage examples Using a `Batch`: @@ -172,6 +179,23 @@ import ktx.graphics.takeScreenshot takeScreenshot(Gdx.files.external("mygame/screenshot.png")) ``` +Creating and customizing a new `LetterboxingViewport`: + +```Kotlin +import com.badlogic.gdx.ApplicationAdapter +import com.badlogic.gdx.utils.viewport.Viewport +import ktx.graphics.LetterboxingViewport + +class Application: ApplicationAdapter() { + val viewport: Viewport = LetterboxingViewport(targetPpiX = 96f, targetPpiY = 96f, aspectRatio = 4f / 3f) + + override fun resize(width: Int, height: Int) { + // Updating viewport to the new screen size: + viewport.update(width, height, true) + } +} +``` + #### Synergy Use [`ktx-math`](../math) for `Vector2` and `Vector3` extensions, including idiomatic Kotlin factory diff --git a/app/src/main/kotlin/ktx/app/letterboxingViewport.kt b/graphics/src/main/kotlin/ktx/graphics/camera.kt similarity index 88% rename from app/src/main/kotlin/ktx/app/letterboxingViewport.kt rename to graphics/src/main/kotlin/ktx/graphics/camera.kt index 7541e262..f18c04f4 100644 --- a/app/src/main/kotlin/ktx/app/letterboxingViewport.kt +++ b/graphics/src/main/kotlin/ktx/graphics/camera.kt @@ -1,4 +1,4 @@ -package ktx.app +package ktx.graphics import com.badlogic.gdx.Application.ApplicationType.Android import com.badlogic.gdx.Application.ApplicationType.iOS @@ -22,16 +22,16 @@ import com.badlogic.gdx.utils.viewport.ScalingViewport * FitViewport is excellent for the actual (2D) game rendering. * @param targetPpiX this is the targeted pixel per inch ratio on X axis, which allows to scale the viewport - * correctly on different devices. Usually about 96 for desktop and WebGL platforms, 160 for mobiles. - * Make sure to call [updateScale] after changing this variable. + * correctly on different devices. Usually about 96 for desktop and WebGL platforms, 160 for mobiles. + * Make sure to call [updateScale] after changing this variable. * @param targetPpiY targeted pixel per inch ratio on Y axis. Usually about 96 for desktop and WebGL platforms, 160 - * for mobiles. Make sure to call [updateScale] after changing this variable. + * for mobiles. Make sure to call [updateScale] after changing this variable. * @param aspectRatio width divided by height. Will preserve this aspect ratio by applying letterboxing. */ class LetterboxingViewport( - var targetPpiX: Float = defaultTargetPpi, - var targetPpiY: Float = defaultTargetPpi, - var aspectRatio: Float = 4f / 3f) : ScalingViewport(Scaling.fit, 0f, 0f) { + var targetPpiX: Float = defaultTargetPpi, + var targetPpiY: Float = defaultTargetPpi, + var aspectRatio: Float = 4f / 3f) : ScalingViewport(Scaling.fit, 0f, 0f) { /** You can directly modify unit per pixel ratio (bypassing PPI check) by modifying this value. * @see updateScale */ var scaleX = 0f diff --git a/app/src/test/kotlin/ktx/app/letterboxingViewportTest.kt b/graphics/src/test/kotlin/ktx/graphics/cameraTest.kt similarity index 99% rename from app/src/test/kotlin/ktx/app/letterboxingViewportTest.kt rename to graphics/src/test/kotlin/ktx/graphics/cameraTest.kt index b34d82d9..a9228799 100644 --- a/app/src/test/kotlin/ktx/app/letterboxingViewportTest.kt +++ b/graphics/src/test/kotlin/ktx/graphics/cameraTest.kt @@ -1,4 +1,4 @@ -package ktx.app +package ktx.graphics import com.badlogic.gdx.Gdx import com.badlogic.gdx.backends.lwjgl.LwjglNativesLoader