ddnet/src/game/editor/layer_quads.cpp

246 lines
5.4 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/generated/client_data.h>
#include <game/client/render.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;
2011-07-12 01:14:46 +00:00
str_copy(m_aName, "Quads", sizeof(m_aName));
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
{
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::Render()
2008-01-12 12:27:55 +00:00
{
2009-10-27 14:38:53 +00:00
Graphics()->TextureSet(-1);
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_TexID);
m_pEditor->RenderTools()->RenderQuads(m_lQuads.base_ptr(), m_lQuads.size(), LAYERRENDERFLAG_OPAQUE|LAYERRENDERFLAG_TRANSPARENT, m_pEditor->EnvelopeEval, m_pEditor);
2008-01-12 12:27:55 +00:00
}
2010-05-29 07:25:38 +00:00
CQuad *CLayerQuads::NewQuad()
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;
2008-01-12 12:27:55 +00:00
int x = 0, y = 0;
2010-05-29 07:25:38 +00:00
q->m_aPoints[0].x = x;
q->m_aPoints[0].y = y;
q->m_aPoints[1].x = x+64;
q->m_aPoints[1].y = y;
q->m_aPoints[2].x = x;
q->m_aPoints[2].y = y+64;
q->m_aPoints[3].x = x+64;
q->m_aPoints[3].y = y+64;
q->m_aPoints[4].x = x+32; // pivot
q->m_aPoints[4].y = y+32;
2008-01-12 12:27:55 +00:00
for(int i = 0; i < 5; i++)
{
2010-05-29 07:25:38 +00:00
q->m_aPoints[i].x <<= 10;
q->m_aPoints[i].y <<= 10;
2008-01-12 12:27:55 +00:00
}
2008-01-12 12:27:55 +00:00
2010-05-29 07:25:38 +00:00
q->m_aTexcoords[0].x = 0;
q->m_aTexcoords[0].y = 0;
2010-05-29 07:25:38 +00:00
q->m_aTexcoords[1].x = 1<<10;
q->m_aTexcoords[1].y = 0;
2010-05-29 07:25:38 +00:00
q->m_aTexcoords[2].x = 0;
q->m_aTexcoords[2].y = 1<<10;
2010-05-29 07:25:38 +00:00
q->m_aTexcoords[3].x = 1<<10;
q->m_aTexcoords[3].y = 1<<10;
2010-05-29 07:25:38 +00:00
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)};
2009-10-27 14:38:53 +00:00
Graphics()->TextureSet(-1);
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);
2010-05-29 07:25:38 +00:00
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;
2008-01-12 12:27:55 +00:00
for(int p = 0; p < 5; p++)
{
2010-05-29 07:25:38 +00:00
n.m_aPoints[p].x -= f2fx(Rect.x);
n.m_aPoints[p].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
}
}
2010-05-29 07:25:38 +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];
2008-01-12 12:27:55 +00:00
for(int p = 0; p < 5; p++)
{
2010-05-29 07:25:38 +00:00
n.m_aPoints[p].x += f2fx(wx);
n.m_aPoints[p].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
}
2010-05-29 07:25:38 +00:00
void CLayerQuads::BrushFlipX()
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
{
}
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];
for(int p = 0; p < 5; p++)
{
2010-05-29 07:25:38 +00:00
vec2 Pos(fx2f(q->m_aPoints[p].x), fx2f(q->m_aPoints[p].y));
Rotate(&Center, &Pos, Amount);
q->m_aPoints[p].x = f2fx(Pos.x);
q->m_aPoints[p].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
{
for(int p = 0; p < 5; p++)
{
2010-05-29 07:25:38 +00:00
*w = max(*w, fx2f(m_lQuads[i].m_aPoints[p].x));
*h = max(*h, fx2f(m_lQuads[i].m_aPoints[p].y));
2008-01-12 12:27:55 +00:00
}
}
}
2010-05-29 07:25:38 +00:00
extern int gs_SelectedPoints;
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
{
2008-01-17 23:09:49 +00:00
PROP_IMAGE=0,
NUM_PROPS,
};
2010-05-29 07:25:38 +00:00
CProperty aProps[] = {
2011-03-20 16:04:03 +00:00
{"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
{
2010-05-29 07:25:38 +00:00
if(NewVal >= 0)
m_Image = NewVal%m_pEditor->m_Map.m_lImages.size();
2008-01-17 23:09:49 +00:00
else
2010-05-29 07:25:38 +00:00
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
}
}