(UNDER CONSTRUCTION) cursetree is now a non-binary tree yippiegit add .
also abstracted ncurses WINDOW into the ct_surface structure. and added ct_bounds to complement ct_dims.
This commit is contained in:
parent
b04f0b4aa3
commit
759920a9cc
9 changed files with 476 additions and 193 deletions
109
cursetree/dims.c
109
cursetree/dims.c
|
|
@ -1,4 +1,5 @@
|
|||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
|
@ -25,35 +26,97 @@ struct ct_dims *dup_dims(const struct ct_dims *const 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 if (*val > max)
|
||||
*val = max;
|
||||
else if (*val < min)
|
||||
*val = min;
|
||||
}
|
||||
#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;
|
||||
// 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);
|
||||
// _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 (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;
|
||||
// if (!_dims0->width || !_dims0->height || !_dims1->width || !_dims1->height)
|
||||
// return 1;
|
||||
|
||||
// propagate bifurcated dimensions
|
||||
*dims0 = _dims0;
|
||||
*dims1 = _dims1;
|
||||
return 0;
|
||||
}
|
||||
// // propagate bifurcated dimensions
|
||||
// *dims0 = _dims0;
|
||||
// *dims1 = _dims1;
|
||||
// return 0;
|
||||
// }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue