Skip to content

Instantly share code, notes, and snippets.

@SnowyPainter
Created August 28, 2021 06:43
Show Gist options
  • Save SnowyPainter/eff57e8f8d0f385876670031093a754f to your computer and use it in GitHub Desktop.
Save SnowyPainter/eff57e8f8d0f385876670031093a754f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <functional>
#include <vector>
#include <map>
#include <tuple>
namespace boost {
struct d2 {
int x = 0;
int y = 0;
};
static d2 zero2d = { 0, 0 };
struct object {
d2 pos = zero2d;
d2 size = zero2d;
};
enum class StackOption {
Horizontal,
Vertical
};
enum class Horizontal {
Left, Right, Center
};
enum class Vertical {
Top, Bottom, Middle
};
class Stacker {
private:
std::function<d2(d2, d2)> allocate;
std::vector<object*> stacked;
int currp = 0;
public:
Stacker(StackOption opt, int spacing) {
if(opt == StackOption::Horizontal)
allocate = [&p = currp, s = spacing](d2 pos, d2 size)->d2 {
pos = {p, pos.y};
p = pos.x + size.x;
return pos;
};
else
allocate = [&p = currp, s = spacing](d2 pos, d2 size)->d2 {
pos = { pos.x, p };
p = pos.y + size.y;
return pos;
};
}
void Stack(object * object) {
object->pos = allocate(object->pos, object->size);
stacked.push_back(object);
}
std::vector<object*> GetAll() {
return stacked;
}
};
class Poser {
public:
static int Top, Middle, Bottom;
static int Left, Center, Right;
std::map<std::tuple<Horizontal, Vertical>, Stacker> stackers;
Poser(d2 relSize, d2 presuppose = zero2d) {
Top = presuppose.y;
Middle = (relSize.y + presuppose.y) / 2;
Bottom = relSize.y + presuppose.y;
Left = presuppose.x;
Center = (relSize.x + presuppose.x) / 2;
Right = relSize.x + presuppose.x;
}
void InitializeDock(Horizontal h, Vertical v, StackOption opt, int space) {
stackers[std::make_tuple(h, v)] = Stacker(opt, space);
}
void Attach(Horizontal h, Vertical v, object * obj) {
stackers[std::make_tuple(h, v)].Stack(obj);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment