ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions
RGBColorPicker.c File Reference
#include <assert.h>
#include "RGBColorPicker.h"

Go to the source code of this file.

Macros

#define _Class   _RGBColorPicker
 

Functions

Class * _RGBColorPicker (void)
 
static void awakeWithDictionary (View *self, const Dictionary *dictionary)
 
static void dealloc (Object *self)
 
static void didSetComponent (Slider *slider, double value)
 SliderDelegate callback for color component modification. More...
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static RGBColorPickerinitWithFrame (RGBColorPicker *self, const SDL_Rect *frame)
 
static void setColor (RGBColorPicker *self, const SDL_Color *color)
 
static void updateBindings (View *self)
 

Macro Definition Documentation

◆ _Class

#define _Class   _RGBColorPicker

Definition at line 28 of file RGBColorPicker.c.

Function Documentation

◆ _RGBColorPicker()

Class * _RGBColorPicker ( void  )

Definition at line 261 of file RGBColorPicker.c.

261 {
262 static Class *clazz;
263 static Once once;
264
265 do_once(&once, {
266 clazz = _initialize(&(const ClassDef) {
267 .name = "RGBColorPicker",
268 .superclass = _Control(),
269 .instanceSize = sizeof(RGBColorPicker),
270 .interfaceOffset = offsetof(RGBColorPicker, interface),
271 .interfaceSize = sizeof(RGBColorPickerInterface),
273 });
274 });
275
276 return clazz;
277}
static void initialize(Class *clazz)
Class * _Control(void)
The Control archetype.
Definition: Control.c:379
RGB(A) color picker.

◆ awakeWithDictionary()

static void awakeWithDictionary ( View self,
const Dictionary *  dictionary 
)
static
See also
View::awakeWithDictionary(View *, const Dictionary *)

Definition at line 88 of file RGBColorPicker.c.

88 {
89
90 super(View, self, awakeWithDictionary, dictionary);
91
92 RGBColorPicker *this = (RGBColorPicker *) self;
93
94 const Inlet inlets[] = MakeInlets(
95 MakeInlet("color", InletTypeColor, &this->color, NULL),
96 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
97 MakeInlet("redSlider", InletTypeView, &this->redSlider, NULL),
98 MakeInlet("redInput", InletTypeView, &this->redInput, NULL),
99 MakeInlet("greenSlider", InletTypeView, &this->greenSlider, NULL),
100 MakeInlet("greenInput", InletTypeView, &this->greenInput, NULL),
101 MakeInlet("blueSlider", InletTypeView, &this->blueSlider, NULL),
102 MakeInlet("blueInput", InletTypeView, &this->blueInput, NULL),
103 MakeInlet("alphaSlider", InletTypeView, &this->alphaSlider, NULL),
104 MakeInlet("alphaInput", InletTypeView, &this->alphaInput, NULL),
105 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
106 );
107
108 $(self, bind, inlets, dictionary);
109}
@ InletTypeColor
Definition: View+JSON.h:62
@ 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
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
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.
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 65 of file RGBColorPicker.c.

65 {
66
67 RGBColorPicker *this = (RGBColorPicker *) self;
68
69 release(this->colorView);
70 release(this->redSlider);
71 release(this->redInput);
72 release(this->greenSlider);
73 release(this->greenInput);
74 release(this->blueSlider);
75 release(this->blueInput);
76 release(this->alphaSlider);
77 release(this->alphaInput);
78 release(this->stackView);
79
80 super(Object, self, dealloc);
81}
static void dealloc(Object *self)

◆ didSetComponent()

static void didSetComponent ( Slider slider,
double  value 
)
static

SliderDelegate callback for color component modification.

Definition at line 35 of file RGBColorPicker.c.

35 {
36
37 RGBColorPicker *this = (RGBColorPicker *) slider->delegate.self;
38
39 const int c = round(value);
40
41 if (slider == this->redSlider) {
42 this->color.r = c;
43 } else if (slider == this->greenSlider) {
44 this->color.g = c;
45 } else if (slider == this->blueSlider) {
46 this->color.b = c;
47 } else if (slider == this->alphaSlider) {
48 this->color.a = c;
49 } else {
50 assert(false);
51 }
52
53 $((View *) this, updateBindings);
54
55 if (this->delegate.didPickColor) {
56 this->delegate.didPickColor(this, &this->color);
57 }
58}
ident self
The delegate self-reference.
Definition: Slider.h:47
SliderDelegate delegate
The delegate.
Definition: Slider.h:83
void updateBindings(View *self)
Updates data bindings, prompting the appropriate layout changes.

◆ init()

static View * init ( View self)
static
See also
View::init(View *)

Definition at line 114 of file RGBColorPicker.c.

114 {
115 return (View *) $((RGBColorPicker *) self, initWithFrame, NULL);
116}
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 245 of file RGBColorPicker.c.

