Skip to content

Instantly share code, notes, and snippets.

@masterex1000
Created October 22, 2020 18:29
Show Gist options
  • Save masterex1000/92c861eeef03e761d0addc6edc0b741f to your computer and use it in GitHub Desktop.
Save masterex1000/92c861eeef03e761d0addc6edc0b741f to your computer and use it in GitHub Desktop.
A small UI helper library intended to be used with raylib. It has a few simple rectangle manipulation functions that make it much easier to write a semi-responsive ui
/**
* This is a simple helper library for handling some common responsive ui stuff
*
* I wrote this because UI dev sucks and I wanted to stop writing the same code
* over and over again.
*
* This also adds some nice things like doing margins if you want. All it does
* is modify the bounds of rectangles but it is suprisingly flexable.
*
* License: I don't care, do anything as long as you don't claim it as your own
*
* Copyright Collin Conrad 2020 (as if this 40 lines of actual code deserves it)
*/
#ifndef AGUI_H
#define AGUI_H
#include "raylib.h"
Rectangle rectMargin(Rectangle box, int marginTop, int marginBot,
int marginLeft, int marginRight);
Rectangle rectMinSize(Rectangle box, Vector2 minSize, bool lockTop,
bool lockBot, bool lockLeft, bool lockRight);
Rectangle rectAnchor(Rectangle box, float startX, float endX, float startY,
float endY);
Rectangle rectStackVertical(Rectangle *box, float height, bool down);
Rectangle rectStackHorizontal(Rectangle *box, float width, bool left);
Rectangle rectApplyScroll(Rectangle *box, Vector2 scroll);
#if defined(AGUI_IMPLEMENTATION)
Rectangle rectMargin(Rectangle box, int marginTop, int marginBot,
int marginLeft, int marginRight) {
Rectangle rect = {box.x + marginLeft, box.y + marginTop,
box.width - marginLeft - marginRight,
box.height - marginTop - marginBot};
return rect;
}
Rectangle rectMinSize(Rectangle box, Vector2 minSize, bool lockTop,
bool lockBot, bool lockLeft, bool lockRight) {
float deltaX = box.width > minSize.x ? 0 : minSize.x - box.width;
float deltaY = box.height > minSize.y ? 0 : minSize.y - box.height;
if (lockLeft && lockRight) deltaX /= 2;
if (lockTop && lockBot) deltaY /= 2;
if (lockTop) // Extend downwards
box.height += deltaY;
if (lockBot) { // Extend Upwards
box.height += deltaY;
box.y -= deltaY;
}
if (lockLeft) { // Extend to the right
box.width += deltaX;
}
if (lockRight) { // Extend to the left
box.width += deltaX;
box.x -= deltaX;
}
return box;
}
Rectangle rectAnchor(Rectangle box, float startX, float endX, float startY,
float endY) {
Rectangle rect = {box.x + box.width * startX, box.y + box.height * startY,
box.width * (endX - startX),
box.height * (endY - startY)};
return rect;
}
Rectangle rectStackVertical(Rectangle *box, float height, bool down) {
Rectangle rect;
if(down) {
rect = (Rectangle) { box->x, box->y, box->width, height };
box->y += height;
} else {
rect = (Rectangle) { box->x, box->y + box->height - height, box->width, height};
box->height -= height;
}
return rect;
}
Rectangle rectStackHorizontal(Rectangle *box, float width, bool left) {
Rectangle rect;
if(left) {
rect = (Rectangle) { box->x, box->y, width, box->height };
box->x += width;
} else {
rect = (Rectangle) { box->x + box->width - width, box->y, width, box->height};
box->width -= width;
}
return rect;
}
Rectangle rectApplyScroll(Rectangle *box, Vector2 scroll) {
return (Rectangle) { box->x + scroll.x, box->y + scroll.y, box->width, box->height};
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment