@@ -44,13 +44,12 @@ def process(c): | |||||
# Perform context dependent calls of desktop functions. | # Perform context dependent calls of desktop functions. | ||||
# Similar to context functions, but no platform is returned. | # Similar to context functions, but no platform is returned. | ||||
desktop_applyConfigInit(p) | desktop_applyConfigInit(p) | ||||
desktop_loadConfigTextures(p) | |||||
ctrl.registerCallback(process) | ctrl.registerCallback(process) | ||||
ctrl.set("cfgPath", CFG) | ctrl.set("cfgPath", CFG) | ||||
ctrl.set("scriptDir", SCRIPT_DIR) | ctrl.set("scriptDir", SCRIPT_DIR) | ||||
#desktop_loadTextures(p) | |||||
wnd = desktop_Window(p) | wnd = desktop_Window(p) | ||||
ctrl.set("didLaunch", True) | ctrl.set("didLaunch", True) | ||||
arcade.run() | arcade.run() |
@@ -1,5 +1,15 @@ | |||||
from cld import * | 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 | # Convert config contents to tree: sections -> keys -> values | ||||
def cfg_aux_tree( | def cfg_aux_tree( | ||||
cfgContents: [str] | cfgContents: [str] | ||||
@@ -1,5 +1,15 @@ | |||||
from cfg_aux import * | 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( | def test_cfg_aux_tree( | ||||
) -> str: | ) -> str: | ||||
lines = [ | lines = [ | ||||
@@ -1,5 +1,6 @@ | |||||
import arcade | |||||
from cfg_aux import * | |||||
from cld import * | from cld import * | ||||
from desktop_aux import * | |||||
from desktop_Platform import * | from desktop_Platform import * | ||||
# Pass config init key-value pairs to context controller | # Pass config init key-value pairs to context controller | ||||
@@ -8,23 +9,29 @@ from desktop_Platform import * | |||||
# 1. Config tree has just been parsed | # 1. Config tree has just been parsed | ||||
def desktop_applyConfigInit(p): | def desktop_applyConfigInit(p): | ||||
if ( | 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 |
@@ -5,5 +5,4 @@ class desktop_Platform: | |||||
self.c = None | self.c = None | ||||
self.ctrl = None | self.ctrl = None | ||||
self.staticSprites = arcade.SpriteList() | self.staticSprites = arcade.SpriteList() | ||||
self.textureDescriptions = {} | |||||
self.textures = {} | self.textures = {} |
@@ -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"]) | |||||
) |
@@ -11,6 +11,7 @@ from cfg_test import * | |||||
functions = [ | functions = [ | ||||
test_cfg_parseConfigTree, | test_cfg_parseConfigTree, | ||||
test_cfg_aux_textureName, | |||||
test_cfg_aux_tree, | test_cfg_aux_tree, | ||||
test_cfg_aux_treeCreateSection, | test_cfg_aux_treeCreateSection, | ||||
test_cfg_aux_treeSetKeyValue, | test_cfg_aux_treeSetKeyValue, | ||||