Print nodes with single child on the same line

This commit is contained in:
2024-01-09 22:18:17 +01:00
parent 47f2ccd87d
commit adcee0a94d

View File

@@ -47,7 +47,7 @@ void deinit(int *game) {
bzObjectPoolDestroy(nodeStatePool); bzObjectPoolDestroy(nodeStatePool);
} }
void igRenderBTNode(const BzAIBTNode *node, const BzAIBTNodeState *state, i32 depth) { void igRenderBTNode(const BzAIBTNode *node, const BzAIBTNodeState *state, bool sameLine, i32 depth) {
const BzAIBTNode *child = bzAIBTNodeChild(node); const BzAIBTNode *child = bzAIBTNodeChild(node);
BzAIBTNodeType type = bzAIBTGetNodeType(node); BzAIBTNodeType type = bzAIBTGetNodeType(node);
char extraInfo[128]; char extraInfo[128];
@@ -58,24 +58,24 @@ void igRenderBTNode(const BzAIBTNode *node, const BzAIBTNodeState *state, i32 de
switch (type) { switch (type) {
case BZ_AIBT_DECOR_REPEAT: case BZ_AIBT_DECOR_REPEAT:
if (hasState) { if (hasState) {
snprintf(extraInfo, sizeof(extraInfo), "(%d < %d)", snprintf(extraInfo, sizeof(extraInfo), " (%d < %d)",
bzAIBTRepeatStateGetIter(state), bzAIBTRepeatStateGetIter(state),
bzAIBTDecorGetRepeat(node)); bzAIBTDecorGetRepeat(node));
} else { } else {
snprintf(extraInfo, sizeof(extraInfo), "(%d)", bzAIBTDecorGetRepeat(node)); snprintf(extraInfo, sizeof(extraInfo), " (%d)", bzAIBTDecorGetRepeat(node));
} }
break; break;
case BZ_AIBT_DECOR_DELAY: case BZ_AIBT_DECOR_DELAY:
if (hasState) { if (hasState) {
snprintf(extraInfo, sizeof(extraInfo), "(%.2f < %.2fms)", snprintf(extraInfo, sizeof(extraInfo), " (%.2f < %.2fms)",
bzAIBTDelayStateGetElapsed(state), bzAIBTDelayStateGetElapsed(state),
bzAIBTDecorGetDelay(node)); bzAIBTDecorGetDelay(node));
} else { } else {
snprintf(extraInfo, sizeof(extraInfo), "(%.2fms)", bzAIBTDecorGetDelay(node)); snprintf(extraInfo, sizeof(extraInfo), " (%.2fms)", bzAIBTDecorGetDelay(node));
} }
break; break;
case BZ_AIBT_ACTION: case BZ_AIBT_ACTION:
snprintf(extraInfo, sizeof(extraInfo), "(%s:%p)", snprintf(extraInfo, sizeof(extraInfo), " (%s:%p)",
bzAIBTActionGetName(node), bzAIBTActionGetName(node),
bzAIBTActionGetFn(node)); bzAIBTActionGetFn(node));
break; break;
@@ -89,10 +89,26 @@ void igRenderBTNode(const BzAIBTNode *node, const BzAIBTNodeState *state, i32 de
if (hasState) if (hasState)
color = (ImVec4) {1.0f, 1.0f, 0.5f, 1.0f}; color = (ImVec4) {1.0f, 1.0f, 0.5f, 1.0f};
igTextColored(color, "%*s%s %s", depth * 2, "", bool hasSingleChild = false;
bzAIBTNodeTypeToStr(type), extraInfo); if (child && bzAIBTNodeNext(child) == NULL) hasSingleChild = true;
const char *suffix = hasSingleChild ? " > " : ": ";
if (sameLine) {
igTextColored(color, "%s%s%s", bzAIBTNodeTypeToStr(type),
extraInfo, suffix);
} else {
igTextColored(color, "%*s%s %s",
depth * 2, "",
bzAIBTNodeTypeToStr(type), extraInfo, suffix);
depth++;
}
while (child) { while (child) {
igRenderBTNode(child, state, depth + 1); if (hasSingleChild) igSameLine(0, 0);
igRenderBTNode(child, state, hasSingleChild, depth);
child = bzAIBTNodeNext(child); child = bzAIBTNodeNext(child);
} }
} }
@@ -100,7 +116,7 @@ void igRenderBTNode(const BzAIBTNode *node, const BzAIBTNodeState *state, i32 de
void igRenderBT(BzAIBTState *state) { void igRenderBT(BzAIBTState *state) {
const BzAIBTNode *root = state->root; const BzAIBTNode *root = state->root;
if (igBegin("BehaviourTree", NULL, 0)) { if (igBegin("BehaviourTree", NULL, 0)) {
igRenderBTNode(root, state->first, 0); igRenderBTNode(root, state->first, false, 0);
} }
igEnd(); igEnd();
} }