2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-06 13:56:21 +00:00
|
|
|
import json
|
2020-11-27 19:53:24 +00:00
|
|
|
import locale
|
2020-11-06 13:59:27 +00:00
|
|
|
import os
|
2020-11-06 13:56:21 +00:00
|
|
|
from typing import Any, Generator
|
2020-11-06 13:29:05 +00:00
|
|
|
|
2020-11-19 02:13:01 +00:00
|
|
|
from .resources import ResourceManager
|
|
|
|
|
2020-11-06 13:29:05 +00:00
|
|
|
|
|
|
|
class Settings:
|
2020-11-06 13:56:21 +00:00
|
|
|
"""
|
|
|
|
This class stores the settings of the game.
|
|
|
|
Settings can be get by using for example settings.TEXTURE_PACK directly.
|
|
|
|
The comment can be get by using settings.get_comment('TEXTURE_PACK').
|
|
|
|
We can define the setting by simply use settings.TEXTURE_PACK = 'new_key'
|
|
|
|
"""
|
2020-11-06 13:29:05 +00:00
|
|
|
def __init__(self):
|
2020-11-06 14:33:26 +00:00
|
|
|
self.KEY_UP_PRIMARY = \
|
|
|
|
['z', 'Touche principale pour aller vers le haut']
|
|
|
|
self.KEY_UP_SECONDARY = \
|
|
|
|
['KEY_UP', 'Touche secondaire pour aller vers le haut']
|
|
|
|
self.KEY_DOWN_PRIMARY = \
|
|
|
|
['s', 'Touche principale pour aller vers le bas']
|
|
|
|
self.KEY_DOWN_SECONDARY = \
|
|
|
|
['KEY_DOWN', 'Touche secondaire pour aller vers le bas']
|
|
|
|
self.KEY_LEFT_PRIMARY = \
|
|
|
|
['q', 'Touche principale pour aller vers la gauche']
|
|
|
|
self.KEY_LEFT_SECONDARY = \
|
|
|
|
['KEY_LEFT', 'Touche secondaire pour aller vers la gauche']
|
|
|
|
self.KEY_RIGHT_PRIMARY = \
|
|
|
|
['d', 'Touche principale pour aller vers la droite']
|
|
|
|
self.KEY_RIGHT_SECONDARY = \
|
|
|
|
['KEY_RIGHT', 'Touche secondaire pour aller vers la droite']
|
2020-11-06 17:25:02 +00:00
|
|
|
self.KEY_ENTER = \
|
|
|
|
['\n', 'Touche pour valider un menu']
|
2020-11-06 16:43:30 +00:00
|
|
|
self.TEXTURE_PACK = ['ascii', 'Pack de textures utilisé']
|
2020-11-27 19:53:24 +00:00
|
|
|
self.LOCALE = [locale.getlocale()[0][:2], 'Langue']
|
2020-11-06 13:29:05 +00:00
|
|
|
|
|
|
|
def __getattribute__(self, item: str) -> Any:
|
|
|
|
superattribute = super().__getattribute__(item)
|
2020-11-06 13:56:21 +00:00
|
|
|
if item.isupper() and item in self.settings_keys:
|
|
|
|
return superattribute[0]
|
2020-11-06 13:29:05 +00:00
|
|
|
return superattribute
|
|
|
|
|
2020-11-06 13:56:21 +00:00
|
|
|
def __setattr__(self, name: str, value: Any) -> None:
|
|
|
|
if name in self.settings_keys:
|
|
|
|
object.__getattribute__(self, name)[0] = value
|
|
|
|
return
|
|
|
|
return super().__setattr__(name, value)
|
|
|
|
|
2020-11-06 13:29:05 +00:00
|
|
|
def get_comment(self, item: str) -> str:
|
|
|
|
"""
|
|
|
|
Retrieve the comment of a setting.
|
|
|
|
"""
|
2020-11-06 14:08:29 +00:00
|
|
|
if item in self.settings_keys:
|
2020-11-06 13:29:05 +00:00
|
|
|
return object.__getattribute__(self, item)[1]
|
|
|
|
for key in self.settings_keys:
|
|
|
|
if getattr(self, key) == item:
|
|
|
|
return object.__getattribute__(self, key)[1]
|
|
|
|
|
|
|
|
@property
|
2020-11-06 13:56:21 +00:00
|
|
|
def settings_keys(self) -> Generator[str, Any, None]:
|
2020-11-06 13:29:05 +00:00
|
|
|
"""
|
|
|
|
Get the list of all parameters.
|
|
|
|
"""
|
|
|
|
return (key for key in self.__dict__)
|
|
|
|
|
2020-11-06 13:56:21 +00:00
|
|
|
def loads_from_string(self, json_str: str) -> None:
|
|
|
|
"""
|
|
|
|
Dump settings
|
|
|
|
"""
|
|
|
|
d = json.loads(json_str)
|
|
|
|
for key in d:
|
|
|
|
setattr(self, key, d[key])
|
|
|
|
|
|
|
|
def dumps_to_string(self) -> str:
|
|
|
|
"""
|
|
|
|
Dump settings
|
|
|
|
"""
|
|
|
|
d = dict()
|
|
|
|
for key in self.settings_keys:
|
|
|
|
d[key] = getattr(self, key)
|
2020-11-06 13:59:27 +00:00
|
|
|
return json.dumps(d, indent=4)
|
2020-11-06 13:56:21 +00:00
|
|
|
|
|
|
|
def load_settings(self) -> None:
|
|
|
|
"""
|
|
|
|
Loads the settings from a file
|
|
|
|
"""
|
2020-11-19 02:13:01 +00:00
|
|
|
file_path = ResourceManager.get_config_path("settings.json")
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
with open(file_path, "r") as f:
|
2020-11-06 13:59:27 +00:00
|
|
|
self.loads_from_string(f.read())
|
2020-11-06 13:56:21 +00:00
|
|
|
|
|
|
|
def write_settings(self) -> None:
|
|
|
|
"""
|
|
|
|
Dumps the settings into a file
|
|
|
|
"""
|
2020-11-19 02:13:01 +00:00
|
|
|
with open(ResourceManager.get_config_path("settings.json"), "w") as f:
|
2020-11-06 13:56:21 +00:00
|
|
|
f.write(self.dumps_to_string())
|