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

Go to the source code of this file.

Macros

#define _Class   _ProgressBar
 

Functions

Class * _ProgressBar (void)
 
static void awakeWithDictionary (View *self, const Dictionary *dictionary)
 
static void dealloc (Object *self)
 
static void formatLabel (ProgressBar *self)
 
static Viewinit (View *self)
 
static void initialize (Class *clazz)
 
static ProgressBarinitWithFrame (ProgressBar *self, const SDL_Rect *frame)
 
static double progress (const ProgressBar *self)
 
static void setLabelFormat (ProgressBar *self, const char *labelFormat)
 
static void setValue (ProgressBar *self, double value)
 

Macro Definition Documentation

◆ _Class

#define _Class   _ProgressBar

Definition at line 29 of file ProgressBar.c.

Function Documentation

◆ _ProgressBar()

Class * _ProgressBar ( void  )

Definition at line 204 of file ProgressBar.c.

204 {
205 static Class *clazz;
206 static Once once;
207
208 do_once(&once, {
209 clazz = _initialize(&(const ClassDef) {
210 .name = "ProgressBar",
211 .superclass = _View(),
212 .instanceSize = sizeof(ProgressBar),
213 .interfaceOffset = offsetof(ProgressBar, interface),
214 .interfaceSize = sizeof(ProgressBarInterface),
216 });
217 });
218
219 return clazz;
220}
static void initialize(Class *clazz)
Definition: ProgressBar.c:186
The ProgressBar type.
Definition: ProgressBar.h:61
Class * _View(void)
The View archetype.
Definition: View.c:1769

◆ awakeWithDictionary()

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

Definition at line 54 of file ProgressBar.c.

54 {
55
56 super(View, self, awakeWithDictionary, dictionary);
57
58 ProgressBar *this = (ProgressBar *) self;
59
60 double value = this->value;
61
62 const Inlet inlets[] = MakeInlets(
63 MakeInlet("background", InletTypeView, &this->background, NULL),
64 MakeInlet("foreground", InletTypeView, &this->foreground, NULL),
65 MakeInlet("label", InletTypeView, &this->label, NULL),
66 MakeInlet("labelFormat", InletTypeCharacters, &this->labelFormat, NULL),
67 MakeInlet("min", InletTypeDouble, &this->min, NULL),
68 MakeInlet("max", InletTypeDouble, &this->max, NULL),
69 MakeInlet("value", InletTypeDouble, &value, NULL)
70 );
71
72 $(self, bind, inlets, dictionary);
73
74 $(this, setValue, value);
75}
@ InletTypeDouble
Definition: View+JSON.h:67
@ InletTypeCharacters
Definition: View+JSON.h:52
@ 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
Inlets enable inbound data binding of View attributes through JSON.
Definition: View+JSON.h:155
void setValue(const ProgressBar *self, double value)
Sets the value, which is clamped to min and max.
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 awakeWithDictionary(View *self, const Dictionary *dictionary)
Wakes this View with the specified Dictionary.
Definition: Box.c:50

◆ dealloc()

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

Definition at line 36 of file ProgressBar.c.

36 {
37
38 ProgressBar *this = (ProgressBar *) self;
39
40 release(this->background);
41 release(this->foreground);
42 release(this->label);
43
44 free(this->labelFormat);
45
46 super(Object, self, dealloc);
47}
static void dealloc(Object *self)
Definition: ProgressBar.c:36

◆ formatLabel()

static void formatLabel ( ProgressBar self)
static

Definition at line 90 of file ProgressBar.c.

90 {
91
92 char text[128];
93 snprintf(text, sizeof(text), self->labelFormat, self->value);
94
95 $(self->label, setText, text);
96}
double value
The progress value.
Definition: ProgressBar.h:109
Text * label
The progress Text.
Definition: ProgressBar.h:94
char * labelFormat
The Label format, e.g. "%0.0lf".
Definition: ProgressBar.h:99
void setText(Text *self, const char *text)
Sets this Text's text.
Definition: Text.c:261

◆ init()

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

Definition at line 80 of file ProgressBar.c.

80 {
81 return (View *) $((ProgressBar *) self, initWithFrame, NULL);
82}
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 186 of file ProgressBar.c.

186 {
187
188 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
189
190 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
191 ((ViewInterface *) clazz->interface)->init = init;
192
193 ((ProgressBarInterface *) clazz->interface)->formatLabel = formatLabel;
194 ((ProgressBarInterface *) clazz->interface)->initWithFrame = initWithFrame;
195 ((ProgressBarInterface *) clazz->interface)->progress = progress;
196 ((ProgressBarInterface *) clazz->interface)->setLabelFormat = setLabelFormat;
197 ((ProgressBarInterface *) clazz->interface)->setValue = setValue;
198}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
double progress(const ProgressBar *self)
Definition: ProgressBar.c:137
void setLabelFormat(ProgressBar *self, const char *labelFormat)
Changes this ProgressBar's label format and calls appropriate update functions.
Definition: ProgressBar.c:145
void formatLabel(ProgressBar *self)
Forces an update on this ProgressBar's label.
Definition: ProgressBar.c:90

◆ initWithFrame()

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

Definition at line 102 of file ProgressBar.c.

102 {
103
104 self = (ProgressBar *) super(View, self, initWithFrame, frame);
105 if (self) {
106
107 self->background = $(alloc(ImageView), initWithFrame, NULL);
108 assert(self->background);
109
110 $((View *) self->background, addClassName, "background");
111 $((View *) self, addSubview, (View *) self->background);
112
113 self->foreground = $(alloc(ImageView), initWithFrame, NULL);
114 assert(self->foreground);
115
116 $((View *) self->foreground, addClassName, "foreground");
117 $((View *) self, addSubview, (View *) self->foreground);
118
119 self->label = $(alloc(Text), initWithText, NULL, NULL);
120 assert(self->label);
121
122 $((View *) self, addSubview, (View *) self->label);
123
124 $(self, setLabelFormat, "%0.0lf%%");
125
126 self->value = -1.0;
127 self->max = 100.0;
128 }
129
130 return self;
131}
ImageViews render an Image in the context of a View hierarchy.
Definition: ImageView.h:43
Label * initWithText(Label *self, const char *text, Font *font)
Initializes this Label with the given text and Font.
Definition: Label.c:96
ImageView * background
The background ImageView.
Definition: ProgressBar.h:78
Text rendered with TrueType fonts.
Definition: Text.h:41
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

◆ progress()

static double progress ( const ProgressBar self)
static

Definition at line 137 of file ProgressBar.c.

137 {
138 return 100.0 * self->value / (self->max - self->min);
139}
double min
The progress bounds.
Definition: ProgressBar.h:104
double max
Definition: ProgressBar.h:104

◆ setLabelFormat()

static void setLabelFormat ( ProgressBar self,
const char *  labelFormat 
)
static

Definition at line 145 of file ProgressBar.c.

145 {
146
147 if (self->labelFormat) {
148 free(self->labelFormat);
149 }
150
151 self->labelFormat = strdup(labelFormat);
152 $(self, formatLabel);
153}

◆ setValue()

static void setValue ( ProgressBar self,
double  value 
)
static

Definition at line 159 of file ProgressBar.c.

159 {
160
161 value = clamp(value, self->min, self->max);
162
163 const double delta = fabs(self->value - value);
164 if (delta > __DBL_EPSILON__) {
165 self->value = value;
166
167 const SDL_Rect bounds = $((View *) self, bounds);
168 const double frac = self->value / (self->max - self->min);
169
170 self->foreground->view.frame.w = bounds.w * frac;
171 self->view.needsLayout = true;
172
173 $(self, formatLabel);
174
175 if (self->delegate.didSetValue) {
176 self->delegate.didSetValue(self, self->value);
177 }
178 }
179}
View view
The superclass.
Definition: ImageView.h:48
void(* didSetValue)(ProgressBar *progressBar, double value)
Delegate callback for ProgressBar value modification.
Definition: ProgressBar.h:54
ImageView * foreground
The foreground ImageView.
Definition: ProgressBar.h:89
ProgressBarDelegate delegate
The delegate.
Definition: ProgressBar.h:83
View view
The superclass.
Definition: ProgressBar.h:66
SDL_Rect bounds(const View *self)
Definition: View.c:394
_Bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition: View.h:221
SDL_Rect frame
The frame, relative to the superview.
Definition: View.h:190