Skip to content

Commit

Permalink
cmd: add explode addon: add jitter to selected nodes' positions
Browse files Browse the repository at this point in the history
explode() jitters selected[] nodes
explode(node,...) jitters specified nodes by name (not id)
  • Loading branch information
qwx9 committed Jan 23, 2025
1 parent 9798c2f commit 54c0750
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/awk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
35 changes: 33 additions & 2 deletions cmd/awkext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -110,6 +111,7 @@ getarray(char *arr)
return a;
}

/* FIXME: silly to do on every call */
static inline Array *
gettabarray(int t)
{
Expand Down Expand Up @@ -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; i<a->size; 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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ enum{
Tedge,
Tlabel,
TCL,
Tselect,
Tnil,

Tlayout = Tnode, /* end of tags affecting layout */
Expand All @@ -32,6 +31,7 @@ enum{
AUNSHOW,
AINFO,
AREFRESH,
AEXPLODE,
};

extern int noreset;
Expand Down
11 changes: 11 additions & 0 deletions graph/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 54c0750

Please sign in to comment.