From 0a0b82551ab6babb5fc1f95e21f206060f58b024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 14 Jul 2024 14:50:53 +0300 Subject: [PATCH] d --- main.py | 3 +-- py/cfg_aux.py | 10 ++++++++++ py/cfg_aux_test.py | 10 ++++++++++ py/desktop.py | 45 ++++++++++++++++++++++++------------------ py/desktop_Platform.py | 1 - py/desktop_aux.py | 37 ++++++++++++++++++++++++++++++++++ py/do-test.py | 1 + 7 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 py/desktop_aux.py diff --git a/main.py b/main.py index bcac173..cbc188e 100644 --- a/main.py +++ b/main.py @@ -44,13 +44,12 @@ def process(c): # Perform context dependent calls of desktop functions. # Similar to context functions, but no platform is returned. desktop_applyConfigInit(p) + desktop_loadConfigTextures(p) ctrl.registerCallback(process) ctrl.set("cfgPath", CFG) ctrl.set("scriptDir", SCRIPT_DIR) -#desktop_loadTextures(p) - wnd = desktop_Window(p) ctrl.set("didLaunch", True) arcade.run() diff --git a/py/cfg_aux.py b/py/cfg_aux.py index 3e7c68b..8a6967c 100644 --- a/py/cfg_aux.py +++ b/py/cfg_aux.py @@ -1,5 +1,15 @@ from cld import * +# Extract texture name from config section +def cfg_aux_textureName( + sectionName: str +) -> str: + prefix = "texture \"" + postfix = "\"" + start = cld_len(prefix) + 1 + end = cld_len(postfix) + return sectionName[start:-end] + # Convert config contents to tree: sections -> keys -> values def cfg_aux_tree( cfgContents: [str] diff --git a/py/cfg_aux_test.py b/py/cfg_aux_test.py index 4909b71..d7cfa7c 100644 --- a/py/cfg_aux_test.py +++ b/py/cfg_aux_test.py @@ -1,5 +1,15 @@ from cfg_aux import * +def test_cfg_aux_textureName( +) -> str: + section = "textures \"t-floor1\"" + tex = cfg_aux_textureName(section) + if ( + tex == "t-floor1" + ): + return "OK: cfg_aux_textureName" + return "ERR: cfg_aux_textureName" + def test_cfg_aux_tree( ) -> str: lines = [ diff --git a/py/desktop.py b/py/desktop.py index 5db1a18..8fd8eb4 100644 --- a/py/desktop.py +++ b/py/desktop.py @@ -1,5 +1,6 @@ -import arcade +from cfg_aux import * from cld import * +from desktop_aux import * from desktop_Platform import * # Pass config init key-value pairs to context controller @@ -8,23 +9,29 @@ from desktop_Platform import * # 1. Config tree has just been parsed def desktop_applyConfigInit(p): if ( - p.c.recentField == "cfgTree" + p.c.recentField != "cfgTree" ): - for key in p.c.cfgTree["init"]: - value = p.c.cfgTree["init"][key] - # Boolean. - if ( - value == "false" - ): - value = False - elif ( - value == "true" - ): - value = True - # Float. - elif ( - cld_isdigit(value) - ): - value = float(value) + return - p.ctrl.set(key, value) + for key in p.c.cfgTree["init"]: + val = p.c.cfgTree["init"][key] + value = desktop_aux_convertValue(val) + p.ctrl.set(key, value) + +# Load textures +# +# Conditions: +# 1. Config tree has just been parsed +def desktop_loadConfigTextures(p): + if ( + p.c.recentField != "cfgTree" + ): + return + + for key in p.c.cfgTree: + if ( + cld_startswith(key, "texture ") + ): + name = cfg_aux_textureName(key) + tex = desktop_aux_loadTexture(p.c.cfgDir, p.c.cfgTree[key]) + p.textures[name] = tex diff --git a/py/desktop_Platform.py b/py/desktop_Platform.py index 0f5c02e..ce6ba95 100644 --- a/py/desktop_Platform.py +++ b/py/desktop_Platform.py @@ -5,5 +5,4 @@ class desktop_Platform: self.c = None self.ctrl = None self.staticSprites = arcade.SpriteList() - self.textureDescriptions = {} self.textures = {} diff --git a/py/desktop_aux.py b/py/desktop_aux.py new file mode 100644 index 0000000..37e3b64 --- /dev/null +++ b/py/desktop_aux.py @@ -0,0 +1,37 @@ +import arcade +from cld import * + +# Convert String config value to Bool or Float if possible +def desktop_aux_convertValue( + value: str +) -> any: + # Bool. + if ( + value == "false" + ): + return False + elif ( + value == "true" + ): + return True + # Float. + elif ( + cld_isdigit(value) + ): + return float(value) + # String. + return value + +# Load texture +def desktop_aux_loadTexture( + resDir: str, + desc: dict[str, str] +): + path = resDir + "/" + desc["file"] + return arcade.load_texture( + path, + x = float(desc["x"]), + y = float(desc["y"]), + width = float(desc["width"]), + height = float(desc["height"]) + ) diff --git a/py/do-test.py b/py/do-test.py index a1f7729..d5dc662 100755 --- a/py/do-test.py +++ b/py/do-test.py @@ -11,6 +11,7 @@ from cfg_test import * functions = [ test_cfg_parseConfigTree, + test_cfg_aux_textureName, test_cfg_aux_tree, test_cfg_aux_treeCreateSection, test_cfg_aux_treeSetKeyValue,