11package com .bladecoder .engine .util ;
22
3+ import com .badlogic .gdx .Application ;
34import com .badlogic .gdx .Gdx ;
45
56public class DPIUtils {
6- public final static float BASE_DPI = 160.0f ;
7-
8- /**
9- * Current DPI
10- */
11- public final static float DPI = BASE_DPI * getLogicalDensity ();
12-
13- /**
14- * The Google recommendations are 48 dp -> 9mm for touchable elements
15- */
16- public final static float TOUCH_MIN_SIZE = 48 * getLogicalDensity ();
17-
18- /**
19- * The Google recommendations of space between UI objects is 8 dp
20- */
21- public final static float UI_SPACE = 8 * getLogicalDensity ();
22-
23- /**
24- * The Google recommendations of space from bottom or top is 16 dp
25- */
26- public final static float MARGIN_SIZE = 16 * getLogicalDensity ();
27-
28- /**
29- * The Google recommendations are 56 dp for action buttons
30- */
31- public final static float BUTTON_SIZE = 56 * getLogicalDensity ();
32-
33- /**
34- * The Google recommendations are 24 dp for icons inside action buttons
35- */
36- public final static float ICON_SIZE = 24 * getLogicalDensity ();
37-
38- /**
39- * The Google recommendations are 8 dp for space between ui elements
40- */
41- public final static float SPACING = 8 * getLogicalDensity ();
42-
43- /**
44- * The screen height in DP
45- */
46- public final static float SCREEN_HEIGHT_DP = Gdx .graphics .getHeight () / getLogicalDensity ();
47-
48- public final static float NORMAL_MULTIPLIER = 1.0f ; // 3-5"
49- public final static float LARGE_MULTIPLIER = 1.5f ; // 5-7"
50- public final static float XLARGE_MULTIPLIER = 2f ; // 8-10"
51- public final static float XXLARGE_MULTIPLIER = 2.5f ; // > 10"
52-
53- public static final float getLogicalDensity () {
54- return Gdx .graphics .getDensity () / Gdx .graphics .getBackBufferScale ();
55- }
56-
57- /**
58- * Calcs the button size based in screen size
59- *
60- * @return The recommended size in pixels
61- */
62- public static float getPrefButtonSize () {
63- return getSizeMultiplier () * BUTTON_SIZE ;
64- }
65-
66- /**
67- * Calcs the minimum size based in screen size
68- *
69- * @return The recommended size in pixels
70- */
71- public static float getTouchMinSize () {
72- return getSizeMultiplier () * TOUCH_MIN_SIZE ;
73- }
74-
75- /**
76- * Calcs the margin size based in screen size
77- *
78- * @return The recommended size in pixels
79- */
80- public static float getMarginSize () {
81- return getSizeMultiplier () * MARGIN_SIZE ;
82- }
83-
84- /**
85- * Calcs the space between ui elements based in screen size
86- *
87- * @return The recommended size in pixels
88- */
89- public static float getSpacing () {
90- return getSizeMultiplier () * SPACING ;
91- }
7+ public final static float BASE_DPI = 160.0f ;
8+
9+ /**
10+ * Current DPI
11+ */
12+ public final static float DPI = BASE_DPI * getLogicalDensity ();
13+
14+ /**
15+ * The Google recommendations are 48 dp -> 9mm for touchable elements
16+ */
17+ public final static float TOUCH_MIN_SIZE = 48 * getLogicalDensity ();
18+
19+ /**
20+ * The Google recommendations of space between UI objects is 8 dp
21+ */
22+ public final static float UI_SPACE = 8 * getLogicalDensity ();
23+
24+ /**
25+ * The Google recommendations of space from bottom or top is 16 dp
26+ */
27+ public final static float MARGIN_SIZE = 16 * getLogicalDensity ();
28+
29+ /**
30+ * The Google recommendations are 56 dp for action buttons
31+ */
32+ public final static float BUTTON_SIZE = 56 * getLogicalDensity ();
33+
34+ /**
35+ * The Google recommendations are 24 dp for icons inside action buttons
36+ */
37+ public final static float ICON_SIZE = 24 * getLogicalDensity ();
38+
39+ /**
40+ * The Google recommendations are 8 dp for space between ui elements
41+ */
42+ public final static float SPACING = 8 * getLogicalDensity ();
43+
44+ /**
45+ * The screen height in DP
46+ */
47+ public final static float SCREEN_HEIGHT_DP = Gdx .graphics .getHeight () / getLogicalDensity ();
48+
49+ public final static float NORMAL_MULTIPLIER = 1.0f ; // 3-5"
50+ public final static float LARGE_MULTIPLIER = 1.5f ; // 5-7"
51+ public final static float XLARGE_MULTIPLIER = 2f ; // 8-10"
52+ public final static float XXLARGE_MULTIPLIER = 2.5f ; // > 10"
53+
54+ public static final float getLogicalDensity () {
55+ return Gdx .graphics .getDensity () / Gdx .graphics .getBackBufferScale ();
56+ }
57+
58+ /**
59+ * Calcs the button size based in screen size
60+ *
61+ * @return The recommended size in pixels
62+ */
63+ public static float getPrefButtonSize () {
64+ return getSizeMultiplier () * BUTTON_SIZE ;
65+ }
66+
67+ /**
68+ * Calcs the minimum size based in screen size
69+ *
70+ * @return The recommended size in pixels
71+ */
72+ public static float getTouchMinSize () {
73+ return getSizeMultiplier () * TOUCH_MIN_SIZE ;
74+ }
75+
76+ /**
77+ * Calcs the margin size based in screen size
78+ *
79+ * @return The recommended size in pixels
80+ */
81+ public static float getMarginSize () {
82+ return getSizeMultiplier () * MARGIN_SIZE ;
83+ }
84+
85+ /**
86+ * Calcs the space between ui elements based in screen size
87+ *
88+ * @return The recommended size in pixels
89+ */
90+ public static float getSpacing () {
91+ return getSizeMultiplier () * SPACING ;
92+ }
9293
9394// public static float getSizeMultiplier() {
9495// float inches = pixelsToInches(Gdx.graphics.getWidth());
@@ -106,27 +107,29 @@ public static float getSpacing() {
106107//
107108// }
108109
109- public static float getSizeMultiplier () {
110- float inches = pixelsToInches (Gdx .graphics .getWidth ());
111- float s = inches / 6f ;
110+ public static float getSizeMultiplier () {
111+ // FIX: In Wayland, the Gdx.graphics.getWidth() does not return the correct value in the first call.
112+ int width = Gdx .graphics .isFullscreen () && Gdx .app .getType () == Application .ApplicationType .Desktop ? Gdx .graphics .getDisplayMode ().width : Gdx .graphics .getWidth ();
113+ float inches = pixelsToInches (width );
114+ float s = inches / 6f ;
112115
113- return Math .max (1.0f , s );
116+ return Math .max (1.0f , s );
114117
115- }
118+ }
116119
117- public static int dpToPixels (int dp ) {
118- return (int ) (dp * getLogicalDensity ());
119- }
120+ public static int dpToPixels (int dp ) {
121+ return (int ) (dp * getLogicalDensity ());
122+ }
120123
121- public static int pixelsToDP (int pixels ) {
122- return (int ) (pixels / getLogicalDensity ());
123- }
124+ public static int pixelsToDP (int pixels ) {
125+ return (int ) (pixels / getLogicalDensity ());
126+ }
124127
125- public static float pixelsToInches (int pixels ) {
126- return pixels / DPI ;
127- }
128+ public static float pixelsToInches (int pixels ) {
129+ return pixels / DPI ;
130+ }
128131
129- public static float ptToPixels (float pts ) {
130- return pts * 72 / DPI ;
131- }
132+ public static float ptToPixels (float pts ) {
133+ return pts * 72 / DPI ;
134+ }
132135}
0 commit comments