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

Go to the source code of this file.

Macros

#define _Class   _Panel
 

Functions

Class * _Panel (void)
 
static void applyStyle (View *self, const Style *style)
 
static void awakeWithDictionary (View *self, const Dictionary *dictionary)
 
static _Bool captureEvent (Control *self, const SDL_Event *event)
 
static SDL_Size contentSize (const Panel *self)
 
static void dealloc (Object *self)
 
static void destroy (Class *clazz)
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static PanelinitWithFrame (Panel *self, const SDL_Rect *frame)
 
static void layoutSubviews (View *self)
 

Variables

static Image_resize
 

Macro Definition Documentation

◆ _Class

#define _Class   _Panel

Definition at line 32 of file Panel.c.

Function Documentation

◆ _Panel()

Class * _Panel ( void  )

Definition at line 268 of file Panel.c.

268 {
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}
static void destroy(Class *clazz)
Definition: Panel.c:260
static void initialize(Class *clazz)
Definition: Panel.c:240
Class * _Control(void)
The Control archetype.
Definition: Control.c:379
Draggable and resizable container Views.
Definition: Panel.h:47

◆ applyStyle()

static void applyStyle ( View self,
const Style style 
)
static
See also
View::applyStyle(View *, const Style *)

Definition at line 55 of file Panel.c.

55 {
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}
@ InletTypeBool
Definition: View+JSON.h:46
#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
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.
void applyStyle(View *self, const Style *style)
Applies the given Style to this View.

◆ awakeWithDictionary()

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

Definition at line 72 of file Panel.c.

72 {
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}
@ InletTypeView
Definition: View+JSON.h:139
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50

◆ captureEvent()

static _Bool captureEvent ( Control self,
const SDL_Event *  event 
)
static
See also
Control::captureEvent(Control *, const SDL_Event *)

Definition at line 120 of file Panel.c.

120 {
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}
@ ControlStateHighlighted
Definition: Control.h:68
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
unsigned int state
The bit mask of ControlState.
Definition: Control.h:110
View view
The superclass.
Definition: Control.h:88
SDL_Size size(const Image *self)
Definition: Image.c:181
The SDL_Size type.
Definition: Types.h:62
int w
Definition: Types.h:63
int h
Definition: Types.h:63
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
SDL_Rect frame
The frame, relative to the superview.
Definition: View.h:190

◆ contentSize()

static SDL_Size contentSize ( const Panel self)
static

Definition at line 166 of file Panel.c.

166 {
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}
StackView * accessoryView
The optional accessories container.
Definition: Panel.h:64
StackView * contentView
The internal container.
Definition: Panel.h:69
int spacing
The subview spacing.
Definition: StackView.h:94
_Bool hidden
If true, this View is not drawn.
Definition: View.h:195
SDL_Size sizeThatContains(const View *self)
Definition: View.c:1423

◆ dealloc()

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

Definition at line 39 of file Panel.c.

39 {
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}
static void dealloc(Object *self)
Definition: Panel.c:39

◆ destroy()

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

Definition at line 260 of file Panel.c.

260 {
261 release(_resize);
262}
static Image * _resize
Definition: Panel.c:30

◆ init()

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

Definition at line 90 of file Panel.c.

90 {
91 return (View *) $((Panel *) self, initWithFrame, NULL);
92}
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 240 of file Panel.c.

240 {
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}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
Image loading.
Definition: Image.h:38
Image * initWithBytes(Image *self, const uint8_t *bytes, size_t length)
Initializes this Image with the specified bytes.
Definition: Image.c:88
SDL_Size contentSize(const Panel *self)
Definition: Panel.c:166
layoutSubviews(View *self)
Performs layout for this View's immediate subviews.
Definition: Box.c:74

◆ initWithFrame()

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

Definition at line 185 of file Panel.c.

185 {
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}
#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
@ ViewAutoresizingWidth
Definition: View.h:87
@ ViewAlignmentInternal
Definition: View.h:77
Button * initWithImage(Button *self, Image *image)
Initializes this Button with the sopecified Image.
Definition: Button.c:122
ImageViews render an Image in the context of a View hierarchy.
Definition: ImageView.h:43
View view
The superclass.
Definition: ImageView.h:48
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
StackViews are containers that manage the arrangement of their subviews.
Definition: StackView.h:68
View view
The superclass.
Definition: StackView.h:73
ViewAlignment alignment
The alignment.
Definition: View.h:149
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
int autoresizingMask
The ViewAutoresizing bitmask.
Definition: View.h:154

◆ layoutSubviews()

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

Definition at line 97 of file Panel.c.

97 {
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}

Variable Documentation

◆ _resize

Image* _resize
static

Definition at line 30 of file Panel.c.