ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Button.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 "Button.h"
27#include "SoundStage.h"
28
29#define _Class _Button
30
31#pragma mark - Object
32
36static void dealloc(Object *self) {
37
38 Button *this = (Button *) self;
39
40 release(this->image);
41 release(this->title);
42
43 super(Object, self, dealloc);
44}
45
46#pragma mark - View
47
51static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
52
53 super(View, self, awakeWithDictionary, dictionary);
54
55 Button *this = (Button *) self;
56
57 const Inlet inlets[] = MakeInlets(
58 MakeInlet("title", InletTypeView, &this->title, NULL)
59 );
60
61 $(self, bind, inlets, dictionary);
62}
63
67static View *init(View *self) {
68 return (View *) $((Button *) self, initWithFrame, NULL);
69}
70
71#pragma mark - Control
72
76static _Bool captureEvent(Control *self, const SDL_Event *event) {
77
78 if (event->type == SDL_MOUSEBUTTONDOWN) {
81 return true;
82 }
83
84 if (event->type == SDL_MOUSEBUTTONUP) {
85 self->state &= ~ControlStateHighlighted;
87 return true;
88 }
89
90 return super(Control, self, captureEvent, event);
91}
92
93#pragma mark - Button
94
99static Button *initWithFrame(Button *self, const SDL_Rect *frame) {
100
101 self = (Button *) super(Control, self, initWithFrame, frame);
102 if (self) {
103
104 self->image = $(alloc(ImageView), initWithFrame, frame);
105 assert(self->image);
106
107 $((View *) self, addSubview, (View *) self->image);
108
109 self->title = $(alloc(Text), initWithText, NULL, NULL);
110 assert(self->title);
111
112 $((View *) self, addSubview, (View *) self->title);
113 }
114
115 return self;
116}
117
122static Button *initWithImage(Button *self, Image *image) {
123
124 self = $(self, initWithFrame, NULL);
125 if (self) {
126 $(self->image, setImage, image);
127 }
128 return self;
129}
130
135static Button *initWithTitle(Button *self, const char *title) {
136
137 self = $(self, initWithFrame, NULL);
138 if (self) {
139 $(self->title, setText, title);
140 }
141 return self;
142}
143
144#pragma mark - Class lifecycle
145
149static void initialize(Class *clazz) {
150
151 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
152
153 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
154 ((ViewInterface *) clazz->interface)->init = init;
155
156 ((ControlInterface *) clazz->interface)->captureEvent = captureEvent;
157
158 ((ButtonInterface *) clazz->interface)->initWithFrame = initWithFrame;
159 ((ButtonInterface *) clazz->interface)->initWithImage = initWithImage;
160 ((ButtonInterface *) clazz->interface)->initWithTitle = initWithTitle;
161}
162
167Class *_Button(void) {
168 static Class *clazz;
169 static Once once;
170
171 do_once(&once, {
172 clazz = _initialize(&(const ClassDef) {
173 .name = "Button",
174 .superclass = _Control(),
175 .instanceSize = sizeof(Button),
176 .interfaceOffset = offsetof(Button, interface),
177 .interfaceSize = sizeof(ButtonInterface),
179 });
180 });
181
182 return clazz;
183}
184
185#undef _Class
static void dealloc(Object *self)
Definition: Button.c:36
static void initialize(Class *clazz)
Definition: Button.c:149
Buttons are Controls that respond to click events.
@ ControlStateHighlighted
Definition: Control.h:68
Sound * _clack
Definition: SoundStage.c:86
Sound * _click
Definition: SoundStage.c:85
OBJECTIVELYMVC_EXPORT void MVC_PlaySound(const Sound *sound)
Plays the specified Sound through the current SoundStage (if any).
Definition: SoundStage.c:141
Sound playback.
@ 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
Box * initWithFrame(Box *self, const SDL_Rect *frame)
Initializes this Box with the given frame.
Definition: Box.c:92
Buttons are Controls that respond to click events.
Definition: Button.h:42
Text * title
The title.
Definition: Button.h:63
Button * initWithImage(Button *self, Image *image)
Initializes this Button with the sopecified Image.
Definition: Button.c:122
Button * initWithTitle(Button *self, const char *title)
Initializes this Button with the specified title.
Definition: Button.c:135
ImageView * image
The image.
Definition: Button.h:58
Class * _Button(void)
The Button archetype.
Definition: Button.c:167
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
Image loading.
Definition: Image.h:38
ImageViews render an Image in the context of a View hierarchy.
Definition: ImageView.h:43
void setImage(ImageView *self, Image *image)
Sets the Image for this ImageView.
Definition: ImageView.c:171
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
Text rendered with TrueType fonts.
Definition: Text.h:41
void setText(Text *self, const char *text)
Sets this Text's text.
Definition: Text.c:261
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 addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition: PageView.c:35
void awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50