ddnet/src/game/editor/layer_quads.cpp

283 lines
6.1 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <base/math.h>
#include <engine/console.h>
2010-05-29 07:25:38 +00:00
#include <engine/graphics.h>
2009-10-27 14:38:53 +00:00
2011-04-19 08:34:51 +00:00
#include "editor.h"
2010-05-29 07:25:38 +00:00
#include <game/client/render.h>
#include <game/generated/client_data.h>
#include <game/localization.h>
2008-01-12 12:27:55 +00:00
2010-05-29 07:25:38 +00:00
CLayerQuads::CLayerQuads()
2008-01-12 12:27:55 +00:00
{
2010-05-29 07:25:38 +00:00
m_Type = LAYERTYPE_QUADS;
m_aName[0] = '\0';
2010-05-29 07:25:38 +00:00
m_Image = -1;
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
CLayerQuads::~CLayerQuads()
2008-01-12 12:27:55 +00:00
{
}
2018-08-19 17:05:42 +00:00
void CLayerQuads::Render(bool QuadPicker)
2008-01-12 12:27:55 +00:00
{
Graphics()->TextureClear();
2010-05-29 07:25:38 +00:00
if(m_Image >= 0 && m_Image < m_pEditor->m_Map.m_lImages.size())
Graphics()->TextureSet(m_pEditor->m_Map.m_lImages[m_Image]->m_Texture);
Graphics()->BlendNone();
2014-01-11 21:57:23 +00:00
m_pEditor->RenderTools()->ForceRenderQuads(m_lQuads.base_ptr(), m_lQuads.size(), LAYERRENDERFLAG_OPAQUE, m_pEditor->EnvelopeEval, m_pEditor);
Graphics()->BlendNormal();
2014-01-11 21:57:23 +00:00
m_pEditor->RenderTools()->ForceRenderQuads(m_lQuads.base_ptr(), m_lQuads.size(), LAYERRENDERFLAG_TRANSPARENT, m_pEditor->EnvelopeEval, m_pEditor);
2008-01-12 12:27:55 +00:00
}
CQuad *CLayerQuads::NewQuad(int x, int y, int Width, int Height)
2008-01-12 12:27:55 +00:00
{
m_pEditor->m_Map.m_Modified = true;
2010-05-29 07:25:38 +00:00
CQuad *q = &m_lQuads[m_lQuads.add(CQuad())];
2008-01-12 12:27:55 +00:00
2010-05-29 07:25:38 +00:00
q->m_PosEnv = -1;
q->m_ColorEnv = -1;
q->m_PosEnvOffset = 0;
q->m_ColorEnvOffset = 0;
Width /= 2;
Height /= 2;
q->m_aPoints[0].x = i2fx(x - Width);
q->m_aPoints[0].y = i2fx(y - Height);
q->m_aPoints[1].x = i2fx(x + Width);
q->m_aPoints[1].y = i2fx(y - Height);
q->m_aPoints[2].x = i2fx(x - Width);
q->m_aPoints[2].y = i2fx(y + Height);
q->m_aPoints[3].x = i2fx(x + Width);
q->m_aPoints[3].y = i2fx(y + Height);
q->m_aPoints[4].x = i2fx(x); // pivot
q->m_aPoints[4].y = i2fx(y);
q->m_aTexcoords[0].x = i2fx(0);
q->m_aTexcoords[0].y = i2fx(0);
q->m_aTexcoords[1].x = i2fx(1);
q->m_aTexcoords[1].y = i2fx(0);
2008-01-12 12:27:55 +00:00
q->m_aTexcoords[2].x = i2fx(0);
q->m_aTexcoords[2].y = i2fx(1);
q->m_aTexcoords[3].x = i2fx(1);
q->m_aTexcoords[3].y = i2fx(1);
q->m_aColors[0].r = 255;
q->m_aColors[0].g = 255;
q->m_aColors[0].b = 255;
q->m_aColors[0].a = 255;
q->m_aColors[1].r = 255;
q->m_aColors[1].g = 255;
q->m_aColors[1].b = 255;
q->m_aColors[1].a = 255;
q->m_aColors[2].r = 255;
q->m_aColors[2].g = 255;
q->m_aColors[2].b = 255;
q->m_aColors[2].a = 255;
q->m_aColors[3].r = 255;
q->m_aColors[3].g = 255;
q->m_aColors[3].b = 255;
q->m_aColors[3].a = 255;
2008-01-12 12:27:55 +00:00
return q;
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::BrushSelecting(CUIRect Rect)
2008-01-12 12:27:55 +00:00
{
// draw selection rectangle
2010-05-29 07:25:38 +00:00
IGraphics::CLineItem Array[4] = {
IGraphics::CLineItem(Rect.x, Rect.y, Rect.x + Rect.w, Rect.y),
IGraphics::CLineItem(Rect.x + Rect.w, Rect.y, Rect.x + Rect.w, Rect.y + Rect.h),
IGraphics::CLineItem(Rect.x + Rect.w, Rect.y + Rect.h, Rect.x, Rect.y + Rect.h),
IGraphics::CLineItem(Rect.x, Rect.y + Rect.h, Rect.x, Rect.y)};
Graphics()->TextureClear();
2009-10-27 14:38:53 +00:00
Graphics()->LinesBegin();
2010-05-29 07:25:38 +00:00
Graphics()->LinesDraw(Array, 4);
2009-10-27 14:38:53 +00:00
Graphics()->LinesEnd();
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
int CLayerQuads::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
2008-01-12 12:27:55 +00:00
{
// create new layers
2010-05-29 07:25:38 +00:00
CLayerQuads *pGrabbed = new CLayerQuads();
pGrabbed->m_pEditor = m_pEditor;
pGrabbed->m_Image = m_Image;
pBrush->AddLayer(pGrabbed);
2008-01-12 12:27:55 +00:00
//dbg_msg("", "%f %f %f %f", rect.x, rect.y, rect.w, rect.h);
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_lQuads.size(); i++)
2008-01-12 12:27:55 +00:00
{
2010-05-29 07:25:38 +00:00
CQuad *q = &m_lQuads[i];
float px = fx2f(q->m_aPoints[4].x);
float py = fx2f(q->m_aPoints[4].y);
if(px > Rect.x && px < Rect.x + Rect.w && py > Rect.y && py < Rect.y + Rect.h)
2008-01-12 12:27:55 +00:00
{
2010-05-29 07:25:38 +00:00
CQuad n;
2008-01-12 12:27:55 +00:00
n = *q;
2020-10-26 14:14:07 +00:00
for(auto &Point : n.m_aPoints)
2008-01-12 12:27:55 +00:00
{
2020-10-26 14:14:07 +00:00
Point.x -= f2fx(Rect.x);
Point.y -= f2fx(Rect.y);
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
pGrabbed->m_lQuads.add(n);
2008-01-12 12:27:55 +00:00
}
}
return pGrabbed->m_lQuads.size() ? 1 : 0;
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::BrushPlace(CLayer *pBrush, float wx, float wy)
2008-01-12 12:27:55 +00:00
{
2010-05-29 07:25:38 +00:00
CLayerQuads *l = (CLayerQuads *)pBrush;
for(int i = 0; i < l->m_lQuads.size(); i++)
2008-01-12 12:27:55 +00:00
{
2010-05-29 07:25:38 +00:00
CQuad n = l->m_lQuads[i];
2020-10-26 14:14:07 +00:00
for(auto &Point : n.m_aPoints)
2008-01-12 12:27:55 +00:00
{
2020-10-26 14:14:07 +00:00
Point.x += f2fx(wx);
Point.y += f2fx(wy);
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
m_lQuads.add(n);
2008-01-12 12:27:55 +00:00
}
m_pEditor->m_Map.m_Modified = true;
2008-01-12 12:27:55 +00:00
}
void Swap(CPoint &a, CPoint &b)
{
2017-03-21 10:24:44 +00:00
CPoint Tmp;
Tmp.x = a.x;
Tmp.y = a.y;
a.x = b.x;
a.y = b.y;
2017-03-21 10:24:44 +00:00
b.x = Tmp.x;
b.y = Tmp.y;
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::BrushFlipX()
2008-01-12 12:27:55 +00:00
{
for(int i = 0; i < m_lQuads.size(); i++)
{
CQuad *q = &m_lQuads[i];
Swap(q->m_aPoints[0], q->m_aPoints[1]);
Swap(q->m_aPoints[2], q->m_aPoints[3]);
}
m_pEditor->m_Map.m_Modified = true;
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::BrushFlipY()
2008-01-12 12:27:55 +00:00
{
for(int i = 0; i < m_lQuads.size(); i++)
{
CQuad *q = &m_lQuads[i];
Swap(q->m_aPoints[0], q->m_aPoints[2]);
Swap(q->m_aPoints[1], q->m_aPoints[3]);
}
m_pEditor->m_Map.m_Modified = true;
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
void Rotate(vec2 *pCenter, vec2 *pPoint, float Rotation)
{
2010-05-29 07:25:38 +00:00
float x = pPoint->x - pCenter->x;
float y = pPoint->y - pCenter->y;
pPoint->x = x * cosf(Rotation) - y * sinf(Rotation) + pCenter->x;
pPoint->y = x * sinf(Rotation) + y * cosf(Rotation) + pCenter->y;
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::BrushRotate(float Amount)
{
2010-05-29 07:25:38 +00:00
vec2 Center;
GetSize(&Center.x, &Center.y);
Center.x /= 2;
Center.y /= 2;
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_lQuads.size(); i++)
{
2010-05-29 07:25:38 +00:00
CQuad *q = &m_lQuads[i];
2020-10-26 14:14:07 +00:00
for(auto &Point : q->m_aPoints)
{
2020-10-26 14:14:07 +00:00
vec2 Pos(fx2f(Point.x), fx2f(Point.y));
2010-05-29 07:25:38 +00:00
Rotate(&Center, &Pos, Amount);
2020-10-26 14:14:07 +00:00
Point.x = f2fx(Pos.x);
Point.y = f2fx(Pos.y);
}
}
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::GetSize(float *w, float *h)
2008-01-12 12:27:55 +00:00
{
*w = 0;
*h = 0;
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_lQuads.size(); i++)
2008-01-12 12:27:55 +00:00
{
2020-10-26 14:14:07 +00:00
for(auto &Point : m_lQuads[i].m_aPoints)
2008-01-12 12:27:55 +00:00
{
2020-10-26 14:14:07 +00:00
*w = maximum(*w, fx2f(Point.x));
*h = maximum(*h, fx2f(Point.y));
2008-01-12 12:27:55 +00:00
}
}
}
2010-05-29 07:25:38 +00:00
int CLayerQuads::RenderProperties(CUIRect *pToolBox)
2008-01-12 12:27:55 +00:00
{
// layer props
2008-01-17 23:09:49 +00:00
enum
2008-01-12 12:27:55 +00:00
{
PROP_IMAGE = 0,
2008-01-17 23:09:49 +00:00
NUM_PROPS,
};
2010-05-29 07:25:38 +00:00
CProperty aProps[] = {
{"Image", m_Image, PROPTYPE_IMAGE, -1, 0},
2008-01-17 23:09:49 +00:00
{0},
};
2010-05-29 07:25:38 +00:00
static int s_aIds[NUM_PROPS] = {0};
int NewVal = 0;
int Prop = m_pEditor->DoProperties(pToolBox, aProps, s_aIds, &NewVal);
if(Prop != -1)
m_pEditor->m_Map.m_Modified = true;
2010-05-29 07:25:38 +00:00
if(Prop == PROP_IMAGE)
2008-01-17 23:09:49 +00:00
{
if(NewVal >= 0)
m_Image = NewVal % m_pEditor->m_Map.m_lImages.size();
else
m_Image = -1;
2008-01-17 23:09:49 +00:00
}
2008-01-13 22:03:32 +00:00
return 0;
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::ModifyImageIndex(INDEX_MODIFY_FUNC Func)
2008-01-19 12:32:08 +00:00
{
2010-05-29 07:25:38 +00:00
Func(&m_Image);
2008-01-19 12:32:08 +00:00
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::ModifyEnvelopeIndex(INDEX_MODIFY_FUNC Func)
2008-01-19 12:32:08 +00:00
{
2010-05-29 07:25:38 +00:00
for(int i = 0; i < m_lQuads.size(); i++)
2008-01-19 12:32:08 +00:00
{
2010-05-29 07:25:38 +00:00
Func(&m_lQuads[i].m_PosEnv);
Func(&m_lQuads[i].m_ColorEnv);
2008-01-19 12:32:08 +00:00
}
}