245 {
246
247 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
248
249 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
250 ((ViewInterface *) clazz->interface)->init = init;
251 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
252
253 ((RGBColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
254 ((RGBColorPickerInterface *) clazz->interface)->setColor = setColor;
255}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
void setColor(HSVColorPicker *self, double hue, double saturation, double value)
Sets the color of the HSVColorPicker.

◆ initWithFrame()

static RGBColorPicker * initWithFrame ( RGBColorPicker self,
const SDL_Rect *  frame 
)
static

Definition at line 141 of file RGBColorPicker.c.

141 {
142
143 self = (RGBColorPicker *) super(Control, self, initWithFrame, frame);
144 if (self) {
145
146 self->stackView = $(alloc(StackView), initWithFrame, NULL);
147 assert(self->stackView);
148
149 $((View *) self, addSubview, (View *) self->stackView);
150
151 self->colorView = $(alloc(View), initWithFrame, &MakeRect(0, 0, 0, 24));
152 assert(self->colorView);
153
154 $(self->colorView, addClassName, "colorView");
155 $((View *) self->stackView, addSubview, self->colorView);
156
157 self->redSlider = $(alloc(Slider), initWithFrame, NULL);
158 assert(self->redSlider);
159
160 self->redSlider->delegate.self = self;
161 self->redSlider->delegate.didSetValue = didSetComponent;
162 self->redSlider->max = 255.0;
163 $(self->redSlider, setLabelFormat, "%.0f");
164
165 self->redInput = $(alloc(Input), initWithFrame, NULL);
166 assert(self->redInput);
167
168 $(self->redInput, setControl, (Control *) self->redSlider);
169 $(self->redInput->label->text, setText, "R");
170
171 $((View *) self->stackView, addSubview, (View *) self->redInput);
172
173 self->greenSlider = $(alloc(Slider), initWithFrame, NULL);
174 assert(self->greenSlider);
175
176 self->greenSlider->delegate.self = self;
177 self->greenSlider->delegate.didSetValue = didSetComponent;
178 self->greenSlider->max = 255.0;
179 $(self->greenSlider, setLabelFormat, "%.0f");
180
181 self->greenInput = $(alloc(Input), initWithFrame, NULL);
182 assert(self->greenInput);
183
184 $(self->greenInput, setControl, (Control *) self->greenSlider);
185 $(self->greenInput->label->text, setText, "G");
186
187 $((View *) self->stackView, addSubview, (View *) self->greenInput);
188
189 self->blueSlider = $(alloc(Slider), initWithFrame, NULL);
190 assert(self->blueSlider);
191
192 self->blueSlider->delegate.self = self;
193 self->blueSlider->delegate.didSetValue = didSetComponent;
194 self->blueSlider->max = 255.0;
195 $(self->blueSlider, setLabelFormat, "%.0f");
196
197 self->blueInput = $(alloc(Input), initWithFrame, NULL);
198 assert(self->blueInput);
199
200 $(self->blueInput, setControl, (Control *) self->blueSlider);
201 $(self->blueInput->label->text, setText, "B");
202
203 $((View *) self->stackView, addSubview, (View *) self->blueInput);
204
205 self->alphaSlider = $(alloc(Slider), initWithFrame, NULL);
206 assert(self->alphaSlider);
207
208 self->alphaSlider->delegate.self = self;
209 self->alphaSlider->delegate.didSetValue = didSetComponent;
210 self->alphaSlider->max = 255.0;
211 $(self->alphaSlider, setLabelFormat, "%.0f");
212
213 self->alphaInput = $(alloc(Input), initWithFrame, NULL);
214 assert(self->alphaInput);
215
216 $(self->alphaInput, setControl, (Control *) self->alphaSlider);
217 $(self->alphaInput->label->text, setText, "A");
218
219 $((View *) self->stackView, addSubview, (View *) self->alphaInput);
220
221 $(self, setColor, &Colors.White);
222 }
223
224 return self;
225}
static void didSetComponent(Slider *slider, double value)
SliderDelegate callback for color component modification.
#define MakeRect(x, y, w, h)
Creates an SDL_Rect with the given origin and size.
Definition: Types.h:74
W3C Color constants.
Definition: Colors.h:37
SDL_Color White
Definition: Colors.h:185
Controls are Views which capture events and dispatch Actions.
Definition: Control.h:83
An Input stacks a Label with a Control.
Definition: Input.h:57
void setControl(Input *self, Control *control)
Sets this Input's Control.
Definition: Input.c:113
void setLabelFormat(ProgressBar *self, const char *labelFormat)
Changes this ProgressBar's label format and calls appropriate update functions.
Definition: ProgressBar.c:145
StackView * stackView
The StackView.
A Control allowing users to drag a handle to select a numeric value.
Definition: Slider.h:62
StackViews are containers that manage the arrangement of their subviews.
Definition: StackView.h:68
void setText(Text *self, const char *text)
Sets this Text's text.
Definition: Text.c:261
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

◆ setColor()

static void setColor ( RGBColorPicker self,
const SDL_Color *  color 
)
static

Definition at line 231 of file RGBColorPicker.c.

231 {
232
233 assert(color);
234
235 self->color = *color;
236
237 $((View *) self, updateBindings);
238}
SDL_Color color
The color.

◆ updateBindings()

static void updateBindings ( View self)
static
See also
View::updateBindings(View *)

Definition at line 121 of file RGBColorPicker.c.

121 {
122
123 super(View, self, updateBindings);
124
125 RGBColorPicker *this = (RGBColorPicker *) self;
126
127 $(this->redSlider, setValue, this->color.r);
128 $(this->greenSlider, setValue, this->color.g);
129 $(this->blueSlider, setValue, this->color.b);
130 $(this->alphaSlider, setValue, this->color.a);
131
132 this->colorView->backgroundColor = this->color;
133}
void setValue(const ProgressBar *self, double value)
Sets the value, which is clamped to min and max.