Skip to content

Commit f143617

Browse files
committed
Update DPI retrieval acording latest Gdx changes.
1 parent fc02af7 commit f143617

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

blade-engine/src/com/bladecoder/engine/util/DPIUtils.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,56 @@
44

55
public class DPIUtils {
66
public final static float BASE_DPI = 160.0f;
7-
7+
88
/**
99
* Current DPI
1010
*/
11-
public final static float DPI = BASE_DPI * Gdx.graphics.getDensity();
11+
public final static float DPI = BASE_DPI * getLogicalDensity();
1212

1313
/**
1414
* The Google recommendations are 48 dp -> 9mm for touchable elements
1515
*/
16-
public final static float TOUCH_MIN_SIZE = 48 * Gdx.graphics.getDensity();
16+
public final static float TOUCH_MIN_SIZE = 48 * getLogicalDensity();
1717

1818
/**
1919
* The Google recommendations of space between UI objects is 8 dp
2020
*/
21-
public final static float UI_SPACE = 8 * Gdx.graphics.getDensity();
21+
public final static float UI_SPACE = 8 * getLogicalDensity();
2222

2323
/**
2424
* The Google recommendations of space from bottom or top is 16 dp
2525
*/
26-
public final static float MARGIN_SIZE = 16 * Gdx.graphics.getDensity();
26+
public final static float MARGIN_SIZE = 16 * getLogicalDensity();
2727

2828
/**
2929
* The Google recommendations are 56 dp for action buttons
3030
*/
31-
public final static float BUTTON_SIZE = 56 * Gdx.graphics.getDensity();
32-
31+
public final static float BUTTON_SIZE = 56 * getLogicalDensity();
32+
3333
/**
3434
* The Google recommendations are 24 dp for icons inside action buttons
3535
*/
36-
public final static float ICON_SIZE = 24 * Gdx.graphics.getDensity();
36+
public final static float ICON_SIZE = 24 * getLogicalDensity();
3737

38-
3938
/**
4039
* The Google recommendations are 8 dp for space between ui elements
4140
*/
42-
public final static float SPACING = 8 * Gdx.graphics.getDensity();
41+
public final static float SPACING = 8 * getLogicalDensity();
4342

4443
/**
4544
* The screen height in DP
4645
*/
47-
public final static float SCREEN_HEIGHT_DP = Gdx.graphics.getHeight()
48-
/ Gdx.graphics.getDensity();
46+
public final static float SCREEN_HEIGHT_DP = Gdx.graphics.getHeight() / getLogicalDensity();
4947

5048
public final static float NORMAL_MULTIPLIER = 1.0f; // 3-5"
5149
public final static float LARGE_MULTIPLIER = 1.5f; // 5-7"
5250
public final static float XLARGE_MULTIPLIER = 2f; // 8-10"
5351
public final static float XXLARGE_MULTIPLIER = 2.5f; // > 10"
5452

53+
public static final float getLogicalDensity() {
54+
return Gdx.graphics.getDensity() / Gdx.graphics.getBackBufferScale();
55+
}
56+
5557
/**
5658
* Calcs the button size based in screen size
5759
*
@@ -60,7 +62,7 @@ public class DPIUtils {
6062
public static float getPrefButtonSize() {
6163
return getSizeMultiplier() * BUTTON_SIZE;
6264
}
63-
65+
6466
/**
6567
* Calcs the minimum size based in screen size
6668
*
@@ -69,7 +71,7 @@ public static float getPrefButtonSize() {
6971
public static float getTouchMinSize() {
7072
return getSizeMultiplier() * TOUCH_MIN_SIZE;
7173
}
72-
74+
7375
/**
7476
* Calcs the margin size based in screen size
7577
*
@@ -78,7 +80,7 @@ public static float getTouchMinSize() {
7880
public static float getMarginSize() {
7981
return getSizeMultiplier() * MARGIN_SIZE;
8082
}
81-
83+
8284
/**
8385
* Calcs the space between ui elements based in screen size
8486
*
@@ -103,7 +105,7 @@ public static float getSpacing() {
103105
// return NORMAL_MULTIPLIER;
104106
//
105107
// }
106-
108+
107109
public static float getSizeMultiplier() {
108110
float inches = pixelsToInches(Gdx.graphics.getWidth());
109111
float s = inches / 6f;
@@ -113,17 +115,17 @@ public static float getSizeMultiplier() {
113115
}
114116

115117
public static int dpToPixels(int dp) {
116-
return (int) (dp * Gdx.graphics.getDensity());
118+
return (int) (dp * getLogicalDensity());
117119
}
118-
120+
119121
public static int pixelsToDP(int pixels) {
120-
return (int) (pixels / Gdx.graphics.getDensity());
122+
return (int) (pixels / getLogicalDensity());
121123
}
122-
124+
123125
public static float pixelsToInches(int pixels) {
124-
return (float)pixels / DPI;
126+
return pixels / DPI;
125127
}
126-
128+
127129
public static float ptToPixels(float pts) {
128130
return pts * 72 / DPI;
129131
}

0 commit comments

Comments
 (0)