ddnet/src/game/editor/mapitems/layer_sounds.cpp

236 lines
5.8 KiB
C++
Raw Normal View History

#include "layer_sounds.h"
#include <game/editor/editor.h>
2014-10-09 10:44:17 +00:00
2014-10-21 10:07:09 +00:00
#include <game/generated/client_data.h>
static const float s_SourceVisualSize = 32.0f;
2014-10-09 10:44:17 +00:00
CLayerSounds::CLayerSounds(CEditor *pEditor) :
CLayer(pEditor)
2014-10-09 10:44:17 +00:00
{
m_Type = LAYERTYPE_SOUNDS;
m_aName[0] = '\0';
2014-10-09 10:44:17 +00:00
m_Sound = -1;
}
CLayerSounds::CLayerSounds(const CLayerSounds &Other) :
CLayer(Other)
{
m_Sound = Other.m_Sound;
m_vSources = Other.m_vSources;
}
2022-02-14 23:32:04 +00:00
CLayerSounds::~CLayerSounds() = default;
2014-10-09 10:44:17 +00:00
2018-08-19 17:05:42 +00:00
void CLayerSounds::Render(bool Tileset)
2014-10-09 10:44:17 +00:00
{
// TODO: nice texture
Graphics()->TextureClear();
2014-10-09 10:44:17 +00:00
Graphics()->BlendNormal();
Graphics()->QuadsBegin();
// draw falloff distance
Graphics()->SetColor(0.6f, 0.8f, 1.0f, 0.4f);
for(const auto &Source : m_vSources)
{
float OffsetX = 0;
float OffsetY = 0;
if(Source.m_PosEnv >= 0)
{
ColorRGBA Channels;
CEditor::EnvelopeEval(Source.m_PosEnvOffset, Source.m_PosEnv, Channels, m_pEditor);
OffsetX = Channels.r;
OffsetY = Channels.g;
}
switch(Source.m_Shape.m_Type)
2014-11-28 19:01:25 +00:00
{
case CSoundShape::SHAPE_CIRCLE:
{
m_pEditor->Graphics()->DrawCircle(fx2f(Source.m_Position.x) + OffsetX, fx2f(Source.m_Position.y) + OffsetY,
Source.m_Shape.m_Circle.m_Radius, 32);
float Falloff = ((float)Source.m_Falloff / 255.0f);
if(Falloff > 0.0f)
m_pEditor->Graphics()->DrawCircle(fx2f(Source.m_Position.x) + OffsetX, fx2f(Source.m_Position.y) + OffsetY,
Source.m_Shape.m_Circle.m_Radius * Falloff, 32);
break;
}
2014-11-28 19:01:25 +00:00
case CSoundShape::SHAPE_RECTANGLE:
{
float Width = fx2f(Source.m_Shape.m_Rectangle.m_Width);
float Height = fx2f(Source.m_Shape.m_Rectangle.m_Height);
m_pEditor->Graphics()->DrawRectExt(fx2f(Source.m_Position.x) + OffsetX - Width / 2, fx2f(Source.m_Position.y) + OffsetY - Height / 2,
Width, Height, 0.0f, IGraphics::CORNER_NONE);
float Falloff = ((float)Source.m_Falloff / 255.0f);
if(Falloff > 0.0f)
m_pEditor->Graphics()->DrawRectExt(fx2f(Source.m_Position.x) + OffsetX - Falloff * Width / 2, fx2f(Source.m_Position.y) + OffsetY - Falloff * Height / 2,
Width * Falloff, Height * Falloff, 0.0f, IGraphics::CORNER_NONE);
break;
}
2014-11-28 19:01:25 +00:00
}
}
2014-10-21 10:07:09 +00:00
Graphics()->QuadsEnd();
// draw handles
2014-10-21 10:07:09 +00:00
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_AUDIO_SOURCE].m_Id);
Graphics()->QuadsBegin();
Graphics()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
m_pEditor->RenderTools()->SelectSprite(SPRITE_AUDIO_SOURCE);
for(const auto &Source : m_vSources)
2014-10-09 10:44:17 +00:00
{
float OffsetX = 0;
float OffsetY = 0;
if(Source.m_PosEnv >= 0)
{
ColorRGBA Channels;
CEditor::EnvelopeEval(Source.m_PosEnvOffset, Source.m_PosEnv, Channels, m_pEditor);
OffsetX = Channels.r;
OffsetY = Channels.g;
}
m_pEditor->RenderTools()->DrawSprite(fx2f(Source.m_Position.x) + OffsetX, fx2f(Source.m_Position.y) + OffsetY, m_pEditor->MapView()->ScaleLength(s_SourceVisualSize));
2014-10-09 10:44:17 +00:00
}
Graphics()->QuadsEnd();
}
CSoundSource *CLayerSounds::NewSource(int x, int y)
2014-10-09 10:44:17 +00:00
{
Autosave copy of current editor map periodically to `auto` folder A copy of the map currently open in the editor is saved every 10 minutes to the `maps/auto` folder (interval configurable, see below). The automatically saved map uses the filename of the original map with an additional timestamp. Per map name 10 autosaves are kept in the `auto` folder before old autosaves will be deleted (number configurable, see below). Add config variable `ed_autosave_interval` (0 - 240, default 10) to configure the interval in minutes at which a copy of the current editor map is automatically saved to the 'auto' folder. Add config variable `ed_autosave_max` (0 - 1000, default 10) to configure the maximum number of autosaves that are kept per map name (0 = no limit). Autosaving will not take place in the 5 seconds immediately after the map was last modified by the user, to avoid interrupting the user with the autosave. This will only delay autosaving for up to 1 minute though, so autosaves are not prevented entirely, should the user continuously edit the map. When the editor is reopened after being closed for more than 10 seconds, the autosave timer will be adjusted to compensate for the time that was not spent on editing in the editor. When the map is saved manually by the user the autosave file is also updated, if it's outdated by at least half of the configured autosave interval. This ensures that autosaves are always available as a periodic backup of the map. When a copy of the current map is saved, this does not update the autosave and will also no longer reset the modified state. The modified state should reflect whether changes have been made that are not saved to the current map file. As saving a copy does not update the current file, the modified state should not be reset in this case. Closes #6693.
2023-06-23 15:39:05 +00:00
m_pEditor->m_Map.OnModify();
2014-10-09 10:44:17 +00:00
m_vSources.emplace_back();
CSoundSource *pSource = &m_vSources[m_vSources.size() - 1];
2014-10-09 10:44:17 +00:00
pSource->m_Position.x = f2fx(x);
pSource->m_Position.y = f2fx(y);
2014-10-09 10:44:17 +00:00
pSource->m_Loop = 1;
pSource->m_Pan = 1;
2014-10-10 17:10:57 +00:00
pSource->m_TimeDelay = 0;
2014-10-09 10:44:17 +00:00
pSource->m_PosEnv = -1;
pSource->m_PosEnvOffset = 0;
2014-10-11 14:05:36 +00:00
pSource->m_SoundEnv = -1;
pSource->m_SoundEnvOffset = 0;
2014-10-11 14:05:36 +00:00
pSource->m_Falloff = 80;
2014-11-28 19:01:25 +00:00
pSource->m_Shape.m_Type = CSoundShape::SHAPE_CIRCLE;
pSource->m_Shape.m_Circle.m_Radius = 1500;
2014-10-09 10:44:17 +00:00
return pSource;
}
void CLayerSounds::BrushSelecting(CUIRect Rect)
{
// draw selection rectangle
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();
2014-10-09 10:44:17 +00:00
Graphics()->LinesBegin();
Graphics()->LinesDraw(Array, 4);
Graphics()->LinesEnd();
}
int CLayerSounds::BrushGrab(std::shared_ptr<CLayerGroup> pBrush, CUIRect Rect)
2014-10-09 10:44:17 +00:00
{
// create new layer
std::shared_ptr<CLayerSounds> pGrabbed = std::make_shared<CLayerSounds>(m_pEditor);
2014-10-09 10:44:17 +00:00
pGrabbed->m_Sound = m_Sound;
pBrush->AddLayer(pGrabbed);
for(const auto &Source : m_vSources)
2014-10-09 10:44:17 +00:00
{
float px = fx2f(Source.m_Position.x);
float py = fx2f(Source.m_Position.y);
2014-10-09 10:44:17 +00:00
if(px > Rect.x && px < Rect.x + Rect.w && py > Rect.y && py < Rect.y + Rect.h)
2014-10-09 10:44:17 +00:00
{
CSoundSource n = Source;
2014-10-09 10:44:17 +00:00
n.m_Position.x -= f2fx(Rect.x);
n.m_Position.y -= f2fx(Rect.y);
pGrabbed->m_vSources.push_back(n);
2014-10-09 10:44:17 +00:00
}
}
return pGrabbed->m_vSources.empty() ? 0 : 1;
2014-10-09 10:44:17 +00:00
}
void CLayerSounds::BrushPlace(std::shared_ptr<CLayer> pBrush, float wx, float wy)
2014-10-09 10:44:17 +00:00
{
std::shared_ptr<CLayerSounds> pSoundLayer = std::static_pointer_cast<CLayerSounds>(pBrush);
for(const auto &Source : pSoundLayer->m_vSources)
2014-10-09 10:44:17 +00:00
{
CSoundSource n = Source;
2014-10-09 10:44:17 +00:00
n.m_Position.x += f2fx(wx);
n.m_Position.y += f2fx(wy);
m_vSources.push_back(n);
2014-10-09 10:44:17 +00:00
}
Autosave copy of current editor map periodically to `auto` folder A copy of the map currently open in the editor is saved every 10 minutes to the `maps/auto` folder (interval configurable, see below). The automatically saved map uses the filename of the original map with an additional timestamp. Per map name 10 autosaves are kept in the `auto` folder before old autosaves will be deleted (number configurable, see below). Add config variable `ed_autosave_interval` (0 - 240, default 10) to configure the interval in minutes at which a copy of the current editor map is automatically saved to the 'auto' folder. Add config variable `ed_autosave_max` (0 - 1000, default 10) to configure the maximum number of autosaves that are kept per map name (0 = no limit). Autosaving will not take place in the 5 seconds immediately after the map was last modified by the user, to avoid interrupting the user with the autosave. This will only delay autosaving for up to 1 minute though, so autosaves are not prevented entirely, should the user continuously edit the map. When the editor is reopened after being closed for more than 10 seconds, the autosave timer will be adjusted to compensate for the time that was not spent on editing in the editor. When the map is saved manually by the user the autosave file is also updated, if it's outdated by at least half of the configured autosave interval. This ensures that autosaves are always available as a periodic backup of the map. When a copy of the current map is saved, this does not update the autosave and will also no longer reset the modified state. The modified state should reflect whether changes have been made that are not saved to the current map file. As saving a copy does not update the current file, the modified state should not be reset in this case. Closes #6693.
2023-06-23 15:39:05 +00:00
m_pEditor->m_Map.OnModify();
2014-10-09 10:44:17 +00:00
}
CUI::EPopupMenuFunctionResult CLayerSounds::RenderProperties(CUIRect *pToolBox)
2014-10-09 10:44:17 +00:00
{
enum
{
PROP_SOUND = 0,
2014-10-09 10:44:17 +00:00
NUM_PROPS,
};
CProperty aProps[] = {
{"Sound", m_Sound, PROPTYPE_SOUND, -1, 0},
{nullptr},
2014-10-09 10:44:17 +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)
{
Autosave copy of current editor map periodically to `auto` folder A copy of the map currently open in the editor is saved every 10 minutes to the `maps/auto` folder (interval configurable, see below). The automatically saved map uses the filename of the original map with an additional timestamp. Per map name 10 autosaves are kept in the `auto` folder before old autosaves will be deleted (number configurable, see below). Add config variable `ed_autosave_interval` (0 - 240, default 10) to configure the interval in minutes at which a copy of the current editor map is automatically saved to the 'auto' folder. Add config variable `ed_autosave_max` (0 - 1000, default 10) to configure the maximum number of autosaves that are kept per map name (0 = no limit). Autosaving will not take place in the 5 seconds immediately after the map was last modified by the user, to avoid interrupting the user with the autosave. This will only delay autosaving for up to 1 minute though, so autosaves are not prevented entirely, should the user continuously edit the map. When the editor is reopened after being closed for more than 10 seconds, the autosave timer will be adjusted to compensate for the time that was not spent on editing in the editor. When the map is saved manually by the user the autosave file is also updated, if it's outdated by at least half of the configured autosave interval. This ensures that autosaves are always available as a periodic backup of the map. When a copy of the current map is saved, this does not update the autosave and will also no longer reset the modified state. The modified state should reflect whether changes have been made that are not saved to the current map file. As saving a copy does not update the current file, the modified state should not be reset in this case. Closes #6693.
2023-06-23 15:39:05 +00:00
m_pEditor->m_Map.OnModify();
}
2014-10-09 10:44:17 +00:00
if(Prop == PROP_SOUND)
{
if(NewVal >= 0)
m_Sound = NewVal % m_pEditor->m_Map.m_vpSounds.size();
else
m_Sound = -1;
2014-10-09 10:44:17 +00:00
}
return CUI::POPUP_KEEP_OPEN;
2014-10-09 10:44:17 +00:00
}
void CLayerSounds::ModifySoundIndex(FIndexModifyFunction Func)
2014-10-09 10:44:17 +00:00
{
2014-10-11 14:05:36 +00:00
Func(&m_Sound);
2014-10-09 10:44:17 +00:00
}
2014-10-11 14:05:36 +00:00
void CLayerSounds::ModifyEnvelopeIndex(FIndexModifyFunction Func)
2014-10-11 14:05:36 +00:00
{
for(auto &Source : m_vSources)
{
Func(&Source.m_SoundEnv);
Func(&Source.m_PosEnv);
}
}
std::shared_ptr<CLayer> CLayerSounds::Duplicate() const
{
return std::make_shared<CLayerSounds>(*this);
}