ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
View+JSON.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <Objectively.h>
#include <ObjectivelyMVC.h>

Go to the source code of this file.

Macros

#define _Class   _View
 
#define ScaleColor(c)   (c > 0.0 && c < 1.0 ? c * 255 : c)
 

Functions

static void bindApplicationDefined (const Inlet *inlet, ident obj)
 InletBinding for InletTypeApplicationDefined.
 
static void bindBool (const Inlet *inlet, ident obj)
 InletBinding for InletTypeBool.
 
static void bindCharacters (const Inlet *inlet, ident obj)
 InletBinding for InletTypeCharacters.
 
static void bindClassNames (const Inlet *inlet, ident obj)
 InletBinding for InletTypeClassNames.
 
static void bindClassNames_enumerate (const Array *array, ident obj, ident data)
 ArrayEnumerator for bindClassNames.
 
static void bindColor (const Inlet *inlet, ident obj)
 InletBinding for InletTypeColor.
 
static void bindDouble (const Inlet *inlet, ident obj)
 InletBinding for InletTypeDouble.
 
static void bindEnum (const Inlet *inlet, ident obj)
 InletBinding for InletTypeEnum.
 
static void bindFloat (const Inlet *inlet, ident obj)
 InletBinding for InletTypeFloat.
 
bool bindInlets (const Inlet *inlets, const Dictionary *dictionary)
 Binds each Inlet specified in inlets to the data provided in dictionary.
 
static void bindInteger (const Inlet *inlet, ident obj)
 InletBinding for InletTypeInteger.
 
static void bindPoint (const Inlet *inlet, ident obj)
 InletBinding for InletTypePoint.
 
static void bindRectangle (const Inlet *inlet, ident obj)
 InletBinding for InletTypeRectangle.
 
static void bindSize (const Inlet *inlet, ident obj)
 InletBinding for InletTypeSize.
 
static void bindStyle (const Inlet *inlet, ident obj)
 InletBinding for InletTypeStyle.
 
static void bindSubviews (const Inlet *inlet, ident obj)
 InletBinding for InletTypeSubviews.
 
static void bindSubviews_enumerate (const Array *array, ident obj, ident data)
 ArrayEnumerator for bind subview recursion.
 
static void bindView (const Inlet *inlet, ident obj)
 Binds the given View with the specified Dictionary.
 

Variables

const InletBinding inletBindings []
 The array of InletBinding functions, indexed by InletType.
 

Macro Definition Documentation

◆ _Class

#define _Class   _View

Definition at line 27 of file View+JSON.c.

◆ ScaleColor

#define ScaleColor (   c)    (c > 0.0 && c < 1.0 ? c * 255 : c)

Function Documentation

◆ bindApplicationDefined()

static void bindApplicationDefined ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeApplicationDefined.

Definition at line 275 of file View+JSON.c.

275 {
276
277 assert(inlet->data);
278
279 ((InletBinding) inlet->data)(inlet, obj);
280}
void(* InletBinding)(const Inlet *inlet, ident obj)
A function pointer for Inlet binding.
Definition View+JSON.h:180
ident data
Type-specific data, e.g. an array of EnumNames.
Definition View+JSON.h:169

◆ bindBool()

static void bindBool ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeBool.

Definition at line 32 of file View+JSON.c.

32 {
33 *((bool *) inlet->dest) = cast(Boole, obj)->value;
34}
ident dest
The Inlet destination.
Definition View+JSON.h:164

◆ bindCharacters()

static void bindCharacters ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeCharacters.

Definition at line 39 of file View+JSON.c.

39 {
40
41 char **dest = inlet->dest;
42
43 if (*dest) {
44 free(*dest);
45 }
46
47 *dest = strdup(cast(String, obj)->chars);
48}

◆ bindClassNames()

static void bindClassNames ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeClassNames.

Definition at line 60 of file View+JSON.c.

60 {
61 $(cast(Array, obj), enumerate, bindClassNames_enumerate, *(View **) inlet->dest);
62}
static void bindClassNames_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for bindClassNames.
Definition View+JSON.c:53
static void enumerate(View *self, ViewEnumerator enumerator, ident data)
Definition View.c:807
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225

◆ bindClassNames_enumerate()

static void bindClassNames_enumerate ( const Array *  array,
ident  obj,
ident  data 
)
static

ArrayEnumerator for bindClassNames.

Definition at line 53 of file View+JSON.c.

53 {
54 $((View *) data, addClassName, cast(String, obj)->chars);
55}
static void addClassName(View *self, const char *className)
Definition View.c:183

◆ bindColor()

static void bindColor ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeColor.

Definition at line 67 of file View+JSON.c.

