ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Checkbox.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 "Checkbox.h"
27
28#include "../Assets/check.png.h"
29
30#define _Class _Checkbox
31
32static Image *_check;
33
34#pragma mark - Object
35
39static void dealloc(Object *self) {
40
41 Checkbox *this = (Checkbox *) self;
42
43 memset(&this->delegate, 0, sizeof(this->delegate));
44
45 release(this->box);
46 release(this->check);
47
48 super(Object, self, dealloc);
49}
50
51#pragma mark - View
52
56static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
57
58 super(View, self, awakeWithDictionary, dictionary);
59
60 Checkbox *this = (Checkbox *) self;
61
62 const Inlet inlets[] = MakeInlets(
63 MakeInlet("check", InletTypeView, &this->check, NULL)
64 );
65
66 $(self, bind, inlets, dictionary);
67}
68
72static View *init(View *self) {
73 return (View *) $((Checkbox *) self, initWithFrame, NULL);
74}
75
76#pragma mark - Control
77
81static bool captureEvent(Control *self, const SDL_Event *event) {
82
83 Checkbox *this = (Checkbox *) self;
84
85 View *view = (View *) self;
86
87 const View *box = (View *) this->box;
88
89 switch (event->type) {
90 case SDL_EVENT_MOUSE_BUTTON_DOWN:
91 if ($(box, didReceiveEvent, event)) {
93 return true;
94 }
95 break;
96 case SDL_EVENT_MOUSE_BUTTON_UP:
97 if ($(box, didReceiveEvent, event)) {
99 self->state &= ~ControlStateHighlighted;
100 if (this->delegate.didToggle) {
101 this->delegate.didToggle(this);
102 }
103 $(view, emitViewEvent, ViewEventChange, NULL);
104 return true;
105 }
106 break;
107 case SDL_EVENT_KEY_DOWN:
108 switch (event->key.key) {
109 case SDLK_SPACE:
110 case SDLK_KP_SPACE:
111 case SDLK_RETURN:
112 case SDLK_KP_ENTER:
114 self->state &= ~ControlStateHighlighted;
115 if (this->delegate.didToggle) {
116 this->delegate.didToggle(this);
117 }
118 $(view, emitViewEvent, ViewEventChange, NULL);
119 return true;
120 }
121 }
122
123 return super(Control, self, captureEvent, event);
124}
125
126#pragma mark - Checkbox
127
132static Checkbox *initWithFrame(Checkbox *self, const SDL_Rect *frame) {
133
134 self = (Checkbox *) super(Control, self, initWithFrame, frame);
135 if (self) {
136
137 self->box = $(alloc(Control), initWithFrame, frame);
138 assert(self->box);
139
140 self->check = $(alloc(ImageView), initWithImage, _check);
141 assert(self->check);
142
143 $((View *) self->box, addSubview, (View *) self->check);
144 $((View *) self, addSubview, (View *) self->box);
145 }
146
147 return self;
148}
149
150#pragma mark - Class lifecycle
151
155static void initialize(Class *clazz) {
156
157 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
158
159 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
160 ((ViewInterface *) clazz->interface)->init = init;
161
162 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
163
164 ((CheckboxInterface *) clazz->interface)->initWithFrame = initWithFrame;
165
166 _check = $(alloc(Image), initWithBytes, check_png, check_png_len - 1, 1.f);
167}
168
172static void destroy(Class *clazz) {
173 release(_check);
174}
175
180Class *_Checkbox(void) {
181 static Class *clazz;
182 static Once once;
183
184 do_once(&once, {
185 clazz = _initialize(&(const ClassDef) {
186 .name = "Checkbox",
187 .superclass = _Control(),
188 .instanceSize = sizeof(Checkbox),
189 .interfaceSize = sizeof(CheckboxInterface),
190 .initialize = initialize,
191 .destroy = destroy,
192 });
193 });
194
195 return clazz;
196}
197
198#undef _Class
static AtlasImage * initWithImage(AtlasImage *self, ImageAtlas *atlas, Image *image)
Definition AtlasImage.c:32
static void destroy(Class *clazz)
Definition Checkbox.c:172
static View * init(View *self)
Definition Checkbox.c:72
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Checkbox.c:56
static bool captureEvent(Control *self, const SDL_Event *event)
Definition Checkbox.c:81
static Checkbox * initWithFrame(Checkbox *self, const SDL_Rect *frame)
Definition Checkbox.c:132
static void dealloc(Object *self)
Definition Checkbox.c:39
static Image * _check
Definition Checkbox.c:32
static void initialize(Class *clazz)
Definition Checkbox.c:155
Class * _Checkbox(void)
Definition Checkbox.c:180
Checkboxes are toggle Controls that respond to click events.
Class * _Control(void)
Definition Control.c:414
@ ControlStateHighlighted
Definition Control.h:67
@ ControlStateSelected
Definition Control.h:68
static Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length, float pixelDensity)
Definition Image.c:118
static void addSubview(View *self, View *subview)
Definition PageView.c:49
@ ViewEventChange
A Control's input value has changed.
Definition Types.h:110
@ 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
static bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition View.c:726
static void emitViewEvent(View *self, ViewEvent code, ident data)
Definition View.c:794
Checkboxes are toggle Controls that respond to click events.
Definition Checkbox.h:67
ImageView * check
The check.
Definition Checkbox.h:93
Control * box
The box.
Definition Checkbox.h:88
Controls are Views which capture and respond to events.
Definition Control.h:83
unsigned int state
The bit mask of ControlState.
Definition Control.h:117
Image loading.
Definition Image.h:41
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
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
void awakeWithDictionary(View *, const Dictionary *)
Wakes this View with the specified Dictionary.
Definition Box.c:50
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:1047