Skip to content

Commit

Permalink
LibGDX:
Browse files Browse the repository at this point in the history
* store Preferences per instance
* disable some android specific ui
  • Loading branch information
Mikhael-Danilov committed Dec 25, 2024
1 parent e82e1ca commit c838dc2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.nyrds.pixeldungeon.game.GameLoop;
import com.nyrds.pixeldungeon.game.GamePreferences;
import com.nyrds.pixeldungeon.ml.BuildConfig;
import com.nyrds.pixeldungeon.ml.R;
import com.nyrds.platform.game.RemixedDungeon;
import com.nyrds.platform.util.StringsManager;
Expand All @@ -15,14 +16,15 @@ public class WndUiSettings extends WndMenuCommon {
@Override
protected void createItems() {

menuItems.add( new MenuCheckBox(R.string.WndSettings_Immersive, GamePreferences.immersed()) {
@Override
protected void onClick() {
super.onClick();
GamePreferences.immerse(checked());
}
});

if(BuildConfig.FLAVOR_platform.equals(Utils.PLATFORM_ANDROID)) {
menuItems.add(new MenuCheckBox(R.string.WndSettings_Immersive, GamePreferences.immersed()) {
@Override
protected void onClick() {
super.onClick();
GamePreferences.immerse(checked());
}
});
}
if(!GamePreferences.classicFont()){
menuItems.add(createTextScaleButtons());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Utils {
public static final String UNKNOWN = "unknown";
public static final String EMPTY_STRING = "";
public static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final String PLATFORM_ANDROID = "android";


@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.nyrds.pixeldungeon.game.GameLoop;
import com.nyrds.platform.game.RemixedDungeon;
import com.nyrds.platform.util.PUtil;

import java.io.IOException;

Expand Down Expand Up @@ -54,6 +54,8 @@ public static void restartApp() {
// Create the command to restart the application
String[] command = new String[]{java,"--add-opens", "java.base/java.util=ALL-UNNAMED", "-Dhttps.protocols=TLSv1.2", "-cp", classPath, mainClass};

PUtil.slog("app", "Restarting application with command: " + String.join(" ", command));

// Execute the command
Process process = runtime.exec(command);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,141 +1,139 @@
package com.nyrds.platform.storage;

import com.badlogic.gdx.Gdx;

import org.hjson.JsonObject;
import org.hjson.JsonValue;
import org.hjson.Stringify;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public enum Preferences {

INSTANCE;

public static final String KEY_IMMERSIVE = "immersive";
public static final String KEY_MUSIC = "music";
public static final String KEY_SOUND_FX = "soundfx";
public static final String KEY_ZOOM = "zoom";
public static final String KEY_LAST_CLASS = "last_class";
public static final String KEY_DONATED = "donated";
public static final String KEY_INTRO = "intro";
public static final String KEY_BRIGHTNESS = "brightness";
public static final String KEY_LOCALE = "locale";
public static final String KEY_QUICKSLOTS = "quickslots";
public static final String KEY_VERSION = "version";
public static final String KEY_FONT_SCALE = "font_scale";
public static final String KEY_CLASSIC_FONT = "classic_font";
public static final String KEY_IMMERSIVE = "immersive";
public static final String KEY_MUSIC = "music";
public static final String KEY_SOUND_FX = "soundfx";
public static final String KEY_ZOOM = "zoom";
public static final String KEY_LAST_CLASS = "last_class";
public static final String KEY_DONATED = "donated";
public static final String KEY_INTRO = "intro";
public static final String KEY_BRIGHTNESS = "brightness";
public static final String KEY_LOCALE = "locale";
public static final String KEY_QUICKSLOTS = "quickslots";
public static final String KEY_VERSION = "version";
public static final String KEY_FONT_SCALE = "font_scale";
public static final String KEY_CLASSIC_FONT = "classic_font";
public static final String KEY_PREMIUM_SETTINGS = "premium_settings";
public static final String KEY_REALTIME = "realtime";
public static final String KEY_ACTIVE_MOD = "active_mod";
public static final String KEY_COLLECT_STATS = "collect_stats";
public static final String KEY_REALTIME = "realtime";
public static final String KEY_ACTIVE_MOD = "active_mod";
public static final String KEY_COLLECT_STATS = "collect_stats";
public static final String KEY_MOVE_TIMEOUT = "move_timeout";
public static final String KEY_USE_PLAY_GAMES = "use_play_games";

public static final String KEY_UI_ZOOM = "ui_zoom";
public static final String KEY_VERSION_STRING = "version_string";
public static final String KEY_TOOL_STYLE = "tool_style";
public static final String KEY_HANDEDNESS = "handedness";

public static final String KEY_EU_CONSENT_LEVEL = "eu_consent_level";
public static final String KEY_USE_ISOMETRIC_TILES = "use_isometric_tiles";
public static final String KEY_ISOMETRIC_TILES_PRESENTED = "isometric_tiles_presented";
public static final String KEY_TILES_QUESTION_ASKED = "tiles_question_asked";

private final Map<String, Integer> intCache = new HashMap<>();
private final Map<String, String> stringCache = new HashMap<>();
private final Map<String, Boolean> boolCache = new HashMap<>();
private final Map<String, Double> doubleCache = new HashMap<>();

com.badlogic.gdx.Preferences prefs = Gdx.app.getPreferences("RemixedDungeon");
private final File preferencesFile = new File("preferences.hjson");
private JsonObject preferencesJson;

public int getInt(String key, int defValue) {
Preferences() {
loadPreferences();
}

private void loadPreferences() {
if (preferencesFile.exists()) {
try (FileReader reader = new FileReader(preferencesFile)) {
preferencesJson = JsonValue.readHjson(reader).asObject();
} catch (IOException e) {
preferencesJson = new JsonObject();
}
} else {
preferencesJson = new JsonObject();
}
}

private void savePreferences() {
try (FileWriter writer = new FileWriter(preferencesFile)) {
preferencesJson.writeTo(writer, Stringify.HJSON);
} catch (IOException e) {
e.printStackTrace();
}
}

public int getInt(String key, int defValue) {
if (intCache.containsKey(key)) {
return intCache.get(key);
}

String defVal = Integer.toString(defValue);
String propVal = getString(key, defVal);
int value;
try {
value = Integer.parseInt(propVal);
} catch (NumberFormatException e) {
put(key, defValue);
value = defValue;
}
intCache.put(key,value);
int value = preferencesJson.getInt(key, defValue);
intCache.put(key, value);
return value;
}

public double getDouble(String key, double defValue) {

if(doubleCache.containsKey(key)) {
if (doubleCache.containsKey(key)) {
return doubleCache.get(key);
}

String defVal = Double.toString(defValue);
String propVal = getString(key, defVal);

double value;
try {
value = Double.parseDouble(propVal);
} catch (NumberFormatException e) {
put(key, defValue);
value = defValue;
}

double value = preferencesJson.getDouble(key, defValue);
doubleCache.put(key, value);
return value;
}

public boolean getBoolean(String key, boolean defValue) {

if(boolCache.containsKey(key)) {
if (boolCache.containsKey(key)) {
return boolCache.get(key);
}

String defVal = Boolean.toString(defValue);
String propVal = getString(key, defVal);
boolean value = Boolean.parseBoolean(propVal);
boolean value = preferencesJson.getBoolean(key, defValue);
boolCache.put(key, value);
return value;
}

public String getString(String key, String defValue) {

if(stringCache.containsKey(key)) {
if (stringCache.containsKey(key)) {
return stringCache.get(key);
}

return prefs.getString(key, defValue);
String value = preferencesJson.getString(key, defValue);
stringCache.put(key, value);
return value;
}

public void put(String key, int value) {

intCache.put(key,value);

String val = Integer.toString(value);
put(key, val);
intCache.put(key, value);
preferencesJson.set(key, value);
savePreferences();
}

public void put(String key, double value) {

doubleCache.put(key,value);

String val = Double.toString(value);
put(key, val);
doubleCache.put(key, value);
preferencesJson.set(key, value);
savePreferences();
}

public void put(String key, boolean value) {

boolCache.put(key,value);

String val = Boolean.toString(value);
put(key, val);
boolCache.put(key, value);
preferencesJson.set(key, value);
savePreferences();
}


public void put(String key, String value) {
stringCache.put(key, value);
prefs.putString(key, value);
prefs.flush();
preferencesJson.set(key, value);
savePreferences();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public class BuildConfig {
public static final boolean DEBUG = false;
//public static final String ASSETS_PATH = "../assets/";
public static final String ASSETS_PATH = "./";
public static final String SAVES_PATH = "./saves/";
public static final String FLAVOR_platform = "desktop";
}

0 comments on commit c838dc2

Please sign in to comment.