121 lines
3.5 KiB
C
121 lines
3.5 KiB
C
#include <assert.h>
|
|
#include <limits.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "dims.h"
|
|
#include "util.h"
|
|
|
|
struct ct_dims *new_dims(int x, int y, int width, int height) {
|
|
struct ct_dims *dims;
|
|
|
|
dims = (struct ct_dims *)malloc(sizeof(struct ct_dims));
|
|
*dims = (struct ct_dims){
|
|
.x = x,
|
|
.y = y,
|
|
.width = width,
|
|
.height = height,
|
|
};
|
|
return dims;
|
|
}
|
|
|
|
struct ct_dims *dup_dims(const struct ct_dims *const dims) {
|
|
struct ct_dims *dup;
|
|
dup = (struct ct_dims *)malloc(sizeof(struct ct_dims));
|
|
memcpy(dup, dims, sizeof(struct ct_dims));
|
|
|
|
return dup;
|
|
}
|
|
|
|
static struct ct_bounds *__bounds(const enum ct_boundtype type, const int wmin,
|
|
const int wmax, const int hmin,
|
|
const int hmax) {
|
|
struct ct_bounds *bounds;
|
|
|
|
bounds = (struct ct_bounds *)malloc(sizeof(struct ct_bounds));
|
|
*bounds = (struct ct_bounds){
|
|
.type = type,
|
|
.wmin = wmin,
|
|
.wmax = wmax,
|
|
.hmin = hmin,
|
|
.hmax = hmax,
|
|
};
|
|
return bounds;
|
|
}
|
|
|
|
static inline void __clamp(int *const val, const int min, const int max,
|
|
const int do_ceiling) {
|
|
if (*val == __BOUND_UNLIMITED)
|
|
*val = do_ceiling ? max : min;
|
|
else
|
|
*val = clampi(*val, min, max);
|
|
}
|
|
#define CLAMP_ABS(val, is_max) \
|
|
(__clamp(&val, __BOUND_ABS_MIN, __BOUND_ABS_MAX, is_max))
|
|
#define CLAMP_REL(val, is_max) \
|
|
(__clamp(&val, __BOUND_REL_MIN, __BOUND_REL_MAX, is_max))
|
|
|
|
struct ct_bounds *bounds_none(void) {
|
|
return __bounds(BOUND_NONE, __BOUND_ABS_MIN, __BOUND_ABS_MAX, __BOUND_ABS_MIN,
|
|
__BOUND_ABS_MAX);
|
|
}
|
|
|
|
struct ct_bounds *bounds_absolute(int wmin, int wmax,
|
|
int hmin, int hmax) {
|
|
CLAMP_ABS(wmin, false);
|
|
CLAMP_ABS(wmax, true);
|
|
CLAMP_ABS(hmin, false);
|
|
CLAMP_ABS(hmax, true);
|
|
return __bounds(BOUND_ABSOLUTE, wmin, wmax, hmin, hmax);
|
|
}
|
|
|
|
struct ct_bounds *bounds_relative(int wmin, int wmax,
|
|
int hmin, int hmax) {
|
|
CLAMP_REL(wmin, false);
|
|
CLAMP_REL(wmax, true);
|
|
CLAMP_REL(hmin, false);
|
|
CLAMP_REL(hmax, true);
|
|
return __bounds(BOUND_RELATIVE, wmin, wmax, hmin, hmax);
|
|
}
|
|
|
|
struct ct_bounds *dup_bounds(const struct ct_bounds *const bounds) {
|
|
struct ct_bounds *dup;
|
|
dup = (struct ct_bounds *)malloc(sizeof(struct ct_bounds));
|
|
memcpy(dup, bounds, sizeof(struct ct_bounds));
|
|
|
|
return dup;
|
|
}
|
|
|
|
/* Calculate the dimensions for nodes resulting from a bifurcation.
|
|
* Returns 0 on success, and 1 on failure if any width/height are 0 characters.
|
|
* WARNING: This function does not guarantee the x,y positions returned
|
|
* WARNING: are valid screen coordinates.
|
|
*/
|
|
// int bifurcate_dims(const struct ct_dims *const parent_dims,
|
|
// const enum ct_axis axis, const float ratio,
|
|
// struct ct_dims **const dims0, struct ct_dims **const
|
|
// dims1) {
|
|
// assert(0 < ratio && ratio < 1);
|
|
// struct ct_dims *_dims0, *_dims1;
|
|
|
|
// _dims0 = dup_dims(parent_dims);
|
|
// _dims1 = dup_dims(parent_dims);
|
|
|
|
// if (axis == AXIS_X) {
|
|
// _dims0->width *= ratio;
|
|
// _dims1->width -= _dims0->width;
|
|
// _dims1->x += _dims0->width;
|
|
// } else {
|
|
// _dims0->height *= ratio;
|
|
// _dims1->height -= _dims0->height;
|
|
// _dims1->y += _dims0->height;
|
|
// }
|
|
|
|
// if (!_dims0->width || !_dims0->height || !_dims1->width || !_dims1->height)
|
|
// return 1;
|
|
|
|
// // propagate bifurcated dimensions
|
|
// *dims0 = _dims0;
|
|
// *dims1 = _dims1;
|
|
// return 0;
|
|
// }
|