summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-02-24 16:13:48 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-02-24 16:13:48 -0500
commit047764ebab345a2bcb3a856c1f1956e849f52354 (patch)
tree1fff56580c7c2c3b721d4d46cfaa0aa551cde789
parent1c68c64874fdc6a8e54603c9c315efb715e85653 (diff)
split depth limit
-rw-r--r--build.c2
-rw-r--r--command.c25
-rw-r--r--main.c1
-rw-r--r--node.c49
4 files changed, 48 insertions, 29 deletions
diff --git a/build.c b/build.c
index a6df23b..7554c50 100644
--- a/build.c
+++ b/build.c
@@ -231,8 +231,6 @@ static void build_frame(Ted *ted, float x1, float y1, float x2, float y2) {
}
}
- // @TODO: check this code
-
// check if we have something like main.c:5 or main.c(5)
// get file name
diff --git a/command.c b/command.c
index 5ee0302..5d98922 100644
--- a/command.c
+++ b/command.c
@@ -311,30 +311,7 @@ void command_execute(Ted *ted, Command c, i64 argument) {
case CMD_SPLIT_HORIZONTAL:
case CMD_SPLIT_VERTICAL:
if (node) {
- if (arr_len(node->tabs) > 1) { // need at least 2 tabs to split
- i32 left_idx = ted_new_node(ted);
- i32 right_idx = ted_new_node(ted);
- if (left_idx >= 0 && right_idx >= 0) {
- Node *left = &ted->nodes[left_idx];
- Node *right = &ted->nodes[right_idx];
- u16 active_tab = node->active_tab;
- // put active tab on the right
- arr_add(right->tabs, node->tabs[active_tab]);
- for (u32 i = 0; i < arr_len(node->tabs); ++i) {
- if (i != active_tab) {
- // put all other tabs on the left
- arr_add(left->tabs, node->tabs[i]);
- }
- }
-
- arr_clear(node->tabs);
- node->split_a = (u16)left_idx;
- node->split_b = (u16)right_idx;
- node->split_vertical = c == CMD_SPLIT_VERTICAL;
- node->split_pos = 0.5f;
- ted->active_node = &ted->nodes[right_idx];
- }
- }
+ node_split(ted, node, c == CMD_SPLIT_VERTICAL);
}
break;
case CMD_SPLIT_JOIN:
diff --git a/main.c b/main.c
index d7dbc3c..a370a45 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,4 @@
// @TODO:
-// - split depth limit
// - resize split (i.e. change split_pos)
// - move tabs between nodes
diff --git a/node.c b/node.c
index f2ac41f..7ecfef5 100644
--- a/node.c
+++ b/node.c
@@ -34,9 +34,10 @@ static void node_free(Node *node) {
// returns index of parent in ted->nodes, or -1 if this is the root node.
static i32 node_parent(Ted *ted, u16 node_idx) {
- assert(node_idx < TED_MAX_NODES && ted->nodes_used[node_idx]);
+ bool *nodes_used = ted->nodes_used;
+ assert(node_idx < TED_MAX_NODES && nodes_used[node_idx]);
for (u16 i = 0; i < TED_MAX_NODES; ++i) {
- if (ted->nodes_used[i]) {
+ if (nodes_used[i]) {
Node *node = &ted->nodes[i];
if (!node->tabs) {
if (node->split_a == node_idx || node->split_b == node_idx)
@@ -47,6 +48,21 @@ static i32 node_parent(Ted *ted, u16 node_idx) {
return -1;
}
+// the root has depth 0, and a child node has 1 more than its parent's depth.
+static u8 node_depth(Ted *ted, u16 node_idx) {
+ u8 depth = 0;
+ while (1) {
+ i32 parent = node_parent(ted, node_idx);
+ if (parent < 0) {
+ break;
+ } else {
+ node_idx = (u16)parent;
+ depth += 1;
+ }
+ }
+ return depth;
+}
+
// join this node with its sibling
static void node_join(Ted *ted, Node *node) {
i32 parent_idx = node_parent(ted, (u16)(node - ted->nodes));
@@ -269,3 +285,32 @@ static void node_frame(Ted *ted, Node *node, Rect r) {
}
}
+static void node_split(Ted *ted, Node *node, bool vertical) {
+ if (node_depth(ted, (u16)(node - ted->nodes)) >= 6) return; // prevent splitting too deep
+
+ if (arr_len(node->tabs) > 1) { // need at least 2 tabs to split
+ i32 left_idx = ted_new_node(ted);
+ i32 right_idx = ted_new_node(ted);
+ if (left_idx >= 0 && right_idx >= 0) {
+ Node *left = &ted->nodes[left_idx];
+ Node *right = &ted->nodes[right_idx];
+ u16 active_tab = node->active_tab;
+ // put active tab on the right
+ arr_add(right->tabs, node->tabs[active_tab]);
+ for (u32 i = 0; i < arr_len(node->tabs); ++i) {
+ if (i != active_tab) {
+ // put all other tabs on the left
+ arr_add(left->tabs, node->tabs[i]);
+ }
+ }
+
+ arr_clear(node->tabs);
+ node->split_a = (u16)left_idx;
+ node->split_b = (u16)right_idx;
+ node->split_vertical = vertical;
+ node->split_pos = 0.5f;
+ if (node == ted->active_node)
+ ted->active_node = &ted->nodes[right_idx];
+ }
+ }
+}