ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Input.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU 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 "Input.h"
27
28const EnumName InputOrientationNames[] = MakeEnumNames(
29 MakeEnumName(InputOrientationLeft),
30 MakeEnumName(InputOrientationRight),
31 MakeEnumName(InputOrientationAbove),
32 MakeEnumName(InputOrientationBelow)
33);
34
35#define _Class _Input
36
37#pragma mark - Object
38
42static void dealloc(Object *self) {
43
44 Input *this = (Input *) self;
45
46 release(this->control);
47 release(this->label);
48
49 super(Object, self, dealloc);
50}
51
52#pragma mark - View
53
57static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
58
59 super(View, self, awakeWithDictionary, dictionary);
60
61 Input *this = (Input *) self;
62
63 const Inlet inlets[] = MakeInlets(
64 MakeInlet("control", InletTypeView, &this->control, NULL),
65 MakeInlet("label", InletTypeView, &this->label, NULL),
66 MakeInlet("orientation", InletTypeEnum, &this->orientation, (ident) InputOrientationNames)
67 );
68
69 $(self, bind, inlets, dictionary);
70
71 $(this, setOrientation, this->orientation);
72}
73
77static View *init(View *self) {
78 return (View *) $((Input *) self, initWithFrame, NULL);
79}
80
81#pragma mark - Input
82
87static Input *initWithFrame(Input *self, const SDL_Rect *frame) {
88
89 self = (Input *) super(StackView, self, initWithFrame, frame);
90 if (self) {
91
92 self->control = $(alloc(Control), initWithFrame, NULL);
93 assert(self->control);
94
95 self->label = $(alloc(Label), initWithText, NULL, NULL);
96 assert(self->label);
97
98 $((View *) self, addSubview, (View *) self->control);
99 $((View *) self, addSubview, (View *) self->label);
100
101 $((View *) self, addClassName, "container");
102
104 }
105
106 return self;
107}
108
113static void setControl(Input *self, Control *control) {
114
115 assert(control);
116
117 $((View *) self, removeSubview, (View *) self->control);
118 release(self->control);
119
120 self->control = retain(control);
121 $((View *) self, addSubview, (View *) self->control);
122
123 $(self, setOrientation, self->orientation);
124}
125
130static void setLabel(Input *self, Label *label) {
131
132 assert(label);
133
134 $((View *) self, removeSubview, (View *) self->label);
135 release(self->label);
136
137 self->label = retain(label);
138 $((View *) self, addSubview, (View *) self->label);
139
140 $(self, setOrientation, self->orientation);
141}
142
147static void setOrientation(Input *self, InputOrientation orientation) {
148
149 self->orientation = orientation;
150
151 $((View *) self->control, removeFromSuperview);
152 $((View *) self->label, removeFromSuperview);
153
154 switch (self->orientation) {
157 $((View *) self, addSubview, (View *) self->label);
158 $((View *) self, addSubview, (View *) self->control);
159 break;
160
163 $((View *) self, addSubview, (View *) self->control);
164 $((View *) self, addSubview, (View *) self->label);
165 break;
166 }
167
168 switch (self->orientation) {
173 break;
174
179 break;
180
186 break;
187 }
188}
189
190#pragma mark - Class lifecycle
191
195static void initialize(Class *clazz) {
196
197 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
198
199 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
200 ((ViewInterface *) clazz->interface)->init = init;
201
202 ((InputInterface *) clazz->interface)->initWithFrame = initWithFrame;
203 ((InputInterface *) clazz->interface)->setControl = setControl;
204 ((InputInterface *) clazz->interface)->setLabel = setLabel;
205 ((InputInterface *) clazz->interface)->setOrientation = setOrientation;
206}
207
212Class *_Input(void) {
213 static Class *clazz;
214 static Once once;
215
216 do_once(&once, {
217 clazz = _initialize(&(const ClassDef) {
218 .name = "Input",
219 .superclass = _StackView(),
220 .instanceSize = sizeof(Input),
221 .interfaceOffset = offsetof(Input, interface),
222 .interfaceSize = sizeof(InputInterface),
224 });
225 });
226
227 return clazz;
228}
229
230#undef _Class
231
static void dealloc(Object *self)
Definition: Input.c:42
const EnumName InputOrientationNames[]
Definition: Input.c:28
static void initialize(Class *clazz)
Definition: Input.c:195
An Input stacks a Control and Label.
InputOrientation
Input orientation defines the relative positioning of the Label.
Definition: Input.h:38
@ InputOrientationLeft
Definition: Input.h:39
@ InputOrientationRight
Definition: Input.h:40
@ InputOrientationAbove
Definition: Input.h:41
@ InputOrientationBelow
Definition: Input.h:42
@ StackViewAxisHorizontal
Definition: StackView.h:44
@ StackViewAxisVertical
Definition: StackView.h:43
@ InletTypeEnum
Definition: View+JSON.h:75
@ InletTypeView
Definition: View+JSON.h:139
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition: View+JSON.h:221
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition: View+JSON.h:216
@ ViewAlignmentMiddleCenter
Definition: View.h:72
@ ViewAlignmentMiddleRight
Definition: View.h:73
@ ViewAlignmentMiddleLeft
Definition: View.h:71
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
CollectionView * initWithFrame(CollectionView *self, const SDL_Rect *frame)
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
Controls are Views which capture events and dispatch Actions.
Definition: Control.h:83
View view
The superclass.
Definition: Control.h:88
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
An Input stacks a Label with a Control.
Definition: Input.h:57
void setOrientation(Input *self, InputOrientation orientation)
Sets this Input's orientation.
Definition: Input.c:147
Label * label
The Label.
Definition: Input.h:78
void setLabel(Input *self, Label *label)
Sets this Input's Label.
Definition: Input.c:130
StackView stackView
The superclass.
Definition: Input.h:62
InputOrientation orientation
The Label orientation.
Definition: Input.h:83
void setControl(Input *self, Control *control)
Sets this Input's Control.
Definition: Input.c:113
Control * control
The Control.
Definition: Input.h:73
Class * _Input(void)
The Input archetype.
Definition: Input.c:212
Labels provide a configurable container for Text.
Definition: Label.h:40
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
View view
The superclass.
Definition: Label.h:45
StackViews are containers that manage the arrangement of their subviews.
Definition: StackView.h:68
StackViewAxis axis
The axis.
Definition: StackView.h:84
Class * _StackView(void)
The StackView archetype.
Definition: StackView.c:262
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition: View.h:133
_Bool bind(View *self, const Inlet *inlets, const Dictionary *dictionary)
Performs data binding for the Inlets described in dictionary.
ViewAlignment alignment
The alignment.
Definition: View.h:149
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition: View.c:120
void removeFromSuperview(View *self)
Removes this View from its superview.
Definition: View.c:1175
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50
void removeSubview(View *self, View *subview)
Removes the given subview from this View.
Definition: PageView.c:58