Day 5 of the Level Editor project

I've decided that today we will implement a delete functionality. If levelEditorDrawMode is set to Delete, the user shall have the power to draw a rectangle and as a consequence all objects that are completely within the boundaries of this rectangle will be removed from the level.

To the Update method, we have to add code that checks for the keypresses: If the user presses '0', Delete mode shall be activated, if he/she presses '1', Solid drawing mode.

if (g_keys->keyDown['0'])
{
keyPressed->keyDown['0'] = true;
}
else if (keyPressed->keyDown['0'] && !g_keys->keyDown['0'])
{
levelEditorDrawMode = Delete;
keyPressed->keyDown['0'] = false;
}

if (g_keys->keyDown['1'])
{
keyPressed->keyDown['1'] = true;
}
else if (keyPressed->keyDown['1'] && !g_keys->keyDown['1'])
{
levelEditorDrawMode = Solid;
keyPressed->keyDown['1'] = false;
}


We also have to take care of the new usecase in the Draw method:

if (newElementActive)
{
switch (newElement->type)
{
case Delete:
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(newElement->coordX0, newElement->coordY0);
glVertex2f(g_mouse->coordX, newElement->coordY0);
glVertex2f(g_mouse->coordX, globalStatics.worldY - g_mouse->coordY);
glVertex2f(newElement->coordX0, globalStatics.worldY - g_mouse->coordY);
glEnd();
break;

case Solid:
glColor3f(1.0f, 0.8f, 0.2f);
glBegin(GL_LINE_LOOP);
glVertex2f(newElement->coordX0, newElement->coordY0);
glVertex2f(g_mouse->coordX, newElement->coordY0);
glVertex2f(g_mouse->coordX, globalStatics.worldY - g_mouse->coordY);
glVertex2f(newElement->coordX0, globalStatics.worldY - g_mouse->coordY);
glEnd();
break;
}
}

Finally, we must once again modify the mouse handling in the Update method. We need to iterate through a linked list and delete the matched items. The following piece of code shows how it's done.

if (g_mouse->buttonPressed != mouse->buttonPressed)
{
if (g_mouse->buttonPressed)
{
if (!newElementActive)
{
newElement = new LevelDrawElement();
newElement->type = levelEditorDrawMode;
newElement->coordX0 = g_mouse->coordX;
newElement->coordY0 = globalStatics.worldY - g_mouse->coordY;
newElementActive = true;
}
else
{
unsigned int newElement_coordX0 = newElement->coordX0;
unsigned int newElement_coordY0 = newElement->coordY0;
unsigned int newElement_coordX1 = g_mouse->coordX;
unsigned int newElement_coordY1 = globalStatics.worldY - g_mouse->coordY;
newElement->coordX0 = min(newElement_coordX0, newElement_coordX1);
newElement->coordX1 = max(newElement_coordX0, newElement_coordX1);
newElement->coordY0 = min(newElement_coordY0, newElement_coordY1);
newElement->coordY1 = max(newElement_coordY0, newElement_coordY1);

switch (levelEditorDrawMode)
{
case Delete:
{
std::list<LevelDrawElement*>::iterator it;

it = level.begin();
while (it != level.end())
{
if (newElement->coordX0 <= (*it)->coordX0
&& newElement->coordY0 <= (*it)->coordY0
&& newElement->coordX1 >= (*it)->coordX1
&& newElement->coordY1 >= (*it)->coordY1)
{
it = level.erase(it);
}
else
{
it++;
}
}
}
break;

case Solid:
level.push_back(newElement);
break;
}

newElementActive = false;
}
}

mouse->buttonPressed = g_mouse->buttonPressed;
}

Now we can delete items that we have already drawn. Great! This is a very useful functionality.

Kommentare

Beliebte Posts aus diesem Blog

Digital Art Natives

Three Branches of Creative Activity: Engineering - Art - Science

Warum ich Mensa verlassen habe