/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "uitoolkit/UIAppearance.h" #include "uitoolkit/UIButton.h" typedef struct UIAppearance_parameter { union { float float32; double float64; Vector2f vector2f; Rect4f rect4f; Color4f color4f; UISliceGrid3x3 sliceGrid3x3; UISliceGrid3x1 sliceGrid3x1; UISliceGrid1x3 sliceGrid1x3; UITypeface * typeface; UIAtlasEntry atlasEntry; } data; UIAppearance_key parentKey; } UIAppearance_parameter; #define PARENT_KEY_NONE UINT32_MAX UIAppearance UIAppearance_init(UIAppearance * parent) { UIAppearance appearance = {parent, NULL}; return appearance; } void UIAppearance_dispose(UIAppearance appearance) { if (appearance.parameters != NULL) { HashTable_dispose(appearance.parameters); } } UIAppearance UIAppearance_copy(UIAppearance appearance) { UIAppearance copy; copy.parent = appearance.parent; if (appearance.parameters == NULL) { copy.parameters = NULL; } else { copy.parameters = HashTable_copy(appearance.parameters); } return copy; } static UIAppearance_parameter * resolveRootParameter(UIAppearance appearance, UIAppearance_key key) { if (appearance.parameters != NULL) { UIAppearance_parameter * parameter = HashTable_get(appearance.parameters, HashTable_uint32Key(key)); if (parameter != NULL) { while (parameter->parentKey != PARENT_KEY_NONE) { UIAppearance_parameter * parentParameter = HashTable_get(appearance.parameters, HashTable_uint32Key(parameter->parentKey)); if (parentParameter == NULL) { if (appearance.parent != NULL) { return resolveRootParameter(*appearance.parent, parameter->parentKey); } return NULL; } parameter = parentParameter; } return parameter; } } if (appearance.parent != NULL) { return resolveRootParameter(*appearance.parent, key); } return NULL; } #define getAppearanceParameterImplementation(field, default_value) \ UIAppearance_parameter * parameter = resolveRootParameter(appearance, key); \ if (parameter != NULL) { \ return parameter->data.field; \ } \ return default_value; float getAppearanceFloat(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(float32, 0.0f); } double getAppearanceDouble(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(float64, 0.0f); } Vector2f getAppearanceVector2f(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(vector2f, VECTOR2f_ZERO); } Rect4f getAppearanceRect4f(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(rect4f, RECT4f_EMPTY); } Color4f getAppearanceColor4f(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(color4f, COLOR4f(1.0f, 1.0f, 1.0f, 1.0f)); } UISliceGrid3x3 getAppearanceSliceGrid3x3(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(sliceGrid3x3, ((UISliceGrid3x3) {0, 0, 0, 0, 0, 0})); } UISliceGrid3x1 getAppearanceSliceGrid3x1(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(sliceGrid3x1, ((UISliceGrid3x1) {0, 0, 0})); } UISliceGrid1x3 getAppearanceSliceGrid1x3(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(sliceGrid1x3, ((UISliceGrid1x3) {0, 0, 0})); } UITypeface * getAppearanceTypeface(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(typeface, NULL); } UIAtlasEntry getAppearanceAtlasEntry(UIAppearance appearance, UIAppearance_key key) { getAppearanceParameterImplementation(atlasEntry, ((UIAtlasEntry) {RECT4f_EMPTY, RECT4f_EMPTY, VECTOR2f_ZERO})); } #define setAppearanceParameterImplementation(field) \ if (appearance->parameters == NULL) { \ appearance->parameters = HashTable_create(sizeof(UIAppearance_parameter)); \ } \ UIAppearance_parameter parameter = {.data = {.field = value}, .parentKey = PARENT_KEY_NONE}; \ HashTable_set(appearance->parameters, HashTable_uint32Key(key), ¶meter); void setAppearanceFloat(UIAppearance * appearance, UIAppearance_key key, float value) { setAppearanceParameterImplementation(float32); } void setAppearanceDouble(UIAppearance * appearance, UIAppearance_key key, double value) { setAppearanceParameterImplementation(float64); } void setAppearanceVector2f(UIAppearance * appearance, UIAppearance_key key, Vector2f value) { setAppearanceParameterImplementation(vector2f); } void setAppearanceRect4f(UIAppearance * appearance, UIAppearance_key key, Rect4f value) { setAppearanceParameterImplementation(rect4f); } void setAppearanceColor4f(UIAppearance * appearance, UIAppearance_key key, Color4f value) { setAppearanceParameterImplementation(color4f); } void setAppearanceSliceGrid3x3(UIAppearance * appearance, UIAppearance_key key, UISliceGrid3x3 value) { setAppearanceParameterImplementation(sliceGrid3x3); } void setAppearanceSliceGrid3x1(UIAppearance * appearance, UIAppearance_key key, UISliceGrid3x1 value) { setAppearanceParameterImplementation(sliceGrid3x1); } void setAppearanceSliceGrid1x3(UIAppearance * appearance, UIAppearance_key key, UISliceGrid1x3 value) { setAppearanceParameterImplementation(sliceGrid1x3); } void setAppearanceTypeface(UIAppearance * appearance, UIAppearance_key key, compat_type(UITypeface *) value) { setAppearanceParameterImplementation(typeface); } void setAppearanceAtlasEntry(UIAppearance * appearance, UIAppearance_key key, UIAtlasEntry value) { setAppearanceParameterImplementation(atlasEntry); } void setAppearanceReference(UIAppearance * appearance, UIAppearance_key childKey, UIAppearance_key parentKey) { if (appearance->parameters == NULL) { appearance->parameters = HashTable_create(sizeof(UIAppearance_parameter)); } UIAppearance_parameter parameter = {.parentKey = parentKey}; HashTable_set(appearance->parameters, HashTable_uint32Key(childKey), ¶meter); }