67 {
68
69 SDL_Color color = Colors.Black;
70
71 if ($((Object *) obj, isKindOfClass, _String())) {
72
73 String *string = cast(String, obj);
74 if (string->length) {
75
76 if (strcmp("none", string->chars) == 0) {
77 return;
78 }
79
80 if (string->chars[0] == '#') {
81 color = MVC_HexToRGBA(string->chars + 1);
82 } else {
83 color = MVC_ColorForName(string->chars);
84 }
85 }
86
87 } else {
88 const Array *array = cast(Array, obj);
89
90 assert(array->count == 4);
91
92 const Number *r = $(array, objectAtIndex, 0);
93 const Number *g = $(array, objectAtIndex, 1);
94 const Number *b = $(array, objectAtIndex, 2);
95 const Number *a = $(array, objectAtIndex, 3);
96
97 #define ScaleColor(c) (c > 0.0 && c < 1.0 ? c * 255 : c)
98
99 color = MakeColor(
100 ScaleColor(r->value),
101 ScaleColor(g->value),
102 ScaleColor(b->value),
103 ScaleColor(a->value)
104 );
105 }
106
107 *((SDL_Color *) inlet->dest) = color;
108}
SDL_Color MVC_ColorForName(const char *name)
Definition Colors.c:181
SDL_Color MVC_HexToRGBA(const char *hex)
Definition Colors.c:639
#define ScaleColor(c)
W3C Color constants.
Definition Colors.h:37
SDL_Color Black
Definition Colors.h:46

◆ bindDouble()

static void bindDouble ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeDouble.

Definition at line 113 of file View+JSON.c.

113 {
114 *((double *) inlet->dest) = cast(Number, obj)->value;
115}

◆ bindEnum()

static void bindEnum ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeEnum.

Definition at line 120 of file View+JSON.c.

120 {
121 *((int *) inlet->dest) = valueof(inlet->data, cast(String, obj)->chars);
122}

◆ bindFloat()

static void bindFloat ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeFloat.

Definition at line 127 of file View+JSON.c.

127 {
128 *((float *) inlet->dest) = cast(Number, obj)->value;
129}

◆ bindInlets()

bool bindInlets ( const Inlet *  inlets,
const Dictionary *  dictionary 
)

Binds each Inlet specified in inlets to the data provided in dictionary.

Parameters
inletsThe Inlets to bind.
dictionaryThe Dictionary from which to bind.
Returns
True if one or more Inlets were bound, false otherwise.

Definition at line 303 of file View+JSON.c.

303 {
304
305 assert(inlets);
306 assert(dictionary);
307
308 bool didBindInlets = false;
309
310 for (const Inlet *inlet = inlets; inlet->name; inlet++) {
311 const ident obj = $(dictionary, objectForKeyPath, inlet->name);
312 if (obj) {
313 BindInlet(inlet, obj);
314 didBindInlets = true;
315 }
316 }
317
318 return didBindInlets;
319}
#define BindInlet(inlet, obj)
Binds the Inlet to obj by invoking the appropriate InletBinding function.
Definition View+JSON.h:238
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
const char * name
The Inlet name, e.g. "alignment".
Definition View+JSON.h:154

◆ bindInteger()

static void bindInteger ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeInteger.

Definition at line 134 of file View+JSON.c.

134 {
135 *((int *) inlet->dest) = cast(Number, obj)->value;
136}

◆ bindPoint()

static void bindPoint ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypePoint.

Definition at line 141 of file View+JSON.c.

141 {
142
143 const Array *array = cast(Array, obj);
144
145 assert(array->count == 2);
146
147 const Number *x = $(array, objectAtIndex, 0);
148 const Number *y = $(array, objectAtIndex, 1);
149
150 *((SDL_Point *) inlet->dest) = MakePoint(x->value, y->value);
151}

◆ bindRectangle()

static void bindRectangle ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeRectangle.

Definition at line 156 of file View+JSON.c.

156 {
157
158 if ($((Object *) obj, isKindOfClass, _Number())) {
159 const Number *n = obj;
160
161 *((SDL_Rect *) inlet->dest) = MakeRect(n->value, n->value, n->value, n->value);
162 } else {
163 const Array *array = cast(Array, obj);
164
165 assert(array->count == 4);
166
167 const Number *x = $(array, objectAtIndex, 0);
168 const Number *y = $(array, objectAtIndex, 1);
169 const Number *w = $(array, objectAtIndex, 2);
170 const Number *h = $(array, objectAtIndex, 3);
171
172 *((SDL_Rect *) inlet->dest) = MakeRect(x->value, y->value, w->value, h->value);
173 }
174}

◆ bindSize()

static void bindSize ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeSize.

Definition at line 179 of file View+JSON.c.

179 {
180
181 const Array *array = cast(Array, obj);
182
183 assert(array->count == 2);
184
185 const Number *w = $(array, objectAtIndex, 0);
186 const Number *h = $(array, objectAtIndex, 1);
187
188 *((SDL_Size *) inlet->dest) = MakeSize(w->value, h->value);
189}

◆ bindStyle()

static void bindStyle ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeStyle.

Definition at line 245 of file View+JSON.c.

245 {
246 $(cast(View, *((View **) inlet->dest))->style, addAttributes, cast(Dictionary, obj));
247}
static void addAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:221

◆ bindSubviews()

