ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Input.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 "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 *this = (View *) self, *label = (View *) self->label, *control = (View *) self->control;
152
153 $(control, removeFromSuperview);
154 $(label, removeFromSuperview);
155
156 switch (self->orientation) {
159 $(this, addSubview, label);
160 $(this, addSubview, control);
161 break;
162
165 $(this, addSubview, control);
166 $(this, addSubview, label);
167 break;
168 }
169
170 switch (self->orientation) {
173 $(label->style, addEnumAttribute, "alignment", ViewAlignmentNames, ViewAlignmentMiddleLeft);
174 $(control->style, addEnumAttribute, "alignment", ViewAlignmentNames, ViewAlignmentMiddleRight);
175 break;
176
179 $(label->style, addEnumAttribute, "alignment", ViewAlignmentNames, ViewAlignmentMiddleRight);
180 $(control->style, addEnumAttribute, "alignment", ViewAlignmentNames, ViewAlignmentMiddleLeft);
181 break;
182
186 $(label->style, addEnumAttribute, "alignment", ViewAlignmentNames, ViewAlignmentMiddleCenter);
187 $(control->style, addEnumAttribute, "alignment", ViewAlignmentNames, ViewAlignmentMiddleCenter);
188 break;
189 }
190
191 $(this, invalidateStyle);
192 $(label, invalidateStyle);
193 $(control, invalidateStyle);
194}
195
196#pragma mark - Class lifecycle
197
201static void initialize(Class *clazz) {
202
203 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
204
205 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
206 ((ViewInterface *) clazz->interface)->init = init;
207
208 ((InputInterface *) clazz->interface)->initWithFrame = initWithFrame;
209 ((InputInterface *) clazz->interface)->setControl = setControl;
210 ((InputInterface *) clazz->interface)->setLabel = setLabel;
211 ((InputInterface *) clazz->interface)->setOrientation = setOrientation;
212}
213
218Class *_Input(void) {
219 static Class *clazz;
220 static Once once;
221
222 do_once(&once, {
223 clazz = _initialize(&(const ClassDef) {
224 .name = "Input",
225 .superclass = _StackView(),
226 .instanceSize = sizeof(Input),
227 .interfaceSize = sizeof(InputInterface),
228 .initialize = initialize,
229 });
230 });
231
232 return clazz;
233}
234
235#undef _Class
236
static View * init(View *self)
Definition Input.c:77
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Input.c:57
static void setControl(Input *self, Control *control)
Definition Input.c:113
Class * _Input(void)
Definition Input.c:218
static void dealloc(Object *self)
Definition Input.c:42
const EnumName InputOrientationNames[]
Definition Input.c:28
static Input * initWithFrame(Input *self, const SDL_Rect *frame)
Definition Input.c:87
static void initialize(Class *clazz)
Definition Input.c:201
static void setLabel(Input *self, Label *label)
Definition Input.c:130
static void setOrientation(Input *self, InputOrientation orientation)
Definition Input.c:147
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
static Label * initWithText(Label *self, const char *text, Font *font)
Definition Label.c:97
static void removeSubview(View *self, View *subview)
Definition PageView.c:72
static void addSubview(View *self, View *subview)
Definition PageView.c:49
const EnumName StackViewAxisNames[]
Definition StackView.c:28
Class * _StackView(void)
Definition StackView.c:358
@ StackViewAxisHorizontal
Definition StackView.h:44
@ StackViewAxisVertical
Definition StackView.h:43
static void addEnumAttribute(Style *self, const char *attr, const EnumName *names, int value)
Definition Style.c:288
@ InletTypeEnum
Definition View+JSON.h:75
@ InletTypeView
Definition View+JSON.h:133
#define MakeInlets(...)
Creates a null-termianted array of Inlets.
Definition View+JSON.h:215
#define MakeInlet(name, type, dest, data)
Creates an Inlet with the specified parameters.
Definition View+JSON.h:210
const EnumName ViewAlignmentNames[]
Definition View.c:63
static void addClassName(View *self, const char *className)
Definition View.c:183
static void invalidateStyle(View *self)
Definition View.c:1092
static void removeFromSuperview(View *self)
Definition View.c:1462
@ ViewAlignmentMiddleCenter
Definition View.h:76
@ ViewAlignmentMiddleRight
Definition View.h:77
@ ViewAlignmentMiddleLeft
Definition View.h:75
Controls are Views which capture and respond to events.
Definition Control.h:83
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
An Input stacks a Label with a Control.
Definition Input.h:57
Label * label
The Label.
Definition Input.h:78
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
Labels provide a configurable container for Text.
Definition Label.h:40
StackViews are containers that manage the arrangement of their subviews.
Definition StackView.h:68
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:1047