ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Theme.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for SDL3 and C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include <Objectively/JSONContext.h>
27
28#include "ImageAtlas.h"
29#include "Log.h"
30#include "Theme.h"
31#include "View.h"
32#include "WindowController.h"
33
34#define _Class _Theme
35
36#pragma mark - Object
37
41static void dealloc(Object *self) {
42
43 Theme *this = (Theme *) self;
44
45 release(this->fontCache);
46 release(this->imageCache);
47 release(this->icons);
48 release(this->stylesheets);
49
50 super(Object, self, dealloc);
51}
52
53#pragma mark - Theme
54
59static void addStylesheet(Theme *self, Stylesheet *stylesheet) {
60 $((Array *) self->stylesheets, addObject, stylesheet);
61}
62
66static String *cacheKey(const FontAttributes *attributes, float pixelDensity) {
67
68 String *name = $$(Font, nameWithAttributes, attributes);
69 assert(name);
70
71 String *key = str("%s@%g", name->chars, pixelDensity);
72 assert(key);
73
74 release(name);
75 return key;
76}
77
82static Image *image(Theme *self, const char *name, float pixelDensity) {
83
84 assert(name);
85
86 String *key = str("%s@%g", name, pixelDensity);
87 assert(key);
88
89 Image *image = $(self->imageCache, objectForKeyPath, key->chars);
90 if (image == NULL) {
91
92 image = $$(Image, imageWithResourceName, name, pixelDensity);
93 if (image) {
94 $(self->imageCache, setObjectForKeyPath, image, key->chars);
95 release(image);
96 }
97 }
98
99 release(key);
100
101 return image;
102}
103
108static Font *font(Theme *self, const FontAttributes *attributes, float pixelDensity) {
109
110 String *key = cacheKey(attributes, pixelDensity);
111
112 Font *font = $(self->fontCache, objectForKeyPath, key->chars);
113 if (font == NULL) {
114
115 Font *resolved = $$(Font, fontWithAttributes, attributes, pixelDensity);
116 assert(resolved);
117
118 // Keyed by the resolved Font's own name: when the requested family is not registered,
119 // Font::fontWithAttributes falls back to the default Font, and caching that under the
120 // requested key would pin the wrong Font to it permanently. This way the request
121 // simply misses again, and heals once the family is registered via Font::cacheFont.
122 const FontAttributes resolvedAttributes = { resolved->family, resolved->size, resolved->style };
123 String *resolvedKey = cacheKey(&resolvedAttributes, pixelDensity);
124
125 font = $(self->fontCache, objectForKeyPath, resolvedKey->chars);
126 if (font == NULL) {
127 $(self->fontCache, setObjectForKeyPath, resolved, resolvedKey->chars);
128 font = resolved;
129 }
130
131 release(resolvedKey);
132 release(resolved);
133 }
134
135 release(key);
136
137 return font;
138}
139
144static ImageAtlas *icons(Theme *self) {
145
146 if (self->icons == NULL) {
147 self->icons = $(alloc(ImageAtlas), init);
148 assert(self->icons);
149 }
150
151 return self->icons;
152}
153
158static AtlasImage *addIcon(Theme *self, const char *name, Image *image) {
159
160 ImageAtlas *atlas = $(self, icons);
161
163
164 if (!$(atlas, compile)) {
165 MVC_LogError("Failed to compile icons after adding %s\n", name);
166 }
167
168 return icon;
169}
170
175static AtlasImage *icon(Theme *self, const char *name) {
176 return $($(self, icons), imageWithName, name);
177}
178
182static ident computeStyle_reduce(const ident obj, ident accumulator, ident data) {
183
184 const Stylesheet *stylesheet = obj;
185
186 const View *view = data;
187
188 const Array *selectors = (Array *) stylesheet->selectors;
189 for (size_t i = 0; i < selectors->count; i++) {
190
191 Selector *selector = $(selectors, objectAtIndex, i);
192 if ($(selector, matchesView, view)) {
193
194 assert(selector->style);
195
196 if (MVC_LogEnabled(SDL_LOG_PRIORITY_VERBOSE)) {
197
198 String *this = $((Object *) selector, description);
199 String *that = $((Object *) view, description);
200
201 MVC_LogVerbose("%s -> %s\n", this->chars, that->chars);
202
203 release(this);
204 release(that);
205 }
206
207 if ($(selector->style, attributeValue, "debug")) {
208 SDL_TriggerBreakpoint();
209 }
210
211 $((Style *) accumulator, addSelector, selector);
212 $((Style *) accumulator, addAttributes, selector->style->attributes);
213 }
214 }
215
216 return accumulator;
217}
218
223static Style *computeStyle(const Theme *self, const View *view) {
224
225 assert(view);
226
227 Style *style = $(alloc(Style), initWithAttributes, NULL);
228 assert(style);
229
230 return $((Array *) self->stylesheets, reduce, computeStyle_reduce, style, (ident) view);
231}
232
237static Theme *init(Theme *self) {
238
239 self = (Theme *) super(Object, self, init);
240 if (self) {
241
242 self->fontCache = $$(Dictionary, dictionary);
243 assert(self->fontCache);
244
245 self->imageCache = $$(Dictionary, dictionary);
246 assert(self->imageCache);
247
248 self->stylesheets = $$(Array, arrayWithCapacity, 8);
249 assert(self->stylesheets);
250
252 }
253
254 return self;
255}
256
261static void removeStylesheet(Theme *self, Stylesheet *stylesheet) {
262 $((Array *) self->stylesheets, removeObject, stylesheet);
263}
264
268static void renderDeviceWillReset_enumerate(const Dictionary *dictionary, ident obj, ident key, ident data) {
269 $((Font *) obj, renderDeviceWillReset);
270}
271
276static void renderDeviceWillReset(Theme *self) {
277 $(self->fontCache, enumerateObjectsAndKeys, renderDeviceWillReset_enumerate, NULL);
278 if (self->icons) {
279 $(self->icons, renderDeviceWillReset);
280 }
281}
282
287static void renderDeviceDidReset(Theme *self) {
288 // Bitmap Textures are recreated lazily by ImageAtlas::texture on next use.
289}
290
295static Theme *theme(SDL_Window *window) {
296
297 assert(window);
298
300 if (windowController) {
301 return windowController->theme;
302 }
303
304 return NULL;
305}
306
307#pragma mark - Class lifecycle
308
312static void initialize(Class *clazz) {
313
314 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
315
316 ((ThemeInterface *) clazz->interface)->addIcon = addIcon;
317 ((ThemeInterface *) clazz->interface)->addStylesheet = addStylesheet;
318 ((ThemeInterface *) clazz->interface)->computeStyle = computeStyle;
319 ((ThemeInterface *) clazz->interface)->font = font;
320 ((ThemeInterface *) clazz->interface)->image = image;
321 ((ThemeInterface *) clazz->interface)->icon = icon;
322 ((ThemeInterface *) clazz->interface)->icons = icons;
323 ((ThemeInterface *) clazz->interface)->init = init;
324 ((ThemeInterface *) clazz->interface)->removeStylesheet = removeStylesheet;
325 ((ThemeInterface *) clazz->interface)->renderDeviceDidReset = renderDeviceDidReset;
326 ((ThemeInterface *) clazz->interface)->renderDeviceWillReset = renderDeviceWillReset;
327 ((ThemeInterface *) clazz->interface)->theme = theme;
328}
329
334Class *_Theme(void) {
335 static Class *clazz;
336 static Once once;
337
338 do_once(&once, {
339 clazz = _initialize(&(const ClassDef) {
340 .name = "Theme",
341 .superclass = _Object(),
342 .instanceSize = sizeof(Theme),
343 .interfaceSize = sizeof(ThemeInterface),
344 .initialize = initialize,
345 });
346 });
347
348 return clazz;
349}
350
351#undef _Class
static String * nameWithAttributes(const FontAttributes *attributes)
Definition Font.c:312
static Font * fontWithAttributes(const FontAttributes *attributes, float pixelDensity)
Definition Font.c:199
static String * name(const Font *self)
Definition Font.c:303
static Image * imageWithResourceName(const char *name, float pixelDensity)
Definition Image.c:74
static AtlasImage * addImageWithName(ImageAtlas *self, const char *name, Image *image)
Definition ImageAtlas.c:96
static AtlasImage * imageWithName(const ImageAtlas *self, const char *name)
Definition ImageAtlas.c:112
static bool compile(ImageAtlas *self)
Definition ImageAtlas.c:170
Packs many Images into one sheet, backed by one Texture.
static String * description(const Object *self)
Definition Label.c:47
View logging facilities via SDL_Log.
#define MVC_LogVerbose(fmt,...)
Definition Log.h:45
#define MVC_LogError(fmt,...)
Definition Log.h:57
#define MVC_LogEnabled(priority)
Definition Log.h:39
static bool matchesView(const Selector *self, const View *view)
Definition Selector.c:277
static Style * initWithAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:394
static ident attributeValue(const Style *self, const char *attr)
Definition Style.c:386
static void addSelector(Style *self, Selector *selector)
Definition Style.c:357
static void addAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:221
static Stylesheet * defaultStylesheet(void)
Definition Stylesheet.c:95
static ident computeStyle_reduce(const ident obj, ident accumulator, ident data)
Reducer for computeStyle.
Definition Theme.c:182
static AtlasImage * icon(Theme *self, const char *name)
Definition Theme.c:175
static ImageAtlas * icons(Theme *self)
Definition Theme.c:144
static void renderDeviceWillReset_enumerate(const Dictionary *dictionary, ident obj, ident key, ident data)
DictionaryEnumerator for renderDeviceWillReset.
Definition Theme.c:268
static String * cacheKey(const FontAttributes *attributes, float pixelDensity)
Builds the cache key for the given attributes and pixel density.
Definition Theme.c:66
static Font * font(Theme *self, const FontAttributes *attributes, float pixelDensity)
Definition Theme.c:108
static Theme * theme(SDL_Window *window)
Definition Theme.c:295
static void renderDeviceWillReset(Theme *self)
Definition Theme.c:276
static void dealloc(Object *self)
Definition Theme.c:41
static void addStylesheet(Theme *self, Stylesheet *stylesheet)
Definition Theme.c:59
static Style * computeStyle(const Theme *self, const View *view)
Definition Theme.c:223
static void removeStylesheet(Theme *self, Stylesheet *stylesheet)
Definition Theme.c:261
static void initialize(Class *clazz)
Definition Theme.c:312
Class * _Theme(void)
Definition Theme.c:334
static Theme * init(Theme *self)
Definition Theme.c:237
static Image * image(Theme *self, const char *name, float pixelDensity)
Definition Theme.c:82
static AtlasImage * addIcon(Theme *self, const char *name, Image *image)
Definition Theme.c:158
static void renderDeviceDidReset(Theme *self)
Definition Theme.c:287
The Theme type.
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
static WindowController * windowController(SDL_Window *window)
A WindowController manages a ViewController and its descendants within an SDL_Window.
An Image packed into an ImageAtlas.
Definition AtlasImage.h:41
The family, size and style attributes that identify a Font, independent of the pixel density at which...
Definition Font.h:64
TrueType fonts.
Definition Font.h:88
char * family
The family name.
Definition Font.h:117
int size
The point size.
Definition Font.h:144
int style
The style.
Definition Font.h:149
Packs many Images into one sheet, backed by one Texture.
Definition ImageAtlas.h:49
ImageAtlas * init(ImageAtlas *self)
Initializes this ImageAtlas.
Definition ImageAtlas.c:260
Image loading.
Definition Image.h:41
Selectors are comprised of one or more SelectorSequences.
Definition Selector.h:49
Style * style
The Style.
Definition Selector.h:82
The Style type.
Definition Style.h:43
Dictionary * attributes
Definition Style.h:59
Stylesheets are comprised of Selectors and Styles.
Definition Stylesheet.h:44
Array * selectors
The Selectors, ordered by specificity.
Definition Stylesheet.h:60
The Theme type.
Definition Theme.h:54
void removeStylesheet(Theme *self, Stylesheet *stylesheet)
Removes the given Stylesheet from this Theme.
Definition Theme.c:261
Array * stylesheets
The Stylesheets, in order of priority.
Definition Theme.h:89
AtlasImage * addIcon(Theme *self, const char *name, Image *image)
Registers image as the icon name, so that :name: in any Text drawn under this Theme renders it inline...
Definition Theme.c:158
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
A WindowController manages a ViewController and its descendants within an SDL_Window.
Theme * theme
The Theme.