File size: 1,391 Bytes
2de3774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import json
import shutil
from pathlib import Path


class ResolutionSettings:
    DEFAULT_RESOLUTIONS_FILE = Path("settings/resolutions.default")
    RESOLUTIONS_FILE = Path("settings/resolutions.json")
    CUSTOM_RESOLUTION = "Custom..."

    def __init__(self):
        self.load_resolutions()

    def load_resolutions(self):
        self.base_ratios = {}

        if not self.RESOLUTIONS_FILE.is_file():
            shutil.copy(self.DEFAULT_RESOLUTIONS_FILE, self.RESOLUTIONS_FILE)

        with self.RESOLUTIONS_FILE.open() as f:
            data = json.load(f)
            for ratio, res in data.items():
                self.base_ratios[ratio] = (res["width"], res["height"])

        self.aspect_ratios = {
            f"{v[0]}x{v[1]} ({k})": v for k, v in self.base_ratios.items()
        }

        return self.base_ratios

    def save_resolutions(self, res_options):
        formatted_options = {}
        for k in res_options:
            formatted_options[k] = {
                "width": res_options[k][0],
                "height": res_options[k][1],
            }

        with open(self.RESOLUTIONS_FILE, "w") as f:
            json.dump(formatted_options, f, indent=2)

        return self.load_resolutions()

    def get_base_aspect_ratios(self, name):
        return self.base_ratios[name]

    def get_aspect_ratios(self, name):
        return self.aspect_ratios[name]