From 54c07501c9e76d1b80a61b444e485d1d971a05a7 Mon Sep 17 00:00:00 2001 From: qwx Date: Thu, 23 Jan 2025 09:12:40 +0000 Subject: [PATCH] cmd: add explode addon: add jitter to selected nodes' positions explode() jitters selected[] nodes explode(node,...) jitters specified nodes by name (not id) --- cmd/awk.c | 1 + cmd/awkext.c | 35 +++++++++++++++++++++++++++++++++-- cmd/cmd.h | 2 +- graph/graph.c | 11 +++++++++++ graph/graph.h | 1 + 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/cmd/awk.c b/cmd/awk.c index 5609e8b..209af6a 100644 --- a/cmd/awk.c +++ b/cmd/awk.c @@ -28,6 +28,7 @@ const Keyword keywords[] = { /* keep sorted: binary searched */ { "eval", FEVAL, BLTIN }, { "exit", EXIT, EXIT }, { "exp", FEXP, BLTIN }, + { "explode", AEXPLODE, ADDON }, { "float", FFLOAT, BLTIN }, { "for", FOR, FOR }, { "func", FUNC, FUNC }, diff --git a/cmd/awkext.c b/cmd/awkext.c index a34f36a..e53c68f 100644 --- a/cmd/awkext.c +++ b/cmd/awkext.c @@ -47,6 +47,7 @@ static tabmap *map; static Tab *tabs; static RWLock buflock, tablock; +/* FIXME: can replace with awk array and lookup */ int gettab(char *s) { @@ -101,7 +102,7 @@ getarray(char *arr) if (freeable(c)) xfree(c->sval); a = makesymtab(NSYMTAB); - c->tval &= ~(STR|NUM|DONTFREE); + c->tval &= ~(STR|NUM|FLT|DONTFREE); c->tval |= ARR; c->sval = (char *)a; }else @@ -110,6 +111,7 @@ getarray(char *arr) return a; } +/* FIXME: silly to do on every call */ static inline Array * gettabarray(int t) { @@ -468,6 +470,33 @@ fnloadall(void) } } +static TNode * +fnexplode(Cell *x, TNode *nextarg) +{ + int i; + ioff id; + Cell *c; + Array *a; + + if(nextarg == nil){ + a = getarray("selected"); + for(i=0; isize; i++){ + for(c=a->tab[i]; c!=nil; c=c->cnext){ + id = atoi(c->nval); + explode(id); + } + } + return nil; + } + for(;nextarg!=nil; nextarg=nextarg->nnext){ + tempfree(x); + x = execute(nextarg); + id = getnodeid(getsval(x)); + explode(id); + } + return nil; +} + static void fnnodecolor(char *lab, Awknum col) { @@ -533,6 +562,9 @@ addon(TNode **a, int) strecpy(selstr, selstr+sizeof selstr, getsval(y)); reqdraw(Reqshallowdraw); break; + case AEXPLODE: /* explode(ids...) */ + nextarg = fnexplode(x, nextarg); + break; default: /* can't happen */ FATAL("illegal function type %d", t); break; @@ -568,7 +600,6 @@ initext(void) [Tedge] = {"edge", nil, 0}, [Tlabel] = {"label", nil, 0}, [TCL] = {"CL", "nodecolor", 1}, - [Tselect] = {"selected", nil, 1}, }, *pp; map = tab_init(); diff --git a/cmd/cmd.h b/cmd/cmd.h index 2f3b77a..770e9d7 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -19,7 +19,6 @@ enum{ Tedge, Tlabel, TCL, - Tselect, Tnil, Tlayout = Tnode, /* end of tags affecting layout */ @@ -32,6 +31,7 @@ enum{ AUNSHOW, AINFO, AREFRESH, + AEXPLODE, }; extern int noreset; diff --git a/graph/graph.c b/graph/graph.c index 8af76c0..3d85b45 100644 --- a/graph/graph.c +++ b/graph/graph.c @@ -78,6 +78,17 @@ setattr(int type, ioff id, V val) } } +void +explode(ioff id) +{ + RNode *r; + + r = rnodes + id; + r->pos[0] += 32.0f * (0.5f - xfrand()); + r->pos[1] += 32.0f * (0.5f - xfrand()); + r->pos[2] += 32.0f * (0.5f - xfrand()); +} + /* FIXME */ /* usable once topology has been loaded */ ioff diff --git a/graph/graph.h b/graph/graph.h index 2acb9f3..7d16cc1 100644 --- a/graph/graph.h +++ b/graph/graph.h @@ -4,6 +4,7 @@ int mergenodes(Graph*, ioff, ioff); ioff newnode(Graph*, char*); ioff newedge(Graph*, ioff, ioff, int, int, char*, ushort*); +void explode(ioff); ioff str2idx(char*); void lockgraph(Graph*, int); void unlockgraph(Graph*, int);