ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
ProgressBar.c
Go to the documentation of this file.
1/*
2 * ObjectivelyMVC: Object oriented MVC framework for SDL3 and 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#include <math.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29
30#include "ProgressBar.h"
31
32#define _Class _ProgressBar
33
34#pragma mark - Object
35
39static void dealloc(Object *self) {
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}
53
54#pragma mark - View
55
59static void awakeWithDictionary(View *self, const Dictionary *dictionary) {
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}
81
85static View *init(View *self) {
86 return (View *) $((ProgressBar *) self, initWithFrame, NULL);
87}
88
95static void layoutSubviews(View *self) {
96
97 ProgressBar *this = (ProgressBar *) self;
98
99 const SDL_Rect bounds = $(self, bounds);
100 const double frac = clamp($(this, progress) / 100.0, 0.0, 1.0);
101
102 this->foreground->view.frame.w = bounds.w * frac;
103
104 super(View, self, layoutSubviews);
105}
106
107#pragma mark - ProgressBar
108
113static void formatLabel(ProgressBar *self) {
114
115 char text[128];
116 snprintf(text, sizeof(text), self->labelFormat, self->value);
117
118 $(self->label, setText, text);
119}
120
125static ProgressBar *initWithFrame(ProgressBar *self, const SDL_Rect *frame) {
126
127 self = (ProgressBar *) super(View, self, initWithFrame, frame);
128 if (self) {
129
130 self->background = $(alloc(ImageView), initWithFrame, NULL);
131 assert(self->background);
132
133 $((View *) self->background, addClassName, "background");
134 $((View *) self, addSubview, (View *) self->background);
135
136 self->foreground = $(alloc(ImageView), initWithFrame, NULL);
137 assert(self->foreground);
138
139 $((View *) self->foreground, addClassName, "foreground");
140 $((View *) self, addSubview, (View *) self->foreground);
141
142 self->label = $(alloc(Text), initWithText, NULL, NULL);
143 assert(self->label);
144
145 $((View *) self, addSubview, (View *) self->label);
146
147 $(self, setLabelFormat, "%0.0lf%%");
148
149 self->value = -1.0;
150 self->max = 100.0;
151 }
152
153 return self;
154}
155
160static double progress(const ProgressBar *self) {
161
162 if (self->max <= self->min) {
163 return 0.0;
164 }
165
166 return 100.0 * (self->value - self->min) / (self->max - self->min);
167}
168
173static void setLabelFormat(ProgressBar *self, const char *labelFormat) {
174
175 if (self->labelFormat) {
176 free(self->labelFormat);
177 }
178
179 self->labelFormat = strdup(labelFormat);
180 $(self, formatLabel);
181}
182
187static void setValue(ProgressBar *self, double value) {
188
189 value = clamp(value, self->min, self->max);
190
191 const double delta = fabs(self->value - value);
192 if (delta > __DBL_EPSILON__) {
193 self->value = value;
194
195 const SDL_Rect bounds = $((View *) self, bounds);
196 const double frac = $(self, progress) / 100.0;
197
198 self->foreground->view.frame.w = bounds.w * frac;
199 $((View *) self, setNeedsLayout);
200
201 $(self, formatLabel);
202
203 if (self->delegate.didSetValue) {
204 self->delegate.didSetValue(self, self->value);
205 }
206 }
207}
208
209#pragma mark - Class lifecycle
210
214static void initialize(Class *clazz) {
215
216 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
217
218 ((ViewInterface *) clazz->interface)->layoutSubviews = layoutSubviews;
219
220 ((ViewInterface *) clazz->interface)->awakeWithDictionary = awakeWithDictionary;
221 ((ViewInterface *) clazz->interface)->init = init;
222
223 ((ProgressBarInterface *) clazz->interface)->formatLabel = formatLabel;
224 ((ProgressBarInterface *) clazz->interface)->initWithFrame = initWithFrame;
225 ((ProgressBarInterface *) clazz->interface)->progress = progress;
226 ((ProgressBarInterface *) clazz->interface)->setLabelFormat = setLabelFormat;
227 ((ProgressBarInterface *) clazz->interface)->setValue = setValue;
228}
229
234Class *_ProgressBar(void) {
235 static Class *clazz;
236 static Once once;
237
238 do_once(&once, {
239 clazz = _initialize(&(const ClassDef) {
240 .name = "ProgressBar",
241 .superclass = _View(),
242 .instanceSize = sizeof(ProgressBar),
243 .interfaceSize = sizeof(ProgressBarInterface),
244 .initialize = initialize,
245 });
246 });
247
248 return clazz;
249}
250
251#undef _Class
static void initialize(Class *clazz)
Definition AtlasImage.c:74
static View * init(View *self)
Definition Box.c:67
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition Box.c:50
static void dealloc(Object *self)
Definition Box.c:35
static Box * initWithFrame(Box *self, const SDL_Rect *frame)
Definition Box.c:92
static void layoutSubviews(View *self)
Definition Box.c:74
static Label * initWithText(Label *self, const char *text, Font *font)
Definition Label.c:97
static View * init(View *self)
Definition ProgressBar.c:85
static void awakeWithDictionary(View *self, const Dictionary *dictionary)
Definition ProgressBar.c:59
static void setValue(ProgressBar *self, double value)
Class * _ProgressBar(void)
static void dealloc(Object *self)
Definition ProgressBar.c:39
static double progress(const ProgressBar *self)
static void setLabelFormat(ProgressBar *self, const char *labelFormat)
static void initialize(Class *clazz)
static void layoutSubviews(View *self)
Definition ProgressBar.c:95
static ProgressBar * initWithFrame(ProgressBar *self, const SDL_Rect *frame)
static void formatLabel(ProgressBar *self)
A Progress bar.
static void setText(Text *self, const char *text)
Definition Text.c:904
@ 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
Class * _View(void)
Definition View.c:2338
static void setNeedsLayout(View *self)
Definition View.c:1867
static SDL_Rect bounds(const View *self)
Definition View.c:552
ImageViews render an Image in the context of a View hierarchy.
Definition ImageView.h:45
Inlets enable inbound data binding of View attributes through JSON.
Definition View+JSON.h:149
The ProgressBar type.
Definition ProgressBar.h:61
double min
The progress bounds.
double value
The progress value.
Text * label
The progress Text.
Definition ProgressBar.h:94
ImageView * background
The background ImageView.
Definition ProgressBar.h:78
ProgressBar * initWithFrame(ProgressBar *self, const SDL_Rect *frame)
void setLabelFormat(ProgressBar *self, const char *labelFormat)
Changes this ProgressBar's label format and calls appropriate update functions.
void formatLabel(ProgressBar *self)
Forces an update on this ProgressBar's label.
char * labelFormat
The Label format, e.g. "%0.0lf".
Definition ProgressBar.h:99
Text rendered with TrueType fonts.
Definition Text.h:131
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
void addSubview(View *self, View *subview)
Adds a subview to this view, to be drawn above its siblings.
Definition PageView.c:49
void addClassName(View *self, const char *className)
Adds the given class name to this View.
Definition View.c:183
SDL_Rect frame
The frame, relative to the superview.
Definition View.h:301