ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
RGBColorPicker.c File Reference
#include <assert.h>
#include <math.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.
 
static View * init (View *self)
 
static void initialize (Class *clazz)
 
static RGBColorPicker * initWithFrame (RGBColorPicker *self, const SDL_Rect *frame)
 
static void setColor (RGBColorPicker *self, const SDL_Color *color)
 
static void updateBindings (View *self, ident data)
 

Macro Definition Documentation

◆ _Class

#define _Class   _RGBColorPicker

Definition at line 29 of file RGBColorPicker.c.

Function Documentation

◆ _RGBColorPicker()

Class * _RGBColorPicker ( void  )

Definition at line 266 of file RGBColorPicker.c.

266 {
267 static Class *clazz;
268 static Once once;
269
270 do_once(&once, {
271 clazz = _initialize(&(const ClassDef) {
272 .name = "RGBColorPicker",
273 .superclass = _Control(),
274 .instanceSize = sizeof(RGBColorPicker),
275 .interfaceSize = sizeof(RGBColorPickerInterface),
276 .initialize = initialize,
277 });
278 });
279
280 return clazz;
281}
Class * _Control(void)
Definition Control.c:414
static void initialize(Class *clazz)
RGB(A) color picker.

◆ awakeWithDictionary()

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

Definition at line 91 of file RGBColorPicker.c.

91 {
92
93 super(View, self, awakeWithDictionary, dictionary);
94
95 RGBColorPicker *this = (RGBColorPicker *) self;
96
97 const Inlet inlets[] = MakeInlets(
98 MakeInlet("color", InletTypeColor, &this->color, NULL),
99 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
100 MakeInlet("redSlider", InletTypeView, &this->redSlider, NULL),
101 MakeInlet("redInput", InletTypeView, &this->redInput, NULL),
102 MakeInlet("greenSlider", InletTypeView, &this->greenSlider, NULL),
103 MakeInlet("greenInput", InletTypeView, &this->greenInput, NULL),
104 MakeInlet("blueSlider", InletTypeView, &this->blueSlider, NULL),
105 MakeInlet("blueInput", InletTypeView, &this->blueInput, NULL),
106 MakeInlet("alphaSlider", InletTypeView, &this->alphaSlider, NULL),
107 MakeInlet("alphaInput", InletTypeView, &this->alphaInput, NULL),
108 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
109 );
110
111 $(self, bind, inlets, dictionary);
112}
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
@ InletTypeColor
Definition View+JSON.h:62
@ 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
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225

◆ dealloc()

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

Definition at line 66 of file RGBColorPicker.c.

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

◆ didSetComponent()

static void didSetComponent ( Slider *  slider,
double  value 
)
static

SliderDelegate callback for color component modification.

Definition at line 36 of file RGBColorPicker.c.

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

◆ init()

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

Definition at line 117 of file RGBColorPicker.c.

117 {
118 return (View *) $((RGBColorPicker *) self, initWithFrame, NULL);
119}
static RGBColorPicker * initWithFrame(RGBColorPicker *self, const SDL_Rect *frame)

◆ initialize()

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

Definition at line 250 of file RGBColorPicker.c.

250 {
251
252 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
253
254 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
255 ((ViewInterface *) clazz->interface)->init = init;
256 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
257
258 ((RGBColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
259 ((RGBColorPickerInterface *) clazz->interface)->setColor = setColor;
260}
static View * init(View *self)
static void setColor(RGBColorPicker *self, const SDL_Color *color)
void setColor(RGBColorPicker *self, const SDL_Color *color)
Sets the color of the RGBColorPicker.
void updateBindings(View *self, ident data)
Updates data bindings, prompting the appropriate layout changes.

◆ initWithFrame()

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

Definition at line 146 of file RGBColorPicker.c.

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

◆ setColor()

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

Definition at line 236 of file RGBColorPicker.c.

236 {
237
238 assert(color);
239
240 self->color = *color;
241
242 $((View *) self, updateBindings, NULL);
243}
SDL_Color color
The color.

◆ updateBindings()

static void updateBindings ( View *  self,
ident  data 
)
static
See also
View::updateBindings(View *, ident)

Definition at line 124 of file RGBColorPicker.c.

124 {
125
126 super(View, self, updateBindings, data);
127
128 RGBColorPicker *this = (RGBColorPicker *) self;
129
130 $(this->redSlider, setValue, this->color.r);
131 $(this->greenSlider, setValue, this->color.g);
132 $(this->blueSlider, setValue, this->color.b);
133 $(this->alphaSlider, setValue, this->color.a);
134
135 const SDL_Color color = this->color;
136 $(this->colorView->style, addColorAttribute, "background-color", &color);
137 $(this->colorView, invalidateStyle);
138}
static void setValue(ProgressBar *self, double value)
static void addColorAttribute(Style *self, const char *attr, const SDL_Color *color)
Definition Style.c:253
static void invalidateStyle(View *self)
Definition View.c:1092