ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions | Variables
View+JSON.c File Reference
#include <assert.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. More...
 
static void bindBool (const Inlet *inlet, ident obj)
 InletBinding for InletTypeBool. More...
 
static void bindCharacters (const Inlet *inlet, ident obj)
 InletBinding for InletTypeCharacters. More...
 
static void bindClassNames (const Inlet *inlet, ident obj)
 InletBinding for InletTypeClassNames. More...
 
static void bindClassNames_enumerate (const Array *array, ident obj, ident data)
 ArrayEnumerator for bindClassNames. More...
 
static void bindColor (const Inlet *inlet, ident obj)
 InletBinding for InletTypeColor. More...
 
static void bindDouble (const Inlet *inlet, ident obj)
 InletBinding for InletTypeDouble. More...
 
static void bindEnum (const Inlet *inlet, ident obj)
 InletBinding for InletTypeEnum. More...
 
static void bindFloat (const Inlet *inlet, ident obj)
 InletBinding for InletTypeFloat. More...
 
static void bindFont (const Inlet *inlet, ident obj)
 InletBinding for InletTypeFont. More...
 
static void bindImage (const Inlet *inlet, ident obj)
 InletBinding for InletTypeImage. More...
 
_Bool bindInlets (const Inlet *inlets, const Dictionary *dictionary)
 Binds each Inlet specified in inlets to the data provided in dictionary. More...
 
static void bindInteger (const Inlet *inlet, ident obj)
 InletBinding for InletTypeInteger. More...
 
static void bindPoint (const Inlet *inlet, ident obj)
 InletBinding for InletTypePoint. More...
 
static void bindRectangle (const Inlet *inlet, ident obj)
 InletBinding for InletTypeRectangle. More...
 
static void bindSize (const Inlet *inlet, ident obj)
 InletBinding for InletTypeSize. More...
 
static void bindStyle (const Inlet *inlet, ident obj)
 InletBinding for InletTypeStyle. More...
 
static void bindSubviews (const Inlet *inlet, ident obj)
 InletBinding for InletTypeSubviews. More...
 
static void bindSubviews_enumerate (const Array *array, ident obj, ident data)
 ArrayEnumerator for bind subview recursion. More...
 
static void bindView (const Inlet *inlet, ident obj)
 Binds the given View with the specified Dictionary. More...
 

Variables

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

Macro Definition Documentation

◆ _Class

#define _Class   _View

Definition at line 26 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 296 of file View+JSON.c.

296 {
297
298 assert(inlet->data);
299
300 ((InletBinding) inlet->data)(inlet, obj);
301}
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 31 of file View+JSON.c.

31 {
32 *((_Bool *) inlet->dest) = cast(Boole, obj)->value;
33}
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 38 of file View+JSON.c.

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

◆ bindClassNames()

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

InletBinding for InletTypeClassNames.

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

59 {
60 $(cast(Array, obj), enumerateObjects, bindClassNames_enumerate, *(View **) inlet->dest);
61}
static void bindClassNames_enumerate(const Array *array, ident obj, ident data)
ArrayEnumerator for bindClassNames.
Definition: View+JSON.c:52
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133

◆ bindClassNames_enumerate()

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

ArrayEnumerator for bindClassNames.

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

52 {
53 $((View *) data, addClassName, cast(String, obj)->chars);
54}
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120

◆ bindColor()

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

InletBinding for InletTypeColor.

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

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

◆ bindDouble()

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

InletBinding for InletTypeDouble.

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

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

◆ bindEnum()

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

InletBinding for InletTypeEnum.

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

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

◆ bindFloat()

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

InletBinding for InletTypeFloat.

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

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

◆ bindFont()

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

InletBinding for InletTypeFont.

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

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

◆ bindImage()

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

InletBinding for InletTypeImage.

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

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

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

326 {
327
328 assert(inlets);
329 assert(dictionary);
330
331 _Bool didBindInlets = false;
332
333 for (const Inlet *inlet = inlets; inlet->name; inlet++) {
334 const ident obj = $(dictionary, objectForKeyPath, inlet->name);
335 if (obj) {
336 BindInlet(inlet, obj);
337 didBindInlets = true;
338 }
339 }
340
341 return didBindInlets;
342}
#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 155 of file View+JSON.c.

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

◆ bindPoint()

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

InletBinding for InletTypePoint.

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

162 {
163
164 const Array *array = cast(Array, obj);
165
166 assert(array->count == 2);
167
168 const Number *x = $(array, objectAtIndex, 0);
169 const Number *y = $(array, objectAtIndex, 1);
170
171 *((SDL_Point *) inlet->dest) = MakePoint(x->value, y->value);
172}
#define MakePoint(x, y)
Creates an SDL_Point with the given coordinates.
Definition: Types.h:69

◆ bindRectangle()

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

InletBinding for InletTypeRectangle.

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

177 {
178
179 if ($((Object *) obj, isKindOfClass, _Number())) {
180 const Number *n = obj;
181
182 *((SDL_Rect *) inlet->dest) = MakeRect(n->value, n->value, n->value, n->value);
183 } else {
184 const Array *array = cast(Array, obj);
185
186 assert(array->count == 4);
187
188 const Number *x = $(array, objectAtIndex, 0);
189 const Number *y = $(array, objectAtIndex, 1);
190 const Number *w = $(array, objectAtIndex, 2);
191 const Number *h = $(array, objectAtIndex, 3);
192
193 *((SDL_Rect *) inlet->dest) = MakeRect(x->value, y->value, w->value, h->value);
194 }
195}
#define MakeRect(x, y, w, h)
Creates an SDL_Rect with the given origin and size.
Definition: Types.h:74

