multiple selections support in editmode

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@6647 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lucas GAUTHERON
2011-09-14 14:59:40 +00:00
parent 5eb99a8bd4
commit 8f90268d82
7 changed files with 236 additions and 129 deletions
+218 -123
View File
@@ -4,21 +4,14 @@
bool editmode = false;
// the current selection, used by almost all editing commands
// the current selections, used by almost all editing commands
// invariant: all code assumes that these are kept inside MINBORD distance of the edge of the map
// => selections are checked when they are made or when the world is reloaded
block sel =
{
variable("selx", 0, 0, 4096, &sel.x, NULL, false),
variable("sely", 0, 0, 4096, &sel.y, NULL, false),
variable("selxs", 0, 0, 4096, &sel.xs, NULL, false),
variable("selys", 0, 0, 4096, &sel.ys, NULL, false),
};
vector<block> sels;
int selh = 0;
bool selset = false;
#define loopselxy(b) { makeundo(); loop(x,sel.xs) loop(y,sel.ys) { sqr *s = S(sel.x+x, sel.y+y); b; } remip(sel); }
#define loopselxy(sel, b) { makeundo(sel); loop(x,(sel).xs) loop(y,(sel).ys) { sqr *s = S((sel).x+x, (sel).y+y); b; } remip(sel); }
#define loopselsxy(b) { loopv(sels) loopselxy(sels[i], b); }
int cx, cy, ch;
@@ -46,7 +39,6 @@ void toggleedit(bool force)
player1->attacking = false;
}
keyrepeat(editmode);
selset = false;
editing = editmode ? 1 : 0;
player1->state = editing ? CS_EDITING : CS_ALIVE;
if(editing && player1->onladder) player1->onladder = false;
@@ -58,29 +50,17 @@ void edittoggle() { toggleedit(false); }
COMMAND(edittoggle, ARG_NONE);
char *editinfo()
bool correctsel(block &s) // ensures above invariant
{
static string info;
if(!editmode) return NULL;
int e = closestent();
if(e<0) return NULL;
entity &c = ents[e];
formatstring(info)("closest entity = %s (%d, %d, %d, %d), selection = (%d, %d)", entnames[c.type], c.attr1, c.attr2, c.attr3, c.attr4, sel.xs, sel.ys);
return info;
}
void correctsel() // ensures above invariant
{
selset = !OUTBORD(sel.x, sel.y);
int bsize = ssize-MINBORD;
if(sel.xs+sel.x>bsize) sel.xs = bsize-sel.x;
if(sel.ys+sel.y>bsize) sel.ys = bsize-sel.y;
if(sel.xs<=0 || sel.ys<=0) selset = false;
int bsize = ssize - MINBORD;
if(s.xs+s.x>bsize) s.xs = bsize-s.x;
if(s.ys+s.y>bsize) s.ys = bsize-s.y;
if(s.xs<=0 || s.ys<=0) return false;
else return !OUTBORD(s.x, s.y);
}
bool noteditmode(const char* func)
{
correctsel();
if(!editmode)
{
if(func && func[0]!='\0') conoutf("\f4[\f3%s\f4]\f5 is only allowed in edit mode", func);
@@ -89,33 +69,86 @@ bool noteditmode(const char* func)
return !editmode;
}
inline bool selset()
{
return (sels.length() > 0);
}
bool noselection()
{
if(!selset) conoutf("no selection");
return !selset;
if(!selset()) conoutf("no selection");
return !selset();
}
char *editinfo()
{
static string info;
if(!editmode) return NULL;
int e = closestent();
if(e<0) return NULL;
entity &c = ents[e];
string selinfo = "no selection";
if(selset()) formatstring(selinfo)("selection = (%d, %d)", (sels.last()).xs, (sels.last()).ys);
formatstring(info)("closest entity = %s (%d, %d, %d, %d), %s", entnames[c.type], c.attr1, c.attr2, c.attr3, c.attr4, selinfo);
return info;
}
#define EDITSEL if(noteditmode("EDITSEL") || noselection()) return
#define EDITSELMP if(noteditmode("EDITSELMP") || noselection() || multiplayer()) return
#define EDITMP if(noteditmode("EDITMP") || multiplayer()) return
void selectpos(int x, int y, int xs, int ys)
// multiple sels
// add a selection to the list
void addselection(int x, int y, int xs, int ys, int h)
{
block s = { x, y, xs, ys };
sel = s;
selh = 0;
correctsel();
block &s = sels.add();
s.x = x; s.y = y; s.xs = xs; s.ys = ys; s.h = h;
if(!correctsel(s)) { sels.drop(); }
}
void makesel()
// reset all selections
void resetselections()
{
block s = { min(lastx,cx), min(lasty,cy), abs(lastx-cx)+1, abs(lasty-cy)+1 };
sel = s;
selh = max(lasth,ch);
correctsel();
if(selset) rtex = *S(sel.x, sel.y);
sels.shrink(0);
}
// reset all invalid selections
void checkselections()
{
loopv(sels) if(!correctsel(sels[i])) sels.remove(i);
}
// selection from cubescript
void selectpos(int x, int y, int xs, int ys)
{
resetselections();
addselection(x, y, xs, ys, 0);
}
// update current selection, or add a new one
void makesel(bool isnew)
{
if(isnew) addselection(min(lastx, cx), min(lasty, cy), abs(lastx-cx)+1, abs(lasty-cy)+1, max(lasth, ch));
else
{
block &cursel = sels.last();
cursel.x = min(lastx, cx); cursel.y = min(lasty, cy);
cursel.xs = abs(lastx-cx)+1; cursel.ys = abs(lasty-cy)+1;
cursel.h = max(lasth, ch);
correctsel(cursel);
}
if(selset()) rtex = *S((sels.last()).x, (sels.last()).y);
}
#define SEL_ATTR(attr) { string buf; loopv(sels) { concatformatstring(buf, "%d ", sels[i].attr); } result(buf); }
COMMANDF(selx, ARG_NONE, (void) { SEL_ATTR(x); });
COMMANDF(sely, ARG_NONE, (void) { SEL_ATTR(y); });
COMMANDF(selxs, ARG_NONE, (void) { SEL_ATTR(xs); });
COMMANDF(selys, ARG_NONE, (void) { SEL_ATTR(ys); });
#undef SEL_ATTR
VAR(flrceil,0,0,2);
VAR(editaxis,0,0,13);
@@ -149,7 +182,7 @@ void cursorupdate() // called every frame fr
if(OUTBORD(cx, cy)) return;
}
if(dragging) makesel();
if(dragging) { makesel(false); };
const int GRIDSIZE = 5;
const float GRIDW = 0.5f;
@@ -192,10 +225,10 @@ void cursorupdate() // called every frame fr
ch = (int)ih;
}
if(selset)
if(selset())
{
linestyle(GRIDS, 0xFF, 0x40, 0x40);
box(sel, (float)selh, (float)selh, (float)selh, (float)selh);
loopv(sels) box(sels[i], (float)sels[i].h, (float)sels[i].h, (float)sels[i].h, (float)sels[i].h);
}
glLineWidth(1);
@@ -214,7 +247,7 @@ void pruneundos(int maxremain) // bound memory
}
}
void makeundo()
void makeundo(block &sel)
{
undos.add(blockcopy(sel));
pruneundos(undomegs<<20);
@@ -229,27 +262,51 @@ void editundo()
freeblock(p);
}
block *copybuf = NULL;
vector<block *> copybuffers;
void resetcopybuffers()
{
loopv(copybuffers) freeblock(copybuffers[i]);
copybuffers.shrink(0);
}
void copy()
{
EDITSELMP;
freeblock(copybuf);
copybuf = blockcopy(sel);
resetcopybuffers();
loopv(sels)
{
block *b = blockcopy(sels[i]);
copybuffers.add(b);
}
}
void paste()
{
EDITMP;
if(!copybuf) { conoutf("nothing to paste"); return; }
sel.xs = copybuf->xs;
sel.ys = copybuf->ys;
correctsel();
if(!selset || sel.xs!=copybuf->xs || sel.ys!=copybuf->ys) { conoutf("incorrect selection"); return; }
makeundo();
copybuf->x = sel.x;
copybuf->y = sel.y;
blockpaste(*copybuf);
EDITSELMP;
if(!copybuffers.length()) { conoutf("nothing to paste"); return; }
loopv(sels)
{
block &sel = sels[i];
int selx = sel.x;
int sely = sel.y;
loopvj(copybuffers)
{
block *copyblock = copybuffers[j];
int dx = copyblock->x - copybuffers[0]->x, dy = copyblock->y - copybuffers[0]->y;
sel.xs = copyblock->xs;
sel.ys = copyblock->ys;
sel.x = selx + dx;
sel.y = sely + dy;
if(!correctsel(sel) || sel.xs!=copyblock->xs || sel.ys!=copyblock->ys) { conoutf("incorrect selection"); return; }
makeundo(sel);
blockpaste(*copyblock, sel.x, sel.y);
}
}
}
// Count the walls of type "type" contained in the current selection
@@ -262,7 +319,7 @@ int countwalls(int type)
conoutf("invalid type");
return 0;
}
loopselxy(if(s->type==type) counter++)
loopselsxy(if(s->type==type) counter++)
return counter;
}
@@ -289,10 +346,14 @@ void editdrag(bool isdown)
lastx = cx;
lasty = cy;
lasth = ch;
selset = false;
tofronttex();
extern vector<keym> keyms;
bool ctrlpressed = false;
loopv(keyms) if(keyms[i].pressed && (keyms[i].code == SDLK_RCTRL || keyms[i].code == SDLK_LCTRL)) ctrlpressed = true;
if(!ctrlpressed) resetselections();
}
makesel();
makesel(isdown);
}
// the core editing function. all the *xy functions perform the core operations
@@ -301,7 +362,7 @@ void editdrag(bool isdown)
void editheightxy(bool isfloor, int amount, block &sel)
{
loopselxy(if(isfloor)
loopselxy(sel, if(isfloor)
{
s->floor += amount;
if(s->floor>=s->ceil) s->floor = s->ceil-1;
@@ -317,8 +378,11 @@ void editheight(int flr, int amount)
{
EDITSEL;
bool isfloor = flr==0;
editheightxy(isfloor, amount, sel);
addmsg(SV_EDITH, "ri6", sel.x, sel.y, sel.xs, sel.ys, isfloor, amount);
loopv(sels)
{
editheightxy(isfloor, amount, sels[i]);
addmsg(SV_EDITH, "ri6", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, isfloor, amount);
}
}
COMMAND(editheight, ARG_2INT);
@@ -327,7 +391,7 @@ COMMAND(editheight, ARG_2INT);
void edittexxy(int type, int t, block &sel)
{
loopselxy(switch(type)
loopselxy(sel, switch(type)
{
case 0: s->ftex = t; break;
case 1: s->wtex = t; break;
@@ -346,8 +410,11 @@ void edittex(int type, int dir)
i = i<0 ? 0 : i+dir;
curedittex[atype] = i = min(max(i, 0), 255);
int t = lasttex = hdr.texlists[atype][i];
edittexxy(type, t, sel);
addmsg(SV_EDITT, "ri6", sel.x, sel.y, sel.xs, sel.ys, type, t);
loopv(sels)
{
edittexxy(type, t, sels[i]);
addmsg(SV_EDITT, "ri6", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, type, t);
}
}
void settex(int texture, int type)
@@ -366,8 +433,11 @@ void settex(int texture, int type)
conoutf("invalid/unavaible texture");
return;
}
edittexxy(type, t, sel);
addmsg(SV_EDITT, "ri6", sel.x, sel.y, sel.xs, sel.ys, type, t);
loopv(sels)
{
edittexxy(type, t, sels[i]);
addmsg(SV_EDITT, "ri6", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, type, t);
}
}
void replace()
@@ -390,17 +460,21 @@ void replace()
void edittypexy(int type, block &sel)
{
loopselxy(s->type = type);
loopselxy(sel, s->type = type);
}
void edittype(int type)
{
EDITSEL;
if(type==CORNER && (sel.xs!=sel.ys || sel.xs==3 || (sel.xs>4 && sel.xs!=8)
|| sel.x&~-sel.xs || sel.y&~-sel.ys))
{ conoutf("corner selection must be power of 2 aligned"); return; }
edittypexy(type, sel);
addmsg(SV_EDITS, "ri5", sel.x, sel.y, sel.xs, sel.ys, type);
loopv(sels)
{
block &sel = sels[i];
if(type==CORNER && (sel.xs!=sel.ys || sel.xs==3 || (sel.xs>4 && sel.xs!=8)
|| sel.x&~-sel.xs || sel.y&~-sel.ys))
{ conoutf("corner selection must be power of 2 aligned"); return; }
edittypexy(type, sel);
addmsg(SV_EDITS, "ri5", sel.x, sel.y, sel.xs, sel.ys, type);
}
}
void heightfield(int t) { edittype(t==0 ? FHF : CHF); }
@@ -414,12 +488,12 @@ COMMAND(corner, ARG_NONE);
void editequalisexy(bool isfloor, block &sel)
{
int low = 127, hi = -128;
loopselxy(
loopselxy(sel,
{
if(s->floor<low) low = s->floor;
if(s->ceil>hi) hi = s->ceil;
});
loopselxy(
loopselxy(sel,
{
if(isfloor) s->floor = low; else s->ceil = hi;
if(s->floor>=s->ceil) s->floor = s->ceil-1;
@@ -430,23 +504,30 @@ void equalize(int flr)
{
bool isfloor = flr==0;
EDITSEL;
editequalisexy(isfloor, sel);
addmsg(SV_EDITE, "ri5", sel.x, sel.y, sel.xs, sel.ys, isfloor);
loopv(sels)
{
block &sel = sels[i];
editequalisexy(isfloor, sel);
addmsg(SV_EDITE, "ri5", sel.x, sel.y, sel.xs, sel.ys, isfloor);
}
}
COMMAND(equalize, ARG_1INT);
void setvdeltaxy(int delta, block &sel)
{
loopselxy(s->vdelta = max(s->vdelta+delta, 0));
loopselxy(sel, s->vdelta = max(s->vdelta+delta, 0));
remipmore(sel);
}
void setvdelta(int delta)
{
EDITSEL;
setvdeltaxy(delta, sel);
addmsg(SV_EDITD, "ri5", sel.x, sel.y, sel.xs, sel.ys, delta);
loopv(sels)
{
setvdeltaxy(delta, sels[i]);
addmsg(SV_EDITD, "ri5", sels[i].x, sels[i].y, sels[i].xs, sels[i].ys, delta);
}
}
const int MAXARCHVERT = 50;
@@ -467,43 +548,55 @@ void archvertex(int span, int vert, int delta)
void arch(int sidedelta, int _a)
{
EDITSELMP;
sel.xs++;
sel.ys++;
if(sel.xs>MAXARCHVERT) sel.xs = MAXARCHVERT;
if(sel.ys>MAXARCHVERT) sel.ys = MAXARCHVERT;
loopselxy(s->vdelta =
sel.xs>sel.ys
? (archverts[sel.xs-1][x] + (y==0 || y==sel.ys-1 ? sidedelta : 0))
: (archverts[sel.ys-1][y] + (x==0 || x==sel.xs-1 ? sidedelta : 0)));
remipmore(sel);
loopv(sels)
{
block &sel = sels[i];
sel.xs++;
sel.ys++;
if(sel.xs>MAXARCHVERT) sel.xs = MAXARCHVERT;
if(sel.ys>MAXARCHVERT) sel.ys = MAXARCHVERT;
loopselxy(sel, s->vdelta =
sel.xs>sel.ys
? (archverts[sel.xs-1][x] + (y==0 || y==sel.ys-1 ? sidedelta : 0))
: (archverts[sel.ys-1][y] + (x==0 || x==sel.xs-1 ? sidedelta : 0)));
remipmore(sel);
}
}
void slope(int xd, int yd)
{
EDITSELMP;
int off = 0;
if(xd<0) off -= xd*sel.xs;
if(yd<0) off -= yd*sel.ys;
sel.xs++;
sel.ys++;
loopselxy(s->vdelta = xd*x+yd*y+off);
remipmore(sel);
loopv(sels)
{
block &sel = sels[i];
int off = 0;
if(xd<0) off -= xd*sel.xs;
if(yd<0) off -= yd*sel.ys;
sel.xs++;
sel.ys++;
loopselxy(sel, s->vdelta = xd*x+yd*y+off);
remipmore(sel);
}
}
void perlin(int scale, int seed, int psize)
{
EDITSELMP;
sel.xs++;
sel.ys++;
makeundo();
sel.xs--;
sel.ys--;
perlinarea(sel, scale, seed, psize);
sel.xs++;
sel.ys++;
remipmore(sel);
sel.xs--;
sel.ys--;
loopv(sels)
{
block &sel = sels[i];
sel.xs++;
sel.ys++;
makeundo(sel);
sel.xs--;
sel.ys--;
perlinarea(sel, scale, seed, psize);
sel.xs++;
sel.ys++;
remipmore(sel);
sel.xs--;
sel.ys--;
}
}
VARF(fullbright, 0, 0, 1,
@@ -518,13 +611,13 @@ VARF(fullbright, 0, 0, 1,
void edittag(int tag)
{
EDITSELMP;
loopselxy(s->tag = tag);
loopselsxy(s->tag = tag);
}
void newent(char *what, char *a1, char *a2, char *a3, char *a4)
{
EDITSEL;
newentity(-1, sel.x, sel.y, (int)camera1->o.z, what, ATOI(a1), ATOI(a2), ATOI(a3), ATOI(a4));
loopv(sels) newentity(-1, sels[i].x, sels[i].y, (int)camera1->o.z, what, ATOI(a1), ATOI(a2), ATOI(a3), ATOI(a4));
}
void movemap(int xo, int yo, int zo) // move whole map
@@ -566,9 +659,9 @@ void movemap(int xo, int yo, int zo) // move whole map
resetmap(false);
}
void selfliprotate(int dir)
void selfliprotate(block &sel, int dir)
{
makeundo();
makeundo(sel);
block *org = blockcopy(sel);
const sqr *q = (const sqr *)(org + 1);
int x1 = sel.x, x2 = sel.x + sel.xs - 1, y1 = sel.y, y2 = sel.y + sel.ys - 1, x, y;
@@ -600,19 +693,21 @@ void selectionrotate(int dir)
dir %= 4;
if(dir < 0) dir += 4;
if(!dir) return;
if(sel.xs != sel.ys) dir = 2;
selfliprotate(dir);
loopv(sels)
{
block &sel = sels[i];
if(sel.xs != sel.ys) dir = 2;
selfliprotate(sel, dir);
}
}
void selectionflip(char *axis)
{
EDITSELMP;
if(!axis || !*axis) return;
switch(toupper(*axis))
{
case 'X': selfliprotate(11); break;
case 'Y': selfliprotate(12); break;
}
char c = toupper(*axis);
if(c != 'X' && c != 'Y') return;
loopv(sels) selfliprotate(sels[i], c == 'X' ? 11 : 12);
}
COMMANDN(select, selectpos, ARG_4INT);