ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Panel.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for OpenGL, SDL2 and GNU 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 "Panel.h"
27
28#include "resize.png.h"
29
30static Image *_resize;
31
32#define _Class _Panel
33
34#pragma mark - Object
35
39static void dealloc(Object *self) {
40
41 Panel *this = (Panel *) self;
42
43 release(this->accessoryView);
44 release(this->contentView);
45 release(this->resizeHandle);
46
47 super(Object, self, dealloc);
48}
49
50#pragma mark - View
51
55static void applyStyle(View *self, const Style *style) {
56
57 super(View, self, applyStyle, style);
58
59 Panel *this = (Panel *) self;
60
61 const Inlet inlets[] = MakeInlets(
62 MakeInlet("draggable", InletTypeBool, &this->isDraggable, NULL),
63 MakeInlet("resizable", InletTypeBool, &this->isResizable, NULL)
64 );
65
66 $(self, bind, inlets, (Dictionary *) style->attributes);
67}
68
72static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
73
74 super(View, self, awakeWithDictionary, dictionary);
75
76 Panel *this = (Panel *) self;
77
78 const Inlet inlets[] = MakeInlets(
79 MakeInlet("accessoryView", InletTypeView, &this->accessoryView, NULL),
80 MakeInlet("contentView", InletTypeView, &this->contentView, NULL),
81 MakeInlet("stackView", InletTypeView, &this->stackView, NULL)
82 );
83
84 $(self, bind, inlets, dictionary);
85}
86
90static View *init(View *self) {
91 return (View *) $((Panel *) self, initWithFrame, NULL);
92}
93
97static void layoutSubviews(View *self) {
98
99 const Panel *this = (Panel *) self;
100
101 const SDL_Size size = $(this, contentSize);
102
103 $((View *) this->contentView, resize, &size);
104
105 super(View, self, layoutSubviews);
106
107 View *resizeHandle = (View *) this->resizeHandle;
108
109 resizeHandle->frame.x = self->frame.w - resizeHandle->frame.w;
110 resizeHandle->frame.y = self->frame.h - resizeHandle->frame.h;
111
112 resizeHandle->hidden = !this->isResizable;
113}
114
115#pragma mark - Control
116
120static _Bool captureEvent(Control *self, const SDL_Event *event) {
121
122 Panel *this = (Panel *) self;
123
124 if (event->type == SDL_MOUSEMOTION && (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON_LMASK)) {
125
126 if ((self->state & ControlStateHighlighted) == 0) {
127 if ($((View *) this->resizeHandle, didReceiveEvent, event)) {
129 this->isResizing = true;
130 } else if (this->isDraggable) {
132 this->isDragging = true;
133 }
134 }
135
136 if (this->isResizing) {
137 SDL_Size size = $((View *) self, size);
138
139 size.w = clamp(size.w + event->motion.xrel, this->minSize.w, this->maxSize.w);
140 size.h = clamp(size.h + event->motion.yrel, this->minSize.h, this->maxSize.h);
141
142 $((View *) self, resize, &size);
143 } else if (this->isDragging) {
144 self->view.frame.x += event->motion.xrel;
145 self->view.frame.y += event->motion.yrel;
146 }
147
148 return true;
149 }
150
151 if (event->type == SDL_MOUSEBUTTONUP && event->button.button == SDL_BUTTON_LEFT) {
152 self->state &= ~ControlStateHighlighted;
153 this->isResizing = this->isDragging = false;
154 return true;
155 }
156
157 return super(Control, self, captureEvent, event);
158}
159
160#pragma mark - Panel
161
166static SDL_Size contentSize(const Panel *self) {
167
168 const View *accessoryView = (View *) self->accessoryView;
169 const View *contentView = (View *) self->contentView;
170
171 SDL_Size size = $(contentView, sizeThatContains);
172
173 if (accessoryView->hidden == false) {
174 const SDL_Size accessorySize = $(accessoryView, sizeThatContains);
175 size.h -= accessorySize.h + self->stackView->spacing;
176 }
177
178 return size;
179}
180
185static Panel *initWithFrame(Panel *self, const SDL_Rect *frame) {
186
187 self = (Panel *) super(Control, self, initWithFrame, frame);
188 if (self) {
189 View *this = (View *) self;
190
191 self->isDraggable = true;
192 self->isResizable = true;
193
194 self->maxSize = MakeSize(INT32_MAX, INT32_MAX);
195
196 self->stackView = $(alloc(StackView), initWithFrame, NULL);
197 assert(self->stackView);
198
199 $(this, addSubview, (View *) self->stackView);
200
201 self->contentView = $(alloc(StackView), initWithFrame, NULL);
202 assert(self->contentView);
203
204 $((View *) self->contentView, addClassName, "contentView");
205 $((View *) self->contentView, addClassName, "container");
206
208
209 $((View *) self->stackView, addSubview, (View *) self->contentView);
210
211 self->accessoryView = $(alloc(StackView), initWithFrame, NULL);
212 assert(self->accessoryView);
213
214 $((View *) self->accessoryView, addClassName, "accessoryView");
215 $((View *) self->accessoryView, addClassName, "container");
216
217 self->accessoryView->view.hidden = true;
218
219 $((View *) self->stackView, addSubview, (View *) self->accessoryView);
220
221 self->resizeHandle = $(alloc(ImageView), initWithImage, _resize);
222 assert(self->resizeHandle);
223
224 self->resizeHandle->view.alignment = ViewAlignmentInternal;
225
226 self->resizeHandle->view.frame.w = DEFAULT_PANEL_RESIZE_HANDLE_SIZE;
227 self->resizeHandle->view.frame.h = DEFAULT_PANEL_RESIZE_HANDLE_SIZE;
228
229 $((View *) self, addSubview, (View *) self->resizeHandle);
230 }
231
232 return self;
233}
234
235#pragma mark - Class lifecycle
236
240static void initialize(Class *clazz) {
241
242 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
243
244 ((ViewInterface *) clazz->interface)->applyStyle = applyStyle;
245 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
246 ((ViewInterface *) clazz->interface)->init = init;
247 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
248
249 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
250
251 ((PanelInterface *) clazz->interface)->contentSize = contentSize;
252 ((PanelInterface *) clazz->interface)->initWithFrame = initWithFrame;
253
254 _resize = $(alloc(Image), initWithBytes, resize_png, resize_png_len);
255}
256
260static void destroy(Class *clazz) {
261 release(_resize);
262}
263
268Class *_Panel(void) {
269 static Class *clazz;
270 static Once once;
271
272 do_once(&once, {
273 clazz = _initialize(&(const ClassDef) {
274 .name = "Panel",
275 .superclass = _Control(),
276 .instanceSize = sizeof(Panel),
277 .interfaceOffset = offsetof(Panel, interface),
278 .interfaceSize = sizeof(PanelInterface),
280 .destroy = destroy,
281 });
282 });
283
284 return clazz;
285}
286
287#undef _Class
288
@ ControlStateHighlighted
Definition: Control.h:68
static void destroy(Class *clazz)
Definition: Panel.c:260
static Image * _resize
Definition: Panel.c:30
static void dealloc(Object *self)
Definition: Panel.c:39
static void initialize(Class *clazz)
Definition: Panel.c:240
Draggable containers.
#define DEFAULT_PANEL_RESIZE_HANDLE_SIZE
Definition: Panel.h:35
#define MakeSize(w, h)
Creates an SDL_Size with the given dimensions.
Definition: Types.h:79
@ InletTypeBool
Definition: View+JSON.h:46
@ 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
@ ViewAutoresizingWidth
Definition: View.h:87
@ ViewAlignmentInternal
Definition: View.h:77
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
Button * initWithImage(Button *self, Image *image)
Initializes this Button with the sopecified Image.
Definition: Button.c:122
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
Controls are Views which capture events and dispatch Actions.
Definition: Control.h:83
_Bool captureEvent(Control *self, const SDL_Event *event)
Captures a given event, potentially altering the state of this Control.
Definition: Button.c:76
Class * _Control(void)
The Control archetype.
Definition: Control.c:379
unsigned int state
The bit mask of ControlState.
Definition: Control.h:110
View view
The superclass.
Definition: Control.h:88
Image loading.
Definition: Image.h:38
SDL_Size size(const Image *self)
Definition: Image.c:181
Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length)
Initializes this Image with the specified bytes.
Definition: Image.c:88
ImageViews render an Image in the context of a View hierarchy.
Definition: ImageView.h:43
View view
The superclass.
Definition: ImageView.h:48
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
Draggable and resizable container Views.
Definition: Panel.h:47
SDL_Size maxSize
The maximum size to which this Panel's frame can be resized.
Definition: Panel.h:99
_Bool isResizable
If true, this Panel may be resized by the user.
Definition: Panel.h:84
_Bool isDraggable
If true, this Panel may be repositioned by the user.
Definition: Panel.h:74
SDL_Size contentSize(const Panel *self)
Definition: Panel.c:166
StackView * accessoryView
The optional accessories container.
Definition: Panel.h:64
Class * _Panel(void)
The Panel archetype.
Definition: Panel.c:268
StackView * contentView
The internal container.
Definition: Panel.h:69
The SDL_Size type.
Definition: Types.h:62
int w
Definition: Types.h:63
int h
Definition: Types.h:63
StackViews are containers that manage the arrangement of their subviews.
Definition: StackView.h:68
View view
The superclass.
Definition: StackView.h:73
int spacing
The subview spacing.
Definition: StackView.h:94
The Style type.
Definition: Style.h:43
Dictionary * attributes
Definition: Style.h:59
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.
ViewAlignment alignment
The alignment.
Definition: View.h:149
_Bool hidden
If true, this View is not drawn.
Definition: View.h:195
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
void applyStyle(View *self, const Style *style)
Applies the given Style to this View.
void resize(View *self, const SDL_Size *size)
Resizes this View to the specified size.
Definition: View.c:1326
_Bool didReceiveEvent(const View *self, const SDL_Event *event)
Definition: View.c:546
int autoresizingMask
The ViewAutoresizing bitmask.
Definition: View.h:154
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50
layoutSubviews(View *self)
Performs layout for this View's immediate subviews.
Definition: Box.c:74
SDL_Size sizeThatContains(const View *self)
Definition: View.c:1423
SDL_Rect frame
The frame, relative to the superview.
Definition: View.h:190