ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
HueColorPicker.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#include <assert.h>
24
25#include "HueColorPicker.h"
26
27#define _Class _HueColorPicker
28
29#pragma mark - Delegates
30
34static void didSetHue(Slider *slider, double value) {
35
36 HueColorPicker *this = (HueColorPicker *) slider->delegate.self;
37
38 this->hue = value;
39
40 $((View *) this, updateBindings);
41
42 if (this->delegate.didPickColor) {
43 this->delegate.didPickColor(this, this->hue, this->saturation, this->value);
44 }
45}
46
47#pragma mark - Object
48
52static void dealloc(Object *self) {
53
54 HueColorPicker *this = (HueColorPicker *) self;
55
56 memset(&this->delegate, 0, sizeof(this->delegate));
57
58 release(this->colorView);
59 release(this->hueInput);
60 release(this->hueSlider);
61 release(this->stackView);
62
63 super(Object, self, dealloc);
64}
65
66#pragma mark - View
67
71static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
72
73 super(View, self, awakeWithDictionary, dictionary);
74
75 HueColorPicker *this = (HueColorPicker *) self;
76
77 const Inlet inlets[] = MakeInlets(
78 MakeInlet("colorView", InletTypeView, &this->colorView, NULL),
79 MakeInlet("hue", InletTypeDouble, &this->hue, NULL),
80 MakeInlet("saturation", InletTypeDouble, &this->saturation, NULL),
81 MakeInlet("value", InletTypeDouble, &this->value, NULL),
82 MakeInlet("hueInput", InletTypeView, &this->hueInput, NULL),
83 MakeInlet("hueSlider", InletTypeView, &this->hueSlider, NULL),
84 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
85 );
86
87 $(self, bind, inlets, dictionary);
88}
89
93static View *init(View *self) {
94 return (View *) $((HueColorPicker *) self, initWithFrame, NULL);
95}
96
100static void updateBindings(View *self) {
101
102 super(View, self, updateBindings);
103
104 HueColorPicker *this = (HueColorPicker *) self;
105
106 $(this->hueSlider, setValue, this->hue);
107
108 this->colorView->backgroundColor = $(this, rgbColor);
109}
110
111#pragma mark - HueColorPicker
112
117static HueColorPicker *initWithFrame(HueColorPicker *self, const SDL_Rect *frame) {
118
119 self = (HueColorPicker *) super(Control, self, initWithFrame, frame);
120 if (self) {
121
122 self->stackView = $(alloc(StackView), initWithFrame, NULL);
123 assert(self->stackView);
124
125 $((View *) self, addSubview, (View *) self->stackView);
126
127 self->colorView = $(alloc(View), initWithFrame, &MakeRect(0, 0, 0, 24));
128 assert(self->colorView);
129
130 $(self->colorView, addClassName, "colorView");
131 $((View *) self->stackView, addSubview, self->colorView);
132
133 self->hueSlider = $(alloc(Slider), initWithFrame, NULL);
134 assert(self->hueSlider);
135
136 self->hueSlider->delegate.self = self;
137 self->hueSlider->delegate.didSetValue = didSetHue;
138 self->hueSlider->max = 360.0;
139 $(self->hueSlider, setLabelFormat, "%.0lf");
140
141 self->hueInput = $(alloc(Input), initWithFrame, NULL);
142 assert(self->hueInput);
143
144 $(self->hueInput, setControl, (Control *) self->hueSlider);
145 $(self->hueInput->label->text, setText, "H");
146
147 $((View *) self->stackView, addSubview, (View *) self->hueInput);
148
149 $(self, setColor, 0.0, 1.0, 1.0);
150 }
151
152 return self;
153}
154
159static SDL_Color rgbColor(const HueColorPicker *self) {
160 return MVC_HSVToRGB(self->hue, self->saturation, self->value);
161}
162
167static void setColor(HueColorPicker *self, double hue, double saturation, double value) {
168
169 self->hue = hue;
170 self->saturation = saturation;
171 self->value = value;
172
173 $((View *) self, updateBindings);
174}
175
180static void setRGBColor(HueColorPicker *self, const SDL_Color *color) {
181
182 MVC_RGBToHSV(color, &self->hue, &self->saturation, &self->value);
183
184 $((View *) self, updateBindings);
185}
186
187#pragma mark - Class lifecycle
188
192static void initialize(Class *clazz) {
193
194 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
195
196 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
197 ((ViewInterface *) clazz->interface)->init = init;
198 ((ViewInterface *) clazz->interface)->updateBindings = updateBindings;
199
200 ((HueColorPickerInterface *) clazz->interface)->initWithFrame = initWithFrame;
201 ((HueColorPickerInterface *) clazz->interface)->rgbColor = rgbColor;
202 ((HueColorPickerInterface *) clazz->interface)->setColor = setColor;
203 ((HueColorPickerInterface *) clazz->interface)->setRGBColor = setRGBColor;
204}
205
210Class *_HueColorPicker(void) {
211 static Class *clazz;
212 static Once once;
213
214 do_once(&once, {
215 clazz = _initialize(&(const ClassDef) {
216 .name = "HueColorPicker",
217 .superclass = _Control(),
218 .instanceSize = sizeof(HueColorPicker),
219 .interfaceOffset = offsetof(HueColorPicker, interface),
220 .interfaceSize = sizeof(HueColorPickerInterface),
222 });
223 });
224
225 return clazz;
226}
227
228#undef _Class
SDL_Color MVC_HSVToRGB(double hue, double saturation, double value)
Definition Colors.c:685
void MVC_RGBToHSV(const SDL_Color *color, double *hue, double *saturation, double *value)
Definition Colors.c:733
Class * _Control(void)
Definition Control.c:391
static SDL_Color rgbColor(const HueColorPicker *self)
static void setColor(HueColorPicker *self, double hue, double saturation, double value)
static View * init(View *self)
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
static HueColorPicker * initWithFrame(HueColorPicker *self, const SDL_Rect *frame)
Class * _HueColorPicker(void)
static void didSetHue(Slider *slider, double value)
SliderDelegate callback for hue modification.
static void dealloc(Object *self)
static void initialize(Class *clazz)
static void updateBindings(View *self)
static void setRGBColor(HueColorPicker *self, const SDL_Color *color)
Hue color picker.
static void setControl(Input *self, Control *control)
Definition Input.c:113
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void setValue(ProgressBar *self, double value)
static void setLabelFormat(ProgressBar *self, const char *labelFormat)
static void setText(Text *self, const char *text)
Definition Text.c:532
@ InletTypeDouble
Definition View+JSON.h:67
@ 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
static void addClassName(View *self, const char *className)
Definition View.c:159
Controls are Views which capture and respond to events.
Definition Control.h:83
The HueColorPicker type.
SDL_Color rgbColor(const HueColorPicker *self)
StackView * stackView
The StackView.
double hue
The color componenets.
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
ident self
The delegate self-reference.
Definition Slider.h:47
A Control allowing users to drag a handle to select a numeric value.
Definition Slider.h:62
SliderDelegate delegate
The delegate.
Definition Slider.h:83
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:134
void updateBindings(View *self)
Updates data bindings, prompting the appropriate layout changes.