◆ bindSize()

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

InletBinding for InletTypeSize.

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

200 {
201
202 const Array *array = cast(Array, obj);
203
204 assert(array->count == 2);
205
206 const Number *w = $(array, objectAtIndex, 0);
207 const Number *h = $(array, objectAtIndex, 1);
208
209 *((SDL_Size *) inlet->dest) = MakeSize(w->value, h->value);
210}
#define MakeSize(w, h)
Creates an SDL_Size with the given dimensions.
Definition: Types.h:79
The SDL_Size type.
Definition: Types.h:62

◆ bindStyle()

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

InletBinding for InletTypeStyle.

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

266 {
267 $(cast(View, *((View **) inlet->dest))->style, addAttributes, cast(Dictionary, obj));
268}
void addAttributes(Style *self, const Dictionary *attributes)
Adds or replaces the attribtues in attributes to this Style.
Definition: Style.c:119

◆ bindSubviews()

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

InletBinding for InletTypeSubviews.

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

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

◆ bindSubviews_enumerate()

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

ArrayEnumerator for bind subview recursion.

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

273 {
274
275 View *subview = NULL;
276
277 const Inlet inlet = MakeInlet(NULL, InletTypeView, &subview, NULL);
278
279 bindView(&inlet, obj);
280
281 $(cast(View, data), addSubview, subview);
282
283 release(subview);
284}
static void bindView(const Inlet *inlet, ident obj)
Binds the given View with the specified Dictionary.
Definition: View+JSON.c:215
@ InletTypeView
Definition: View+JSON.h:139
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition: View+JSON.h:216
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35

◆ bindView()

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

Binds the given View with the specified Dictionary.

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

215 {
216
217 View *dest = *(View **) inlet->dest;
218
219 const Dictionary *dictionary = cast(Dictionary, obj);
220
221 const String *className = $(dictionary, objectForKeyPath, "class");
222 if (className || dest == NULL) {
223
224 Class *clazz = className ? classForName(className->chars) : _View();
225 assert(clazz);
226
227 const Class *c = clazz;
228 while (c) {
229 if (c == _View()) {
230 break;
231 }
232 c = c->def.superclass;
233 }
234 assert(c);
235
236 if (clazz != _View()) {
237 if (interfaceof(View, clazz)->init == interfaceof(View, clazz->def.superclass)->init) {
238 MVC_LogWarn("%s does not implement View::init\n", clazz->def.name);
239 }
240 }
241
242 View *view = $((View *) _alloc(clazz), init);
243 assert(view);
244
245 $(view, awakeWithDictionary, dictionary);
246
247 if (dest) {
248 if (dest->superview) {
249 $(dest->superview, replaceSubview, dest, view);
250 }
251 release(dest);
252 }
253
254 *(View **) inlet->dest = view;
255
256 } else if (dest) {
257 $(dest, awakeWithDictionary, dictionary);
258 } else {
259 MVC_LogWarn("Inlet %s has NULL destination and no className specified\n", inlet->name);
260 }
261}
#define MVC_LogWarn(fmt,...)
Definition: Log.h:52
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
View * superview
The super View.
Definition: View.h:258
Class * _View(void)
The View archetype.
Definition: View.c:1769
void replaceSubview(View *self, View *subview, View *replacement)
Replaces the specified subview with the given replacement.
Definition: View.c:1302
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50

Variable Documentation

◆ inletBindings

const InletBinding inletBindings[]
Initial value:
= {
}
static void bindColor(const Inlet *inlet, ident obj)
InletBinding for InletTypeColor.
Definition: View+JSON.c:66
static void bindEnum(const Inlet *inlet, ident obj)
InletBinding for InletTypeEnum.
Definition: View+JSON.c:119
static void bindPoint(const Inlet *inlet, ident obj)
InletBinding for InletTypePoint.
Definition: View+JSON.c:162
static void bindApplicationDefined(const Inlet *inlet, ident obj)
InletBinding for InletTypeApplicationDefined.
Definition: View+JSON.c:296
static void bindImage(const Inlet *inlet, ident obj)
InletBinding for InletTypeImage.
Definition: View+JSON.c:145
static void bindFont(const Inlet *inlet, ident obj)
InletBinding for InletTypeFont.
Definition: View+JSON.c:133
static void bindDouble(const Inlet *inlet, ident obj)
InletBinding for InletTypeDouble.
Definition: View+JSON.c:112
static void bindClassNames(const Inlet *inlet, ident obj)
InletBinding for InletTypeClassNames.
Definition: View+JSON.c:59
static void bindInteger(const Inlet *inlet, ident obj)
InletBinding for InletTypeInteger.
Definition: View+JSON.c:155
static void bindFloat(const Inlet *inlet, ident obj)
InletBinding for InletTypeFloat.
Definition: View+JSON.c:126
static void bindRectangle(const Inlet *inlet, ident obj)
InletBinding for InletTypeRectangle.
Definition: View+JSON.c:177
static void bindSize(const Inlet *inlet, ident obj)
InletBinding for InletTypeSize.
Definition: View+JSON.c:200
static void bindBool(const Inlet *inlet, ident obj)
InletBinding for InletTypeBool.
Definition: View+JSON.c:31
static void bindCharacters(const Inlet *inlet, ident obj)
InletBinding for InletTypeCharacters.
Definition: View+JSON.c:38
static void bindSubviews(const Inlet *inlet, ident obj)
InletBinding for InletTypeSubviews.
Definition: View+JSON.c:289
static void bindStyle(const Inlet *inlet, ident obj)
InletBinding for InletTypeStyle.
Definition: View+JSON.c:266

The array of InletBinding functions, indexed by InletType.

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