Hello GameDev.net. I've decided to make a GUI toolkit for fun. What I mean by a "GUI toolkit" is a library for creating windows, forms, and dialog boxes by combining elements such as buttons, labels, scroll-bars and checkboxes. But let's not focus on all that right now, what's important is what happens to the most basic element, a "frame", which is simply a rectangle of a given position and size.
I've encountered a design problem. Or maybe it's a math problem. I'm not sure.
I want to have several ways to change the position and size of the components:
The first is to statically set the size and position of an element.
The second is to click-and-drag the body or the edge of an element to change their position/size "by hand".
The third to tell the element to "set size to contents", where the element automatically shrinks or expands to exactly circumscribe all child elements.
The fourth is to set the size or position of an element as a percentage of parent element's size.
Additionally, an element may be moved around by it's parent to fit some additional constraint, for example "elements must be aligned horizontally without overlapping".
Each of these behaviors I have already coded and, individually, they work. The problems start when I try to make several of those to happen at the same time.
I do not know which order to "invalidate" elements in. By invalidating, I mean "tell the element to resize/reposition itself and reposition it's children in a way that all constraints are satisfied".
You probably see the problem by now. With so many dependencies/constraints, everything depends on everything! And since any element is only aware of the constraints that directly relate to it, by attempting to fix those constraints, it would break the constraints imposed on other elements. So I would have an infinite loop of things getting invalidated and then trying to fix themselves (this looks hilarious on the screen btw, you click on something and it just spirals out of control and flies beyond the edges of the screen).
Here's a very simple example: Frame A is a parent of Frame B. I initially set the size of both frames to 100. Then I tell A "size = to contents" and I tell B "size = 101% parent size". B would grow bigger, so A would grow bigger, so B would grow bigger, ad infinitum. Everything would freeze because two elements would play catch-up forever.
I think I have multiple circular dependencies. Or maybe a better way to look at this is like a system of N variables and M equations, and it's over-constrained.
TL;DR: My GUI elements have constraints on their relative positions and sizes and I don't know how to resolve them.