ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
ProgressBar.c File Reference
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.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 View * init (View *self)
 
static void initialize (Class *clazz)
 
static ProgressBar * initWithFrame (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 32 of file ProgressBar.c.

Function Documentation

◆ _ProgressBar()

Class * _ProgressBar ( void  )

Definition at line 214 of file ProgressBar.c.

214 {
215 static Class *clazz;
216 static Once once;
217
218 do_once(&once, {
219 clazz = _initialize(&(const ClassDef) {
220 .name = "ProgressBar",
221 .superclass = _View(),
222 .instanceSize = sizeof(ProgressBar),
223 .interfaceSize = sizeof(ProgressBarInterface),
224 .initialize = initialize,
225 });
226 });
227
228 return clazz;
229}
static void initialize(Class *clazz)
Class * _View(void)
Definition View.c:2336
The ProgressBar type.
Definition ProgressBar.h:61

◆ awakeWithDictionary()

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

Definition at line 59 of file ProgressBar.c.

59 {
60
61 super(View, self, awakeWithDictionary, dictionary);
62
63 ProgressBar *this = (ProgressBar *) self;
64
65 double value = this->value;
66
67 const Inlet inlets[] = MakeInlets(
68 MakeInlet("background", InletTypeView, &this->background, NULL),
69 MakeInlet("foreground", InletTypeView, &this->foreground, NULL),
70 MakeInlet("label", InletTypeView, &this->label, NULL),
71 MakeInlet("labelFormat", InletTypeCharacters, &this->labelFormat, NULL),
72 MakeInlet("min", InletTypeDouble, &this->min, NULL),
73 MakeInlet("max", InletTypeDouble, &this->max, NULL),
74 MakeInlet("value", InletTypeDouble, &value, NULL)
75 );
76
77 $(self, bind, inlets, dictionary);
78
79 $(this, setValue, value);
80}
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ProgressBar.c:59
static void setValue(ProgressBar *self, double value)
@ InletTypeDouble
Definition View+JSON.h:67
@ InletTypeCharacters
Definition View+JSON.h:52
@ 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
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

◆ dealloc()

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

Definition at line 39 of file ProgressBar.c.

39 {
40
41 ProgressBar *this = (ProgressBar *) self;
42
43 memset(&this->delegate, 0, sizeof(this->delegate));
44
45 release(this->background);
46 release(this->foreground);
47 release(this->label);
48
49 free(this->labelFormat);
50
51 super(Object, self, dealloc);
52}
static void dealloc(Object *self)
Definition ProgressBar.c:39

◆ formatLabel()

static void formatLabel ( ProgressBar *  self)
static

Definition at line 95 of file ProgressBar.c.

95 {
96
97 char text[128];
98 snprintf(text, sizeof(text), self->labelFormat, self->value);
99
100 $(self->label, setText, text);
101}
static void setText(Text *self, const char *text)
Definition Text.c:899
double value
The progress value.
Text * label
The progress Text.
Definition ProgressBar.h:94
char * labelFormat
The Label format, e.g. "%0.0lf".
Definition ProgressBar.h:99

◆ init()

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

Definition at line 85 of file ProgressBar.c.

85 {
86 return (View *) $((ProgressBar *) self, initWithFrame, NULL);
87}
static ProgressBar * initWithFrame(ProgressBar *self, const SDL_Rect *frame)

◆ initialize()

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

Definition at line 196 of file ProgressBar.c.

196 {
197
198 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
199
200 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
201 ((ViewInterface *) clazz->interface)->init = init;
202
203 ((ProgressBarInterface *) clazz->interface)->formatLabel = formatLabel;
204 ((ProgressBarInterface *) clazz->interface)->initWithFrame = initWithFrame;
205 ((ProgressBarInterface *) clazz->interface)->progress = progress;
206 ((ProgressBarInterface *) clazz->interface)->setLabelFormat = setLabelFormat;
207 ((ProgressBarInterface *) clazz->interface)->setValue = setValue;
208}
static View * init(View *self)
Definition ProgressBar.c:85
static double progress(const ProgressBar *self)
static void setLabelFormat(ProgressBar *self, const char *labelFormat)
static void formatLabel(ProgressBar *self)
Definition ProgressBar.c:95
double progress(const ProgressBar *self)
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:1047

◆ initWithFrame()

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

Definition at line 107 of file ProgressBar.c.

107 {
108
109 self = (ProgressBar *) super(View, self, initWithFrame, frame);
110 if (self) {
111
112 self->background = $(alloc(ImageView), initWithFrame, NULL);
113 assert(self->background);
114
115 $((View *) self->background, addClassName, "background");
116 $((View *) self, addSubview, (View *) self->background);
117
118 self->foreground = $(alloc(ImageView), initWithFrame, NULL);
119 assert(self->foreground);
120
121 $((View *) self->foreground, addClassName, "foreground");
122 $((View *) self, addSubview, (View *) self->foreground);
123
124 self->label = $(alloc(Text), initWithText, NULL, NULL);
125 assert(self->label);
126
127 $((View *) self, addSubview, (View *) self->label);
128
129 $(self, setLabelFormat, "%0.0lf%%");
130
131 self->value = -1.0;
132 self->max = 100.0;
133 }
134
135 return self;
136}
static Label * initWithText(Label *self, const char *text, Font *font)
Definition Label.c:97
static void addSubview(View *self, View *subview)
Definition PageView.c:49
static void addClassName(View *self, const char *className)
Definition View.c:183
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
ImageView * background
The background ImageView.
Definition ProgressBar.h:78
Text rendered with TrueType fonts.
Definition Text.h:131

◆ progress()

static double progress ( const ProgressBar *  self)
static

Definition at line 142 of file ProgressBar.c.

142 {
143
144 if (self->max <= self->min) {
145 return 0.0;
146 }
147
148 return 100.0 * (self->value - self->min) / (self->max - self->min);
149}
double min
The progress bounds.

◆ setLabelFormat()

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

Definition at line 155 of file ProgressBar.c.

155 {
156
157 if (self->labelFormat) {
158 free(self->labelFormat);
159 }
160
161 self->labelFormat = strdup(labelFormat);
162 $(self, formatLabel);
163}

◆ setValue()

static void setValue ( ProgressBar *  self,
double  value 
)
static

Definition at line 169 of file ProgressBar.c.

169 {
170
171 value = clamp(value, self->min, self->max);
172
173 const double delta = fabs(self->value - value);
174 if (delta > __DBL_EPSILON__) {
175 self->value = value;
176
177 const SDL_Rect bounds = $((View *) self, bounds);
178 const double frac = $(self, progress) / 100.0;
179
180 self->foreground->view.frame.w = bounds.w * frac;
181 $((View *) self, setNeedsLayout);
182
183 $(self, formatLabel);
184
185 if (self->delegate.didSetValue) {
186 self->delegate.didSetValue(self, self->value);
187 }
188 }
189}
static void setNeedsLayout(View *self)
Definition View.c:1865
static SDL_Rect bounds(const View *self)
Definition View.c:552
View view
The superclass.
Definition ImageView.h:50
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
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:301