static void bindSubviews ( const Inlet *  inlet,
ident  obj 
)
static

InletBinding for InletTypeSubviews.

Definition at line 268 of file View+JSON.c.

268 {
269 $(cast(Array, obj), enumerate, bindSubviews_enumerate, *(View **) inlet->dest);
270}
static void bindSubviews_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for bind subview recursion.
Definition View+JSON.c:252

◆ bindSubviews_enumerate()

static void bindSubviews_enumerate ( const Array *  array,
ident  obj,
ident  data 
)
static

ArrayEnumerator for bind subview recursion.

Definition at line 252 of file View+JSON.c.

252 {
253
254 View *subview = NULL;
255
256 const Inlet inlet = MakeInlet(NULL, InletTypeView, &subview, NULL);
257
258 bindView(&inlet, obj);
259
260 $(cast(View, data), addSubview, subview);
261
262 release(subview);
263}
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void bindView(const Inlet *inlet, ident obj)
Binds the given View with the specified Dictionary.
Definition View+JSON.c:194
@ InletTypeView
Definition View+JSON.h:133
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:210

◆ bindView()

static void bindView ( const Inlet *  inlet,
ident  obj 
)
static

Binds the given View with the specified Dictionary.

Definition at line 194 of file View+JSON.c.

194 {
195
196 View *dest = *(View **) inlet->dest;
197
198 const Dictionary *dictionary = cast(Dictionary, obj);
199
200 const String *className = $(dictionary, objectForKeyPath, "class");
201 if (className || dest == NULL) {
202
203 Class *clazz = className ? classForName(className->chars) : _View();
204 assert(clazz);
205
206 const Class *c = clazz;
207 while (c) {
208 if (c == _View()) {
209 break;
210 }
211 c = c->def.superclass;
212 }
213 assert(c);
214
215 if (clazz != _View()) {
216 if (interfaceof(View, clazz)->init == interfaceof(View, clazz->def.superclass)->init) {
217 MVC_LogWarn("%s does not implement View::init\n", clazz->def.name);
218 }
219 }
220
221 View *view = $((View *) _alloc(clazz), init);
222 assert(view);
223
224 $(view, awakeWithDictionary, dictionary);
225
226 if (dest) {
227 if (dest->superview) {
228 $(dest->superview, replaceSubview, dest, view);
229 }
230 release(dest);
231 }
232
233 *(View **) inlet->dest = view;
234
235 } else if (dest) {
236 $(dest, awakeWithDictionary, dictionary);
237 } else {
238 MVC_LogWarn("Inlet %s has NULL destination and no className specified\n", inlet->name);
239 }
240}
static View * init(View *self)
Definition Box.c:67
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Box.c:50
#define MVC_LogWarn(fmt,...)
Definition Log.h:54
Class * _View(void)
Definition View.c:2336
static void replaceSubview(View *self, View *subview, View *replacement)
Definition View.c:1663
View * init(View *self)
Initializes this View.
Definition Box.c:67

Variable Documentation

◆ inletBindings

const InletBinding inletBindings[]
Initial value:
= {
}
static void bindColor(const Inlet *inlet, ident obj)
InletBinding for InletTypeColor.
Definition View+JSON.c:67
static void bindEnum(const Inlet *inlet, ident obj)
InletBinding for InletTypeEnum.
Definition View+JSON.c:120
static void bindPoint(const Inlet *inlet, ident obj)
InletBinding for InletTypePoint.
Definition View+JSON.c:141
static void bindApplicationDefined(const Inlet *inlet, ident obj)
InletBinding for InletTypeApplicationDefined.
Definition View+JSON.c:275
static void bindDouble(const Inlet *inlet, ident obj)
InletBinding for InletTypeDouble.
Definition View+JSON.c:113
static void bindClassNames(const Inlet *inlet, ident obj)
InletBinding for InletTypeClassNames.
Definition View+JSON.c:60
static void bindInteger(const Inlet *inlet, ident obj)
InletBinding for InletTypeInteger.
Definition View+JSON.c:134
static void bindFloat(const Inlet *inlet, ident obj)
InletBinding for InletTypeFloat.
Definition View+JSON.c:127
static void bindRectangle(const Inlet *inlet, ident obj)
InletBinding for InletTypeRectangle.
Definition View+JSON.c:156
static void bindSize(const Inlet *inlet, ident obj)
InletBinding for InletTypeSize.
Definition View+JSON.c:179
static void bindBool(const Inlet *inlet, ident obj)
InletBinding for InletTypeBool.
Definition View+JSON.c:32
static void bindCharacters(const Inlet *inlet, ident obj)
InletBinding for InletTypeCharacters.
Definition View+JSON.c:39
static void bindSubviews(const Inlet *inlet, ident obj)
InletBinding for InletTypeSubviews.
Definition View+JSON.c:268
static void bindStyle(const Inlet *inlet, ident obj)
InletBinding for InletTypeStyle.
Definition View+JSON.c:245

The array of InletBinding functions, indexed by InletType.

Definition at line 285 of file View+JSON.c.