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.
 
static void bindFont (const Inlet *inlet, ident obj)
 InletBinding for InletTypeFont.
 
static void bindImage (const Inlet *inlet, ident obj)
 InletBinding for InletTypeImage.
 
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 297 of file View+JSON.c.

297 {
298
299 assert(inlet->data);
300
301 ((InletBinding) inlet->data)(inlet, obj);
302}
void(* InletBinding)(const Inlet *inlet, ident obj)
A function pointer for Inlet binding.
Definition View+JSON.h:186
ident data
Type-specific data, e.g. an array of EnumNames.
Definition View+JSON.h:175

◆ 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:170

◆ 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:749
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134

◆ 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:159

◆ 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}

◆ bindFont()

static void bindFont ( const Inlet inlet,
ident  obj 
)
static

InletBinding for InletTypeFont.

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

134 {
135
136 release(*(Font **) inlet->dest);
137
138 // TODO
139
140 //*((Font **) inlet->dest) = $(alloc(Font), initWithName, cast(String, obj)->chars);
141}
TrueType fonts.
Definition Font.h:63

◆ bindImage()

static void bindImage ( const Inlet inlet,
ident  obj 
)
static

InletBinding for InletTypeImage.

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

146 {
147
148 release(*(Image **) inlet->dest);
149
150 *((Image **) inlet->dest) = $(alloc(Image), initWithResourceName, cast(String, obj)->chars);
151}
static Image * initWithResourceName(Image *self, const char *name)
Definition Image.c:142
Image loading.
Definition Image.h:38

◆ 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 327 of file View+JSON.c.

327 {
328
329 assert(inlets);
330 assert(dictionary);
331
332 bool didBindInlets = false;
333
334 for (const Inlet *inlet = inlets; inlet->name; inlet++) {
335 const ident obj = $(dictionary, objectForKeyPath, inlet->name);
336 if (obj) {
337 BindInlet(inlet, obj);
338 didBindInlets = true;
339 }
340 }
341
342 return didBindInlets;
343}
#define BindInlet(inlet, obj)
Binds the Inlet to obj by invoking the appropriate InletBinding function.
Definition View+JSON.h:244
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:155
const char * name
The Inlet name, e.g. "alignment".
Definition View+JSON.h:160

◆ bindInteger()

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

InletBinding for InletTypeInteger.

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

156 {
157 *((int *) inlet->dest) = cast(Number, obj)->value;
158}

◆ bindPoint()

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

InletBinding for InletTypePoint.

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

163 {
164
165 const Array *array = cast(Array, obj);
166
167 assert(array->count == 2);
168
169 const Number *x = $(array, objectAtIndex, 0);
170 const Number *y = $(array, objectAtIndex, 1);
171
172 *((SDL_Point *) inlet->dest) = MakePoint(x->value, y->value);
173}

◆ bindRectangle()

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

InletBinding for InletTypeRectangle.

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

178 {
179
180 if ($((Object *) obj, isKindOfClass, _Number())) {
181 const Number *n = obj;
182
183 *((SDL_Rect *) inlet->dest) = MakeRect(n->value, n->value, n->value, n->value);
184 } else {
185 const Array *array = cast(Array, obj);
186
187 assert(array->count == 4);
188
189 const Number *x = $(array, objectAtIndex, 0);
190 const Number *y = $(array, objectAtIndex, 1);
191 const Number *w = $(array, objectAtIndex, 2);
192 const Number *h = $(array, objectAtIndex, 3);
193
194 *((SDL_Rect *) inlet->dest) = MakeRect(x->value, y->value, w->value, h->value);
195 }
196}

◆ bindSize()

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

InletBinding for InletTypeSize.

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

201 {
202
203 const Array *array = cast(Array, obj);
204
205 assert(array->count == 2);
206
207 const Number *w = $(array, objectAtIndex, 0);
208 const Number *h = $(array, objectAtIndex, 1);
209
210 *((SDL_Size *) inlet->dest) = MakeSize(w->value, h->value);
211}

◆ bindStyle()

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

InletBinding for InletTypeStyle.

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

267 {
268 $(cast(View, *((View **) inlet->dest))->style, addAttributes, cast(Dictionary, obj));
269}
static void addAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:119

◆ bindSubviews()

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

InletBinding for InletTypeSubviews.

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

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

◆ bindSubviews_enumerate()

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

ArrayEnumerator for bind subview recursion.

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

274 {
275
276 View *subview = NULL;
277
278 const Inlet inlet = MakeInlet(NULL, InletTypeView, &subview, NULL);
279
280 bindView(&inlet, obj);
281
282 $(cast(View, data), addSubview, subview);
283
284 release(subview);
285}
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:216
@ InletTypeView
Definition View+JSON.h:139
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:216

◆ bindView()

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

Binds the given View with the specified Dictionary.

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

216 {
217
218 View *dest = *(View **) inlet->dest;
219
220 const Dictionary *dictionary = cast(Dictionary, obj);
221
222 const String *className = $(dictionary, objectForKeyPath, "class");
223 if (className || dest == NULL) {
224
225 Class *clazz = className ? classForName(className->chars) : _View();
226 assert(clazz);
227
228 const Class *c = clazz;
229 while (c) {
230 if (c == _View()) {
231 break;
232 }
233 c = c->def.superclass;
234 }
235 assert(c);
236
237 if (clazz != _View()) {
238 if (interfaceof(View, clazz)->init == interfaceof(View, clazz->def.superclass)->init) {
239 MVC_LogWarn("%s does not implement View::init\n", clazz->def.name);
240 }
241 }
242
243 View *view = $((View *) _alloc(clazz), init);
244 assert(view);
245
246 $(view, awakeWithDictionary, dictionary);
247
248 if (dest) {
249 if (dest->superview) {
250 $(dest->superview, replaceSubview, dest, view);
251 }
252 release(dest);
253 }
254
255 *(View **) inlet->dest = view;
256
257 } else if (dest) {
258 $(dest, awakeWithDictionary, dictionary);
259 } else {
260 MVC_LogWarn("Inlet %s has NULL destination and no className specified\n", inlet->name);
261 }
262}
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:2067
static void replaceSubview(View *self, View *subview, View *replacement)
Definition View.c:1482
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:163
static void bindApplicationDefined(const Inlet *inlet, ident obj)
InletBinding for InletTypeApplicationDefined.
Definition View+JSON.c:297
static void bindImage(const Inlet *inlet, ident obj)
InletBinding for InletTypeImage.
Definition View+JSON.c:146
static void bindFont(const Inlet *inlet, ident obj)
InletBinding for InletTypeFont.
Definition View+JSON.c:134
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:156
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:178
static void bindSize(const Inlet *inlet, ident obj)
InletBinding for InletTypeSize.
Definition View+JSON.c:201
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:290
static void bindStyle(const Inlet *inlet, ident obj)
InletBinding for InletTypeStyle.
Definition View+JSON.c:267

The array of InletBinding functions, indexed by InletType.

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