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 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 32 of file ProgressBar.c.

Function Documentation

◆ _ProgressBar()

Class * _ProgressBar ( void  )

Definition at line 209 of file ProgressBar.c.

209 {
210 static Class *clazz;
211 static Once once;
212
213 do_once(&once, {
214 clazz = _initialize(&(const ClassDef) {
215 .name = "ProgressBar",
216 .superclass = _View(),
217 .instanceSize = sizeof(ProgressBar),
218 .interfaceOffset = offsetof(ProgressBar, interface),
219 .interfaceSize = sizeof(ProgressBarInterface),
221 });
222 });
223
224 return clazz;
225}
static void initialize(Class *clazz)
Class * _View(void)
Definition View.c:2067
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: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
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:134

◆ 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:532
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 191 of file ProgressBar.c.

191 {
192
193 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
194
195 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
196 ((ViewInterface *) clazz->interface)->init = init;
197
198 ((ProgressBarInterface *) clazz->interface)->formatLabel = formatLabel;
199 ((ProgressBarInterface *) clazz->interface)->initWithFrame = initWithFrame;
200 ((ProgressBarInterface *) clazz->interface)->progress = progress;
201 ((ProgressBarInterface *) clazz->interface)->setLabelFormat = setLabelFormat;
202 ((ProgressBarInterface *) clazz->interface)->setValue = setValue;
203}
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:989

◆ 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:159
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:69

◆ progress()

static double progress ( const ProgressBar self)
static

Definition at line 142 of file ProgressBar.c.

142 {
143 return 100.0 * self->value / (self->max - self->min);
144}
double min
The progress bounds.

◆ setLabelFormat()

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

Definition at line 150 of file ProgressBar.c.

150 {
151
152 if (self->labelFormat) {
153 free(self->labelFormat);
154 }
155
156 self->labelFormat = strdup(labelFormat);
157 $(self, formatLabel);
158}

◆ setValue()

static void setValue ( ProgressBar self,
double  value 
)
static

Definition at line 164 of file ProgressBar.c.

164 {
165
166 value = clamp(value, self->min, self->max);
167
168 const double delta = fabs(self->value - value);
169 if (delta > __DBL_EPSILON__) {
170 self->value = value;
171
172 const SDL_Rect bounds = $((View *) self, bounds);
173 const double frac = self->value / (self->max - self->min);
174
175 self->foreground->view.frame.w = bounds.w * frac;
176 self->view.needsLayout = true;
177
178 $(self, formatLabel);
179
180 if (self->delegate.didSetValue) {
181 self->delegate.didSetValue(self, self->value);
182 }
183 }
184}
static SDL_Rect bounds(const View *self)
Definition View.c:492
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
View view
The superclass.
Definition ProgressBar.h:66
bool needsLayout
If true, this View will layout its subviews before it is drawn.
Definition View.h:222
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:191