generally fixing up how nodes are inserted into the tree

This commit is contained in:
Matthew Kosarek 2023-12-15 16:39:01 -05:00
parent 6984692305
commit 3cec993799
2 changed files with 21 additions and 7 deletions

View File

@ -37,7 +37,7 @@ geom::Rectangle Node::get_rectangle()
return area;
}
geom::Rectangle Node::new_node_position()
geom::Rectangle Node::new_node_position(int index)
{
if (is_window())
{
@ -45,20 +45,24 @@ geom::Rectangle Node::new_node_position()
return {};
}
if (index < 0)
index = sub_nodes.size();
if (direction == NodeDirection::horizontal)
{
auto width_per_item = area.size.width.as_int() / static_cast<float>(sub_nodes.size() + 1);
auto new_size = geom::Size{geom::Width{width_per_item}, area.size.height};
auto new_position = geom::Point{
area.top_left.x.as_int() + area.size.width.as_int() - width_per_item,
area.top_left.x.as_int() + (index * width_per_item),
area.top_left.y
};
geom::Rectangle new_node_rect = {new_position, new_size};
int divvied = ceil(new_node_rect.size.width.as_int() / static_cast<float>(sub_nodes.size()));
std::shared_ptr<Node> prev_node;
for (auto node : sub_nodes)
for (int i = 0; i < sub_nodes.size(); i++)
{
auto node = sub_nodes[i];
auto node_rect = node->get_rectangle();
node_rect.size.width = geom::Width{node_rect.size.width.as_int() - divvied};
@ -68,6 +72,11 @@ geom::Rectangle Node::new_node_position()
prev_node->get_rectangle().top_left.x.as_int() + prev_node->get_rectangle().size.width.as_int()};
}
if (i == index)
{
node_rect.top_left.x = geom::X{node_rect.top_left.x.as_int() + width_per_item};
}
node->set_rectangle(node_rect);
prev_node = node;
}
@ -80,14 +89,15 @@ geom::Rectangle Node::new_node_position()
auto new_size = geom::Size{area.size.width, height_per_item};
auto new_position = geom::Point{
area.top_left.x,
area.top_left.y.as_int() + area.size.height.as_int() - height_per_item
area.top_left.y.as_int() + (index * height_per_item)
};
geom::Rectangle new_node_rect = {new_position, new_size};
int divvied = ceil(new_node_rect.size.height.as_int() / static_cast<float>(sub_nodes.size()));
std::shared_ptr<Node> prev_node;
for (auto node : sub_nodes)
for (int i = 0; i < sub_nodes.size(); i++)
{
auto node = sub_nodes[i];
auto node_rect = node->get_rectangle();
node_rect.size.height = geom::Height {node_rect.size.height.as_int() - divvied};
@ -97,6 +107,9 @@ geom::Rectangle Node::new_node_position()
prev_node->get_rectangle().top_left.y.as_int() + prev_node->get_rectangle().size.height.as_int()};
}
if (i == index)
node_rect.top_left.y = geom::Y{node_rect.top_left.y.as_int() + height_per_item};
node->set_rectangle(node_rect);
prev_node = node;
}
@ -306,6 +319,7 @@ bool Node::move_node(int from, int to) {
void Node::insert_node(std::shared_ptr<Node> node, int index)
{
auto position = new_node_position(index);
node->set_rectangle(position);
sub_nodes.insert(sub_nodes.begin() + index, node);
redistribute_size();
}

View File

@ -51,7 +51,7 @@ public:
geom::Rectangle get_rectangle();
/// Makes room for a new node on the lane.
geom::Rectangle new_node_position();
geom::Rectangle new_node_position(int index = -1);
/// Append the node to the lane
void add_node(std::shared_ptr<Node>);