Programming/vgui soundscape maker.cpp: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(FIXED: Made it so the 'Sound list' panel now works for soundscapes, allowing you to look for the specific soundscape to insert.)
(Added clipboard for copying and pasting)
 
(One intermediate revision by the same user not shown)
Line 30: Line 30:
#include <vgui/IInput.h>
#include <vgui/IInput.h>
#include <vgui/ISurface.h>
#include <vgui/ISurface.h>
#include <ienginevgui.h>
#include <filesystem.h>
#include <filesystem.h>
#include <usermessages.h>
#include <usermessages.h>
Line 362: Line 363:
Mode_Soundscape,
Mode_Soundscape,
Mode_Looping,
Mode_Looping,
};
//soundscape clipboard type
enum class SoundscapeClipboardType
{
Type_SoundscapeNone,
Type_SoundscapeName,
Type_SoundscapeData,
Type_SoundscapeRandomWave,
};
};


Line 423: Line 433:


bool g_bSSMHack = false;
bool g_bSSMHack = false;
//max clipboard size
#define MAX_CLIPBOARD_ITEMS 10
//current clipboard stuff
static CUtlVector<KeyValues*> CurrClipboardName; //for soundscape name
static CUtlVector<KeyValues*> CurrClipboardData; //for soundscape data
static CUtlVector<KeyValues*> CurrClipboardRandom; //for random wave


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Line 503: Line 521:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static int VectorSortFunc(char* const* p1, char* const* p2)
static int VectorSortFunc(char* const* p1, char* const* p2)
{
return Q_stricmp(*p1, *p2);
}
//-----------------------------------------------------------------------------
// Purpose: Sort function for utl vector
//-----------------------------------------------------------------------------
static int VectorSortFunc(const char* const* p1, const char* const* p2)
{
{
return Q_stricmp(*p1, *p2);
return Q_stricmp(*p1, *p2);
Line 663: Line 689:




//soundscape maker text editor panel
#define TEXT_PANEL_WIDTH 760
#define TEXT_PANEL_HEIGHT 630
#define TEXT_PANEL_COMMAND_SET "Set"
#define TEXT_PANEL_COMMAND_SET_OK "SetOk"
#define TEXT_PANEL_COMMAND_FIND "FInd"


class CSoundscapeTextPanel : public vgui::Frame
//simple clipboard panel
class CSoundscapeClipboard : public vgui::Frame
{
{
public:
public:
DECLARE_CLASS_SIMPLE(CSoundscapeTextPanel, vgui::Frame);
DECLARE_CLASS_SIMPLE(CSoundscapeClipboard, vgui::Frame)


CSoundscapeTextPanel(vgui::VPANEL parent, const char* name);
CSoundscapeClipboard(SoundscapeClipboardType type);


//sets the keyvalues
//creates all the buttons
void Set(KeyValues* keyvalues);
void CreateButtons();
void RecursiveSetText(KeyValues* keyvalues, CUtlBuffer& buffer, int indent);


//other
//other
void OnCommand(const char* pszCommand);
void OnCommand(const char* pszCommand);
void PerformLayout();
void OnClose();
void OnClose() { BaseClass::OnClose(); }


private:
private:
CTextPanelTextEntry* m_Text;
SoundscapeClipboardType m_Type;
vgui::Button* m_SetButton;
vgui::TextEntry* m_FindTextEntry;
vgui::Button* m_FindButton;
};
};
//static soundscape clipboard panel
static CSoundscapeClipboard* g_SoundscapeClipboard;


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Constructor
// Purpose: Constructor
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
CSoundscapeTextPanel::CSoundscapeTextPanel(vgui::VPANEL parent, const char* name)
CSoundscapeClipboard::CSoundscapeClipboard(SoundscapeClipboardType type)
: BaseClass(nullptr, name)
: BaseClass(nullptr, "SoundscapeMakerClipboard"), m_Type(type)
{
{
SetParent(parent);
//get the size of the panel
int tall = 30;


SetKeyBoardInputEnabled(true);
switch (type)
SetMouseInputEnabled(true);
{
case SoundscapeClipboardType::Type_SoundscapeName:
tall += 29 * CurrClipboardName.Count();
break;
case SoundscapeClipboardType::Type_SoundscapeData:
tall += 29 * CurrClipboardData.Count();
break;
case SoundscapeClipboardType::Type_SoundscapeRandomWave:
tall += 29 * CurrClipboardRandom.Count();
break;
}


SetProportional(false);
SetParent(enginevgui->GetPanel(VGuiPanel_t::PANEL_TOOLS));
SetTitleBarVisible(true);
SetMinimizeButtonVisible(false);
SetMaximizeButtonVisible(false);
SetCloseButtonVisible(true);
SetCloseButtonVisible(true);
SetSizeable(true);
SetSize(300, tall);
SetMoveable(true);
MoveToCenterOfScreen();
SetVisible(false);
SetTitle("Soundscape Clipboard", true);
SetMinimumSize(575, 120);
SetSizeable(false);
 
SetDeleteSelfOnClose(true);
int ScreenWide, ScreenTall;
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);
 
SetTitle("Soundscape Text Editor", true);
SetSize(TEXT_PANEL_WIDTH, TEXT_PANEL_HEIGHT);
SetPos((ScreenWide - TEXT_PANEL_WIDTH) / 2, (ScreenTall - TEXT_PANEL_HEIGHT) / 2);
 


SetVisible(true);
RequestFocus();
MoveToFront();


//make text entry
CreateButtons();
m_Text = new CTextPanelTextEntry(this, "EditBox");
m_Text->SetBounds(5, 25, TEXT_PANEL_WIDTH - 10, TEXT_PANEL_HEIGHT - 55);
m_Text->SetEnabled(true);
m_Text->SetMultiline(true);
m_Text->SetVerticalScrollbar(true);
 
//make set button
m_SetButton = new vgui::Button(this, "SetButton", "Apply Changes To Keyvalue Maker");
m_SetButton->SetBounds(5, TEXT_PANEL_HEIGHT - 27, 250, 25);
m_SetButton->SetCommand(TEXT_PANEL_COMMAND_SET);
 
//make find text entry
m_FindTextEntry = new vgui::TextEntry(this, "FindTextEntry");
m_FindTextEntry->SetBounds(450, TEXT_PANEL_HEIGHT - 27, 200, 25);
 
//make find button
m_FindButton = new vgui::Button(this, "FindButton", "Find String");
m_FindButton->SetBounds(655, TEXT_PANEL_HEIGHT - 27, 100, 25);
m_FindButton->SetCommand(TEXT_PANEL_COMMAND_FIND);
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Sets the keyvalues
// Purpose: Creates all the clipboard buttons
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::Set(KeyValues* keyvalues)
void CSoundscapeClipboard::CreateButtons()
{
{
//write everything into a buffer
switch (m_Type)
CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
{
 
case SoundscapeClipboardType::Type_SoundscapeName:
//now write the keyvalues
KeyValues* pCurrent = keyvalues;
while (pCurrent)
{
{
RecursiveSetText(pCurrent, buf, 0);
//add all the buttons
for (int i = 0; i < CurrClipboardName.Count(); i++)
{
vgui::Button* button = new vgui::Button(this, CFmtStr("PasteButton%d", i), CFmtStr("%.50s", CurrClipboardName[i]->GetName()));
button->SetBounds(10, 29 + (i * 27), 280, 25);
button->SetCommand(CFmtStr("$PASTE%d", i));
}
break;
}
case SoundscapeClipboardType::Type_SoundscapeData:
{
//add all the buttons
for (int i = 0; i < CurrClipboardData.Count(); i++)
{
vgui::Button* button = new vgui::Button(this, CFmtStr("PasteButton%d", i), "");
 
//set text
const char* name = CurrClipboardData[i]->GetName();
if (!Q_stricmp(name, "playrandom"))
{
button->SetText("playrandom");
}
else if (!Q_stricmp(name, "playlooping"))
{
const char* looping = CurrClipboardData[i]->GetString("wave");
if (strlen(looping) > 25)
looping += strlen(looping) - 25;
 
button->SetText(CFmtStr("%s : '%s'", name, looping));
}
else
{
const char* looping = CurrClipboardData[i]->GetString("name");
if (strlen(looping) > 25)
looping += strlen(looping) - 25;


//put a newline
button->SetText(CFmtStr("%s : '%s'", name, looping));
if (pCurrent->GetNextTrueSubKey())
}
buf.PutChar('\n');


//get next
//set other stuff
pCurrent = pCurrent->GetNextTrueSubKey();
button->SetBounds(10, 29 + (i * 27), 280, 25);
button->SetCommand(CFmtStr("$PASTE%d", i));
}
break;
}
case SoundscapeClipboardType::Type_SoundscapeRandomWave:
{
//add all the buttons
for (int i = 0; i < CurrClipboardRandom.Count(); i++)
{
vgui::Button* button = new vgui::Button(this, CFmtStr("PasteButton%d", i), CurrClipboardRandom[i]->GetString());
button->SetBounds(10, 29 + (i * 27), 280, 25);
button->SetCommand(CFmtStr("$PASTE%d", i));
}
break;
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Called when focus is killed
//-----------------------------------------------------------------------------
void CSoundscapeClipboard::OnClose()
{
g_SoundscapeClipboard = nullptr;


//write that to the m_Text
BaseClass::OnClose();
m_Text->SetText((const char*)buf.Base());
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Recursively writes to a util buffer
// Purpose: Called on command
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::RecursiveSetText(KeyValues* keyvalues, CUtlBuffer& buffer, int indent)
void CSoundscapeClipboard::OnCommand(const char* command)
{
{
//write \t indent
if (Q_stristr(command, "$PASTE") == command)
for (int i = 0; i < indent; i++)
{
buffer.PutString("    ");
//get index
int index = atoi(command + 6);


//write name
//so this is what i am gonna do:
buffer.PutChar('"');
// 1. copy KeyValue from index <index> to the top of the clipboard
buffer.PutString(keyvalues->GetName());
// 2. call g_SoundscapeMaker.PasteFromClipboard((int)m_Type);
buffer.PutString("\"\n");
// 3. remove keyvalues at last index of clipboard CUtlVector
switch (m_Type)
{
case SoundscapeClipboardType::Type_SoundscapeName:
if (index >= CurrClipboardName.Count() || CurrClipboardName.Count() <= 0)
return;


//write {
CurrClipboardName.AddToTail(CurrClipboardName[index]);
for (int i = 0; i < indent; i++)
g_SoundscapeMaker->PasteFromClipboard((int)m_Type);
buffer.PutString("    ");
CurrClipboardName.Remove(CurrClipboardName.Count() - 1);
break;
case SoundscapeClipboardType::Type_SoundscapeData:
if (index >= CurrClipboardData.Count() || CurrClipboardData.Count() <= 0)
return;


buffer.PutString("{\n");
CurrClipboardData.AddToTail(CurrClipboardData[index]);
g_SoundscapeMaker->PasteFromClipboard((int)m_Type);
CurrClipboardData.Remove(CurrClipboardData.Count() - 1);
break;
case SoundscapeClipboardType::Type_SoundscapeRandomWave:
if (index >= CurrClipboardRandom.Count() || CurrClipboardRandom.Count() <= 0)
return;


//increment indent
CurrClipboardRandom.AddToTail(CurrClipboardRandom[index]);
indent++;
g_SoundscapeMaker->PasteFromClipboard((int)m_Type);
CurrClipboardRandom.Remove(CurrClipboardRandom.Count() - 1);
break;
}
}


//write all the keys first
BaseClass::OnCommand(command);
FOR_EACH_VALUE(keyvalues, value)
}
{
for (int i = 0; i < indent; i++)
buffer.PutString("    ");


//write name and value
buffer.PutChar('"');
buffer.PutString(value->GetName());
buffer.PutString("\"    ");


buffer.PutChar('"');
buffer.PutString(value->GetString());
buffer.PutString("\"\n");
}


//write all the subkeys now
//soundscape maker text editor panel
FOR_EACH_TRUE_SUBKEY(keyvalues, value)
#define TEXT_PANEL_WIDTH 760
{
#define TEXT_PANEL_HEIGHT 630
//increment indent
RecursiveSetText(value, buffer, indent);


if (value->GetNextTrueSubKey())
#define TEXT_PANEL_COMMAND_SET "Set"
buffer.PutChar('\n');
#define TEXT_PANEL_COMMAND_SET_OK "SetOk"
}
#define TEXT_PANEL_COMMAND_FIND "FInd"


//decrement indent
class CSoundscapeTextPanel : public vgui::Frame
indent--;
{
public:
DECLARE_CLASS_SIMPLE(CSoundscapeTextPanel, vgui::Frame);


//write ending }
CSoundscapeTextPanel(vgui::VPANEL parent, const char* name);
for (int i = 0; i < indent; i++)
buffer.PutString("    ");


buffer.PutString("}\n");
//sets the keyvalues
}
void Set(KeyValues* keyvalues);
void RecursiveSetText(KeyValues* keyvalues, CUtlBuffer& buffer, int indent);
 
//other
void OnCommand(const char* pszCommand);
void PerformLayout();
void OnClose() { BaseClass::OnClose(); }
 
private:
CTextPanelTextEntry* m_Text;
vgui::Button* m_SetButton;
vgui::TextEntry* m_FindTextEntry;
vgui::Button* m_FindButton;
};


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called on command
// Purpose: Constructor
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::OnCommand(const char* pszCommand)
CSoundscapeTextPanel::CSoundscapeTextPanel(vgui::VPANEL parent, const char* name)
: BaseClass(nullptr, name)
{
{
if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_SET))
SetParent(parent);
{
//play sound
vgui::surface()->PlaySound("ui/buttonclickrelease.wav");


//check first incase you accidentally press it
SetKeyBoardInputEnabled(true);
vgui::QueryBox* popup = new vgui::QueryBox("Set File?", "Are you sure you want to set the current keyvalues for the keyvalue maker?\nIf there are errors then this could break the keyvalue file.", this);
SetMouseInputEnabled(true);
popup->SetOKCommand(new KeyValues("Command", "command", TEXT_PANEL_COMMAND_SET_OK));
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);
return;
}


//set text
SetProportional(false);
if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_SET_OK))
SetTitleBarVisible(true);
{
SetMinimizeButtonVisible(false);
//get string
SetMaximizeButtonVisible(false);
int len = m_Text->GetTextLength() + 1;
SetCloseButtonVisible(true);
SetSizeable(true);
SetMoveable(true);
SetVisible(false);
SetMinimumSize(575, 120);


char* buf = new char[len];
int ScreenWide, ScreenTall;
m_Text->GetText(buf, len);
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);


g_SoundscapeMaker->SetBuffer(buf);
SetTitle("Soundscape Text Editor", true);
SetSize(TEXT_PANEL_WIDTH, TEXT_PANEL_HEIGHT);
SetPos((ScreenWide - TEXT_PANEL_WIDTH) / 2, (ScreenTall - TEXT_PANEL_HEIGHT) / 2);


//delete string
delete[] buf;


//hide this
SetVisible(false);
return;
}


//find text
//make text entry
if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_FIND))
m_Text = new CTextPanelTextEntry(this, "EditBox");
{
m_Text->SetBounds(5, 25, TEXT_PANEL_WIDTH - 10, TEXT_PANEL_HEIGHT - 55);
//get buffer
m_Text->SetEnabled(true);
char buf[128];
m_Text->SetMultiline(true);
m_FindTextEntry->GetText(buf, sizeof(buf));
m_Text->SetVerticalScrollbar(true);


int index = m_Text->_cursorPos + 1;
//make set button
int find = -1;
m_SetButton = new vgui::Button(this, "SetButton", "Apply Changes To Keyvalue Maker");
m_SetButton->SetBounds(5, TEXT_PANEL_HEIGHT - 27, 250, 25);
m_SetButton->SetCommand(TEXT_PANEL_COMMAND_SET);


//go in reversed order if holding shift
//make find text entry
if (vgui::input()->IsKeyDown(KEY_LSHIFT) || vgui::input()->IsKeyDown(KEY_RSHIFT))
m_FindTextEntry = new vgui::TextEntry(this, "FindTextEntry");
{
m_FindTextEntry->SetBounds(450, TEXT_PANEL_HEIGHT - 27, 200, 25);


//see if we find index
//make find button
find = Q_vecrstr(m_Text->m_TextStream, 0, index - 2, buf);
m_FindButton = new vgui::Button(this, "FindButton", "Find String");
if (find == -1)
m_FindButton->SetBounds(655, TEXT_PANEL_HEIGHT - 27, 100, 25);
m_FindButton->SetCommand(TEXT_PANEL_COMMAND_FIND);
}


//look again
//-----------------------------------------------------------------------------
find = Q_vecrstr(m_Text->m_TextStream, index, m_Text->m_TextStream.Count() - 1, buf);
// Purpose: Sets the keyvalues
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::Set(KeyValues* keyvalues)
{
//write everything into a buffer
CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);


}
//now write the keyvalues
else
KeyValues* pCurrent = keyvalues;
{
while (pCurrent)
{
RecursiveSetText(pCurrent, buf, 0);


//see if we find index
//put a newline
find = Q_vecstr(m_Text->m_TextStream, index, m_Text->m_TextStream.Count(), buf);
if (pCurrent->GetNextTrueSubKey())
if (find == -1)
buf.PutChar('\n');


//look again
//get next
find = Q_vecstr(m_Text->m_TextStream, 0, index, buf);
pCurrent = pCurrent->GetNextTrueSubKey();
}


}
//write that to the m_Text
m_Text->SetText((const char*)buf.Base());
}


//check for invalid index
//-----------------------------------------------------------------------------
if (find == -1)
// Purpose: Recursively writes to a util buffer
{
//-----------------------------------------------------------------------------
//play an error sound
void CSoundscapeTextPanel::RecursiveSetText(KeyValues* keyvalues, CUtlBuffer& buffer, int indent)
vgui::surface()->PlaySound("resource/warning.wav");
{
//write \t indent
for (int i = 0; i < indent; i++)
buffer.PutString("   ");


//get text
//write name
char error[512];
buffer.PutChar('"');
Q_snprintf(error, sizeof(error), "Couldnt find any instances of '%s'", buf);
buffer.PutString(keyvalues->GetName());
buffer.PutString("\"\n");
 
//write {
for (int i = 0; i < indent; i++)
buffer.PutString("    ");


//show an error
buffer.PutString("{\n");
vgui::QueryBox* popup = new vgui::QueryBox("No Instances Found", error, this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);


return;
//increment indent
}
indent++;


//get number of newlines
//write all the keys first
/*int newline = 0;
FOR_EACH_VALUE(keyvalues, value)
int column = 0;
{
for (int i = 0; i < find; i++)
for (int i = 0; i < indent; i++)
{
buffer.PutString("    ");
if (m_Text->m_TextStream[i] == '\n')
{
newline++;
column = 0;
}
else
{
column++;
}
}*/


//select that
//write name and value
m_Text->_cursorPos = find;
buffer.PutChar('"');
m_Text->_select[0] = find;
buffer.PutString(value->GetName());
m_Text->_select[1] = find + Q_strlen(buf);
buffer.PutString("\"    ");
m_Text->LayoutVerticalScrollBarSlider();
 
m_Text->Repaint();
buffer.PutChar('"');
m_Text->RequestFocus();
buffer.PutString(value->GetString());
buffer.PutString("\"\n");
}
 
//write all the subkeys now
FOR_EACH_TRUE_SUBKEY(keyvalues, value)
{
//increment indent
RecursiveSetText(value, buffer, indent);


return;
if (value->GetNextTrueSubKey())
buffer.PutChar('\n');
}
}


BaseClass::OnCommand(pszCommand);
//decrement indent
indent--;
 
//write ending }
for (int i = 0; i < indent; i++)
buffer.PutString("    ");
 
buffer.PutString("}\n");
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called on panel size changed
// Purpose: Called on command
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::PerformLayout()
void CSoundscapeTextPanel::OnCommand(const char* pszCommand)
{
{
BaseClass::PerformLayout();
if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_SET))
{
//play sound
vgui::surface()->PlaySound("ui/buttonclickrelease.wav");


int wide, tall;
//check first incase you accidentally press it
GetSize(wide, tall);
vgui::QueryBox* popup = new vgui::QueryBox("Set File?", "Are you sure you want to set the current keyvalues for the keyvalue maker?\nIf there are errors then this could break the keyvalue file.", this);
 
popup->SetOKCommand(new KeyValues("Command", "command", TEXT_PANEL_COMMAND_SET_OK));
if (m_Text)
popup->SetCancelButtonVisible(false);
m_Text->SetBounds(5, 25, wide - 10, tall - 55);
popup->AddActionSignalTarget(this);
 
popup->DoModal(this);
if (m_SetButton)
return;
m_SetButton->SetBounds(5, tall - 27, 250, 25);
}


if (m_FindTextEntry)
//set text
m_FindTextEntry->SetBounds(wide - 310, tall - 27, 200, 25);
if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_SET_OK))
{
//get string
int len = m_Text->GetTextLength() + 1;


if (m_FindButton)
char* buf = new char[len];
m_FindButton->SetBounds(wide - 105, tall - 27, 100, 25);
m_Text->GetText(buf, len);
}


//soundscape settings panel
g_SoundscapeMaker->SetBuffer(buf);
static CSoundscapeTextPanel* g_SoundscapeTextPanel = nullptr;


//delete string
delete[] buf;


//hide this
SetVisible(false);
return;
}


//find text
if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_FIND))
{
//get buffer
char buf[128];
m_FindTextEntry->GetText(buf, sizeof(buf));


//soundscape maker text editor panel
int index = m_Text->_cursorPos + 1;
#define DEBUG_PANEL_WIDTH 725
int find = -1;
#define DEBUG_PANEL_HEIGHT 530


#define DEBUG_PANEL_COMMAND_CLEAR "Clear"
//go in reversed order if holding shift
if (vgui::input()->IsKeyDown(KEY_LSHIFT) || vgui::input()->IsKeyDown(KEY_RSHIFT))
{


class CSoundscapeDebugPanel : public vgui::Frame
//see if we find index
{
find = Q_vecrstr(m_Text->m_TextStream, 0, index - 2, buf);
public:
if (find == -1)
DECLARE_CLASS_SIMPLE(CSoundscapeDebugPanel, vgui::Frame);


CSoundscapeDebugPanel(vgui::VPANEL parent, const char* name);
//look again
find = Q_vecrstr(m_Text->m_TextStream, index, m_Text->m_TextStream.Count() - 1, buf);


//sets the keyvalues
}
void AddMessage(Color color, const char* text);
else
{


//other
//see if we find index
void OnCommand(const char* pszCommand);
find = Q_vecstr(m_Text->m_TextStream, index, m_Text->m_TextStream.Count(), buf);
void PerformLayout();
if (find == -1)
void OnClose() { BaseClass::OnClose(); }


private:
//look again
vgui::RichText* m_Text;
find = Q_vecstr(m_Text->m_TextStream, 0, index, buf);
vgui::Button* m_ClearButton;
vgui::Label* m_SoundscapesFadingInText;


public:
}
CGraphPanel* m_PanelSoundscapesFadingIn;
};


//-----------------------------------------------------------------------------
//check for invalid index
// Purpose: Constructor
if (find == -1)
//-----------------------------------------------------------------------------
{
CSoundscapeDebugPanel::CSoundscapeDebugPanel(vgui::VPANEL parent, const char* name)
//play an error sound
: BaseClass(nullptr, name)
vgui::surface()->PlaySound("resource/warning.wav");
{
SetParent(parent);


SetKeyBoardInputEnabled(true);
//get text
SetMouseInputEnabled(true);
char error[512];
Q_snprintf(error, sizeof(error), "Couldnt find any instances of '%s'", buf);


SetProportional(false);
//show an error
SetTitleBarVisible(true);
vgui::QueryBox* popup = new vgui::QueryBox("No Instances Found", error, this);
SetMinimizeButtonVisible(false);
popup->SetOKButtonText("Ok");
SetMaximizeButtonVisible(false);
popup->SetCancelButtonVisible(false);
SetCloseButtonVisible(true);
popup->AddActionSignalTarget(this);
SetSizeable(true);
popup->DoModal(this);
SetMoveable(true);
SetVisible(false);
SetMinimumSize(575, 280);


SetTitle("Soundscape Debug Panel", true);
return;
SetSize(DEBUG_PANEL_WIDTH, DEBUG_PANEL_HEIGHT);
}
SetPos(0, 0);


//get number of newlines
/*int newline = 0;
int column = 0;
for (int i = 0; i < find; i++)
{
if (m_Text->m_TextStream[i] == '\n')
{
newline++;
column = 0;
}
else
{
column++;
}
}*/


//select that
m_Text->_cursorPos = find;
m_Text->_select[0] = find;
m_Text->_select[1] = find + Q_strlen(buf);
m_Text->LayoutVerticalScrollBarSlider();
m_Text->Repaint();
m_Text->RequestFocus();


//make text entry
return;
m_Text = new vgui::RichText(this, "DebugText");
}
m_Text->SetBounds(5, 25, DEBUG_PANEL_WIDTH - 10, DEBUG_PANEL_HEIGHT - 55);
m_Text->SetEnabled(true);
m_Text->SetVerticalScrollbar(true);
 
//make clear button
m_ClearButton = new vgui::Button(this, "ClearButton", "Clear");
m_ClearButton->SetBounds(5, DEBUG_PANEL_HEIGHT - 215, DEBUG_PANEL_WIDTH - 10, 25);
m_ClearButton->SetCommand(DEBUG_PANEL_COMMAND_CLEAR);
 
//make fading in label
m_SoundscapesFadingInText = new vgui::Label(this, "LabelFadingIn", "Soundscapes Fading In");
m_SoundscapesFadingInText->SetBounds(5, DEBUG_PANEL_HEIGHT - 187, DEBUG_PANEL_WIDTH - 10, 20);
 
//make soundscapes fading in thing
m_PanelSoundscapesFadingIn = new CGraphPanel(this, "SoundscapesFadingIn");
m_PanelSoundscapesFadingIn->SetBounds(5, DEBUG_PANEL_HEIGHT - 165, DEBUG_PANEL_WIDTH - 10, 155);
m_PanelSoundscapesFadingIn->SetMaxTextValue(1.0f);
m_PanelSoundscapesFadingIn->SetHorizontalLinesMax(5);
}
 
//-----------------------------------------------------------------------------
// Purpose: adds a message to the debug panel
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::AddMessage(Color color, const char* text)
{
m_Text->InsertColorChange(color);
m_Text->InsertString(text);
 
m_Text->SetMaximumCharCount(100000);
}
 
//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, DEBUG_PANEL_COMMAND_CLEAR))
{
//clear the text
m_Text->SetText("");
m_Text->GotoTextEnd();
return;
}


BaseClass::OnCommand(pszCommand);
BaseClass::OnCommand(pszCommand);
Line 1,091: Line 1,168:
// Purpose: Called on panel size changed
// Purpose: Called on panel size changed
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::PerformLayout()
void CSoundscapeTextPanel::PerformLayout()
{
{
BaseClass::PerformLayout();
BaseClass::PerformLayout();
Line 1,098: Line 1,175:
GetSize(wide, tall);
GetSize(wide, tall);


m_Text->SetBounds(5, 25, wide - 10, tall - 245);
if (m_Text)
m_ClearButton->SetBounds(5, tall - 215, wide - 10, 25);
m_Text->SetBounds(5, 25, wide - 10, tall - 55);
m_PanelSoundscapesFadingIn->SetBounds(5, tall - 165, wide - 10, 155);
 
m_SoundscapesFadingInText->SetBounds(5, tall - 187, wide - 10, 20);
if (m_SetButton)
m_SetButton->SetBounds(5, tall - 27, 250, 25);
 
if (m_FindTextEntry)
m_FindTextEntry->SetBounds(wide - 310, tall - 27, 200, 25);
 
if (m_FindButton)
m_FindButton->SetBounds(wide - 105, tall - 27, 100, 25);
}
}


//soundscape debug panel
//soundscape settings panel
static CSoundscapeDebugPanel* g_SoundscapeDebugPanel = nullptr;
static CSoundscapeTextPanel* g_SoundscapeTextPanel = nullptr;
 
 


//-----------------------------------------------------------------------------
// Purpose: Function to print text to debug panel
//-----------------------------------------------------------------------------
void SoundscapePrint(Color color, const char* msg, ...)
{
//format string
va_list args;
va_start(args, msg);


char buf[2048];
//soundscape maker text editor panel
Q_vsnprintf(buf, sizeof(buf), msg, args);
#define DEBUG_PANEL_WIDTH 725
g_SoundscapeDebugPanel->AddMessage(color, buf);
#define DEBUG_PANEL_HEIGHT 530


va_end(args);
#define DEBUG_PANEL_COMMAND_CLEAR "Clear"
}


//-----------------------------------------------------------------------------
class CSoundscapeDebugPanel : public vgui::Frame
// Purpose: Function to add a line to the soundscape debug panel
//-----------------------------------------------------------------------------
void SoundscapeAddLine(Color color, float speed, float width, bool accending)
{
{
if (g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->GetNumLines() <= 6)
public:
g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->AddLine(accending, color.r(), color.g(), color.b(), speed, width);
DECLARE_CLASS_SIMPLE(CSoundscapeDebugPanel, vgui::Frame);
}


//-----------------------------------------------------------------------------
CSoundscapeDebugPanel(vgui::VPANEL parent, const char* name);
// Purpose: Function to get debug line num
//-----------------------------------------------------------------------------
int SoundscapeGetLineNum()
{
return g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->GetNumLines();
}


//vector positions
//sets the keyvalues
Vector g_SoundscapePositions[] = {
void AddMessage(Color color, const char* text);
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin
};


//other
void OnCommand(const char* pszCommand);
void PerformLayout();
void OnClose() { BaseClass::OnClose(); }


private:
vgui::RichText* m_Text;
vgui::Button* m_ClearButton;
vgui::Label* m_SoundscapesFadingInText;


#define SETTINGS_PANEL_WIDTH 350
public:
#define SETTINGS_PANEL_HEIGHT 277
CGraphPanel* m_PanelSoundscapesFadingIn;
};


#define SETTINGS_PANEL_COMMAND_POS1 "GetPos0"
//-----------------------------------------------------------------------------
#define SETTINGS_PANEL_COMMAND_POS2 "GetPos1"
// Purpose: Constructor
#define SETTINGS_PANEL_COMMAND_POS3 "GetPos2"
//-----------------------------------------------------------------------------
#define SETTINGS_PANEL_COMMAND_POS4 "GetPos3"
CSoundscapeDebugPanel::CSoundscapeDebugPanel(vgui::VPANEL parent, const char* name)
#define SETTINGS_PANEL_COMMAND_POS5 "GetPos4"
: BaseClass(nullptr, name)
#define SETTINGS_PANEL_COMMAND_POS6 "GetPos5"
{
#define SETTINGS_PANEL_COMMAND_POS7 "GetPos6"
SetParent(parent);
#define SETTINGS_PANEL_COMMAND_POS8 "GetPos7"
#define SETTINGS_PANEL_COMMAND_SHOW "ShowPositions"
#define SETTINGS_PANEL_COMMAND_DEBUG "Debug"
 
#define MAX_SOUNDSCAPES 8
 
//soundscape maker settings panel
class CSoundscapeSettingsPanel : public vgui::Frame
{
public:
DECLARE_CLASS_SIMPLE(CSoundscapeSettingsPanel, vgui::Frame);
 
CSoundscapeSettingsPanel(vgui::VPANEL parent, const char* name);
 
//other
void OnCommand(const char* pszCommand);
 
//sets the text
void SetItem(int index, const Vector& value);
 
//message funcs
MESSAGE_FUNC_PARAMS(OnTextChanged, "TextChanged", data);
 
~CSoundscapeSettingsPanel();
 
private:
//position text entries
vgui::TextEntry* m_TextEntryPos0;
vgui::TextEntry* m_TextEntryPos1;
vgui::TextEntry* m_TextEntryPos2;
vgui::TextEntry* m_TextEntryPos3;
vgui::TextEntry* m_TextEntryPos4;
vgui::TextEntry* m_TextEntryPos5;
vgui::TextEntry* m_TextEntryPos6;
vgui::TextEntry* m_TextEntryPos7;
vgui::CheckButton* m_ShowSoundscapePositions;
vgui::Button* m_ShowSoundscapeDebug;
 
friend class CSoundscapeMaker;
};
 
 
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSoundscapeSettingsPanel::CSoundscapeSettingsPanel(vgui::VPANEL parent, const char* name)
: BaseClass(nullptr, name)
{
SetParent(parent);


SetKeyBoardInputEnabled(true);
SetKeyBoardInputEnabled(true);
Line 1,222: Line 1,240:
SetMaximizeButtonVisible(false);
SetMaximizeButtonVisible(false);
SetCloseButtonVisible(true);
SetCloseButtonVisible(true);
SetSizeable(false);
SetSizeable(true);
SetMoveable(true);
SetMoveable(true);
SetVisible(false);
SetVisible(false);
SetMinimumSize(575, 280);


//set the size and pos
SetTitle("Soundscape Debug Panel", true);
int ScreenWide, ScreenTall;
SetSize(DEBUG_PANEL_WIDTH, DEBUG_PANEL_HEIGHT);
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);
SetPos(0, 0);


SetTitle("Soundscape Maker Settings", true);
SetSize(SETTINGS_PANEL_WIDTH, SETTINGS_PANEL_HEIGHT);
SetPos((ScreenWide - SETTINGS_PANEL_WIDTH) / 2, (ScreenTall - SETTINGS_PANEL_HEIGHT) / 2);




//make text entry
m_Text = new vgui::RichText(this, "DebugText");
m_Text->SetBounds(5, 25, DEBUG_PANEL_WIDTH - 10, DEBUG_PANEL_HEIGHT - 55);
m_Text->SetEnabled(true);
m_Text->SetVerticalScrollbar(true);


//load settings
//make clear button
KeyValues* settings = new KeyValues("settings");
m_ClearButton = new vgui::Button(this, "ClearButton", "Clear");
if (!settings->LoadFromFile(filesystem, "cfg/soundscape_maker.txt", "MOD"))
m_ClearButton->SetBounds(5, DEBUG_PANEL_HEIGHT - 215, DEBUG_PANEL_WIDTH - 10, 25);
ConWarning("Failed to load settings for 'cfg/soundscape_maker.txt'. Using default settings.");
m_ClearButton->SetCommand(DEBUG_PANEL_COMMAND_CLEAR);


//get positions
//make fading in label
const char* pos0 = settings->GetString("Position0", "0 0 0");
m_SoundscapesFadingInText = new vgui::Label(this, "LabelFadingIn", "Soundscapes Fading In");
const char* pos1 = settings->GetString("Position1", "0 0 0");
m_SoundscapesFadingInText->SetBounds(5, DEBUG_PANEL_HEIGHT - 187, DEBUG_PANEL_WIDTH - 10, 20);
const char* pos2 = settings->GetString("Position2", "0 0 0");
const char* pos3 = settings->GetString("Position3", "0 0 0");
const char* pos4 = settings->GetString("Position4", "0 0 0");
const char* pos5 = settings->GetString("Position5", "0 0 0");
const char* pos6 = settings->GetString("Position6", "0 0 0");
const char* pos7 = settings->GetString("Position7", "0 0 0");


//create position text 1
//make soundscapes fading in thing
m_TextEntryPos0 = new vgui::TextEntry(this, "PosTextEntry0");
m_PanelSoundscapesFadingIn = new CGraphPanel(this, "SoundscapesFadingIn");
m_TextEntryPos0->SetEnabled(true);
m_PanelSoundscapesFadingIn->SetBounds(5, DEBUG_PANEL_HEIGHT - 165, DEBUG_PANEL_WIDTH - 10, 155);
m_TextEntryPos0->SetText(pos0 ? pos0 : "0 0 0");
m_PanelSoundscapesFadingIn->SetMaxTextValue(1.0f);
m_TextEntryPos0->SetBounds(5, 30, 230, 20);
m_PanelSoundscapesFadingIn->SetHorizontalLinesMax(5);
m_TextEntryPos0->SetMaximumCharCount(32);
}


//create position 1 button
//-----------------------------------------------------------------------------
vgui::Button* m_ButtonPos0 = new vgui::Button(this, "PosButton0", "Find Position 0", this, SETTINGS_PANEL_COMMAND_POS1);
// Purpose: adds a message to the debug panel
m_ButtonPos0->SetBounds(240, 30, 100, 20);
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::AddMessage(Color color, const char* text)
{
m_Text->InsertColorChange(color);
m_Text->InsertString(text);


//create position text 1
m_Text->SetMaximumCharCount(100000);
m_TextEntryPos1 = new vgui::TextEntry(this, "PosTextEntry1");
}
m_TextEntryPos1->SetEnabled(true);
m_TextEntryPos1->SetText(pos1 ? pos1 : "0 0 0");
m_TextEntryPos1->SetBounds(5, 55, 230, 20);
m_TextEntryPos1->SetMaximumCharCount(32);


//create position 2 button
//-----------------------------------------------------------------------------
vgui::Button* m_ButtonPos1 = new vgui::Button(this, "PosButton1", "Find Position 1", this, SETTINGS_PANEL_COMMAND_POS2);
// Purpose: Called on command
m_ButtonPos1->SetBounds(240, 55, 100, 20);
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, DEBUG_PANEL_COMMAND_CLEAR))
{
//clear the text
m_Text->SetText("");
m_Text->GotoTextEnd();
return;
}


//create position text 3
BaseClass::OnCommand(pszCommand);
m_TextEntryPos2 = new vgui::TextEntry(this, "PosTextEntry0");
}
m_TextEntryPos2->SetEnabled(true);
m_TextEntryPos2->SetText(pos2 ? pos2 : "0 0 0");
m_TextEntryPos2->SetBounds(5, 80, 230, 20);
m_TextEntryPos2->SetMaximumCharCount(32);


//create position 1 button
//-----------------------------------------------------------------------------
vgui::Button* m_ButtonPos2 = new vgui::Button(this, "PosButton2", "Find Position 2", this, SETTINGS_PANEL_COMMAND_POS3);
// Purpose: Called on panel size changed
m_ButtonPos2->SetBounds(240, 80, 100, 20);
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::PerformLayout()
{
BaseClass::PerformLayout();
 
int wide, tall;
GetSize(wide, tall);


// create position text 4
m_Text->SetBounds(5, 25, wide - 10, tall - 245);
m_TextEntryPos3 = new vgui::TextEntry(this, "PosTextEntry3");
m_ClearButton->SetBounds(5, tall - 215, wide - 10, 25);
m_TextEntryPos3->SetEnabled(true);
m_PanelSoundscapesFadingIn->SetBounds(5, tall - 165, wide - 10, 155);
m_TextEntryPos3->SetText(pos3 ? pos3 : "0 0 0");
m_SoundscapesFadingInText->SetBounds(5, tall - 187, wide - 10, 20);
m_TextEntryPos3->SetBounds(5, 105, 230, 20);
}
m_TextEntryPos3->SetMaximumCharCount(32);


// create position 4 button
//soundscape debug panel
vgui::Button* m_ButtonPos3 = new vgui::Button(this, "PosButton3", "Find Position 3", this, SETTINGS_PANEL_COMMAND_POS4);
static CSoundscapeDebugPanel* g_SoundscapeDebugPanel = nullptr;
m_ButtonPos3->SetBounds(240, 105, 100, 20);


// create position text 5
//-----------------------------------------------------------------------------
m_TextEntryPos4 = new vgui::TextEntry(this, "PosTextEntry4");
// Purpose: Function to print text to debug panel
m_TextEntryPos4->SetEnabled(true);
//-----------------------------------------------------------------------------
m_TextEntryPos4->SetText(pos4 ? pos4 : "0 0 0");
void SoundscapePrint(Color color, const char* msg, ...)
m_TextEntryPos4->SetBounds(5, 130, 230, 20);
{
m_TextEntryPos4->SetMaximumCharCount(32);
//format string
va_list args;
va_start(args, msg);


// create position 5 button
char buf[2048];
vgui::Button* m_ButtonPos4 = new vgui::Button(this, "PosButton4", "Find Position 4", this, SETTINGS_PANEL_COMMAND_POS5);
Q_vsnprintf(buf, sizeof(buf), msg, args);
m_ButtonPos4->SetBounds(240, 130, 100, 20);
g_SoundscapeDebugPanel->AddMessage(color, buf);


// create position text 6
va_end(args);
m_TextEntryPos5 = new vgui::TextEntry(this, "PosTextEntry5");
}
m_TextEntryPos5->SetEnabled(true);
 
m_TextEntryPos5->SetText(pos5 ? pos5 : "0 0 0");
//-----------------------------------------------------------------------------
m_TextEntryPos5->SetBounds(5, 155, 230, 20);
// Purpose: Function to add a line to the soundscape debug panel
m_TextEntryPos5->SetMaximumCharCount(32);
//-----------------------------------------------------------------------------
void SoundscapeAddLine(Color color, float speed, float width, bool accending)
{
if (g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->GetNumLines() <= 6)
g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->AddLine(accending, color.r(), color.g(), color.b(), speed, width);
}


// create position 6 button
//-----------------------------------------------------------------------------
vgui::Button* m_ButtonPos5 = new vgui::Button(this, "PosButton5", "Find Position 5", this, SETTINGS_PANEL_COMMAND_POS6);
// Purpose: Function to get debug line num
m_ButtonPos5->SetBounds(240, 155, 100, 20);
//-----------------------------------------------------------------------------
int SoundscapeGetLineNum()
{
return g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->GetNumLines();
}


// create position text 7
//vector positions
m_TextEntryPos6 = new vgui::TextEntry(this, "PosTextEntry6");
Vector g_SoundscapePositions[] = {
m_TextEntryPos6->SetEnabled(true);
vec3_origin,
m_TextEntryPos6->SetText(pos6 ? pos6 : "0 0 0");
vec3_origin,
m_TextEntryPos6->SetBounds(5, 180, 230, 20);
vec3_origin,
m_TextEntryPos6->SetMaximumCharCount(32);
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin,
vec3_origin
};


// create position 7 button
#define SETTINGS_PANEL_WIDTH 350
vgui::Button* m_ButtonPos6 = new vgui::Button(this, "PosButton6", "Find Position 6", this, SETTINGS_PANEL_COMMAND_POS7);
#define SETTINGS_PANEL_HEIGHT 277
m_ButtonPos6->SetBounds(240, 180, 100, 20);


// create position text 8
#define SETTINGS_PANEL_COMMAND_POS1 "GetPos0"
m_TextEntryPos7 = new vgui::TextEntry(this, "PosTextEntry7");
#define SETTINGS_PANEL_COMMAND_POS2 "GetPos1"
m_TextEntryPos7->SetEnabled(true);
#define SETTINGS_PANEL_COMMAND_POS3 "GetPos2"
m_TextEntryPos7->SetText(pos7 ? pos7 : "0 0 0");
#define SETTINGS_PANEL_COMMAND_POS4 "GetPos3"
m_TextEntryPos7->SetBounds(5, 205, 230, 20);
#define SETTINGS_PANEL_COMMAND_POS5 "GetPos4"
m_TextEntryPos7->SetMaximumCharCount(32);
#define SETTINGS_PANEL_COMMAND_POS6 "GetPos5"
#define SETTINGS_PANEL_COMMAND_POS7 "GetPos6"
#define SETTINGS_PANEL_COMMAND_POS8 "GetPos7"
#define SETTINGS_PANEL_COMMAND_SHOW "ShowPositions"
#define SETTINGS_PANEL_COMMAND_DEBUG "Debug"


// create position 8 button
#define MAX_SOUNDSCAPES 8
vgui::Button* m_ButtonPos7 = new vgui::Button(this, "PosButton7", "Find Position 7", this, SETTINGS_PANEL_COMMAND_POS8);
m_ButtonPos7->SetBounds(240, 205, 100, 20);


// create show soundscape positions checkbox
//soundscape maker settings panel
m_ShowSoundscapePositions = new vgui::CheckButton(this, "ShowCheckox", "Show Soundscape Positions");
class CSoundscapeSettingsPanel : public vgui::Frame
m_ShowSoundscapePositions->SetBounds(75, 225, 200, 20);
{
m_ShowSoundscapePositions->SetCommand(SETTINGS_PANEL_COMMAND_SHOW);
public:
m_ShowSoundscapePositions->SetSelected(settings->GetBool("ShowSoundscapes", false));
DECLARE_CLASS_SIMPLE(CSoundscapeSettingsPanel, vgui::Frame);


//set convar value
CSoundscapeSettingsPanel(vgui::VPANEL parent, const char* name);
ConVar* cv = cvar->FindVar("__ss_draw");
if (cv)
cv->SetValue(m_ShowSoundscapePositions->IsSelected());


//create divider
//other
vgui::Divider* div = new vgui::Divider(this, "Divider");
void OnCommand(const char* pszCommand);
div->SetBounds(-2, 247, SETTINGS_PANEL_WIDTH + 4, 2);


//create debug thing
//sets the text
m_ShowSoundscapeDebug = new vgui::Button(this, "DebugInfo", "Show soundscape debug panel");
void SetItem(int index, const Vector& value);
m_ShowSoundscapeDebug->SetBounds(20, 254, SETTINGS_PANEL_WIDTH - 40, 20);
m_ShowSoundscapeDebug->SetCommand(SETTINGS_PANEL_COMMAND_DEBUG);


//set server positions
//message funcs
ConCommand* cc = cvar->FindCommand("__ss_maker_set");
MESSAGE_FUNC_PARAMS(OnTextChanged, "TextChanged", data);
if (cc)
{
CCommand args;


//do pos 0
~CSoundscapeSettingsPanel();
if (pos0)
{
args.Tokenize(CFmtStr("ssmaker 0 %s 1", pos0));
cc->Dispatch(args);


UTIL_StringToVector(g_SoundscapePositions[0].Base(), pos0);
private:
}
//position text entries
vgui::TextEntry* m_TextEntryPos0;
vgui::TextEntry* m_TextEntryPos1;
vgui::TextEntry* m_TextEntryPos2;
vgui::TextEntry* m_TextEntryPos3;
vgui::TextEntry* m_TextEntryPos4;
vgui::TextEntry* m_TextEntryPos5;
vgui::TextEntry* m_TextEntryPos6;
vgui::TextEntry* m_TextEntryPos7;
vgui::CheckButton* m_ShowSoundscapePositions;
vgui::Button* m_ShowSoundscapeDebug;


//do pos 1
friend class CSoundscapeMaker;
if (pos1)
};
{
args.Tokenize(CFmtStr("ssmaker 1 %s 1", pos1));
cc->Dispatch(args);


UTIL_StringToVector(g_SoundscapePositions[1].Base(), pos1);
}


//do pos 2
//-----------------------------------------------------------------------------
if (pos2)
// Purpose: Constructor
{
//-----------------------------------------------------------------------------
args.Tokenize(CFmtStr("ssmaker 2 %s 1", pos2));
CSoundscapeSettingsPanel::CSoundscapeSettingsPanel(vgui::VPANEL parent, const char* name)
cc->Dispatch(args);
: BaseClass(nullptr, name)
{
SetParent(parent);


UTIL_StringToVector(g_SoundscapePositions[2].Base(), pos2);
SetKeyBoardInputEnabled(true);
}
SetMouseInputEnabled(true);


//do pos 3
SetProportional(false);
if (pos3)
SetTitleBarVisible(true);
{
SetMinimizeButtonVisible(false);
args.Tokenize(CFmtStr("ssmaker 3 %s 1", pos3));
SetMaximizeButtonVisible(false);
cc->Dispatch(args);
SetCloseButtonVisible(true);
SetSizeable(false);
SetMoveable(true);
SetVisible(false);


UTIL_StringToVector(g_SoundscapePositions[3].Base(), pos3);
//set the size and pos
}
int ScreenWide, ScreenTall;
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);


//do pos 4
SetTitle("Soundscape Maker Settings", true);
if (pos4)
SetSize(SETTINGS_PANEL_WIDTH, SETTINGS_PANEL_HEIGHT);
{
SetPos((ScreenWide - SETTINGS_PANEL_WIDTH) / 2, (ScreenTall - SETTINGS_PANEL_HEIGHT) / 2);
args.Tokenize(CFmtStr("ssmaker 4 %s 1", pos4));
cc->Dispatch(args);


UTIL_StringToVector(g_SoundscapePositions[4].Base(), pos4);
}


//do pos 5
if (pos5)
{
args.Tokenize(CFmtStr("ssmaker 5 %s 1", pos5));
cc->Dispatch(args);


UTIL_StringToVector(g_SoundscapePositions[5].Base(), pos5);
//load settings
}
KeyValues* settings = new KeyValues("settings");
if (!settings->LoadFromFile(filesystem, "cfg/soundscape_maker.txt", "MOD"))
ConWarning("Failed to load settings for 'cfg/soundscape_maker.txt'. Using default settings.");


//do pos 6
//get positions
if (pos6)
const char* pos0 = settings->GetString("Position0", "0 0 0");
{
const char* pos1 = settings->GetString("Position1", "0 0 0");
args.Tokenize(CFmtStr("ssmaker 6 %s 1", pos6));
const char* pos2 = settings->GetString("Position2", "0 0 0");
cc->Dispatch(args);
const char* pos3 = settings->GetString("Position3", "0 0 0");
const char* pos4 = settings->GetString("Position4", "0 0 0");
const char* pos5 = settings->GetString("Position5", "0 0 0");
const char* pos6 = settings->GetString("Position6", "0 0 0");
const char* pos7 = settings->GetString("Position7", "0 0 0");


UTIL_StringToVector(g_SoundscapePositions[6].Base(), pos6);
//create position text 1
}
m_TextEntryPos0 = new vgui::TextEntry(this, "PosTextEntry0");
m_TextEntryPos0->SetEnabled(true);
m_TextEntryPos0->SetText(pos0 ? pos0 : "0 0 0");
m_TextEntryPos0->SetBounds(5, 30, 230, 20);
m_TextEntryPos0->SetMaximumCharCount(32);


//do pos 7
//create position 1 button
if (pos7)
vgui::Button* m_ButtonPos0 = new vgui::Button(this, "PosButton0", "Find Position 0", this, SETTINGS_PANEL_COMMAND_POS1);
{
m_ButtonPos0->SetBounds(240, 30, 100, 20);
args.Tokenize(CFmtStr("ssmaker 7 %s", pos7));
cc->Dispatch(args);


UTIL_StringToVector(g_SoundscapePositions[7].Base(), pos7);
//create position text 1
}
m_TextEntryPos1 = new vgui::TextEntry(this, "PosTextEntry1");
}
m_TextEntryPos1->SetEnabled(true);
m_TextEntryPos1->SetText(pos1 ? pos1 : "0 0 0");
m_TextEntryPos1->SetBounds(5, 55, 230, 20);
m_TextEntryPos1->SetMaximumCharCount(32);


//delete settings
//create position 2 button
settings->deleteThis();
vgui::Button* m_ButtonPos1 = new vgui::Button(this, "PosButton1", "Find Position 1", this, SETTINGS_PANEL_COMMAND_POS2);
}
m_ButtonPos1->SetBounds(240, 55, 100, 20);


//-----------------------------------------------------------------------------
//create position text 3
// Purpose: Called on command
m_TextEntryPos2 = new vgui::TextEntry(this, "PosTextEntry0");
//-----------------------------------------------------------------------------
m_TextEntryPos2->SetEnabled(true);
void CSoundscapeSettingsPanel::OnCommand(const char* pszCommand)
m_TextEntryPos2->SetText(pos2 ? pos2 : "0 0 0");
{
m_TextEntryPos2->SetBounds(5, 80, 230, 20);
if (Q_strstr(pszCommand, "GetPos") == pszCommand)
m_TextEntryPos2->SetMaximumCharCount(32);
{
//search for number
pszCommand = pszCommand + 6;


//execute command
//create position 1 button
static ConCommand* cc = cvar->FindCommand("__ss_maker_start");
vgui::Button* m_ButtonPos2 = new vgui::Button(this, "PosButton2", "Find Position 2", this, SETTINGS_PANEL_COMMAND_POS3);
if (cc)
m_ButtonPos2->SetBounds(240, 80, 100, 20);
{
//hide everything first
g_SoundscapeMaker->SetAllVisible(false);


CCommand args;
// create position text 4
args.Tokenize(CFmtStr("ssmaker %d", atoi(pszCommand)));
m_TextEntryPos3 = new vgui::TextEntry(this, "PosTextEntry3");
cc->Dispatch(args);
m_TextEntryPos3->SetEnabled(true);
}
m_TextEntryPos3->SetText(pos3 ? pos3 : "0 0 0");
m_TextEntryPos3->SetBounds(5, 105, 230, 20);
m_TextEntryPos3->SetMaximumCharCount(32);


return;
// create position 4 button
}
vgui::Button* m_ButtonPos3 = new vgui::Button(this, "PosButton3", "Find Position 3", this, SETTINGS_PANEL_COMMAND_POS4);
m_ButtonPos3->SetBounds(240, 105, 100, 20);


else if (!Q_strcmp(pszCommand, SETTINGS_PANEL_COMMAND_SHOW))
// create position text 5
{
m_TextEntryPos4 = new vgui::TextEntry(this, "PosTextEntry4");
static ConVar* cv = cvar->FindVar("__ss_draw");
m_TextEntryPos4->SetEnabled(true);
if (cv)
m_TextEntryPos4->SetText(pos4 ? pos4 : "0 0 0");
cv->SetValue(m_ShowSoundscapePositions->IsSelected());
m_TextEntryPos4->SetBounds(5, 130, 230, 20);
m_TextEntryPos4->SetMaximumCharCount(32);


return;
// create position 5 button
}
vgui::Button* m_ButtonPos4 = new vgui::Button(this, "PosButton4", "Find Position 4", this, SETTINGS_PANEL_COMMAND_POS5);
m_ButtonPos4->SetBounds(240, 130, 100, 20);


//handle debug thing
// create position text 6
else if (!Q_strcmp(pszCommand, SETTINGS_PANEL_COMMAND_DEBUG))
m_TextEntryPos5 = new vgui::TextEntry(this, "PosTextEntry5");
{
m_TextEntryPos5->SetEnabled(true);
g_SoundscapeDebugPanel->SetVisible(true);
m_TextEntryPos5->SetText(pos5 ? pos5 : "0 0 0");
g_SoundscapeDebugPanel->RequestFocus();
m_TextEntryPos5->SetBounds(5, 155, 230, 20);
g_SoundscapeDebugPanel->MoveToFront();
m_TextEntryPos5->SetMaximumCharCount(32);
return;
}


BaseClass::OnCommand(pszCommand);
// create position 6 button
}
vgui::Button* m_ButtonPos5 = new vgui::Button(this, "PosButton5", "Find Position 5", this, SETTINGS_PANEL_COMMAND_POS6);
m_ButtonPos5->SetBounds(240, 155, 100, 20);


//-----------------------------------------------------------------------------
// create position text 7
// Purpose: Constructor
m_TextEntryPos6 = new vgui::TextEntry(this, "PosTextEntry6");
//-----------------------------------------------------------------------------
m_TextEntryPos6->SetEnabled(true);
void CSoundscapeSettingsPanel::SetItem(int index, const Vector& value)
m_TextEntryPos6->SetText(pos6 ? pos6 : "0 0 0");
{
m_TextEntryPos6->SetBounds(5, 180, 230, 20);
const char* text = CFmtStr("%.3f %.3f %.3f", value.x, value.y, value.z);
m_TextEntryPos6->SetMaximumCharCount(32);


//check index
// create position 7 button
switch (index)
vgui::Button* m_ButtonPos6 = new vgui::Button(this, "PosButton6", "Find Position 6", this, SETTINGS_PANEL_COMMAND_POS7);
{
m_ButtonPos6->SetBounds(240, 180, 100, 20);
case 0:
m_TextEntryPos0->RequestFocus();
m_TextEntryPos0->SetText(text);
g_SoundscapePositions[0] = value;
break;


case 1:
// create position text 8
m_TextEntryPos1->RequestFocus();
m_TextEntryPos7 = new vgui::TextEntry(this, "PosTextEntry7");
m_TextEntryPos1->SetText(text);
m_TextEntryPos7->SetEnabled(true);
g_SoundscapePositions[1] = value;
m_TextEntryPos7->SetText(pos7 ? pos7 : "0 0 0");
break;
m_TextEntryPos7->SetBounds(5, 205, 230, 20);
m_TextEntryPos7->SetMaximumCharCount(32);


case 2:
// create position 8 button
m_TextEntryPos2->RequestFocus();
vgui::Button* m_ButtonPos7 = new vgui::Button(this, "PosButton7", "Find Position 7", this, SETTINGS_PANEL_COMMAND_POS8);
m_TextEntryPos2->SetText(text);
m_ButtonPos7->SetBounds(240, 205, 100, 20);
g_SoundscapePositions[2] = value;
break;
case 3:
m_TextEntryPos3->RequestFocus();
m_TextEntryPos3->SetText(text);
g_SoundscapePositions[3] = value;
break;


case 4:
// create show soundscape positions checkbox
m_TextEntryPos4->RequestFocus();
m_ShowSoundscapePositions = new vgui::CheckButton(this, "ShowCheckox", "Show Soundscape Positions");
m_TextEntryPos4->SetText(text);
m_ShowSoundscapePositions->SetBounds(75, 225, 200, 20);
g_SoundscapePositions[4] = value;
m_ShowSoundscapePositions->SetCommand(SETTINGS_PANEL_COMMAND_SHOW);
break;
m_ShowSoundscapePositions->SetSelected(settings->GetBool("ShowSoundscapes", false));


case 5:
//set convar value
m_TextEntryPos5->RequestFocus();
ConVar* cv = cvar->FindVar("__ss_draw");
m_TextEntryPos5->SetText(text);
if (cv)
g_SoundscapePositions[5] = value;
cv->SetValue(m_ShowSoundscapePositions->IsSelected());
break;


case 6:
//create divider
m_TextEntryPos6->RequestFocus();
vgui::Divider* div = new vgui::Divider(this, "Divider");
m_TextEntryPos6->SetText(text);
div->SetBounds(-2, 247, SETTINGS_PANEL_WIDTH + 4, 2);
g_SoundscapePositions[6] = value;
break;


case 7:
//create debug thing
m_TextEntryPos7->RequestFocus();
m_ShowSoundscapeDebug = new vgui::Button(this, "DebugInfo", "Show soundscape debug panel");
m_TextEntryPos7->SetText(text);
m_ShowSoundscapeDebug->SetBounds(20, 254, SETTINGS_PANEL_WIDTH - 40, 20);
g_SoundscapePositions[7] = value;
m_ShowSoundscapeDebug->SetCommand(SETTINGS_PANEL_COMMAND_DEBUG);
break;
}
}


//-----------------------------------------------------------------------------
//set server positions
// Purpose: Called on text changed
ConCommand* cc = cvar->FindCommand("__ss_maker_set");
//-----------------------------------------------------------------------------
if (cc)
void CSoundscapeSettingsPanel::OnTextChanged(KeyValues* kv)
{
{
CCommand args;
static ConCommand* cc = cvar->FindCommand("__ss_maker_set");


//check focus
//do pos 0
if (m_TextEntryPos0->HasFocus())
if (pos0)
{
{
//get text
args.Tokenize(CFmtStr("ssmaker 0 %s 1", pos0));
char buf[512];
cc->Dispatch(args);
m_TextEntryPos0->GetText(buf, sizeof(buf));


//convert to vector
UTIL_StringToVector(g_SoundscapePositions[0].Base(), pos0);
UTIL_StringToVector(g_SoundscapePositions[0].Base(), buf);
}


//do command
//do pos 1
if (cc)
if (pos1)
{
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 1 %s 1", pos1));
args.Tokenize(CFmtStr("ssmaker 0 %s 1", buf));
cc->Dispatch(args);
cc->Dispatch(args);
UTIL_StringToVector(g_SoundscapePositions[1].Base(), pos1);
}
}


return;
//do pos 2
}
if (pos2)
{
args.Tokenize(CFmtStr("ssmaker 2 %s 1", pos2));
cc->Dispatch(args);


//check focus
UTIL_StringToVector(g_SoundscapePositions[2].Base(), pos2);
if (m_TextEntryPos1->HasFocus())
}
{
//get text
char buf[512];
m_TextEntryPos1->GetText(buf, sizeof(buf));


//convert to vector
//do pos 3
UTIL_StringToVector(g_SoundscapePositions[1].Base(), buf);
if (pos3)
 
//do command
if (cc)
{
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 3 %s 1", pos3));
args.Tokenize(CFmtStr("ssmaker 1 %s 1", buf));
cc->Dispatch(args);
cc->Dispatch(args);
UTIL_StringToVector(g_SoundscapePositions[3].Base(), pos3);
}
}


return;
//do pos 4
}
if (pos4)
 
//check focus
if (m_TextEntryPos2->HasFocus())
{
//get text
char buf[512];
m_TextEntryPos2->GetText(buf, sizeof(buf));
 
//convert to vector
UTIL_StringToVector(g_SoundscapePositions[2].Base(), buf);
 
//do command
if (cc)
{
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 4 %s 1", pos4));
args.Tokenize(CFmtStr("ssmaker 2 %s 1", buf));
cc->Dispatch(args);
cc->Dispatch(args);
UTIL_StringToVector(g_SoundscapePositions[4].Base(), pos4);
}
}


return;
//do pos 5
}
if (pos5)
{
args.Tokenize(CFmtStr("ssmaker 5 %s 1", pos5));
cc->Dispatch(args);
 
UTIL_StringToVector(g_SoundscapePositions[5].Base(), pos5);
}


//check focus
//do pos 6
if (m_TextEntryPos3->HasFocus())
if (pos6)
{
{
//get text
args.Tokenize(CFmtStr("ssmaker 6 %s 1", pos6));
char buf[512];
cc->Dispatch(args);
m_TextEntryPos3->GetText(buf, sizeof(buf));


//convert to vector
UTIL_StringToVector(g_SoundscapePositions[6].Base(), pos6);
UTIL_StringToVector(g_SoundscapePositions[3].Base(), buf);
}


//do command
//do pos 7
if (cc)
if (pos7)
{
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 7 %s", pos7));
args.Tokenize(CFmtStr("ssmaker 3 %s 1", buf));
cc->Dispatch(args);
cc->Dispatch(args);
UTIL_StringToVector(g_SoundscapePositions[7].Base(), pos7);
}
}
return;
}
}


//check focus
//delete settings
if (m_TextEntryPos4->HasFocus())
settings->deleteThis();
{
}
//get text
char buf[512];
m_TextEntryPos4->GetText(buf, sizeof(buf));


//convert to vector
//-----------------------------------------------------------------------------
UTIL_StringToVector(g_SoundscapePositions[4].Base(), buf);
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeSettingsPanel::OnCommand(const char* pszCommand)
{
if (Q_strstr(pszCommand, "GetPos") == pszCommand)
{
//search for number
pszCommand = pszCommand + 6;


//do command
//execute command
static ConCommand* cc = cvar->FindCommand("__ss_maker_start");
if (cc)
if (cc)
{
{
//hide everything first
g_SoundscapeMaker->SetAllVisible(false);
CCommand args;
CCommand args;
args.Tokenize(CFmtStr("ssmaker 4 %s 1", buf));
args.Tokenize(CFmtStr("ssmaker %d", atoi(pszCommand)));
cc->Dispatch(args);
cc->Dispatch(args);
}
}
Line 1,659: Line 1,677:
}
}


//check focus
else if (!Q_strcmp(pszCommand, SETTINGS_PANEL_COMMAND_SHOW))
if (m_TextEntryPos5->HasFocus())
{
{
//get text
static ConVar* cv = cvar->FindVar("__ss_draw");
char buf[512];
if (cv)
m_TextEntryPos5->GetText(buf, sizeof(buf));
cv->SetValue(m_ShowSoundscapePositions->IsSelected());
 
//convert to vector
UTIL_StringToVector(g_SoundscapePositions[5].Base(), buf);
 
//do command
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 5 %s 1", buf));
cc->Dispatch(args);
}


return;
return;
}
}


//check focus
//handle debug thing
if (m_TextEntryPos6->HasFocus())
else if (!Q_strcmp(pszCommand, SETTINGS_PANEL_COMMAND_DEBUG))
{
{
//get text
g_SoundscapeDebugPanel->SetVisible(true);
char buf[512];
g_SoundscapeDebugPanel->RequestFocus();
m_TextEntryPos6->GetText(buf, sizeof(buf));
g_SoundscapeDebugPanel->MoveToFront();
 
return;
//convert to vector
}
UTIL_StringToVector(g_SoundscapePositions[6].Base(), buf);
 
//do command
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 6 %s 1", buf));
cc->Dispatch(args);
}
 
return;
}
 
//check focus
if (m_TextEntryPos7->HasFocus())
{
//get text
char buf[512];
m_TextEntryPos7->GetText(buf, sizeof(buf));
 
//convert to vector
UTIL_StringToVector(g_SoundscapePositions[7].Base(), buf);
 
//do command
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 7 %s 1", buf));
cc->Dispatch(args);
}
 
return;
}


BaseClass::OnCommand(pszCommand);
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Destructor
// Purpose: Constructor
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
CSoundscapeSettingsPanel::~CSoundscapeSettingsPanel()
void CSoundscapeSettingsPanel::SetItem(int index, const Vector& value)
{
{
//save everything
const char* text = CFmtStr("%.3f %.3f %.3f", value.x, value.y, value.z);
KeyValues* settings = new KeyValues("settings");


//get text's
//check index
char text0[64];
switch (index)
char text1[64];
{
char text2[64];
case 0:
char text3[64];
m_TextEntryPos0->RequestFocus();
char text4[64];
m_TextEntryPos0->SetText(text);
char text5[64];
g_SoundscapePositions[0] = value;
char text6[64];
break;
char text7[64];


m_TextEntryPos0->GetText(text0, sizeof(text0));
case 1:
m_TextEntryPos1->GetText(text1, sizeof(text1));
m_TextEntryPos1->RequestFocus();
m_TextEntryPos2->GetText(text2, sizeof(text2));
m_TextEntryPos1->SetText(text);
m_TextEntryPos3->GetText(text3, sizeof(text3));
g_SoundscapePositions[1] = value;
m_TextEntryPos4->GetText(text4, sizeof(text4));
break;
m_TextEntryPos5->GetText(text5, sizeof(text5));
m_TextEntryPos6->GetText(text6, sizeof(text6));
m_TextEntryPos7->GetText(text7, sizeof(text7));


//save text entries
case 2:
settings->SetString("Position0", text0);
m_TextEntryPos2->RequestFocus();
settings->SetString("Position1", text1);
m_TextEntryPos2->SetText(text);
settings->SetString("Position2", text2);
g_SoundscapePositions[2] = value;
settings->SetString("Position3", text3);
break;
settings->SetString("Position4", text4);
case 3:
settings->SetString("Position5", text5);
m_TextEntryPos3->RequestFocus();
settings->SetString("Position6", text6);
m_TextEntryPos3->SetText(text);
settings->SetString("Position7", text7);
g_SoundscapePositions[3] = value;
break;


//save check buttons
case 4:
settings->SetBool("ShowSoundscapes", m_ShowSoundscapePositions->IsSelected());
m_TextEntryPos4->RequestFocus();
m_TextEntryPos4->SetText(text);
g_SoundscapePositions[4] = value;
break;


//save to file
case 5:
settings->SaveToFile(filesystem, "cfg/soundscape_maker.txt", "MOD");
m_TextEntryPos5->RequestFocus();
settings->deleteThis();
m_TextEntryPos5->SetText(text);
}
g_SoundscapePositions[5] = value;
break;


//static soundscape settings panel
case 6:
static CSoundscapeSettingsPanel* g_SettingsPanel = nullptr;
m_TextEntryPos6->RequestFocus();
m_TextEntryPos6->SetText(text);
g_SoundscapePositions[6] = value;
break;


case 7:
m_TextEntryPos7->RequestFocus();
m_TextEntryPos7->SetText(text);
g_SoundscapePositions[7] = value;
break;
}
}


//button
//-----------------------------------------------------------------------------
class CSoundscapeButton : public vgui::Button
// Purpose: Called on text changed
//-----------------------------------------------------------------------------
void CSoundscapeSettingsPanel::OnTextChanged(KeyValues* kv)
{
{
public:
static ConCommand* cc = cvar->FindCommand("__ss_maker_set");
DECLARE_CLASS_SIMPLE(CSoundscapeButton, vgui::Button)


CSoundscapeButton(vgui::Panel* parent, const char* name, const char* text, vgui::Panel* target = nullptr, const char* command = nullptr)
//check focus
: BaseClass(parent, name, text, target, command), m_bIsSelected(false)
if (m_TextEntryPos0->HasFocus())
{
{
m_ColorSelected = Color(200, 200, 200, 200);
//get text
m_FgColorSelected = Color(0, 0, 0, 255);
char buf[512];
}
m_TextEntryPos0->GetText(buf, sizeof(buf));


//apply scheme settings
//convert to vector
void ApplySchemeSettings(vgui::IScheme* scheme)
UTIL_StringToVector(g_SoundscapePositions[0].Base(), buf);
{
 
BaseClass::ApplySchemeSettings(scheme);
//do command
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 0 %s 1", buf));
cc->Dispatch(args);
}


m_ColorNotSelected = GetButtonArmedBgColor();
return;
m_FgColorNotSelectedd = GetButtonArmedFgColor();
}
}


//paints the background
//check focus
void PaintBackground()
if (m_TextEntryPos1->HasFocus())
{
{
if (m_bIsSelected)
//get text
SetBgColor(m_ColorSelected);
char buf[512];
else
m_TextEntryPos1->GetText(buf, sizeof(buf));
SetBgColor(m_ColorNotSelected);


BaseClass::PaintBackground();
//convert to vector
}
UTIL_StringToVector(g_SoundscapePositions[1].Base(), buf);


//paints
//do command
void Paint()
if (cc)
{
{
if (m_bIsSelected)
CCommand args;
SetFgColor(m_FgColorSelected);
args.Tokenize(CFmtStr("ssmaker 1 %s 1", buf));
else
cc->Dispatch(args);
SetFgColor(m_FgColorNotSelectedd);
}


BaseClass::Paint();
return;
}
}


//is this selected or not
//check focus
bool m_bIsSelected;
if (m_TextEntryPos2->HasFocus())
static Color m_ColorSelected;
{
static Color m_ColorNotSelected;
//get text
static Color m_FgColorSelected;
char buf[512];
static Color m_FgColorNotSelectedd;
m_TextEntryPos2->GetText(buf, sizeof(buf));
};


Color CSoundscapeButton::m_ColorSelected = Color();
//convert to vector
Color CSoundscapeButton::m_ColorNotSelected = Color();
UTIL_StringToVector(g_SoundscapePositions[2].Base(), buf);
Color CSoundscapeButton::m_FgColorSelected = Color();
Color CSoundscapeButton::m_FgColorNotSelectedd = Color();


//do command
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 2 %s 1", buf));
cc->Dispatch(args);
}


//soundscape combo box
return;
}


class CSoundListComboBox : public vgui::ComboBox
//check focus
{
if (m_TextEntryPos3->HasFocus())
public:
{
DECLARE_CLASS_SIMPLE(CSoundListComboBox, vgui::ComboBox);
//get text
char buf[512];
m_TextEntryPos3->GetText(buf, sizeof(buf));


CSoundListComboBox(Panel* parent, const char* panelName, int numLines, bool allowEdit) :
//convert to vector
BaseClass(parent, panelName, numLines, allowEdit) {}
UTIL_StringToVector(g_SoundscapePositions[3].Base(), buf);


//on key typed. check for menu item with text inside it and if found then
//do command
//select that item.
if (cc)
void OnKeyTyped(wchar_t unichar)
{
//check for ctrl or shift down
if (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LCONTROL) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RCONTROL) || unichar == '`')
return;
 
//open up this combo box
if (unichar == 13)
{
{
ShowMenu();
CCommand args;
return;
args.Tokenize(CFmtStr("ssmaker 3 %s 1", buf));
cc->Dispatch(args);
}
}


BaseClass::OnKeyTyped(unichar);
return;
 
}
//check for backspace
if (unichar == 8 || unichar == '_')
return;


//check focus
if (m_TextEntryPos4->HasFocus())
{
//get text
//get text
char buf[512];
char buf[512];
GetText(buf, sizeof(buf));
m_TextEntryPos4->GetText(buf, sizeof(buf));


//start from current index + 1
//convert to vector
int start = GetMenu()->GetActiveItem() + 1;
UTIL_StringToVector(g_SoundscapePositions[4].Base(), buf);


//look for sound with same name starting from the start first
//do command
for (int i = start; i < g_SoundDirectories.Count(); i++)
if (cc)
{
{
if (Q_stristr(g_SoundDirectories[i], buf))
CCommand args;
{
args.Tokenize(CFmtStr("ssmaker 4 %s 1", buf));
GetMenu()->SetCurrentlyHighlightedItem(i);
cc->Dispatch(args);
return;
}
}
}


//now cheeck from 0 to the start
return;
for (int i = 0; i < start; i++)
}
{
 
if (Q_stristr(g_SoundDirectories[i], buf))
//check focus
{
if (m_TextEntryPos5->HasFocus())
GetMenu()->SetCurrentlyHighlightedItem(i);
{
return;
//get text
}
char buf[512];
}
m_TextEntryPos5->GetText(buf, sizeof(buf));
}
};


//convert to vector
UTIL_StringToVector(g_SoundscapePositions[5].Base(), buf);


//sounds list panel
//do command
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 5 %s 1", buf));
cc->Dispatch(args);
}


#define SOUND_LIST_PANEL_WIDTH 375
return;
#define SOUND_LIST_PANEL_HEIGHT 255
}
#define SOUND_LIST_PLAY_COMMAND "PlaySound"
#define SOUND_LIST_STOP_COMMAND "StopSound"
#define SOUND_LIST_INSERT_COMMAND "Insert"
#define SOUND_LIST_RELOAD_COMMAND "Reload"
#define SOUND_LIST_SEARCH_COMMAND "Search"


class CSoundListPanel : public vgui::Frame
//check focus
{
if (m_TextEntryPos6->HasFocus())
public:
{
DECLARE_CLASS_SIMPLE(CSoundListPanel, vgui::Frame);
//get text
char buf[512];
m_TextEntryPos6->GetText(buf, sizeof(buf));


CSoundListPanel(vgui::VPANEL parent, const char* name);
//convert to vector
UTIL_StringToVector(g_SoundscapePositions[6].Base(), buf);


//initalizes sound combo box
//do command
void InitalizeSounds();
if (cc)
void InitalizeSoundscapes();
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 6 %s 1", buf));
cc->Dispatch(args);
}


//sets if this is currently using the soundscape panel or sound panel
return;
void SetIsUsingSoundPanel(bool bUsing);
}


//other
//check focus
void OnCommand(const char* pszCommand);
if (m_TextEntryPos7->HasFocus())
void OnClose();
{
//get text
char buf[512];
m_TextEntryPos7->GetText(buf, sizeof(buf));


private:
//convert to vector
friend class CSoundscapeMaker;
UTIL_StringToVector(g_SoundscapePositions[7].Base(), buf);


//are we currently in the 'sound' panel or 'soundscape' panel
//do command
bool bCurrentlyInSoundPanel = true;
if (cc)
{
CCommand args;
args.Tokenize(CFmtStr("ssmaker 7 %s 1", buf));
cc->Dispatch(args);
}


CSoundListComboBox* m_SoundsList; //for sounds
return;
CSoundListComboBox* m_SoundscapesList; //for soundscapes
}
vgui::TextEntry* m_SearchText;
vgui::Button* m_SearchButton;
vgui::Button* m_PlayButton;
vgui::Button* m_StopSoundButton;
vgui::Button* m_InsertButton;
vgui::Button* m_ReloadSounds;


//current sound guid
}
int m_iSongGuid = -1;
};


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Constructor
// Purpose: Destructor
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
CSoundListPanel::CSoundListPanel(vgui::VPANEL parent, const char* name)
CSoundscapeSettingsPanel::~CSoundscapeSettingsPanel()
: BaseClass(nullptr, name)
{
{
SetParent(parent);
//save everything
KeyValues* settings = new KeyValues("settings");


SetKeyBoardInputEnabled(true);
//get text's
SetMouseInputEnabled(true);
char text0[64];
char text1[64];
char text2[64];
char text3[64];
char text4[64];
char text5[64];
char text6[64];
char text7[64];


SetProportional(false);
m_TextEntryPos0->GetText(text0, sizeof(text0));
SetTitleBarVisible(true);
m_TextEntryPos1->GetText(text1, sizeof(text1));
SetMinimizeButtonVisible(false);
m_TextEntryPos2->GetText(text2, sizeof(text2));
SetMaximizeButtonVisible(false);
m_TextEntryPos3->GetText(text3, sizeof(text3));
SetCloseButtonVisible(true);
m_TextEntryPos4->GetText(text4, sizeof(text4));
SetSizeable(false);
m_TextEntryPos5->GetText(text5, sizeof(text5));
SetMoveable(true);
m_TextEntryPos6->GetText(text6, sizeof(text6));
SetVisible(false);
m_TextEntryPos7->GetText(text7, sizeof(text7));


//set the size and pos
//save text entries
int ScreenWide, ScreenTall;
settings->SetString("Position0", text0);
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);
settings->SetString("Position1", text1);
settings->SetString("Position2", text2);
settings->SetString("Position3", text3);
settings->SetString("Position4", text4);
settings->SetString("Position5", text5);
settings->SetString("Position6", text6);
settings->SetString("Position7", text7);


SetTitle("Sounds List", true);
//save check buttons
SetSize(SOUND_LIST_PANEL_WIDTH, SOUND_LIST_PANEL_HEIGHT);
settings->SetBool("ShowSoundscapes", m_ShowSoundscapePositions->IsSelected());
SetPos((ScreenWide - SOUND_LIST_PANEL_WIDTH) / 2, (ScreenTall - SOUND_LIST_PANEL_HEIGHT) / 2);


//create combo box's
//save to file
m_SoundsList = new CSoundListComboBox(this, "SoundsList", 20, true);
settings->SaveToFile(filesystem, "cfg/soundscape_maker.txt", "MOD");
m_SoundsList->SetBounds(5, 25, SOUND_LIST_PANEL_WIDTH - 15, 20);
settings->deleteThis();
m_SoundsList->AddActionSignalTarget(this);
}
m_SoundsList->SetVisible(true);


m_SoundscapesList = new CSoundListComboBox(this, "SoundscapesList", 20, true);
//static soundscape settings panel
m_SoundscapesList->SetBounds(5, 25, SOUND_LIST_PANEL_WIDTH - 15, 20);
static CSoundscapeSettingsPanel* g_SettingsPanel = nullptr;
m_SoundscapesList->AddActionSignalTarget(this);
m_SoundscapesList->SetVisible(false);


//make divider
vgui::Divider* divider1 = new vgui::Divider(this, "Divider");
divider1->SetBounds(-5, 48, SOUND_LIST_PANEL_WIDTH + 10, 2);


//create text
#define BUTTON_MENU_COMMAND_COPY_CLIPBOARD "CopyClipboard"
vgui::Label* label1 = new vgui::Label(this, "FindSound", "Find Sound");
label1->SetBounds(147, 51, 120, 20);


//create text entry
//button
m_SearchText = new vgui::TextEntry(this, "SearchTextEntry");
class CSoundscapeButton : public vgui::Button
m_SearchText->SetBounds(5, 75, SOUND_LIST_PANEL_WIDTH - 15, 20);
{
m_SearchText->SetEnabled(true);
public:
m_SearchText->SetText("");
DECLARE_CLASS_SIMPLE(CSoundscapeButton, vgui::Button)


//create search for button
CSoundscapeButton(vgui::Panel* parent, const char* name, const char* text, vgui::Panel* target = nullptr, const char* command = nullptr, KeyValues* kv = nullptr, SoundscapeClipboardType type = SoundscapeClipboardType::Type_SoundscapeNone)
m_SearchButton = new vgui::Button(this, "SearchButton", "Search For");
: BaseClass(parent, name, text, target, command), m_bIsSelected(false), m_KeyValues(kv), m_KeyValuesType(type)
m_SearchButton->SetBounds(5, 100, SOUND_LIST_PANEL_WIDTH - 15, 20);;
{
m_SearchButton->SetEnabled(true);
m_ColorSelected = Color(200, 200, 200, 200);
m_SearchButton->SetCommand(SOUND_LIST_SEARCH_COMMAND);
m_FgColorSelected = Color(0, 0, 0, 255);
}


//make divider
//apply scheme settings
vgui::Divider* divider2 = new vgui::Divider(this, "Divider");
void ApplySchemeSettings(vgui::IScheme* scheme)
divider2->SetBounds(-5, 124, SOUND_LIST_PANEL_WIDTH + 10, 2);
{
BaseClass::ApplySchemeSettings(scheme);


//create text
m_ColorNotSelected = GetButtonArmedBgColor();
vgui::Label* label2 = new vgui::Label(this, "SoundButtons", "Sound Buttons");
m_FgColorNotSelected = GetButtonArmedFgColor();
label2->SetBounds(140, 127, 120, 20);
}


//create play button
//paints the background
m_PlayButton = new vgui::Button(this, "PlayButton", "Play Sound", this);
void PaintBackground()
m_PlayButton->SetBounds(5, 150, SOUND_LIST_PANEL_WIDTH - 15, 20);
{
m_PlayButton->SetCommand(SOUND_LIST_PLAY_COMMAND);
if (m_bIsSelected)
SetBgColor(m_ColorSelected);
else
SetBgColor(m_ColorNotSelected);


//create stop sound button
BaseClass::PaintBackground();
m_StopSoundButton = new vgui::Button(this, "StopSound", "Stop Sound", this);
}
m_StopSoundButton->SetBounds(5, 175, SOUND_LIST_PANEL_WIDTH - 15, 20);
m_StopSoundButton->SetCommand(SOUND_LIST_STOP_COMMAND);


//create sound insert button
//paints
m_InsertButton = new vgui::Button(this, "InsertSound", "Insert Sound", this);
void Paint()
m_InsertButton->SetBounds(5, 200, SOUND_LIST_PANEL_WIDTH - 15, 20);
{
m_InsertButton->SetCommand(SOUND_LIST_INSERT_COMMAND);
if (m_bIsSelected)
SetFgColor(m_FgColorSelected);
else
SetFgColor(m_FgColorNotSelected);


//create reload sounds button
BaseClass::Paint();
m_ReloadSounds = new vgui::Button(this, "ReloadSounds", "Reload Sounds", this);
}
m_ReloadSounds->SetBounds(5, 225, SOUND_LIST_PANEL_WIDTH - 15, 20);
m_ReloadSounds->SetCommand(SOUND_LIST_RELOAD_COMMAND);
}


//-----------------------------------------------------------------------------
//mouse release
// Purpose: Called on command
void OnMouseReleased(vgui::MouseCode code)
//-----------------------------------------------------------------------------
void CSoundListPanel::OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, SOUND_LIST_SEARCH_COMMAND))
{
{
//get text
if (code != vgui::MouseCode::MOUSE_RIGHT)
char buf[512];
return BaseClass::OnMouseReleased(code);
m_SearchText->GetText(buf, sizeof(buf));
 
//this should never happen but just in case
if (!m_KeyValues)
return;
 
//get cursor pos
int x, y;
vgui::surface()->SurfaceGetCursorPos(x, y);


//check for shift key
//show menu
bool shift = (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LSHIFT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RSHIFT));
vgui::Menu* menu = new vgui::Menu(this, "Clipboard");
menu->AddMenuItem("CopyToClipboard", "Copy", BUTTON_MENU_COMMAND_COPY_CLIPBOARD, this);
menu->SetBounds(x, y, 200, 50);
menu->SetVisible(true);


//vector of texts
BaseClass::Paint();
CUtlVector<char*> SoundNames;
}
CSoundListComboBox* SoundList = bCurrentlyInSoundPanel ? m_SoundsList : m_SoundscapesList;


//if we are in soundscape mode then set the SoundNames to all the soundscapes. else set SoundNames to g_SoundDirectories
//mouse release
if (!bCurrentlyInSoundPanel)
void OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, BUTTON_MENU_COMMAND_COPY_CLIPBOARD))
{
{
for (int i = 0; i < m_SoundscapesList->GetItemCount(); i++)
//create copy of keyvalues
switch (m_KeyValuesType)
{
case SoundscapeClipboardType::Type_SoundscapeName:
{
{
//insert
//copy
char* tmpbuf = new char[512];
if (CurrClipboardName.Count() >= MAX_CLIPBOARD_ITEMS)
m_SoundscapesList->GetItemText(i, tmpbuf, 512);
{
CurrClipboardName[0]->deleteThis();
CurrClipboardName.Remove(0);
}
 
CurrClipboardName.AddToTail(m_KeyValues->MakeCopy());


SoundNames.AddToTail(tmpbuf);
//debug message
SoundscapePrint(Color(255, 255, 255, 255), "Soundscape: '%s' Coppied to clipboard.\n", m_KeyValues->GetName());
break;
}
}
}
case SoundscapeClipboardType::Type_SoundscapeData:
else
{
{
//copy
SoundNames = g_SoundDirectories;
if (CurrClipboardData.Count() >= MAX_CLIPBOARD_ITEMS)
}
{
CurrClipboardData[0]->deleteThis();
CurrClipboardData.Remove(0);
}


if (shift)
//make copy
{
CurrClipboardData.AddToTail(m_KeyValues->MakeCopy());
//start from current index - 1
int start = SoundList->GetMenu()->GetActiveItem() - 1;


//look for sound with same name starting from the start first and going down
//debug message
for (int i = start; i >= 0; i--)
SoundscapePrint(Color(255, 255, 255, 255), "Soundscape Data: '%s' Coppied to clipboard.\n", m_KeyValues->GetName());
break;
}
case SoundscapeClipboardType::Type_SoundscapeRandomWave:
{
{
if (Q_stristr(SoundNames[i], buf))
//copy
if (CurrClipboardRandom.Count() >= MAX_CLIPBOARD_ITEMS)
{
{
//select item
CurrClipboardRandom[0]->deleteThis();
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
CurrClipboardRandom.Remove(0);
SoundList->ActivateItem(i);
}
 
CurrClipboardRandom.AddToTail(m_KeyValues->MakeCopy());


//set text
//debug message
SoundList->SetText(SoundNames[i]);
SoundscapePrint(Color(255, 255, 255, 255), "Soundscape Random Wave: '%s' Coppied to clipboard.\n", m_KeyValues->GetString());
break;
}
}
}
}


//delete all soundscapes if we need to
//is this selected or not
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
bool m_bIsSelected;
delete[] SoundNames[i];
static Color m_ColorSelected;
static Color m_ColorNotSelected;
static Color m_FgColorSelected;
static Color m_FgColorNotSelected;


return;
KeyValues* m_KeyValues = nullptr;
}
SoundscapeClipboardType m_KeyValuesType;
}
};


Color CSoundscapeButton::m_ColorSelected = Color();
Color CSoundscapeButton::m_ColorNotSelected = Color();
Color CSoundscapeButton::m_FgColorSelected = Color();
Color CSoundscapeButton::m_FgColorNotSelected = Color();


//now cheeck from the SoundNames to the start
for (int i = SoundNames.Count() - 1; i > start; i--)
{
if (Q_stristr(SoundNames[i], buf))
{
//select item
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->ActivateItem(i);


//set text
//soundscape combo box
SoundList->SetText(SoundNames[i]);


//delete all soundscapes if we need to
class CSoundListComboBox : public vgui::ComboBox
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
{
delete[] SoundNames[i];
public:
DECLARE_CLASS_SIMPLE(CSoundListComboBox, vgui::ComboBox);


return;
CSoundListComboBox(Panel* parent, const char* panelName, int numLines, bool allowEdit) :
}
BaseClass(parent, panelName, numLines, allowEdit) {}
}
 
}
//on key typed. check for menu item with text inside it and if found then
else
//select that item.
void OnKeyTyped(wchar_t unichar)
{
//check for ctrl or shift down
if (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LCONTROL) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RCONTROL) || unichar == '`')
return;
 
//open up this combo box
if (unichar == 13)
{
{
//start from current index + 1
ShowMenu();
int start = SoundList->GetMenu()->GetActiveItem() + 1;
return;
}


//look for sound with same name starting from the start first
BaseClass::OnKeyTyped(unichar);
for (int i = start; i < SoundNames.Count(); i++)
{
if (Q_stristr(SoundNames[i], buf))
{
//select item
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->ActivateItem(i);


//set text
//check for backspace
SoundList->SetText(SoundNames[i]);
if (unichar == 8 || unichar == '_')
return;


//delete all soundscapes if we need to
//get text
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
char buf[512];
delete[] SoundNames[i];
GetText(buf, sizeof(buf));


return;
//start from current index + 1
}
int start = GetMenu()->GetActiveItem() + 1;
}


//look for sound with same name starting from the start first
for (int i = start; i < g_SoundDirectories.Count(); i++)
{
if (Q_stristr(g_SoundDirectories[i], buf))
{
GetMenu()->SetCurrentlyHighlightedItem(i);
return;
}
}


//now cheeck from 0 to the start
//now cheeck from 0 to the start
for (int i = 0; i < start; i++)
for (int i = 0; i < start; i++)
{
{
if (Q_stristr(SoundNames[i], buf))
if (Q_stristr(g_SoundDirectories[i], buf))
{
{
//select item
GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
return;
SoundList->ActivateItem(i);
 
//set text
SoundList->SetText(SoundNames[i]);
 
//delete all soundscapes if we need to
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
delete[] SoundNames[i];
 
return;
}
}
}
}
}
}
};


//delete all soundscapes if we need to
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
delete[] SoundNames[i];


return;
//sounds list panel
}
else if (!Q_strcmp(pszCommand, SOUND_LIST_PLAY_COMMAND))
{
//get the sound
char buf[512];
m_SoundsList->GetText(buf, sizeof(buf));


//stop the sound
#define SOUND_LIST_PANEL_WIDTH 375
if (enginesound->IsSoundStillPlaying(m_iSongGuid))
#define SOUND_LIST_PANEL_HEIGHT 255
{
#define SOUND_LIST_PLAY_COMMAND "PlaySound"
enginesound->StopSoundByGuid(m_iSongGuid);
#define SOUND_LIST_STOP_COMMAND "StopSound"
m_iSongGuid = -1;
#define SOUND_LIST_INSERT_COMMAND "Insert"
}
#define SOUND_LIST_RELOAD_COMMAND "Reload"
#define SOUND_LIST_SEARCH_COMMAND "Search"


//precache and play the sound
class CSoundListPanel : public vgui::Frame
if (!enginesound->IsSoundPrecached(buf))
{
enginesound->PrecacheSound(buf);
public:
DECLARE_CLASS_SIMPLE(CSoundListPanel, vgui::Frame);


enginesound->EmitAmbientSound(buf, 1, 100);
CSoundListPanel(vgui::VPANEL parent, const char* name);
m_iSongGuid = enginesound->GetGuidForLastSoundEmitted();
return;
}
else if (!Q_strcmp(pszCommand, SOUND_LIST_STOP_COMMAND))
{
//stop the sound
if (m_iSongGuid != -1 && enginesound->IsSoundStillPlaying(m_iSongGuid))
{
enginesound->StopSoundByGuid(m_iSongGuid);
m_iSongGuid = -1;
}


return;
//initalizes sound combo box
}
void InitalizeSounds();
else if (!Q_strcmp(pszCommand, SOUND_LIST_INSERT_COMMAND))
void InitalizeSoundscapes(CUtlVector<const char*>& OtherSoundscapes);
{
//make not visible
SetVisible(false);


//stop the sound
//sets if this is currently using the soundscape panel or sound panel
if (enginesound->IsSoundStillPlaying(m_iSongGuid))
void SetIsUsingSoundPanel(bool bUsing);
{
enginesound->StopSoundByGuid(m_iSongGuid);
m_iSongGuid = -1;
}


//get the sound
//other
char buf[512];
void OnCommand(const char* pszCommand);
void OnClose();


if (bCurrentlyInSoundPanel)
private:
m_SoundsList->GetText(buf, sizeof(buf));
friend class CSoundscapeMaker;
else
m_SoundscapesList->GetText(buf, sizeof(buf));


//set the sound text
//are we currently in the 'sound' panel or 'soundscape' panel
g_SoundscapeMaker->SetSoundText(buf);
bool bCurrentlyInSoundPanel = true;
return;
}
else if (!Q_strcmp(pszCommand, SOUND_LIST_RELOAD_COMMAND))
{
if (bCurrentlyInSoundPanel)
{
//clear everything for the combo box and reload it
m_SoundsList->RemoveAll();
InitalizeSounds();
}
else
{
//clear everything for the combo box and reload it
m_SoundscapesList->RemoveAll();


bool bPrev = g_bSSMHack;
CSoundListComboBox* m_SoundsList; //for sounds
g_bSSMHack = true;
CSoundListComboBox* m_SoundscapesList; //for soundscapes
vgui::TextEntry* m_SearchText;
vgui::Button* m_SearchButton;
vgui::Button* m_PlayButton;
vgui::Button* m_StopSoundButton;
vgui::Button* m_InsertButton;
vgui::Button* m_ReloadSounds;


//reload all the soundscape files
//current sound guid
enginesound->StopAllSounds(true);
int m_iSongGuid = -1;
};


g_SoundscapeSystem.StartNewSoundscape(nullptr);
//-----------------------------------------------------------------------------
g_SoundscapeSystem.RemoveAll();
// Purpose: Constructor
g_SoundscapeSystem. Init();
//-----------------------------------------------------------------------------
CSoundListPanel::CSoundListPanel(vgui::VPANEL parent, const char* name)
: BaseClass(nullptr, name)
{
SetParent(parent);


g_bSSMHack = bPrev;
SetKeyBoardInputEnabled(true);
SetMouseInputEnabled(true);


InitalizeSoundscapes();
SetProportional(false);
}
SetTitleBarVisible(true);
SetMinimizeButtonVisible(false);
SetMaximizeButtonVisible(false);
SetCloseButtonVisible(true);
SetSizeable(false);
SetMoveable(true);
SetVisible(false);


return;
//set the size and pos
}
int ScreenWide, ScreenTall;
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);


BaseClass::OnCommand(pszCommand);
SetTitle("Sounds List", true);
}
SetSize(SOUND_LIST_PANEL_WIDTH, SOUND_LIST_PANEL_HEIGHT);
SetPos((ScreenWide - SOUND_LIST_PANEL_WIDTH) / 2, (ScreenTall - SOUND_LIST_PANEL_HEIGHT) / 2);


//-----------------------------------------------------------------------------
//create combo box's
// Purpose: Called on panel close
m_SoundsList = new CSoundListComboBox(this, "SoundsList", 20, true);
//-----------------------------------------------------------------------------
m_SoundsList->SetBounds(5, 25, SOUND_LIST_PANEL_WIDTH - 15, 20);
void CSoundListPanel::OnClose()
m_SoundsList->AddActionSignalTarget(this);
{
m_SoundsList->SetVisible(true);
OnCommand(SOUND_LIST_STOP_COMMAND);
BaseClass::OnClose();
}


//-----------------------------------------------------------------------------
m_SoundscapesList = new CSoundListComboBox(this, "SoundscapesList", 20, true);
// Purpose: Initalizes the sounds list
m_SoundscapesList->SetBounds(5, 25, SOUND_LIST_PANEL_WIDTH - 15, 20);
//-----------------------------------------------------------------------------
m_SoundscapesList->AddActionSignalTarget(this);
void CSoundListPanel::InitalizeSounds()
m_SoundscapesList->SetVisible(false);
{
//get the sound array
GetSoundNames();


//add all the sounds
//make divider
for (int i = 0; i < g_SoundDirectories.Size(); i++)
vgui::Divider* divider1 = new vgui::Divider(this, "Divider");
m_SoundsList->AddItem(g_SoundDirectories[i], nullptr);
divider1->SetBounds(-5, 48, SOUND_LIST_PANEL_WIDTH + 10, 2);


m_SoundsList->ActivateItem(0);
//create text
}
vgui::Label* label1 = new vgui::Label(this, "FindSound", "Find Sound");
label1->SetBounds(147, 51, 120, 20);


//-----------------------------------------------------------------------------
//create text entry
// Purpose: Initalizes the soundscape list
m_SearchText = new vgui::TextEntry(this, "SearchTextEntry");
//-----------------------------------------------------------------------------
m_SearchText->SetBounds(5, 75, SOUND_LIST_PANEL_WIDTH - 15, 20);
void CSoundListPanel::InitalizeSoundscapes()
m_SearchText->SetEnabled(true);
{
m_SearchText->SetText("");
//add all the soundscapes
for (int i = 0; i < g_SoundscapeSystem.m_soundscapes.Count(); i++)
m_SoundscapesList->AddItem(g_SoundscapeSystem.m_soundscapes[i]->GetName(), nullptr);


m_SoundscapesList->ActivateItem(0);
//create search for button
}
m_SearchButton = new vgui::Button(this, "SearchButton", "Search For");
m_SearchButton->SetBounds(5, 100, SOUND_LIST_PANEL_WIDTH - 15, 20);;
m_SearchButton->SetEnabled(true);
m_SearchButton->SetCommand(SOUND_LIST_SEARCH_COMMAND);


//-----------------------------------------------------------------------------
//make divider
// Purpose: Sets if this panel is currently the sound panel or soundscape
vgui::Divider* divider2 = new vgui::Divider(this, "Divider");
// selector panel.
divider2->SetBounds(-5, 124, SOUND_LIST_PANEL_WIDTH + 10, 2);
//-----------------------------------------------------------------------------
void CSoundListPanel::SetIsUsingSoundPanel(bool bUsing)
{
bCurrentlyInSoundPanel = bUsing;


//disable stuff
//create text
if (bUsing)
vgui::Label* label2 = new vgui::Label(this, "SoundButtons", "Sound Buttons");
{
label2->SetBounds(140, 127, 120, 20);
//set 'reload' text
m_ReloadSounds->SetText("Reload Sounds");


m_SoundscapesList->SetVisible(false);
//create play button
m_SoundsList->SetVisible(true);
m_PlayButton = new vgui::Button(this, "PlayButton", "Play Sound", this);
m_PlayButton->SetBounds(5, 150, SOUND_LIST_PANEL_WIDTH - 15, 20);
m_PlayButton->SetCommand(SOUND_LIST_PLAY_COMMAND);


//enable the play button
//create stop sound button
m_PlayButton->SetEnabled(true);
m_StopSoundButton = new vgui::Button(this, "StopSound", "Stop Sound", this);
m_StopSoundButton->SetEnabled(true);
m_StopSoundButton->SetBounds(5, 175, SOUND_LIST_PANEL_WIDTH - 15, 20);
}
m_StopSoundButton->SetCommand(SOUND_LIST_STOP_COMMAND);
else
{
//set 'reload' text
m_ReloadSounds->SetText("Reload Soundscapes");


m_SoundscapesList->SetVisible(true);
//create sound insert button
m_SoundsList->SetVisible(false);
m_InsertButton = new vgui::Button(this, "InsertSound", "Insert Sound", this);
m_InsertButton->SetBounds(5, 225, SOUND_LIST_PANEL_WIDTH - 15, 20);
m_InsertButton->SetCommand(SOUND_LIST_INSERT_COMMAND);


//disable the play button
//create reload sounds button
m_PlayButton->SetEnabled(false);
m_ReloadSounds = new vgui::Button(this, "ReloadSounds", "Reload Sounds", this);
m_StopSoundButton->SetEnabled(false);
m_ReloadSounds->SetBounds(5, 200, SOUND_LIST_PANEL_WIDTH - 15, 20);
}
m_ReloadSounds->SetCommand(SOUND_LIST_RELOAD_COMMAND);
}
}


//static sound list instance
//-----------------------------------------------------------------------------
static CSoundListPanel* g_SoundPanel = nullptr;
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundListPanel::OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, SOUND_LIST_SEARCH_COMMAND))
{
//get text
char buf[512];
m_SearchText->GetText(buf, sizeof(buf));


//check for shift key
bool shift = (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LSHIFT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RSHIFT));


//soundscape list
//vector of texts
CUtlVector<char*> SoundNames;
CSoundListComboBox* SoundList = bCurrentlyInSoundPanel ? m_SoundsList : m_SoundscapesList;


//if we are in soundscape mode then set the SoundNames to all the soundscapes. else set SoundNames to g_SoundDirectories
if (!bCurrentlyInSoundPanel)
{
for (int i = 0; i < m_SoundscapesList->GetItemCount(); i++)
{
//insert
char* tmpbuf = new char[512];
m_SoundscapesList->GetItemText(i, tmpbuf, 512);


#define ADD_SOUNDSCAPE_COMMAND "AddSoundscape"
SoundNames.AddToTail(tmpbuf);
}
}
else
{
SoundNames = g_SoundDirectories;
}


if (shift)
{
//start from current index - 1
int start = SoundList->GetMenu()->GetActiveItem() - 1;


//soundscape list class
//look for sound with same name starting from the start first and going down
class CSoundscapeList : public vgui::Divider
for (int i = start; i >= 0; i--)
{
{
public:
if (Q_stristr(SoundNames[i], buf))
DECLARE_CLASS_SIMPLE(CSoundscapeList, vgui::Divider);
{
//select item
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->ActivateItem(i);


//constructor
//set text
CSoundscapeList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height);
SoundList->SetText(SoundNames[i]);


//menu item stuff
//delete all soundscapes if we need to
virtual void AddButton(const char* name, const char* text, const char* command, vgui::Panel* parent);
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
virtual void Clear();
delete[] SoundNames[i];


//other
return;
virtual void OnMouseWheeled(int delta);
}
virtual void OnMouseReleased(vgui::MouseCode code);
}


virtual void OnCommand(const char* pszCommand);
virtual void PaintBackground();


virtual void OnKeyCodePressed(vgui::KeyCode code);
//now cheeck from the SoundNames to the start
for (int i = SoundNames.Count() - 1; i > start; i--)
{
if (Q_stristr(SoundNames[i], buf))
{
//select item
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->ActivateItem(i);


//message funcs
//set text
MESSAGE_FUNC_INT(ScrollBarMoved, "ScrollBarSliderMoved", position);
SoundList->SetText(SoundNames[i]);


protected:
//delete all soundscapes if we need to
friend class CSoundscapeMaker;
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
delete[] SoundNames[i];


//keyvalue list.
return;
KeyValues* m_Keyvalues = nullptr;
}
}
}
else
{
//start from current index + 1
int start = SoundList->GetMenu()->GetActiveItem() + 1;


//says "Soundscapes List"
//look for sound with same name starting from the start first
vgui::Label* m_pLabel;
for (int i = start; i < SoundNames.Count(); i++)
vgui::ScrollBar* m_pSideSlider;
{
if (Q_stristr(SoundNames[i], buf))
{
//select item
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->ActivateItem(i);


//menu
//set text
vgui::Menu* menu;
SoundList->SetText(SoundNames[i]);


//menu button stuff
//delete all soundscapes if we need to
CUtlVector<CSoundscapeButton*> m_MenuButtons;
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
int m_iCurrentY;
delete[] SoundNames[i];
int m_iMax;
int m_AmtAdded;
};


//-----------------------------------------------------------------------------
return;
// Purpose: Constructor for soundscape list panel
}
//-----------------------------------------------------------------------------
}
CSoundscapeList::CSoundscapeList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
: BaseClass(parent, name)
{
//create the text
m_pLabel = new vgui::Label(this, "ListsText", text);
m_pLabel->SetVisible(true);
m_pLabel->SetBounds(text_x_pos, 2, 150, 20);


//create the side slider
m_pSideSlider = new vgui::ScrollBar(this, "ListsSlider", true);
m_pSideSlider->SetBounds(width - 20, 0, 20, height - 2);
m_pSideSlider->SetValue(0);
m_pSideSlider->SetEnabled(false);
m_pSideSlider->SetRange(0, 0);
m_pSideSlider->SetButtonPressedScrollValue(1);
m_pSideSlider->SetRangeWindow(0);
m_pSideSlider->AddActionSignalTarget(this);


m_iCurrentY = 22;
//now cheeck from 0 to the start
m_iMax = max;
for (int i = 0; i < start; i++)
m_Keyvalues = nullptr;
{
}
if (Q_stristr(SoundNames[i], buf))
{
//select item
SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
SoundList->ActivateItem(i);


//-----------------------------------------------------------------------------
//set text
// Purpose: adds a button to the soundscape list
SoundList->SetText(SoundNames[i]);
//-----------------------------------------------------------------------------
void CSoundscapeList::AddButton(const char* name, const char* text, const char* command, vgui::Panel* parent)
{
//create a new button
CSoundscapeButton* button = new CSoundscapeButton(this, name, text, parent, command);
button->SetBounds(5, m_iCurrentY, GetWide() - 30, 20);


//increment current y
//delete all soundscapes if we need to
m_iCurrentY = m_iCurrentY + 22;
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
delete[] SoundNames[i];


//add button to array
return;
m_MenuButtons.AddToTail(button);
}
}
}


//if the count is more then m_iMax then set slider value
//delete all soundscapes if we need to
if (m_MenuButtons.Count() > m_iMax)
if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
{
delete[] SoundNames[i];
int max = m_MenuButtons.Count() - m_iMax;


m_pSideSlider->SetRange(0, max);
return;
m_pSideSlider->SetRangeWindow(1);
m_pSideSlider->SetEnabled(true);
}
}
else if (!Q_strcmp(pszCommand, SOUND_LIST_PLAY_COMMAND))
{
//get the sound
char buf[512];
m_SoundsList->GetText(buf, sizeof(buf));


m_AmtAdded++;
//stop the sound
if (enginesound->IsSoundStillPlaying(m_iSongGuid))
{
enginesound->StopSoundByGuid(m_iSongGuid);
m_iSongGuid = -1;
}


//check to see if we need to scroll down
//precache and play the sound
if (m_MenuButtons.Count() >= m_iMax)
if (!enginesound->IsSoundPrecached(buf))
OnMouseWheeled(-1);
enginesound->PrecacheSound(buf);
}


//-----------------------------------------------------------------------------
enginesound->EmitAmbientSound(buf, 1, 100);
// Purpose: Clears everything for this list
m_iSongGuid = enginesound->GetGuidForLastSoundEmitted();
//-----------------------------------------------------------------------------
return;
void CSoundscapeList::Clear()
}
{
else if (!Q_strcmp(pszCommand, SOUND_LIST_STOP_COMMAND))
//reset the slider
{
m_pSideSlider->SetValue(0);
//stop the sound
m_pSideSlider->SetEnabled(false);
if (m_iSongGuid != -1 && enginesound->IsSoundStillPlaying(m_iSongGuid))
m_pSideSlider->SetRange(0, 0);
{
m_pSideSlider->SetButtonPressedScrollValue(1);
enginesound->StopSoundByGuid(m_iSongGuid);
m_pSideSlider->SetRangeWindow(0);
m_iSongGuid = -1;
}


//delete and clear the buttons
return;
for (int i = 0; i < m_MenuButtons.Count(); i++)
}
m_MenuButtons[i]->DeletePanel();
else if (!Q_strcmp(pszCommand, SOUND_LIST_INSERT_COMMAND))
{
//make not visible
SetVisible(false);


m_MenuButtons.RemoveAll();
//stop the sound
if (enginesound->IsSoundStillPlaying(m_iSongGuid))
{
enginesound->StopSoundByGuid(m_iSongGuid);
m_iSongGuid = -1;
}


//reset current y
//get the sound
m_iCurrentY = 22;
char buf[512];


m_AmtAdded = 0;
if (bCurrentlyInSoundPanel)
}
m_SoundsList->GetText(buf, sizeof(buf));
else
m_SoundscapesList->GetText(buf, sizeof(buf));


//-----------------------------------------------------------------------------
//set the sound text
// Purpose: Called when a mouse is wheeled
g_SoundscapeMaker->SetSoundText(buf);
//-----------------------------------------------------------------------------
return;
void CSoundscapeList::OnMouseWheeled(int delta)
}
{
else if (!Q_strcmp(pszCommand, SOUND_LIST_RELOAD_COMMAND))
//check for scroll down
{
if (delta == -1)
if (bCurrentlyInSoundPanel)
m_pSideSlider->SetValue(m_pSideSlider->GetValue() + 1);
{
//clear everything for the combo box and reload it
m_SoundsList->RemoveAll();
InitalizeSounds();
}
else
{
//clear everything for the combo box and reload it
m_SoundscapesList->RemoveAll();


//check for scroll up
bool bPrev = g_bSSMHack;
else if (delta == 1)
g_bSSMHack = true;
m_pSideSlider->SetValue(m_pSideSlider->GetValue() - 1);
}


//-----------------------------------------------------------------------------
//reload all the soundscape files
// Purpose: Called when a mouse code is released
enginesound->StopAllSounds(true);
//-----------------------------------------------------------------------------
void CSoundscapeList::OnMouseReleased(vgui::MouseCode code)
{
if (code != vgui::MouseCode::MOUSE_RIGHT)
return;


//get cursor pos
g_SoundscapeSystem.StartNewSoundscape(nullptr);
int x, y;
g_SoundscapeSystem.RemoveAll();
vgui::surface()->SurfaceGetCursorPos(x, y);
g_SoundscapeSystem. Init();


//create menu
g_bSSMHack = bPrev;
menu = new vgui::Menu(this, "Menu");
menu->AddMenuItem("AddSoundscape", "Add Soundscape", ADD_SOUNDSCAPE_COMMAND, this);
menu->SetBounds(x, y, 200, 50);
menu->SetVisible(true);
}


//-----------------------------------------------------------------------------
//load all the temporary soundscapes
// Purpose: Called on command
CUtlVector<const char*> OtherSoundscapes;
//-----------------------------------------------------------------------------
for (KeyValues* curr = g_SoundscapeMaker->GetPanelFile(); curr; curr = curr->GetNextKey())
void CSoundscapeList::OnCommand(const char* pszCommand)
{
{
if (curr == g_SoundscapeMaker->GetPanelSelected())
if (!Q_strcmp(pszCommand, ADD_SOUNDSCAPE_COMMAND))
continue;
{
const char* name = CFmtStr("New Soundscape %d", m_AmtAdded);
AddButton(name, name, name, GetParent());


//add to keyvalues file
OtherSoundscapes.AddToTail(curr->GetName());
KeyValues* kv = new KeyValues(name);
}
KeyValues* tmp = m_Keyvalues;
KeyValues* tmp2 = tmp;


//get last subkey
InitalizeSoundscapes(OtherSoundscapes);
while (tmp != nullptr)
{
tmp2 = tmp;
tmp = tmp->GetNextTrueSubKey();
}
}


//add to last subkey
tmp2->SetNextKey(kv);
GetParent()->OnCommand(name);
return;
return;
}
}
Line 2,525: Line 2,558:


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Paints the background
// Purpose: Called on panel close
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeList::PaintBackground()
void CSoundListPanel::OnClose()
{
{
//colors
OnCommand(SOUND_LIST_STOP_COMMAND);
static Color EnabledColor = Color(100, 100, 100, 200);
BaseClass::OnClose();
static Color DisabledColor = Color(60, 60, 60, 200);
}


//if m_KeyValues then paint the default color
//-----------------------------------------------------------------------------
if (m_Keyvalues)
// Purpose: Initalizes the sounds list
SetBgColor(EnabledColor);
//-----------------------------------------------------------------------------
else
void CSoundListPanel::InitalizeSounds()
SetBgColor(DisabledColor);
{
//get the sound array
GetSoundNames();
 
//add all the sounds
for (int i = 0; i < g_SoundDirectories.Size(); i++)
m_SoundsList->AddItem(g_SoundDirectories[i], nullptr);


BaseClass::PaintBackground();
m_SoundsList->ActivateItem(0);
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called on keyboard code pressed
// Purpose: Initalizes the soundscape list
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeList::OnKeyCodePressed(vgui::KeyCode code)
void CSoundListPanel::InitalizeSoundscapes(CUtlVector<const char*>& OtherSoundscapes)
{
{
//check for arrow
//remove everything
if (code == KEY_UP)
m_SoundscapesList->RemoveAll();
 
//add all the soundscapes
for (int i = 0; i < g_SoundscapeSystem.m_soundscapes.Count(); i++)
OtherSoundscapes.AddToTail(g_SoundscapeSystem.m_soundscapes[i]->GetName());
 
OtherSoundscapes.Sort(VectorSortFunc);
 
//quickly remove duplicatesd
for (int i = 1; i < OtherSoundscapes.Count(); )
{
{
//find selected item
if (!Q_strcmp(OtherSoundscapes[i], OtherSoundscapes[i - 1]))
for (int i = 0; i < m_MenuButtons.Count(); i++)
{
{
if (m_MenuButtons[i]->m_bIsSelected)
OtherSoundscapes.Remove(i);
{
continue;
//check for size and to see if we can select item
if (i - 1 < 0)
return;
 
//select that item
GetParent()->OnCommand(m_MenuButtons[i - 1]->GetCommand()->GetString("command"));
return;
}
}
}
i++;
}
}


//check for arrow
for (int i = 0; i < OtherSoundscapes.Size(); i++)
if (code == KEY_DOWN)
m_SoundscapesList->AddItem(OtherSoundscapes[i], nullptr);
{
//find selected item
for (int i = 0; i < m_MenuButtons.Count(); i++)
{
if (m_MenuButtons[i]->m_bIsSelected)
{
//check for size and to see if we can select item
if (i + 1 >= m_MenuButtons.Count())
return;


//select that item
m_SoundscapesList->ActivateItem(0);
GetParent()->OnCommand(m_MenuButtons[i + 1]->GetCommand()->GetString("command"));
return;
}
}
}
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called on scroll bar moved
// Purpose: Sets if this panel is currently the sound panel or soundscape
// selector panel.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeList::ScrollBarMoved(int delta)
void CSoundListPanel::SetIsUsingSoundPanel(bool bUsing)
{
{
int position = m_pSideSlider->GetValue();
bCurrentlyInSoundPanel = bUsing;


//move everything down (if needed)
//disable stuff
for (int i = 0; i < m_MenuButtons.Count(); i++)
if (bUsing)
{
{
//make not visible if i < position
//set 'reload' text
if (i < position)
m_ReloadSounds->SetText("Reload Sounds");
{
m_MenuButtons[i]->SetVisible(false);
continue;
}


m_MenuButtons[i]->SetPos(5, 22 * ((i - position) + 1));
m_SoundscapesList->SetVisible(false);
m_MenuButtons[i]->SetVisible(true);
m_SoundsList->SetVisible(true);
}
}


//enable the play button
m_PlayButton->SetEnabled(true);
m_StopSoundButton->SetEnabled(true);


//set texts
m_PlayButton->SetText("Play Sound");
m_StopSoundButton->SetText("Stop Sound");
m_InsertButton->SetText("Insert Sound");


//set title
SetTitle("Sounds List", true);
}
else
{
//set 'reload' text
m_ReloadSounds->SetText("Reload Soundscapes");


//soundscape data list
m_SoundscapesList->SetVisible(true);
m_SoundsList->SetVisible(false);


//disable the play button
m_PlayButton->SetEnabled(false);
m_StopSoundButton->SetEnabled(false);


#define NEW_PLAYLOOPING_COMMAND "NewLooping"
//set texts
#define NEW_SOUNDSCAPE_COMMAND "NewSoundscape"
m_PlayButton->SetText("Play Soundscape");
#define NEW_RANDOM_COMMAND "NewRandom"
m_StopSoundButton->SetText("Stop Soundscape");
m_InsertButton->SetText("Insert Soundscape");
 
//set stuff
SetTitle("Soundscape List", true);
}
}
 
//static sound list instance
static CSoundListPanel* g_SoundPanel = nullptr;
 
 
//soundscape list
 
 
#define ADD_SOUNDSCAPE_COMMAND "AddSoundscape"
#define PASTE_FROM_CLIBOARD_COMMAND "PasteFromClipboard"
#define OPEN_CLIBOARD_COMMAND "OpenClipboard"




class CSoundscapeDataList : public CSoundscapeList
//soundscape list class
class CSoundscapeList : public vgui::Divider
{
{
public:
public:
DECLARE_CLASS_SIMPLE(CSoundscapeDataList, CSoundscapeList);
DECLARE_CLASS_SIMPLE(CSoundscapeList, vgui::Divider);
 
//constructor
CSoundscapeList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height);


CSoundscapeDataList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
//menu item stuff
: CSoundscapeList(parent, name, text, text_x_pos, max, width, height)
virtual void AddButton(const char* name, const char* text, const char* command, vgui::Panel* parent, KeyValues* add, SoundscapeClipboardType type);
{}
virtual void Clear();


//override right click functionality
//other
virtual void OnMouseWheeled(int delta);
virtual void OnMouseReleased(vgui::MouseCode code);
virtual void OnMouseReleased(vgui::MouseCode code);


void OnCommand(const char* pszCommand);
virtual void OnCommand(const char* pszCommand);
virtual void PaintBackground();
 
virtual void OnKeyCodeReleased(vgui::KeyCode code);
 
//message funcs
MESSAGE_FUNC_INT(ScrollBarMoved, "ScrollBarSliderMoved", position);


private:
protected:
friend class CSoundscapeMaker;
friend class CSoundscapeMaker;
};


//keyvalue list.
KeyValues* m_Keyvalues = nullptr;


//-----------------------------------------------------------------------------
//says "Soundscapes List"
// Purpose: Called when a mouse code is released
vgui::Label* m_pLabel;
//-----------------------------------------------------------------------------
vgui::ScrollBar* m_pSideSlider;
void CSoundscapeDataList::OnMouseReleased(vgui::MouseCode code)
{
//if no soundscape is selected or mouse code != right then return
if (code != vgui::MouseCode::MOUSE_RIGHT || !m_Keyvalues)
return;


//get cursor pos
//menu
int x, y;
vgui::Menu* menu;
vgui::surface()->SurfaceGetCursorPos(x, y);
 
//create menu
menu = new vgui::Menu(this, "Menu");
menu->AddMenuItem("AddLooping", "Add Looping Sound", NEW_PLAYLOOPING_COMMAND, this);
menu->AddMenuItem("AddSoundscape", "Add Soundscape", NEW_SOUNDSCAPE_COMMAND, this);
menu->AddMenuItem("AddSoundscape", "Add Random Sounds", NEW_RANDOM_COMMAND, this);
menu->SetBounds(x, y, 200, 50);
menu->SetVisible(true);
}


//menu button stuff
CUtlVector<CSoundscapeButton*> m_MenuButtons;
int m_iCurrentY;
int m_iMax;
int m_AmtAdded;
};


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called on command
// Purpose: Constructor for soundscape list panel
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeDataList::OnCommand(const char* pszCommand)
CSoundscapeList::CSoundscapeList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
: BaseClass(parent, name)
{
{
if (!Q_strcmp(pszCommand, NEW_PLAYLOOPING_COMMAND))
//create the text
{
m_pLabel = new vgui::Label(this, "ListsText", text);
int LoopingNum = 0;
m_pLabel->SetVisible(true);
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
m_pLabel->SetBounds(text_x_pos, 2, 150, 20);
{
//store data name
const char* name = data->GetName();


//increment variables based on name
//create the side slider
if (!Q_strcasecmp(name, "playlooping"))
m_pSideSlider = new vgui::ScrollBar(this, "ListsSlider", true);
LoopingNum++;
m_pSideSlider->SetBounds(width - 20, 0, 20, height - 2);
}
m_pSideSlider->SetValue(0);
m_pSideSlider->SetEnabled(false);
m_pSideSlider->SetRange(0, 0);
m_pSideSlider->SetButtonPressedScrollValue(1);
m_pSideSlider->SetRangeWindow(0);
m_pSideSlider->AddActionSignalTarget(this);


//add the keyvalue to both this and the keyvalues
m_iCurrentY = 22;
AddButton("playlooping", "playlooping", CFmtStr("$playlooping%d", LoopingNum + 1), GetParent());
m_iMax = max;
m_Keyvalues = nullptr;
}


//add the keyvalues
//-----------------------------------------------------------------------------
KeyValues* kv = new KeyValues("playlooping");
// Purpose: adds a button to the soundscape list
kv->SetFloat("volume", 1);
//-----------------------------------------------------------------------------
kv->SetInt("pitch", 100);
void CSoundscapeList::AddButton(const char* name, const char* text, const char* command, vgui::Panel* parent, KeyValues* add, SoundscapeClipboardType type)
{
//create a new button
CSoundscapeButton* button = new CSoundscapeButton(this, name, text, parent, command, add, type);
button->SetBounds(5, m_iCurrentY, GetWide() - 30, 20);


m_Keyvalues->AddSubKey(kv);
//increment current y
m_iCurrentY = m_iCurrentY + 22;


GetParent()->OnCommand(CFmtStr("$playlooping%d", LoopingNum + 1));
//add button to array
m_MenuButtons.AddToTail(button);


return;
//if the count is more then m_iMax then set slider value
}
if (m_MenuButtons.Count() > m_iMax)
else if (!Q_strcmp(pszCommand, NEW_SOUNDSCAPE_COMMAND))
{
{
int SoundscapeNum = 0;
int max = m_MenuButtons.Count() - m_iMax;
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
{
//store data name
const char* name = data->GetName();


//increment variables based on name
m_pSideSlider->SetRange(0, max);
if (!Q_strcasecmp(name, "playsoundscape"))
m_pSideSlider->SetRangeWindow(1);
SoundscapeNum++;
m_pSideSlider->SetEnabled(true);
}
}


AddButton("playsoundscape", "playsoundscape", CFmtStr("$playsoundscape%d", SoundscapeNum + 1), GetParent());
m_AmtAdded++;


//add the keyvalues
//check to see if we need to scroll down
KeyValues* kv = new KeyValues("playsoundscape");
if (m_MenuButtons.Count() >= m_iMax)
kv->SetFloat("volume", 1);
OnMouseWheeled(-1);
}


//add the keyvalue to both this and the keyvalues
//-----------------------------------------------------------------------------
m_Keyvalues->AddSubKey(kv);
// Purpose: Clears everything for this list
//-----------------------------------------------------------------------------
void CSoundscapeList::Clear()
{
//reset the slider
m_pSideSlider->SetValue(0);
m_pSideSlider->SetEnabled(false);
m_pSideSlider->SetRange(0, 0);
m_pSideSlider->SetButtonPressedScrollValue(1);
m_pSideSlider->SetRangeWindow(0);


GetParent()->OnCommand(CFmtStr("$playsoundscape%d", SoundscapeNum + 1));
//delete and clear the buttons
for (int i = 0; i < m_MenuButtons.Count(); i++)
m_MenuButtons[i]->DeletePanel();


return;
m_MenuButtons.RemoveAll();
}
else if (!Q_strcmp(pszCommand, NEW_RANDOM_COMMAND))
{
int RandomNum = 0;
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
{
//store data name
const char* name = data->GetName();


//increment variables based on name
//reset current y
if (!Q_strcasecmp(name, "playrandom"))
m_iCurrentY = 22;
RandomNum++;
}


AddButton("playrandom", "playrandom", CFmtStr("$playrandom%d", RandomNum + 1), GetParent());
m_AmtAdded = 0;
}


//add the keyvalues
//-----------------------------------------------------------------------------
KeyValues* kv = new KeyValues("playrandom");
// Purpose: Called when a mouse is wheeled
kv->SetString("volume", "0.5,0.8");
//-----------------------------------------------------------------------------
kv->SetInt("pitch", 100);
void CSoundscapeList::OnMouseWheeled(int delta)
kv->SetString("time", "10,20");
{
//check for scroll down
if (delta == -1)
m_pSideSlider->SetValue(m_pSideSlider->GetValue() + 1);
 
//check for scroll up
else if (delta == 1)
m_pSideSlider->SetValue(m_pSideSlider->GetValue() - 1);
}


//make rndwave subkey
//-----------------------------------------------------------------------------
KeyValues* rndwave = new KeyValues("rndwave");
// Purpose: Called when a mouse code is released
kv->AddSubKey(rndwave);
//-----------------------------------------------------------------------------
void CSoundscapeList::OnMouseReleased(vgui::MouseCode code)
{
if (code != vgui::MouseCode::MOUSE_RIGHT)
return;


//add the keyvalue to both this and the keyvalues
//get cursor pos
m_Keyvalues->AddSubKey(kv);
int x, y;
vgui::surface()->SurfaceGetCursorPos(x, y);


//make the parent show the new item
//create menu
GetParent()->OnCommand(CFmtStr("$playrandom%d", RandomNum + 1));
menu = new vgui::Menu(this, "Menu");
menu->AddMenuItem("AddSoundscape", "Add Soundscape", ADD_SOUNDSCAPE_COMMAND, this);


return;
//check clipboard item
if (CurrClipboardName.Count() > 0)
{
menu->AddSeparator();
menu->AddMenuItem("PasteFromClipboard", "Paste", PASTE_FROM_CLIBOARD_COMMAND, this);
menu->AddMenuItem("OpenClipboard", "Open Clipboard", OPEN_CLIBOARD_COMMAND, this);
}
}


BaseClass::OnCommand(pszCommand);
menu->SetBounds(x, y, 200, 50);
menu->SetVisible(true);
}
}


//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeList::OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, ADD_SOUNDSCAPE_COMMAND))
{
const char* name = CFmtStr("New Soundscape %d", m_AmtAdded);


//soundscape rndwave data list
//add to keyvalues file
KeyValues* kv = new KeyValues(name);
KeyValues* tmp = m_Keyvalues;
KeyValues* tmp2 = tmp;
 
AddButton(name, name, name, GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeName);


//get last subkey
while (tmp != nullptr)
{
tmp2 = tmp;
tmp = tmp->GetNextTrueSubKey();
}


#define NEW_RNDWAVE_WAVE_COMMAND "NewRNDWave"
//add to last subkey
tmp2->SetNextKey(kv);


GetParent()->OnCommand(name);
return;
}
else if (!Q_strcmp(pszCommand, PASTE_FROM_CLIBOARD_COMMAND))
{
int index = CurrClipboardName.Count() - 1;


class CSoundscapeRndwaveList : public CSoundscapeList
const char* name = CFmtStr("%s - (Copy %d)", CurrClipboardName[index]->GetName(), m_AmtAdded);
{
public:
DECLARE_CLASS_SIMPLE(CSoundscapeDataList, CSoundscapeList);


CSoundscapeRndwaveList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
//add to keyvalues file
: CSoundscapeList(parent, name, text, text_x_pos, max, width, height)
KeyValues* kv = new KeyValues(name);
{}
CurrClipboardName[index]->CopySubkeys(kv);


//override right click functionality
KeyValues* tmp = m_Keyvalues;
virtual void OnMouseReleased(vgui::MouseCode code);
KeyValues* tmp2 = tmp;


void OnCommand(const char* pszCommand);
AddButton(name, name, name, GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeName);


private:
//get last subkey
friend class CSoundscapeMaker;
while (tmp != nullptr)
};
{
tmp2 = tmp;
tmp = tmp->GetNextTrueSubKey();
}


//add to last subkey
tmp2->SetNextKey(kv);


//-----------------------------------------------------------------------------
GetParent()->OnCommand(name);
// Purpose: Called when a mouse code is released
//-----------------------------------------------------------------------------
void CSoundscapeRndwaveList::OnMouseReleased(vgui::MouseCode code)
{
//if no soundscape is selected or mouse code != right then return
if (code != vgui::MouseCode::MOUSE_RIGHT || !m_Keyvalues)
return;
return;
}
else if (!Q_strcmp(pszCommand, OPEN_CLIBOARD_COMMAND))
{
if (g_SoundscapeClipboard)
g_SoundscapeClipboard->DeletePanel();


//get cursor pos
g_SoundscapeClipboard = new CSoundscapeClipboard(SoundscapeClipboardType::Type_SoundscapeName);
int x, y;
return;
vgui::surface()->SurfaceGetCursorPos(x, y);
}


//create menu
BaseClass::OnCommand(pszCommand);
menu = new vgui::Menu(this, "Menu");
menu->AddMenuItem("AddRandom", "Add Random Wave", NEW_RNDWAVE_WAVE_COMMAND, this);
menu->SetBounds(x, y, 200, 50);
menu->SetVisible(true);
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called on command
// Purpose: Paints the background
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeRndwaveList::OnCommand(const char* pszCommand)
void CSoundscapeList::PaintBackground()
{
{
if (!Q_strcmp(pszCommand, NEW_RNDWAVE_WAVE_COMMAND) && m_Keyvalues)
//colors
{
static Color EnabledColor = Color(100, 100, 100, 200);
//get number of keyvalues
static Color DisabledColor = Color(60, 60, 60, 200);
int num = 0;


FOR_EACH_VALUE(m_Keyvalues, kv)
//if m_KeyValues then paint the default color
num++;
if (m_Keyvalues)
SetBgColor(EnabledColor);
else
SetBgColor(DisabledColor);


//add keyvalues and button
BaseClass::PaintBackground();
AddButton("Rndwave", "", CFmtStr("$rndwave%d", num + 1), GetParent());
}


KeyValues* add = new KeyValues("wave");
//-----------------------------------------------------------------------------
add->SetString(nullptr, "");
// Purpose: Called on keyboard code pressed
m_Keyvalues->AddSubKey(add);
//-----------------------------------------------------------------------------
 
void CSoundscapeList::OnKeyCodeReleased(vgui::KeyCode code)
//forward command to parent
{
GetParent()->OnCommand(CFmtStr("$rndwave%d", num + 1));
//check for arrow
if (code == KEY_UP)
{
//find selected item
for (int i = 0; i < m_MenuButtons.Count(); i++)
{
if (m_MenuButtons[i]->m_bIsSelected)
{
//check for size and to see if we can select item
if (i - 1 < 0)
return;


return;
//select that item
GetParent()->OnCommand(m_MenuButtons[i - 1]->GetCommand()->GetString("command"));
return;
}
}
}
}


BaseClass::OnCommand(pszCommand);
//check for arrow
if (code == KEY_DOWN)
{
//find selected item
for (int i = 0; i < m_MenuButtons.Count(); i++)
{
if (m_MenuButtons[i]->m_bIsSelected)
{
//check for size and to see if we can select item
if (i + 1 >= m_MenuButtons.Count())
return;
 
//select that item
GetParent()->OnCommand(m_MenuButtons[i + 1]->GetCommand()->GetString("command"));
return;
}
}
}
}
}


//-----------------------------------------------------------------------------
// Purpose: Called on scroll bar moved
//-----------------------------------------------------------------------------
void CSoundscapeList::ScrollBarMoved(int delta)
{
int position = m_pSideSlider->GetValue();


 
//move everything down (if needed)
//soundscape panel
for (int i = 0; i < m_MenuButtons.Count(); i++)
{
//make not visible if i < position
if (i < position)
{
m_MenuButtons[i]->SetVisible(false);
continue;
}
 
m_MenuButtons[i]->SetPos(5, 22 * ((i - position) + 1));
m_MenuButtons[i]->SetVisible(true);
}
}




#define SOUNDSCAPE_PANEL_WIDTH 760
#define SOUNDSCAPE_PANEL_HEIGHT 630


#define NEW_BUTTON_COMMAND "$NewSoundscape"
#define SAVE_BUTTON_COMMAND "$SaveSoundscape"
#define LOAD_BUTTON_COMMAND "$LoadSoundscape"
#define OPTIONS_BUTTON_COMMAND "$ShowOptions"
#define EDIT_BUTTON_COMMAND "$Edit"
#define RESET_BUTTON_COMMAND "$ResetSoundscapes"
#define SOUNDS_LIST_BUTTON_COMMAND "$ShowSoundsList"
#define PLAY_SOUNDSCAPE_COMMAND "$PlaySoundscape"
#define RESET_SOUNDSCAPE_BUTTON_COMMAND "$ResetSoundscape"
#define DELETE_CURRENT_ITEM_COMMAND "$DeleteItem"


//static bool to determin if the soundscape panel should show or not
//soundscape data list
bool g_ShowSoundscapePanel = false;
bool g_IsPlayingSoundscape = false;


//soundscape maker panel
class CSoundscapeMaker : public vgui::Frame, CAutoGameSystem
{
public:
DECLARE_CLASS_SIMPLE(CSoundscapeMaker, vgui::Frame)


CSoundscapeMaker(vgui::VPANEL parent);
#define NEW_PLAYLOOPING_COMMAND "NewLooping"
#define NEW_SOUNDSCAPE_COMMAND "NewSoundscape"
#define NEW_RANDOM_COMMAND "NewRandom"


//tick functions
void OnTick();


//other functions
class CSoundscapeDataList : public CSoundscapeList
void OnClose();
{
void OnCommand(const char* pszCommand);
public:
DECLARE_CLASS_SIMPLE(CSoundscapeDataList, CSoundscapeList);


void PlaySelectedSoundscape();
CSoundscapeDataList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
void LoadFile(KeyValues* file);
: CSoundscapeList(parent, name, text, text_x_pos, max, width, height)
{}


void OnKeyCodePressed(vgui::KeyCode code);
//override right click functionality
virtual void OnMouseReleased(vgui::MouseCode code);


void SetSoundText(const char* text);
void OnCommand(const char* pszCommand);


//to play the soundscape on map spawn
private:
void LevelInitPostEntity();
friend class CSoundscapeMaker;
};


//sets the keyvalue file
void Set(const char* buffer);


//message pointer funcs
//-----------------------------------------------------------------------------
MESSAGE_FUNC_CHARPTR(OnFileSelected, "FileSelected", fullpath);
// Purpose: Called when a mouse code is released
MESSAGE_FUNC_PARAMS(OnTextChanged, "TextChanged", data);
//-----------------------------------------------------------------------------
void CSoundscapeDataList::OnMouseReleased(vgui::MouseCode code)
{
//if no soundscape is selected or mouse code != right then return
if (code != vgui::MouseCode::MOUSE_RIGHT || !m_Keyvalues)
return;


~CSoundscapeMaker();
//get cursor pos
int x, y;
vgui::surface()->SurfaceGetCursorPos(x, y);


private:
//create menu
//the soundscape keyvalues file
menu = new vgui::Menu(this, "Menu");
KeyValues* m_KeyValues = nullptr;
menu->AddMenuItem("AddLooping", "Add Looping Sound", NEW_PLAYLOOPING_COMMAND, this);
menu->AddMenuItem("AddSoundscape", "Add Soundscape", NEW_SOUNDSCAPE_COMMAND, this);
menu->AddMenuItem("AddSoundscape", "Add Random Sounds", NEW_RANDOM_COMMAND, this);


private:
//add clipboard thing
void CreateEverything();
if (CurrClipboardData.Count() > 0)
{
menu->AddSeparator();
menu->AddMenuItem("PasteFromClipboard", "Paste", PASTE_FROM_CLIBOARD_COMMAND, this);
menu->AddMenuItem("OpenClipboard", "Open Clipboard", OPEN_CLIBOARD_COMMAND, this);
}


private:
menu->SetBounds(x, y, 200, 50);
//lists all the soundscapes
menu->SetVisible(true);
CSoundscapeList* m_SoundscapesList;
}
CSoundscapeDataList* m_pDataList;
CSoundscapeRndwaveList* m_pSoundList;


//buttons
vgui::Button* m_ButtonNew = nullptr;
vgui::Button* m_ButtonSave = nullptr;
vgui::Button* m_ButtonLoad = nullptr;
vgui::Button* m_ButtonOptions = nullptr;
vgui::Button* m_EditButton = nullptr;


//file load and save dialogs
//-----------------------------------------------------------------------------
vgui::FileOpenDialog* m_FileSave = nullptr;
// Purpose: Called on command
vgui::FileOpenDialog* m_FileLoad = nullptr;
//-----------------------------------------------------------------------------
bool m_bWasFileLoad = false;
void CSoundscapeDataList::OnCommand(const char* pszCommand)
{
if (!Q_strcmp(pszCommand, NEW_PLAYLOOPING_COMMAND))
{
int LoopingNum = 0;
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
{
//store data name
const char* name = data->GetName();


//text entry for name
//increment variables based on name
vgui::TextEntry* m_TextEntryName;
if (!Q_strcasecmp(name, "playlooping"))
LoopingNum++;
}


//combo box for dsp effects
//add the keyvalues
vgui::ComboBox* m_DspEffects;
KeyValues* kv = new KeyValues("playlooping");
vgui::ComboBox* m_SoundLevels;
kv->SetFloat("volume", 1);
kv->SetInt("pitch", 100);


//sound data text entry
//add the keyvalue to both this and the keyvalues
vgui::TextEntry* m_TimeTextEntry;
AddButton("playlooping", "playlooping", CFmtStr("$playlooping%d", LoopingNum + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);
vgui::TextEntry* m_VolumeTextEntry;
vgui::TextEntry* m_PitchTextEntry;
vgui::TextEntry* m_PositionTextEntry;
vgui::TextEntry* m_SoundNameTextEntry;


//play sound button
m_Keyvalues->AddSubKey(kv);
vgui::Button* m_SoundNamePlay;
 
GetParent()->OnCommand(CFmtStr("$playlooping%d", LoopingNum + 1));


//play/reset soundscape buttons
return;
vgui::CheckButton* m_PlaySoundscapeButton;
}
vgui::Button* m_ResetSoundscapeButton;
else if (!Q_strcmp(pszCommand, NEW_SOUNDSCAPE_COMMAND))
vgui::Button* m_DeleteCurrentButton;
{
int SoundscapeNum = 0;
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
{
//store data name
const char* name = data->GetName();


//current selected soundscape
//increment variables based on name
CSoundscapeButton* m_pCurrentSelected = nullptr;
if (!Q_strcasecmp(name, "playsoundscape"))
KeyValues* m_kvCurrSelected = nullptr;
SoundscapeNum++;
KeyValues* m_kvCurrSound = nullptr;
}
KeyValues* m_kvCurrRndwave = nullptr;


int m_iCurrRndWave = 0;
//add the keyvalues
KeyValues* kv = new KeyValues("playsoundscape");
kv->SetFloat("volume", 1);


//currently in non randomwave thing
AddButton("playsoundscape", "playsoundscape", CFmtStr("$playsoundscape%d", SoundscapeNum + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);
SoundscapeMode m_iSoundscapeMode = SoundscapeMode::Mode_Random;


//temporary added soundscapes
//add the keyvalue to both this and the keyvalues
CUtlVector<KeyValues*> m_TmpAddedSoundscapes;
m_Keyvalues->AddSubKey(kv);
};


//user message hook
GetParent()->OnCommand(CFmtStr("$playsoundscape%d", SoundscapeNum + 1));
void _SoundscapeMaker_Recieve(bf_read& bf);


//-----------------------------------------------------------------------------
return;
// Purpose: Constructor for soundscape maker panel
}
//-----------------------------------------------------------------------------
else if (!Q_strcmp(pszCommand, NEW_RANDOM_COMMAND))
CSoundscapeMaker::CSoundscapeMaker(vgui::VPANEL parent)
: BaseClass(nullptr, "SoundscapeMaker")
{
static bool bRegistered = false;
if (!bRegistered)
{
{
usermessages->HookMessage("SoundscapeMaker_Recieve", _SoundscapeMaker_Recieve);
int RandomNum = 0;
bRegistered = true;
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
}
{
//store data name
const char* name = data->GetName();


//set variables
//increment variables based on name
m_pCurrentSelected = nullptr;
if (!Q_strcasecmp(name, "playrandom"))
RandomNum++;
}


SetParent(parent);
//add the keyvalues
KeyValues* kv = new KeyValues("playrandom");


SetKeyBoardInputEnabled(true);
SetMouseInputEnabled(true);


SetProportional(false);
kv->SetString("volume", "0.5,0.8");
SetTitleBarVisible(true);
kv->SetInt("pitch", 100);
SetMinimizeButtonVisible(false);
kv->SetString("time", "10,20");
SetMaximizeButtonVisible(false);
SetCloseButtonVisible(true);
SetSizeable(false);
SetMoveable(true);
SetVisible(g_ShowSoundscapePanel);


int ScreenWide, ScreenTall;
AddButton("playrandom", "playrandom", CFmtStr("$playrandom%d", RandomNum + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);


SetTitle("Soundscape Maker (New File)", true);
//make rndwave subkey
SetSize(SOUNDSCAPE_PANEL_WIDTH, SOUNDSCAPE_PANEL_HEIGHT);
KeyValues* rndwave = new KeyValues("rndwave");
SetPos((ScreenWide - SOUNDSCAPE_PANEL_WIDTH) / 2, (ScreenTall - SOUNDSCAPE_PANEL_HEIGHT) / 2);
kv->AddSubKey(rndwave);


//add the keyvalue to both this and the keyvalues
m_Keyvalues->AddSubKey(kv);


//make the parent show the new item
GetParent()->OnCommand(CFmtStr("$playrandom%d", RandomNum + 1));


//add a tick signal for every 50 ms
return;
vgui::ivgui()->AddTickSignal(GetVPanel(), 50);
}
else if (!Q_strcmp(pszCommand, PASTE_FROM_CLIBOARD_COMMAND))
{
int index = CurrClipboardData.Count() - 1;


CreateEverything();
const char* type = CurrClipboardData[index]->GetName();
}


//-----------------------------------------------------------------------------
//get num of that item
// Purpose: Creates everything for this panel
int NumItem = 0;
//-----------------------------------------------------------------------------
FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
void CSoundscapeMaker::CreateEverything()
{
{
//store data name
//create the divider that will be the outline for the inside of the panel
const char* name = data->GetName();
vgui::Divider* PanelOutline = new vgui::Divider(this, "InsideOutline");
PanelOutline->SetEnabled(false);
PanelOutline->SetBounds(5, 25, SOUNDSCAPE_PANEL_WIDTH - 10, SOUNDSCAPE_PANEL_HEIGHT - 62);


//create the buttons
//increment variables based on name
//create the buttons
if (!Q_strcasecmp(name, type))
m_ButtonNew = new vgui::Button(this, "NewButton", "New Soundscape File");
NumItem++;
m_ButtonNew->SetVisible(true);
}
m_ButtonNew->SetBounds(7, 600, 145, 25);
m_ButtonNew->SetCommand(NEW_BUTTON_COMMAND);
m_ButtonNew->SetDepressedSound("ui/buttonclickrelease.wav");


m_ButtonSave = new vgui::Button(this, "SaveButton", "Save Soundscapes");
//add the keyvalues
m_ButtonSave->SetVisible(true);
KeyValues* kv = new KeyValues(type);
m_ButtonSave->SetBounds(157, 600, 145, 25);
CurrClipboardData[index]->CopySubkeys(kv);
m_ButtonSave->SetCommand(SAVE_BUTTON_COMMAND);
m_ButtonSave->SetDepressedSound("ui/buttonclickrelease.wav");


m_ButtonLoad = new vgui::Button(this, "LoadButton", "Load Soundscapes");
AddButton(type, type, CFmtStr("$%s%d", type, NumItem + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);
m_ButtonLoad->SetVisible(true);
m_ButtonLoad->SetBounds(307, 600, 145, 25);
m_ButtonLoad->SetCommand(LOAD_BUTTON_COMMAND);
m_ButtonLoad->SetDepressedSound("ui/buttonclickrelease.wav");


m_ButtonOptions = new vgui::Button(this, "OptionsButton", "Show Options Panel");
//add the keyvalue to both this and the keyvalues
m_ButtonOptions->SetVisible(true);
m_Keyvalues->AddSubKey(kv);
m_ButtonOptions->SetBounds(457, 600, 145, 25);
m_ButtonOptions->SetCommand(OPTIONS_BUTTON_COMMAND);
m_ButtonOptions->SetDepressedSound("ui/buttonclickrelease.wav");


m_EditButton = new vgui::Button(this, "EditButton", "Show Text Editor");
//make the parent show the new item
m_EditButton->SetVisible(true);
GetParent()->OnCommand(CFmtStr("$%s%d", type, NumItem + 1));
m_EditButton->SetBounds(607, 600, 145, 25);
return;
m_EditButton->SetCommand(EDIT_BUTTON_COMMAND);
}
m_EditButton->SetDepressedSound("ui/buttonclickrelease.wav");
else if (!Q_strcmp(pszCommand, OPEN_CLIBOARD_COMMAND))
{
if (g_SoundscapeClipboard)
g_SoundscapeClipboard->DeletePanel();


//create the soundscapes menu
g_SoundscapeClipboard = new CSoundscapeClipboard(SoundscapeClipboardType::Type_SoundscapeData);
m_SoundscapesList = new CSoundscapeList(this, "SoundscapesList", "Soundscapes:", 90, 22, 300, 550);
return;
m_SoundscapesList->SetBounds(15, 35, 300, 550);
}
m_SoundscapesList->SetVisible(true);


//create data list
BaseClass::OnCommand(pszCommand);
m_pDataList = new CSoundscapeDataList(this, "SoudscapeDataList", "Soundscape Data:", 35, 10, 200, 310);
}
m_pDataList->SetBounds(327, 275, 200, 310);
m_pDataList->SetVisible(true);


//create sound list
m_pSoundList = new CSoundscapeRndwaveList(this, "SoudscapeDataList", "Random Sounds:", 40, 10, 200, 310);
m_pSoundList->SetBounds(542, 275, 200, 310);
m_pSoundList->SetVisible(true);


//name text entry
//soundscape rndwave data list
m_TextEntryName = new vgui::TextEntry(this, "NameTextEntry");
m_TextEntryName->SetEnabled(false);
m_TextEntryName->SetBounds(325, 40, 295, 20);
m_TextEntryName->SetMaximumCharCount(50);


//dsp effects combo box
m_DspEffects = new vgui::ComboBox(this, "DspEffects", sizeof(g_DspEffects) / sizeof(g_DspEffects[0]), false);
m_DspEffects->SetEnabled(false);
m_DspEffects->SetBounds(325, 65, 295, 20);
m_DspEffects->SetText("");
m_DspEffects->AddActionSignalTarget(this);


for (int i = 0; i < sizeof(g_DspEffects) / sizeof(g_DspEffects[i]); i++)
#define NEW_RNDWAVE_WAVE_COMMAND "NewRNDWave"
m_DspEffects->AddItem(g_DspEffects[i], nullptr);


//time text entry
m_TimeTextEntry = new vgui::TextEntry(this, "TimeTextEntry");
m_TimeTextEntry->SetBounds(325, 90, 295, 20);
m_TimeTextEntry->SetEnabled(false);
m_TimeTextEntry->SetVisible(true);


//volume text entry
class CSoundscapeRndwaveList : public CSoundscapeList
m_VolumeTextEntry = new vgui::TextEntry(this, "VolumeTextEntry");
{
m_VolumeTextEntry->SetBounds(325, 115, 295, 20);
public:
m_VolumeTextEntry->SetEnabled(false);
DECLARE_CLASS_SIMPLE(CSoundscapeDataList, CSoundscapeList);
m_VolumeTextEntry->SetVisible(true);


//pitch text entry
CSoundscapeRndwaveList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
m_PitchTextEntry = new vgui::TextEntry(this, "PitchTextEntry");
: CSoundscapeList(parent, name, text, text_x_pos, max, width, height)
m_PitchTextEntry->SetBounds(325, 140, 295, 20);
{}
m_PitchTextEntry->SetEnabled(false);
m_PitchTextEntry->SetVisible(true);


//position text entry
//override right click functionality
m_PositionTextEntry = new vgui::TextEntry(this, "PositionTextEntry");
virtual void OnMouseReleased(vgui::MouseCode code);
m_PositionTextEntry->SetBounds(325, 165, 295, 20);
m_PositionTextEntry->SetEnabled(false);
m_PositionTextEntry->SetVisible(true);


//sound levels
void OnCommand(const char* pszCommand);
m_SoundLevels = new vgui::ComboBox(this, "SoundLevels", sizeof(g_SoundLevels) / sizeof(g_SoundLevels[0]), false);
m_SoundLevels->SetEnabled(false);
m_SoundLevels->SetBounds(325, 190, 295, 20);
m_SoundLevels->SetText("");
m_SoundLevels->AddActionSignalTarget(this);


for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
private:
m_SoundLevels->AddItem(g_SoundLevels[i], nullptr);
friend class CSoundscapeMaker;
};


//sound name
m_SoundNameTextEntry = new vgui::TextEntry(this, "SoundName");
m_SoundNameTextEntry->SetBounds(325, 215, 215, 20);
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNameTextEntry->SetVisible(true);


//sound list button
//-----------------------------------------------------------------------------
m_SoundNamePlay = new vgui::Button(this, "SoundPlayButton", "Sounds List");
// Purpose: Called when a mouse code is released
m_SoundNamePlay->SetBounds(545, 215, 75, 20);
//-----------------------------------------------------------------------------
m_SoundNamePlay->SetCommand(SOUNDS_LIST_BUTTON_COMMAND);
void CSoundscapeRndwaveList::OnMouseReleased(vgui::MouseCode code)
m_SoundNamePlay->SetEnabled(false);
{
//if no soundscape is selected or mouse code != right then return
if (code != vgui::MouseCode::MOUSE_RIGHT || !m_Keyvalues)
return;


//starts the soundscape
//get cursor pos
m_PlaySoundscapeButton = new vgui::CheckButton(this, "PlaySoundscape", "Play Soundscape");
int x, y;
m_PlaySoundscapeButton->SetBounds(330, 243, 125, 20);
vgui::surface()->SurfaceGetCursorPos(x, y);
m_PlaySoundscapeButton->SetCommand(PLAY_SOUNDSCAPE_COMMAND);
m_PlaySoundscapeButton->SetEnabled(false);
m_PlaySoundscapeButton->SetSelected(false);


//reset soundscape button
//create menu
m_ResetSoundscapeButton = new vgui::Button(this, "ResetSoundscape", "Restart Soundscape");
menu = new vgui::Menu(this, "Menu");
m_ResetSoundscapeButton->SetBounds(465, 243, 125, 20);
menu->AddMenuItem("AddRandom", "Add Random Wave", NEW_RNDWAVE_WAVE_COMMAND, this);
m_ResetSoundscapeButton->SetCommand(RESET_SOUNDSCAPE_BUTTON_COMMAND);
m_ResetSoundscapeButton->SetEnabled(false);


//delete this item
//add clipboard thing
m_DeleteCurrentButton = new vgui::Button(this, "DeleteItem", "Delete Current Item");
if (CurrClipboardRandom.Count() > 0)
m_DeleteCurrentButton->SetBounds(595, 243, 135, 20);
{
m_DeleteCurrentButton->SetCommand(DELETE_CURRENT_ITEM_COMMAND);
menu->AddSeparator();
m_DeleteCurrentButton->SetEnabled(false);
menu->AddMenuItem("PasteFromClipboard", "Paste", PASTE_FROM_CLIBOARD_COMMAND, this);
menu->AddMenuItem("OpenClipboard", "Open Clipboard", OPEN_CLIBOARD_COMMAND, this);
}


//create the soundscape name text
menu->SetBounds(x, y, 200, 50);
vgui::Label* NameLabel = new vgui::Label(this, "NameLabel", "Soundscape Name");
menu->SetVisible(true);
NameLabel->SetBounds(635, 40, 125, 20);
}


//create the soundscape dsp text
vgui::Label* DspLabel = new vgui::Label(this, "DspLabel", "Soundscape Dsp");
DspLabel->SetBounds(635, 65, 125, 20);


//create the soundscape time text
//-----------------------------------------------------------------------------
vgui::Label* TimeLabel = new vgui::Label(this, "TimeLabel", "Sound Time");
// Purpose: Called on command
TimeLabel->SetBounds(635, 90, 125, 20);
//-----------------------------------------------------------------------------
 
void CSoundscapeRndwaveList::OnCommand(const char* pszCommand)
//create the soundscape volumn text
{
vgui::Label* VolumeLabel = new vgui::Label(this, "VolumeLabel", "Sound Volume");
if (!Q_strcmp(pszCommand, NEW_RNDWAVE_WAVE_COMMAND) && m_Keyvalues)
VolumeLabel->SetBounds(635, 115, 125, 20);
{
//get number of keyvalues
int num = 0;


//create the soundscape pitch text
FOR_EACH_VALUE(m_Keyvalues, kv)
vgui::Label* PitchLabel = new vgui::Label(this, "PitchLabel", "Sound Pitch");
num++;
PitchLabel->SetBounds(635, 140, 125, 20);


//create the soundscape position text
KeyValues* add = new KeyValues("wave");
vgui::Label* PositionLabel = new vgui::Label(this, "PositionLabel", "Sound Position");
add->SetString(nullptr, "");
PositionLabel->SetBounds(635, 165, 125, 20);


//create the soundscape sound level text
//add keyvalues and button
vgui::Label* SoundLevelLabel = new vgui::Label(this, "SoundLevelLabel", "Sound Level");
AddButton("Rndwave", "", CFmtStr("$rndwave%d", num + 1), GetParent(), add, SoundscapeClipboardType::Type_SoundscapeRandomWave);
SoundLevelLabel->SetBounds(635, 190, 125, 20);


//create the soundscape sound name text
m_Keyvalues->AddSubKey(add);
vgui::Label* SoundName = new vgui::Label(this, "SoundName", "Sound Name");
SoundName->SetBounds(635, 215, 125, 20);


//create the soundscape keyvalues and load it
//forward command to parent
m_KeyValues = new KeyValues("Empty Soundscape");
GetParent()->OnCommand(CFmtStr("$rndwave%d", num + 1));


LoadFile(m_KeyValues);
return;
}
}


//-----------------------------------------------------------------------------
else if (!Q_strcmp(pszCommand, PASTE_FROM_CLIBOARD_COMMAND))
// Purpose: Called every tick for the soundscape maker
{
//-----------------------------------------------------------------------------
//get number of keyvalues
void CSoundscapeMaker::OnTick()
int num = 0;
{
//set the visibility
static bool bPrevVisible = g_ShowSoundscapePanel;
if (g_ShowSoundscapePanel != bPrevVisible)
SetVisible(g_ShowSoundscapePanel);


//set the old visibility
FOR_EACH_VALUE(m_Keyvalues, kv)
bPrevVisible = g_ShowSoundscapePanel;
num++;
}
 
int index = CurrClipboardRandom.Count() - 1;


//-----------------------------------------------------------------------------
const char* text = CurrClipboardRandom[index]->GetString();
// Purpose: Called when the close button is pressed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnClose()
{
//hide the other panels
g_SoundPanel->OnClose();
g_SettingsPanel->OnClose();
g_SoundscapeTextPanel->OnClose();


g_ShowSoundscapePanel = false;
KeyValues* add = new KeyValues("wave");
}
add->SetString(nullptr, text);


//-----------------------------------------------------------------------------
//get last / or \ and make the string be that + 1
// Purpose: Play the selected soundscape
char* fslash = Q_strrchr(text, '/');
//-----------------------------------------------------------------------------
char* bslash = Q_strrchr(text, '\\');
void CSoundscapeMaker::PlaySelectedSoundscape()
{
//set debug stuff
SoundscapePrint(Color(255, 255, 255, 255), "\n\n\n=============== %s %s =================\n\n", m_kvCurrSelected ? "Starting Soundscape: " : "Stopping Current Soundscape", m_kvCurrSelected ? m_kvCurrSelected->GetName() : "");
g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->Clear();


g_IsPlayingSoundscape = true;
if (fslash > bslash)
g_bSSMHack = true;
text = fslash + 1;
else if (bslash > fslash)
text = bslash + 1;


//remove all the temporary soundscapes from the soundscape system
//add keyvalues and button
for (int i = 0; i < m_TmpAddedSoundscapes.Count(); i++)
AddButton("Rndwave", text, CFmtStr("$rndwave%d", num + 1), GetParent(), add, SoundscapeClipboardType::Type_SoundscapeRandomWave);
{
for (int j = 0; j < g_SoundscapeSystem.m_soundscapes.Count(); j++)
{
if (g_SoundscapeSystem.m_soundscapes[j] == m_TmpAddedSoundscapes[i])
{
g_SoundscapeSystem.m_soundscapes.Remove(j);
break;
}
}
}


m_TmpAddedSoundscapes.RemoveAll();
m_Keyvalues->AddSubKey(add);


//forward command to parent
GetParent()->OnCommand(CFmtStr("$rndwave%d", num + 1));
return;
}
else if (!Q_strcmp(pszCommand, OPEN_CLIBOARD_COMMAND))
{
if (g_SoundscapeClipboard)
g_SoundscapeClipboard->DeletePanel();


//change audio params position
g_SoundscapeClipboard = new CSoundscapeClipboard(SoundscapeClipboardType::Type_SoundscapeRandomWave);
g_SoundscapeSystem.m_params.localBits = 0x7f;
return;
for (int i = 0; i < MAX_SOUNDSCAPES - 1; i++)
}
g_SoundscapeSystem.m_params.localSound.Set(i, g_SoundscapePositions[i]);


BaseClass::OnCommand(pszCommand);
}


//if m_kvCurrSelected then add all the "playsoundscape" soundscape keyvalues
//into the g_SoundscapeSystem.m_soundscapes array
if (m_kvCurrSelected)
{
CUtlVector<const char*> SoundscapeNames;
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, subkey)
{
//look for playsoundscape file
if (!Q_strcasecmp(subkey->GetName(), "playsoundscape"))
{
const char* name = subkey->GetString("name", nullptr);
if (!name || !name[0] || SoundscapeNames.Find(name) != SoundscapeNames.InvalidIndex())
continue;


SoundscapeNames.AddToTail(name);
//soundscape panel
}
}


//now look for each keyvalue
for (int i = 0; i < SoundscapeNames.Count(); i++)
{
for (KeyValues* subkey = m_KeyValues; subkey != nullptr; subkey = subkey->GetNextTrueSubKey())
{
//look for playsoundscape file
if (!Q_strcmp(subkey->GetName(), SoundscapeNames[i]))
{
//add it to the soundscape system
m_TmpAddedSoundscapes.AddToTail(subkey);
g_SoundscapeSystem.m_soundscapes.AddToTail(subkey);
}
}
}
}


//stop all sounds
#define SOUNDSCAPE_PANEL_WIDTH 760
enginesound->StopAllSounds(true);
#define SOUNDSCAPE_PANEL_HEIGHT 630


//stop the current soundscape and start a new soundscape
#define NEW_BUTTON_COMMAND "$NewSoundscape"
g_SoundscapeSystem.StartNewSoundscape(nullptr);
#define SAVE_BUTTON_COMMAND "$SaveSoundscape"
g_SoundscapeSystem.StartNewSoundscape(m_kvCurrSelected);
#define LOAD_BUTTON_COMMAND "$LoadSoundscape"
#define OPTIONS_BUTTON_COMMAND "$ShowOptions"
#define EDIT_BUTTON_COMMAND "$Edit"
#define RESET_BUTTON_COMMAND "$ResetSoundscapes"
#define SOUNDS_LIST_BUTTON_COMMAND "$ShowSoundsList"
#define PLAY_SOUNDSCAPE_COMMAND "$PlaySoundscape"
#define RESET_SOUNDSCAPE_BUTTON_COMMAND "$ResetSoundscape"
#define DELETE_CURRENT_ITEM_COMMAND "$DeleteItem"


//start debug graphs
//static bool to determin if the soundscape panel should show or not
g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->Start();
bool g_ShowSoundscapePanel = false;
bool g_IsPlayingSoundscape = false;


g_bSSMHack = false;
//soundscape maker panel
}
class CSoundscapeMaker : public vgui::Frame, CAutoGameSystem
 
//-----------------------------------------------------------------------------
// Purpose: Called when a button or something else gets pressed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnCommand(const char* pszCommand)
{
{
public:
DECLARE_CLASS_SIMPLE(CSoundscapeMaker, vgui::Frame)


//check for close command first
CSoundscapeMaker(vgui::VPANEL parent);
if (!Q_strcmp(pszCommand, "Close"))
{
BaseClass::OnCommand(pszCommand);
return;
}


//check for the save button command
//tick functions
else if (!Q_strcmp(pszCommand, SAVE_BUTTON_COMMAND))
void OnTick();
{
//initalize the file save dialog
if (!m_FileSave)
{
//get the current game directory
char buf[512];
filesystem->RelativePathToFullPath("scripts", "MOD", buf, sizeof(buf));


//create the save dialog
//other functions
m_FileSave = new vgui::FileOpenDialog(this, "Save Soundscape File", false);
void OnClose();
m_FileSave->AddFilter("*.txt", "Soundscape Text File", true);
void OnCommand(const char* pszCommand);
m_FileSave->AddFilter("*.*", "All Files (*.*)", false);
void Paste(SoundscapeClipboardType type);
m_FileSave->SetStartDirectory(buf);
m_FileSave->AddActionSignalTarget(this);
}


//show the dialog
void PlaySelectedSoundscape();
m_FileSave->DoModal(false);
void LoadFile(KeyValues* file);
m_FileSave->Activate();


//file wasnt loadad
void OnKeyCodePressed(vgui::KeyCode code);
m_bWasFileLoad = false;


return;
void SetSoundText(const char* text);
}


//check for load button command
//to play the soundscape on map spawn
else if (!Q_strcmp(pszCommand, LOAD_BUTTON_COMMAND))
void LevelInitPostEntity();
{
//initalize the file save dialog
if (!m_FileLoad)
{
//get the current game directory
char buf[512];
filesystem->RelativePathToFullPath("scripts", "MOD", buf, sizeof(buf));


//create the load dialog
//sets the keyvalue file
m_FileLoad = new vgui::FileOpenDialog(this, "Load Soundscape File", true);
void Set(const char* buffer);
m_FileLoad->AddFilter("*.txt", "Soundscape Text File", true);
m_FileLoad->AddFilter("*.*", "All Files (*.*)", false);
m_FileLoad->SetStartDirectory(buf);
m_FileLoad->AddActionSignalTarget(this);
}


//show the file load dialog
//message pointer funcs
m_FileLoad->DoModal(false);
MESSAGE_FUNC_CHARPTR(OnFileSelected, "FileSelected", fullpath);
m_FileLoad->Activate();
MESSAGE_FUNC_PARAMS(OnTextChanged, "TextChanged", data);


//file was loadad
~CSoundscapeMaker();
m_bWasFileLoad = true;


return;
public:
}


//check for options panel button
//the soundscape keyvalues file
else if (!Q_strcmp(pszCommand, OPTIONS_BUTTON_COMMAND))
KeyValues* m_KeyValues = nullptr;
{
g_SettingsPanel->SetVisible(true);
g_SettingsPanel->MoveToFront();
g_SettingsPanel->RequestFocus();
return;
}


//check for edit panel button
private:
else if (!Q_strcmp(pszCommand, EDIT_BUTTON_COMMAND))
void CreateEverything();
{
g_SoundscapeTextPanel->SetVisible(true);
g_SoundscapeTextPanel->MoveToFront();
g_SoundscapeTextPanel->RequestFocus();
g_SoundscapeTextPanel->Set(m_KeyValues);
return;
}


//check for new soundscape
private:
else if (!Q_strcmp(pszCommand, NEW_BUTTON_COMMAND))
//lists all the soundscapes
{
CSoundscapeList* m_SoundscapesList;
//make sure you want to create a new soundscape file
CSoundscapeDataList* m_pDataList;
vgui::QueryBox* popup = new vgui::QueryBox("New File?", "Are you sure you want to create a new soundscape file?", this);
CSoundscapeRndwaveList* m_pSoundList;
popup->SetOKCommand(new KeyValues("Command", "command", RESET_BUTTON_COMMAND));
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);


return;
//buttons
}
vgui::Button* m_ButtonNew = nullptr;
vgui::Button* m_ButtonSave = nullptr;
vgui::Button* m_ButtonLoad = nullptr;
vgui::Button* m_ButtonOptions = nullptr;
vgui::Button* m_EditButton = nullptr;


//check for reset soundscape
//file load and save dialogs
else if (!Q_strcmp(pszCommand, RESET_BUTTON_COMMAND))
vgui::FileOpenDialog* m_FileSave = nullptr;
{
vgui::FileOpenDialog* m_FileLoad = nullptr;
m_kvCurrSelected = nullptr;
bool m_bWasFileLoad = false;


//stop all soundscapes before deleting the old soundscapes
//text entry for name
if (g_IsPlayingSoundscape)
vgui::TextEntry* m_TextEntryName;
PlaySelectedSoundscape();


m_KeyValues->deleteThis();
//combo box for dsp effects
m_KeyValues = new KeyValues("Empty Soundscape");
vgui::ComboBox* m_DspEffects;
vgui::ComboBox* m_SoundLevels;


//reset title
//sound data text entry
SetTitle("Soundscape Maker (New File)", true);
vgui::TextEntry* m_TimeTextEntry;
vgui::TextEntry* m_VolumeTextEntry;
vgui::TextEntry* m_PitchTextEntry;
vgui::TextEntry* m_PositionTextEntry;
vgui::TextEntry* m_SoundNameTextEntry;


LoadFile(m_KeyValues);
//play sound button
return;
vgui::Button* m_SoundNamePlay;
}


//check for play sound
//play/reset soundscape buttons
else if (!Q_strcmp(pszCommand, SOUNDS_LIST_BUTTON_COMMAND))
vgui::CheckButton* m_PlaySoundscapeButton;
{
vgui::Button* m_ResetSoundscapeButton;
//initalize the sounds
vgui::Button* m_DeleteCurrentButton;
static bool g_SoundPanelInitalized = false;
if (!g_SoundPanelInitalized)
{
g_SoundPanelInitalized = true;
g_SoundPanel->InitalizeSounds();
g_SoundPanel->InitalizeSoundscapes();
}


//get sound text entry name
//current selected soundscape
char buf[512];
CSoundscapeButton* m_pCurrentSelected = nullptr;
m_SoundNameTextEntry->GetText(buf, sizeof(buf));


//check the current mode
public:
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
KeyValues* m_kvCurrSelected = nullptr;
g_SoundPanel->SetIsUsingSoundPanel(false);
 
else
private:
{
KeyValues* m_kvCurrSound = nullptr;
g_SoundPanel->SetIsUsingSoundPanel(true);
KeyValues* m_kvCurrRndwave = nullptr;


//look for item with same name
int m_iCurrRndWave = 0;
for (int i = 0; i < g_SoundDirectories.Count(); i++)
{
if (!Q_strcmp(buf, g_SoundDirectories[i]))
{
//select item
g_SoundPanel->m_SoundsList->ActivateItem(i);
g_SoundPanel->m_SoundsList->SetText(buf);


break;
//currently in non randomwave thing
}
SoundscapeMode m_iSoundscapeMode = SoundscapeMode::Mode_Random;
}
}


g_SoundPanel->SetVisible(true);
//temporary added soundscapes
g_SoundPanel->MoveToFront();
CUtlVector<KeyValues*> m_TmpAddedSoundscapes;
g_SoundPanel->RequestFocus();
};
return;
}


//check for play soundscape
//user message hook
else if (!Q_strcmp(pszCommand, PLAY_SOUNDSCAPE_COMMAND))
void _SoundscapeMaker_Recieve(bf_read& bf);
{
if (m_PlaySoundscapeButton->IsSelected())
{
//enable the reset soundscape button
m_ResetSoundscapeButton->SetEnabled(true);


//play the soundscape
//-----------------------------------------------------------------------------
PlaySelectedSoundscape();
// Purpose: Constructor for soundscape maker panel
}
//-----------------------------------------------------------------------------
else
CSoundscapeMaker::CSoundscapeMaker(vgui::VPANEL parent)
{
: BaseClass(nullptr, "SoundscapeMaker")
//disable the reset soundscape button
{
m_ResetSoundscapeButton->SetEnabled(false);
static bool bRegistered = false;
if (!bRegistered)
{
usermessages->HookMessage("SoundscapeMaker_Recieve", _SoundscapeMaker_Recieve);
bRegistered = true;
}


g_IsPlayingSoundscape = false;
//set variables
m_pCurrentSelected = nullptr;


//stop all sounds and soundscapes
SetParent(parent);
enginesound->StopAllSounds(true);
g_SoundscapeSystem.StartNewSoundscape(nullptr);
}


return;
SetKeyBoardInputEnabled(true);
}
SetMouseInputEnabled(true);


//check for play soundscape
SetProportional(false);
else if (!Q_strcmp(pszCommand, RESET_SOUNDSCAPE_BUTTON_COMMAND))
SetTitleBarVisible(true);
{
SetMinimizeButtonVisible(false);
PlaySelectedSoundscape();
SetMaximizeButtonVisible(false);
return;
SetCloseButtonVisible(true);
}
SetSizeable(false);
SetMoveable(true);
SetVisible(g_ShowSoundscapePanel);


int ScreenWide, ScreenTall;
vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);


//check for delete item
SetTitle("Soundscape Maker (New File)", true);
else if (!Q_strcmp(pszCommand, DELETE_CURRENT_ITEM_COMMAND))
SetSize(SOUNDSCAPE_PANEL_WIDTH, SOUNDSCAPE_PANEL_HEIGHT);
{
SetPos((ScreenWide - SOUNDSCAPE_PANEL_WIDTH) / 2, (ScreenTall - SOUNDSCAPE_PANEL_HEIGHT) / 2);
//check for current rndwave
if (m_kvCurrRndwave && m_SoundNameTextEntry->IsEnabled())
{
if (!m_kvCurrRndwave || m_iCurrRndWave <= 0)
return;


//get the keyvalues by the index
int curr = 0;
KeyValues* prev = nullptr;


FOR_EACH_VALUE(m_kvCurrRndwave, keyvalues)
{
if (++curr == m_iCurrRndWave)
{
//delete
if (prev)
prev->SetNextKey(keyvalues->GetNextValue());
else
{
m_kvCurrRndwave->m_pSub = keyvalues->GetNextValue();
m_iCurrRndWave = -1;
}


curr = curr - 1;
//add a tick signal for every 50 ms
vgui::ivgui()->AddTickSignal(GetVPanel(), 50);


keyvalues->SetNextKey(nullptr);
CreateEverything();
keyvalues->deleteThis();
}
break;
}


//-----------------------------------------------------------------------------
// Purpose: Creates everything for this panel
//-----------------------------------------------------------------------------
void CSoundscapeMaker::CreateEverything()
{
//create the divider that will be the outline for the inside of the panel
vgui::Divider* PanelOutline = new vgui::Divider(this, "InsideOutline");
PanelOutline->SetEnabled(false);
PanelOutline->SetBounds(5, 25, SOUNDSCAPE_PANEL_WIDTH - 10, SOUNDSCAPE_PANEL_HEIGHT - 62);


//create the buttons
//create the buttons
m_ButtonNew = new vgui::Button(this, "NewButton", "New Soundscape File");
m_ButtonNew->SetVisible(true);
m_ButtonNew->SetBounds(7, 600, 145, 25);
m_ButtonNew->SetCommand(NEW_BUTTON_COMMAND);
m_ButtonNew->SetDepressedSound("ui/buttonclickrelease.wav");


prev = keyvalues;
m_ButtonSave = new vgui::Button(this, "SaveButton", "Save Soundscapes");
}
m_ButtonSave->SetVisible(true);
 
m_ButtonSave->SetBounds(157, 600, 145, 25);
//reset everything
m_ButtonSave->SetCommand(SAVE_BUTTON_COMMAND);
m_SoundNameTextEntry->SetText("");
m_ButtonSave->SetDepressedSound("ui/buttonclickrelease.wav");
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);


//store vector
m_ButtonLoad = new vgui::Button(this, "LoadButton", "Load Soundscapes");
auto& vec = m_pSoundList->m_MenuButtons;
m_ButtonLoad->SetVisible(true);
m_ButtonLoad->SetBounds(307, 600, 145, 25);
m_ButtonLoad->SetCommand(LOAD_BUTTON_COMMAND);
m_ButtonLoad->SetDepressedSound("ui/buttonclickrelease.wav");


//remove it
m_ButtonOptions = new vgui::Button(this, "OptionsButton", "Show Options Panel");
delete vec[curr];
m_ButtonOptions->SetVisible(true);
vec.Remove(curr);
m_ButtonOptions->SetBounds(457, 600, 145, 25);
m_ButtonOptions->SetCommand(OPTIONS_BUTTON_COMMAND);
m_ButtonOptions->SetDepressedSound("ui/buttonclickrelease.wav");


//move everything down
m_EditButton = new vgui::Button(this, "EditButton", "Show Text Editor");
m_pSoundList->m_iCurrentY = m_pSoundList->m_iCurrentY - 22;
m_EditButton->SetVisible(true);
m_pSoundList->m_Keyvalues = m_kvCurrRndwave;
m_EditButton->SetBounds(607, 600, 145, 25);
m_EditButton->SetCommand(EDIT_BUTTON_COMMAND);
m_EditButton->SetDepressedSound("ui/buttonclickrelease.wav");


if (vec.Count() >= m_pSoundList->m_iMax)
//create the soundscapes menu
{
m_SoundscapesList = new CSoundscapeList(this, "SoundscapesList", "Soundscapes:", 90, 22, 300, 550);
m_pSoundList->OnMouseWheeled(1);
m_SoundscapesList->SetBounds(15, 35, 300, 550);
m_SoundscapesList->SetVisible(true);


int min, max;
//create data list
m_pSoundList->m_pSideSlider->GetRange(min, max);
m_pDataList = new CSoundscapeDataList(this, "SoudscapeDataList", "Soundscape Data:", 35, 10, 200, 310);
m_pSoundList->m_pSideSlider->SetRange(0, max - 1);
m_pDataList->SetBounds(327, 275, 200, 310);
}
m_pDataList->SetVisible(true);


for (int i = curr; i < vec.Count(); i++)
//create sound list
{
m_pSoundList = new CSoundscapeRndwaveList(this, "SoudscapeDataList", "Random Sounds:", 40, 10, 200, 310);
//move everything down
m_pSoundList->SetBounds(542, 275, 200, 310);
int x, y = 0;
m_pSoundList->SetVisible(true);
vec[i]->GetPos(x, y);
vec[i]->SetPos(x, y - 22);
}


//reset every command
//name text entry
int WaveAmount = 0;
m_TextEntryName = new vgui::TextEntry(this, "NameTextEntry");
for (int i = 0; i < vec.Count(); i++)
m_TextEntryName->SetEnabled(false);
{
m_TextEntryName->SetBounds(325, 40, 295, 20);
//store data name
m_TextEntryName->SetMaximumCharCount(256);
const char* name = vec[i]->GetCommand()->GetString("command");


//increment variables based on name
//dsp effects combo box
if (Q_stristr(name, "$rndwave") == name)
m_DspEffects = new vgui::ComboBox(this, "DspEffects", sizeof(g_DspEffects) / sizeof(g_DspEffects[0]), false);
{
m_DspEffects->SetEnabled(false);
WaveAmount++;
m_DspEffects->SetBounds(325, 65, 295, 20);
vec[i]->SetCommand(CFmtStr("$rndwave%d", WaveAmount));
m_DspEffects->SetText("");
}
m_DspEffects->AddActionSignalTarget(this);
}


//bounds check
for (int i = 0; i < sizeof(g_DspEffects) / sizeof(g_DspEffects[i]); i++)
if (vec.Count() <= 0)
m_DspEffects->AddItem(g_DspEffects[i], nullptr);
{
m_kvCurrRndwave = nullptr;
m_pSoundList->m_Keyvalues = nullptr;


//restart soundscape
//time text entry
PlaySelectedSoundscape();
m_TimeTextEntry = new vgui::TextEntry(this, "TimeTextEntry");
m_TimeTextEntry->SetBounds(325, 90, 295, 20);
m_TimeTextEntry->SetEnabled(false);
m_TimeTextEntry->SetVisible(true);


return;
//volume text entry
}
m_VolumeTextEntry = new vgui::TextEntry(this, "VolumeTextEntry");
m_VolumeTextEntry->SetBounds(325, 115, 295, 20);
m_VolumeTextEntry->SetEnabled(false);
m_VolumeTextEntry->SetVisible(true);


//select next item
//pitch text entry
if (m_iCurrRndWave <= vec.Count())
m_PitchTextEntry = new vgui::TextEntry(this, "PitchTextEntry");
OnCommand(CFmtStr("$rndwave%d", curr + 1));
m_PitchTextEntry->SetBounds(325, 140, 295, 20);
else
m_PitchTextEntry->SetEnabled(false);
OnCommand(CFmtStr("$rndwave%d", curr));
m_PitchTextEntry->SetVisible(true);
}
else if (m_kvCurrSound)
{
//find keyvalue with same pointer and get the index
int tmpindex = 0;
int index = -1;


KeyValues* prev = nullptr;
//position text entry
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, keyvalues)
m_PositionTextEntry = new vgui::TextEntry(this, "PositionTextEntry");
{
m_PositionTextEntry->SetBounds(325, 165, 295, 20);
if (m_kvCurrSound == keyvalues)
m_PositionTextEntry->SetEnabled(false);
{
m_PositionTextEntry->SetVisible(true);
//remove it
if (prev)
prev->SetNextKey(keyvalues->GetNextTrueSubKey());
else
m_kvCurrSelected->m_pSub = keyvalues->GetNextTrueSubKey();


keyvalues->SetNextKey(nullptr);
//sound levels
keyvalues->deleteThis();
m_SoundLevels = new vgui::ComboBox(this, "SoundLevels", sizeof(g_SoundLevels) / sizeof(g_SoundLevels[0]), false);
m_SoundLevels->SetEnabled(false);
m_SoundLevels->SetBounds(325, 190, 295, 20);
m_SoundLevels->SetText("");
m_SoundLevels->AddActionSignalTarget(this);


//get index
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
index = tmpindex;
m_SoundLevels->AddItem(g_SoundLevels[i], nullptr);
break;
}


prev = keyvalues;
//sound name
m_SoundNameTextEntry = new vgui::TextEntry(this, "SoundName");
m_SoundNameTextEntry->SetBounds(325, 215, 215, 20);
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNameTextEntry->SetVisible(true);


//increment
//sound list button
tmpindex++;
m_SoundNamePlay = new vgui::Button(this, "SoundPlayButton", "Sounds List");
}
m_SoundNamePlay->SetBounds(545, 215, 75, 20);
m_SoundNamePlay->SetCommand(SOUNDS_LIST_BUTTON_COMMAND);
m_SoundNamePlay->SetEnabled(false);


//error
//starts the soundscape
if (index == -1)
m_PlaySoundscapeButton = new vgui::CheckButton(this, "PlaySoundscape", "Play Soundscape");
return;
m_PlaySoundscapeButton->SetBounds(330, 243, 125, 20);
m_PlaySoundscapeButton->SetCommand(PLAY_SOUNDSCAPE_COMMAND);
m_PlaySoundscapeButton->SetEnabled(false);
m_PlaySoundscapeButton->SetSelected(false);


//store vector
//reset soundscape button
auto& vec = m_pDataList->m_MenuButtons;
m_ResetSoundscapeButton = new vgui::Button(this, "ResetSoundscape", "Restart Soundscape");
m_ResetSoundscapeButton->SetBounds(465, 243, 125, 20);
m_ResetSoundscapeButton->SetCommand(RESET_SOUNDSCAPE_BUTTON_COMMAND);
m_ResetSoundscapeButton->SetEnabled(false);


//remove it
//delete this item
delete vec[index];
m_DeleteCurrentButton = new vgui::Button(this, "DeleteItem", "Delete Current Item");
vec.Remove(index);
m_DeleteCurrentButton->SetBounds(595, 243, 135, 20);
m_DeleteCurrentButton->SetCommand(DELETE_CURRENT_ITEM_COMMAND);
m_DeleteCurrentButton->SetEnabled(false);


//move everything down
//create the soundscape name text
m_pDataList->m_iCurrentY = m_pDataList->m_iCurrentY - 22;
vgui::Label* NameLabel = new vgui::Label(this, "NameLabel", "Soundscape Name");
m_pDataList->m_Keyvalues = m_kvCurrSelected;
NameLabel->SetBounds(635, 40, 125, 20);


for (int i = index; i < vec.Count(); i++)
//create the soundscape dsp text
{
vgui::Label* DspLabel = new vgui::Label(this, "DspLabel", "Soundscape Dsp");
//move everything down
DspLabel->SetBounds(635, 65, 125, 20);
int x, y = 0;
vec[i]->GetPos(x, y);
vec[i]->SetPos(x, y - 22);
}


if (vec.Count() >= m_pDataList->m_iMax)
//create the soundscape time text
{
vgui::Label* TimeLabel = new vgui::Label(this, "TimeLabel", "Sound Time");
m_pDataList->OnMouseWheeled(1);
TimeLabel->SetBounds(635, 90, 125, 20);


int min, max;
//create the soundscape volumn text
m_pDataList->m_pSideSlider->GetRange(min, max);
vgui::Label* VolumeLabel = new vgui::Label(this, "VolumeLabel", "Sound Volume");
VolumeLabel->SetBounds(635, 115, 125, 20);


if (max > 0)
//create the soundscape pitch text
m_pDataList->m_pSideSlider->SetRange(0, max - 1);
vgui::Label* PitchLabel = new vgui::Label(this, "PitchLabel", "Sound Pitch");
else
PitchLabel->SetBounds(635, 140, 125, 20);
m_pDataList->m_pSideSlider->SetRange(0, 0);
}


//reset the names of each button
//create the soundscape position text
int RandomNum = 0;
vgui::Label* PositionLabel = new vgui::Label(this, "PositionLabel", "Sound Position");
int LoopingNum = 0;
PositionLabel->SetBounds(635, 165, 125, 20);
int SoundscapeNum = 0;


//change the commands of the buttons
//create the soundscape sound level text
for (int i = 0; i < vec.Count(); i++)
vgui::Label* SoundLevelLabel = new vgui::Label(this, "SoundLevelLabel", "Sound Level");
{
SoundLevelLabel->SetBounds(635, 190, 125, 20);
//store data name
const char* name = vec[i]->GetCommand()->GetString("command");


//increment variables based on name
//create the soundscape sound name text
if (Q_stristr(name, "$playrandom") == name)
vgui::Label* SoundName = new vgui::Label(this, "SoundName", "Sound Name");
{
SoundName->SetBounds(635, 215, 125, 20);
RandomNum++;
vec[i]->SetCommand(CFmtStr("$playrandom%d", RandomNum));
}


if (Q_stristr(name, "$playlooping") == name)
//create the soundscape keyvalues and load it
{
m_KeyValues = new KeyValues("Empty Soundscape");
LoopingNum++;
vec[i]->SetCommand(CFmtStr("$playlooping%d", LoopingNum));
}


if (Q_stristr(name, "$playsoundscape") == name)
LoadFile(m_KeyValues);
{
}
SoundscapeNum++;
vec[i]->SetCommand(CFmtStr("$playsoundscape%d", SoundscapeNum));
}
}


//reset everything
//-----------------------------------------------------------------------------
m_SoundLevels->SetText("");
// Purpose: Called every tick for the soundscape maker
m_SoundNameTextEntry->SetText("");
//-----------------------------------------------------------------------------
m_TimeTextEntry->SetText("");
void CSoundscapeMaker::OnTick()
m_PitchTextEntry->SetText("");
{
m_PositionTextEntry->SetText("");
//set the visibility
m_VolumeTextEntry->SetText("");
static bool bPrevVisible = g_ShowSoundscapePanel;
if (g_ShowSoundscapePanel != bPrevVisible)
SetVisible(g_ShowSoundscapePanel);


m_SoundLevels->SetEnabled(false);
//set the old visibility
m_SoundNameTextEntry->SetEnabled(false);
bPrevVisible = g_ShowSoundscapePanel;
m_TimeTextEntry->SetEnabled(false);
}
m_PitchTextEntry->SetEnabled(false);
 
m_PositionTextEntry->SetEnabled(false);
//-----------------------------------------------------------------------------
m_VolumeTextEntry->SetEnabled(false);
// Purpose: Called when the close button is pressed
m_SoundNamePlay->SetEnabled(false);
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnClose()
{
//hide the other panels
g_SoundPanel->OnClose();
g_SettingsPanel->OnClose();
g_SoundscapeTextPanel->OnClose();


m_pSoundList->Clear();
g_ShowSoundscapePanel = false;
}


m_kvCurrSound = nullptr;
//-----------------------------------------------------------------------------
m_pSoundList->m_Keyvalues = nullptr;
// Purpose: Play the selected soundscape
//-----------------------------------------------------------------------------
void CSoundscapeMaker::PlaySelectedSoundscape()
{
//set debug stuff
SoundscapePrint(Color(255, 255, 255, 255), "\n\n\n=============== %s %s =================\n\n", m_kvCurrSelected ? "Starting Soundscape: " : "Stopping Current Soundscape", m_kvCurrSelected ? m_kvCurrSelected->GetName() : "");
g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->Clear();


//bounds checking
g_IsPlayingSoundscape = true;
if (index >= vec.Count())
g_bSSMHack = true;
index = vec.Count() - 1; // fix bounds more safely


//select the button
//remove all the temporary soundscapes from the soundscape system
if (index >= 0)
for (int i = 0; i < m_TmpAddedSoundscapes.Count(); i++)
OnCommand(vec[index]->GetCommand()->GetString("command"));
{
}
for (int j = 0; j < g_SoundscapeSystem.m_soundscapes.Count(); j++)
else if (m_kvCurrSelected)
{
{
if (m_KeyValues == m_kvCurrSelected)
if (g_SoundscapeSystem.m_soundscapes[j] == m_TmpAddedSoundscapes[i])
{
{
//play an error sound
g_SoundscapeSystem.m_soundscapes.Remove(j);
vgui::surface()->PlaySound("resource/warning.wav");
break;
}
}
}


//show an error
m_TmpAddedSoundscapes.RemoveAll();
vgui::QueryBox* popup = new vgui::QueryBox("Error", "Can not delete base soundscape!", this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);


return;
//change audio params position
}
g_SoundscapeSystem.m_params.localBits = 0x7f;
for (int i = 0; i < MAX_SOUNDSCAPES - 1; i++)
g_SoundscapeSystem.m_params.localSound.Set(i, g_SoundscapePositions[i]);


//find keyvalue with same pointer and get the index
int tmpindex = 0;
int index = -1;


KeyValues* prev = nullptr;
//if m_kvCurrSelected then add all the "playsoundscape" soundscape keyvalues
for (KeyValues* keyvalues = m_KeyValues; keyvalues != nullptr; keyvalues = keyvalues->GetNextTrueSubKey())
//into the g_SoundscapeSystem.m_soundscapes array
if (m_kvCurrSelected)
{
CUtlVector<const char*> SoundscapeNames;
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, subkey)
{
//look for playsoundscape file
if (!Q_strcasecmp(subkey->GetName(), "playsoundscape"))
{
{
if (m_kvCurrSelected == keyvalues)
const char* name = subkey->GetString("name", nullptr);
{
if (!name || !name[0] || SoundscapeNames.Find(name) != SoundscapeNames.InvalidIndex())
//remove it
continue;
if (!prev)
break;


prev->SetNextKey(keyvalues->GetNextTrueSubKey());
SoundscapeNames.AddToTail(name);
keyvalues->SetNextKey(nullptr);
}
keyvalues->deleteThis();
}


//get index
//now look for each keyvalue
index = tmpindex;
for (int i = 0; i < SoundscapeNames.Count(); i++)
break;
{
for (KeyValues* subkey = m_KeyValues; subkey != nullptr; subkey = subkey->GetNextTrueSubKey())
{
//look for playsoundscape file
if (!Q_strcmp(subkey->GetName(), SoundscapeNames[i]))
{
//add it to the soundscape system
m_TmpAddedSoundscapes.AddToTail(subkey);
g_SoundscapeSystem.m_soundscapes.AddToTail(subkey);
}
}
prev = keyvalues;
//increment
tmpindex++;
}
}
}
}


//error
//stop all sounds
if (index == -1)
enginesound->StopAllSounds(true);
return;


//store vector
//stop the current soundscape and start a new soundscape
auto& vec = m_SoundscapesList->m_MenuButtons;
g_SoundscapeSystem.StartNewSoundscape(nullptr);
g_SoundscapeSystem.StartNewSoundscape(m_kvCurrSelected);


//remove it
//start debug graphs
delete vec[index];
g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->Start();
vec.Remove(index);


//move everything down
g_bSSMHack = false;
m_SoundscapesList->m_iCurrentY = m_SoundscapesList->m_iCurrentY - 22;
}


for (int i = index; i < vec.Count(); i++)
//-----------------------------------------------------------------------------
{
// Purpose: Called when a button or something else gets pressed
//move everything down
//-----------------------------------------------------------------------------
int x, y = 0;
void CSoundscapeMaker::OnCommand(const char* pszCommand)
vec[i]->GetPos(x, y);
{
vec[i]->SetPos(x, y - 22);
//check for close command first
}
if (!Q_strcmp(pszCommand, "Close"))
{
BaseClass::OnCommand(pszCommand);
return;
}


if (vec.Count() >= m_SoundscapesList->m_iMax)
//check for the save button command
{
else if (!Q_strcmp(pszCommand, SAVE_BUTTON_COMMAND))
m_SoundscapesList->OnMouseWheeled(1);
{
//initalize the file save dialog
if (!m_FileSave)
{
//get the current game directory
char buf[512];
filesystem->RelativePathToFullPath("scripts", "MOD", buf, sizeof(buf));


int min, max;
//create the save dialog
m_SoundscapesList->m_pSideSlider->GetRange(min, max);
m_FileSave = new vgui::FileOpenDialog(this, "Save Soundscape File", false);
m_SoundscapesList->m_pSideSlider->SetRange(0, max - 1);
m_FileSave->AddFilter("*.txt", "Soundscape Text File", true);
}
m_FileSave->AddFilter("*.*", "All Files (*.*)", false);
m_FileSave->SetStartDirectory(buf);
m_FileSave->AddActionSignalTarget(this);
}


//reset everything
//show the dialog
m_DspEffects->SetText("");
m_FileSave->DoModal(false);
m_SoundLevels->SetText("");
m_FileSave->Activate();
m_TextEntryName->SetText("");
m_SoundNameTextEntry->SetText("");
m_TimeTextEntry->SetText("");
m_PitchTextEntry->SetText("");
m_PositionTextEntry->SetText("");
m_VolumeTextEntry->SetText("");


m_DspEffects->SetEnabled(false);
//file wasnt loadad
m_SoundLevels->SetEnabled(false);
m_bWasFileLoad = false;
m_TextEntryName->SetEnabled(false);
m_SoundNameTextEntry->SetEnabled(false);
m_TimeTextEntry->SetEnabled(false);
m_PitchTextEntry->SetEnabled(false);
m_PositionTextEntry->SetEnabled(false);
m_VolumeTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);
 
m_pDataList->Clear();
m_pSoundList->Clear();
 
m_kvCurrSound = nullptr;
m_pDataList->m_Keyvalues = nullptr;
 
//go to next soundscape
if (!prev)
{
//restart soundscape
PlaySelectedSoundscape();
return;
}


if (prev->GetNextTrueSubKey())
OnCommand(prev->GetNextTrueSubKey()->GetName());
else
OnCommand(prev->GetName());
}
//restart soundscape
PlaySelectedSoundscape();
return;
return;
}
}


//check for "playrandom", "playsoundscape" or "playlooping"
//check for load button command
if (Q_stristr(pszCommand, "$playrandom") == pszCommand)
else if (!Q_strcmp(pszCommand, LOAD_BUTTON_COMMAND))
{
{
//get the selected number
//initalize the file save dialog
char* str_number = (char*)(pszCommand + 11);
if (!m_FileLoad)
int number = atoi(str_number);
if (number != 0)
{
{
//look for button with same command
//get the current game directory
auto& vec = m_pDataList->m_MenuButtons;
char buf[512];
for (int i = 0; i < vec.Count(); i++)
filesystem->RelativePathToFullPath("scripts", "MOD", buf, sizeof(buf));
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}


//create the load dialog
m_FileLoad = new vgui::FileOpenDialog(this, "Load Soundscape File", true);
m_FileLoad->AddFilter("*.txt", "Soundscape Text File", true);
m_FileLoad->AddFilter("*.*", "All Files (*.*)", false);
m_FileLoad->SetStartDirectory(buf);
m_FileLoad->AddActionSignalTarget(this);
}


//clear the m_pSoundList
//show the file load dialog
m_pSoundList->Clear();
m_FileLoad->DoModal(false);
m_pSoundList->m_Keyvalues = nullptr;
m_FileLoad->Activate();


//store variables
//file was loadad
KeyValues* data = nullptr;
m_bWasFileLoad = true;
int curr = 0;


//get subkey
return;
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
}
{
if (Q_strcasecmp(sounds->GetName(), "playrandom"))
continue;


if (++curr == number)
//check for options panel button
{
else if (!Q_strcmp(pszCommand, OPTIONS_BUTTON_COMMAND))
data = sounds;
{
break;
g_SettingsPanel->SetVisible(true);
}
g_SettingsPanel->MoveToFront();
}
g_SettingsPanel->RequestFocus();
return;
}


//no data
//check for edit panel button
if (!data)
else if (!Q_strcmp(pszCommand, EDIT_BUTTON_COMMAND))
return;
{
g_SoundscapeTextPanel->SetVisible(true);
g_SoundscapeTextPanel->MoveToFront();
g_SoundscapeTextPanel->RequestFocus();
g_SoundscapeTextPanel->Set(m_KeyValues);
return;
}


m_kvCurrSound = data;
//check for new soundscape
m_kvCurrRndwave = nullptr;
else if (!Q_strcmp(pszCommand, NEW_BUTTON_COMMAND))
{
//make sure you want to create a new soundscape file
vgui::QueryBox* popup = new vgui::QueryBox("New File?", "Are you sure you want to create a new soundscape file?", this);
popup->SetOKCommand(new KeyValues("Command", "command", RESET_BUTTON_COMMAND));
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);


//set the random times
return;
m_TimeTextEntry->SetText(data->GetString("time", "10,20"));
}
m_VolumeTextEntry->SetText(data->GetString("volume", "0.5,0.8"));
m_PitchTextEntry->SetText(data->GetString("pitch", "100"));
m_PositionTextEntry->SetText(data->GetString("position", ""));
m_SoundNameTextEntry->SetText("");


//get snd level index
//check for reset soundscape
int index = 8; //8 = SNDLVL_NORM
else if (!Q_strcmp(pszCommand, RESET_BUTTON_COMMAND))
const char* name = data->GetString("soundlevel", nullptr);
{
m_kvCurrSelected = nullptr;


//check for the name
//stop all soundscapes before deleting the old soundscapes
if (name)
if (g_IsPlayingSoundscape)
{
PlaySelectedSoundscape();


//loop through the sound levels to find the right one
m_KeyValues->deleteThis();
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
m_KeyValues = new KeyValues("Empty Soundscape");
{
if (!Q_strcmp(name, g_SoundLevels[i]))
{
index = i;
break;
}
}
}


//select the index
//reset title
m_SoundLevels->ActivateItem(index);
SetTitle("Soundscape Maker (New File)", true);


//enable the text entries
LoadFile(m_KeyValues);
m_TimeTextEntry->SetEnabled(true);
return;
m_VolumeTextEntry->SetEnabled(true);
}
m_PitchTextEntry->SetEnabled(true);
m_PositionTextEntry->SetEnabled(true);
m_SoundLevels->SetEnabled(true);
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);


g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
//check for play sound
g_SoundPanel->SetVisible(false);
else if (!Q_strcmp(pszCommand, SOUNDS_LIST_BUTTON_COMMAND))
{
//initalize the sounds
static bool g_SoundPanelInitalized = false;
if (!g_SoundPanelInitalized)
{
g_SoundPanelInitalized = true;
g_SoundPanel->InitalizeSounds();
}


//check for randomwave subkey
//get sound text entry name
if ((data = data->FindKey("rndwave")) == nullptr)
char buf[512];
return;
m_SoundNameTextEntry->GetText(buf, sizeof(buf));


m_kvCurrRndwave = data;
//check the current mode
m_pSoundList->m_Keyvalues = data;
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
{
g_SoundPanel->SetIsUsingSoundPanel(false);


//add all the data
//load all the temporary soundscapes
int i = 0;
CUtlVector<const char*> OtherSoundscapes;
FOR_EACH_VALUE(data, sound)
for (KeyValues* curr = m_KeyValues; curr; curr = curr->GetNextKey())
{
{
const char* name = sound->GetName();
if (curr == m_kvCurrSelected)
continue;


//get real text
OtherSoundscapes.AddToTail(curr->GetName());
const char* text = sound->GetString();
}


//get last / or \ and make the string be that + 1
g_SoundPanel->InitalizeSoundscapes(OtherSoundscapes);
char* fslash = Q_strrchr(text, '/');
}
char* bslash = Q_strrchr(text, '\\');
else
{
g_SoundPanel->SetIsUsingSoundPanel(true);


//no forward slash and no back slash
//look for item with same name
if (!fslash && !bslash)
for (int i = 0; i < g_SoundDirectories.Count(); i++)
{
if (!Q_strcmp(buf, g_SoundDirectories[i]))
{
{
text = text;
//select item
}
g_SoundPanel->m_SoundsList->ActivateItem(i);
else
g_SoundPanel->m_SoundsList->SetText(buf);
{
if (fslash > bslash)
text = fslash + 1;


else if (bslash > fslash)
break;
text = bslash + 1;
}
}
m_pSoundList->AddButton(name, text, CFmtStr("$rndwave%d", ++i), this);
}
}
}


m_iSoundscapeMode = SoundscapeMode::Mode_Random;
g_SoundPanel->SetVisible(true);
return;
g_SoundPanel->MoveToFront();
}
g_SoundPanel->RequestFocus();
return;
}
}
else if (Q_stristr(pszCommand, "$playlooping") == pszCommand)
 
//check for play soundscape
else if (!Q_strcmp(pszCommand, PLAY_SOUNDSCAPE_COMMAND))
{
{
//get the selected number
if (m_PlaySoundscapeButton->IsSelected())
char* str_number = (char*)(pszCommand + 12);
int number = atoi(str_number);
if (number != 0)
{
{
//look for button with same command
//enable the reset soundscape button
auto& vec = m_pDataList->m_MenuButtons;
m_ResetSoundscapeButton->SetEnabled(true);
for (int i = 0; i < vec.Count(); i++)
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}


//play the soundscape
PlaySelectedSoundscape();
}
else
{
//disable the reset soundscape button
m_ResetSoundscapeButton->SetEnabled(false);


//clear the m_pSoundList
g_IsPlayingSoundscape = false;
m_pSoundList->Clear();
m_pSoundList->m_Keyvalues = nullptr;


//store variables
//stop all sounds and soundscapes
KeyValues* data = nullptr;
enginesound->StopAllSounds(true);
int curr = 0;
g_SoundscapeSystem.StartNewSoundscape(nullptr);
}


//get subkey
return;
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
}
{
 
if (Q_strcasecmp(sounds->GetName(), "playlooping"))
//check for play soundscape
continue;
else if (!Q_strcmp(pszCommand, RESET_SOUNDSCAPE_BUTTON_COMMAND))
{
PlaySelectedSoundscape();
return;
}


if (++curr == number)
{
data = sounds;
break;
}
}


//no data
//check for delete item
if (!data)
else if (!Q_strcmp(pszCommand, DELETE_CURRENT_ITEM_COMMAND))
{
//check for current rndwave
if (m_kvCurrRndwave && m_SoundNameTextEntry->IsEnabled())
{
if (!m_kvCurrRndwave || m_iCurrRndWave <= 0)
return;
return;


m_kvCurrSound = data;
//get the keyvalues by the index
m_kvCurrRndwave = nullptr;
int curr = 0;
KeyValues* prev = nullptr;


//set the random times
FOR_EACH_VALUE(m_kvCurrRndwave, keyvalues)
m_TimeTextEntry->SetText("");
m_VolumeTextEntry->SetText(data->GetString("volume", "1"));
m_PitchTextEntry->SetText(data->GetString("pitch", "100"));
m_PositionTextEntry->SetText(data->GetString("position", ""));
m_SoundNameTextEntry->SetText(data->GetString("wave", ""));
 
//get snd level index
int index = 8; //8 = SNDLVL_NORM
const char* name = data->GetString("soundlevel", nullptr);
 
//check for the name
if (name)
{
{
 
if (++curr == m_iCurrRndWave)
//loop through the sound levels to find the right one
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
{
{
if (!Q_strcmp(name, g_SoundLevels[i]))
//delete
if (prev)
prev->SetNextKey(keyvalues->GetNextValue());
else
{
{
index = i;
m_kvCurrRndwave->m_pSub = keyvalues->GetNextValue();
break;
m_iCurrRndWave = -1;
}
}
curr = curr - 1;
keyvalues->SetNextKey(nullptr);
keyvalues->deleteThis();
break;
}
}
}


//select the index
m_SoundLevels->ActivateItem(index);


//enable the text entries
m_TimeTextEntry->SetEnabled(false);
m_VolumeTextEntry->SetEnabled(true);
m_PitchTextEntry->SetEnabled(true);
m_PositionTextEntry->SetEnabled(true);
m_SoundLevels->SetEnabled(true);
m_SoundNameTextEntry->SetEnabled(true);
m_SoundNamePlay->SetEnabled(true);
g_SoundPanel->SetVisible(false);


m_iSoundscapeMode = SoundscapeMode::Mode_Looping;
prev = keyvalues;
return;
}
}
else if (Q_stristr(pszCommand, "$playsoundscape") == pszCommand)
{
//get the selected number
char* str_number = (char*)(pszCommand + 15);
int number = atoi(str_number);
if (number != 0)
{
//look for button with same command
auto& vec = m_pDataList->m_MenuButtons;
for (int i = 0; i < vec.Count(); i++)
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}
}


//reset everything
m_SoundNameTextEntry->SetText("");
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);


//clear the m_pSoundList
//store vector
m_pSoundList->Clear();
auto& vec = m_pSoundList->m_MenuButtons;
m_pSoundList->m_Keyvalues = nullptr;
 
//remove it
delete vec[curr];
vec.Remove(curr);


//store variables
//move everything down
KeyValues* data = nullptr;
m_pSoundList->m_iCurrentY = m_pSoundList->m_iCurrentY - 22;
int curr = 0;
m_pSoundList->m_Keyvalues = m_kvCurrRndwave;


//get subkey
if (vec.Count() >= m_pSoundList->m_iMax)
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
{
{
if (Q_strcasecmp(sounds->GetName(), "playsoundscape"))
m_pSoundList->OnMouseWheeled(1);
continue;


if (++curr == number)
int min, max;
{
m_pSoundList->m_pSideSlider->GetRange(min, max);
data = sounds;
m_pSoundList->m_pSideSlider->SetRange(0, max - 1);
break;
}
}
}


//no data
for (int i = curr; i < vec.Count(); i++)
if (!data)
{
return;
//move everything down
int x, y = 0;
vec[i]->GetPos(x, y);
vec[i]->SetPos(x, y - 22);
}


m_kvCurrSound = data;
//reset every command
m_kvCurrRndwave = nullptr;
int WaveAmount = 0;
for (int i = 0; i < vec.Count(); i++)
{
//store data name
const char* name = vec[i]->GetCommand()->GetString("command");


//set the random times
//increment variables based on name
m_TimeTextEntry->SetText("");
if (Q_stristr(name, "$rndwave") == name)
m_VolumeTextEntry->SetText(data->GetString("volume", "1"));
m_PositionTextEntry->SetText(data->GetString("positionoverride", ""));
m_SoundNameTextEntry->SetText(data->GetString("name", ""));
m_PitchTextEntry->SetText("");
 
//get snd level index
int index = 8; //8 = SNDLVL_NORM
const char* name = data->GetString("soundlevel", nullptr);
 
//check for the name
if (name)
{
 
//loop through the sound levels to find the right one
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
{
{
if (!Q_strcmp(name, g_SoundLevels[i]))
WaveAmount++;
{
vec[i]->SetCommand(CFmtStr("$rndwave%d", WaveAmount));
index = i;
break;
}
}
}
}
}


//select the index
//bounds check
m_SoundLevels->ActivateItem(index);
if (vec.Count() <= 0)
{
m_kvCurrRndwave = nullptr;
m_pSoundList->m_Keyvalues = nullptr;


//enable the text entries
//restart soundscape
m_TimeTextEntry->SetEnabled(true);
PlaySelectedSoundscape();
m_VolumeTextEntry->SetEnabled(true);
m_PitchTextEntry->SetEnabled(false);
m_PositionTextEntry->SetEnabled(true);
m_SoundLevels->SetEnabled(true);
m_SoundNameTextEntry->SetEnabled(true);
m_TimeTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(true);


g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
return;
g_SoundPanel->SetVisible(false);
}


m_iSoundscapeMode = SoundscapeMode::Mode_Soundscape;
//select next item
return;
if (m_iCurrRndWave <= vec.Count())
OnCommand(CFmtStr("$rndwave%d", curr + 1));
else
OnCommand(CFmtStr("$rndwave%d", curr));
}
}
}
else if (m_kvCurrSound)
else if (Q_stristr(pszCommand, "$rndwave") == pszCommand)
{
{
//find keyvalue with same pointer and get the index
if (!m_kvCurrRndwave)
int tmpindex = 0;
return;
int index = -1;


//get the selected number
KeyValues* prev = nullptr;
char* str_number = (char*)(pszCommand + 8);
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, keyvalues)
m_iCurrRndWave = atoi(str_number);
if (m_iCurrRndWave != 0)
{
//look for button with same command
auto& vec = m_pSoundList->m_MenuButtons;
for (int i = 0; i < vec.Count(); i++)
{
{
//if the button doesnt have the same command then de-select it. else select it
if (m_kvCurrSound == keyvalues)
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
{
vec[i]->m_bIsSelected = true;
//remove it
else
if (prev)
vec[i]->m_bIsSelected = false;
prev->SetNextKey(keyvalues->GetNextTrueSubKey());
}
else
m_kvCurrSelected->m_pSub = keyvalues->GetNextTrueSubKey();


keyvalues->SetNextKey(nullptr);
keyvalues->deleteThis();


int i = 0;
//get index
index = tmpindex;
break;
}


//get value
prev = keyvalues;
KeyValues* curr = nullptr;
 
FOR_EACH_VALUE(m_kvCurrRndwave, wave)
//increment
{
tmpindex++;
if (++i == m_iCurrRndWave)
{
curr = wave;
break;
}
}
}


//if no curr then throw an error
//error
if (!curr)
if (index == -1)
{
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");
 
//show error
char buf[1028];
Q_snprintf(buf, sizeof(buf), "Failed to get rndwave '%d' for subkey \"%s\"\nfor current soundscape file!", i, m_kvCurrSelected->GetName());
 
//show an error
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);
return;
return;
}


m_SoundNameTextEntry->SetEnabled(true);
//store vector
m_SoundNameTextEntry->SetText(curr->GetString());
auto& vec = m_pDataList->m_MenuButtons;


m_SoundNamePlay->SetEnabled(true);
//remove it
delete vec[index];
vec.Remove(index);


m_iSoundscapeMode = SoundscapeMode::Mode_Random;
//move everything down
return;
m_pDataList->m_iCurrentY = m_pDataList->m_iCurrentY - 22;
}
m_pDataList->m_Keyvalues = m_kvCurrSelected;
}


//look for button with the same name as the command
for (int i = index; i < vec.Count(); i++)
{
//store vars
CUtlVector<CSoundscapeButton*>& array = m_SoundscapesList->m_MenuButtons;
 
//de-select button
if (m_pCurrentSelected)
m_pCurrentSelected->m_bIsSelected = false;
 
//check for name
for (int i = 0; i < m_SoundscapesList->m_MenuButtons.Size(); i++)
{
//check button name
if (!Q_strcmp(array[i]->GetCommand()->GetString("command"), pszCommand))
{
{
//found it
//move everything down
m_pCurrentSelected = array[i];
int x, y = 0;
break;
vec[i]->GetPos(x, y);
vec[i]->SetPos(x, y - 22);
}
}
}


//find selected keyvalue
if (vec.Count() >= m_pDataList->m_iMax)
{
m_pDataList->OnMouseWheeled(1);


//set needed stuff
int min, max;
if (m_pCurrentSelected)
m_pDataList->m_pSideSlider->GetRange(min, max);
{
//select button
m_pCurrentSelected->m_bIsSelected = true;


m_DeleteCurrentButton->SetEnabled(false);
if (max > 0)
m_pDataList->m_pSideSlider->SetRange(0, max - 1);
else
m_pDataList->m_pSideSlider->SetRange(0, 0);
}


//reset the selected kv
//reset the names of each button
m_kvCurrSelected = nullptr;
int RandomNum = 0;
int LoopingNum = 0;
int SoundscapeNum = 0;


//find selected keyvalues
//change the commands of the buttons
for (int i = 0; i < vec.Count(); i++)
{
//store data name
const char* name = vec[i]->GetCommand()->GetString("command");
 
//increment variables based on name
if (Q_stristr(name, "$playrandom") == name)
{
RandomNum++;
vec[i]->SetCommand(CFmtStr("$playrandom%d", RandomNum));
}
 
if (Q_stristr(name, "$playlooping") == name)
{
LoopingNum++;
vec[i]->SetCommand(CFmtStr("$playlooping%d", LoopingNum));
}


for (KeyValues* kv = m_KeyValues; kv != nullptr; kv = kv->GetNextTrueSubKey())
if (Q_stristr(name, "$playsoundscape") == name)
{
if (!Q_strcmp(kv->GetName(), pszCommand))
{
{
m_kvCurrSelected = kv;
SoundscapeNum++;
break;
vec[i]->SetCommand(CFmtStr("$playsoundscape%d", SoundscapeNum));
}
}
}
}


//set
//reset everything
m_kvCurrSound = nullptr;
m_SoundLevels->SetText("");
m_kvCurrRndwave = nullptr;
m_SoundNameTextEntry->SetText("");
 
m_TimeTextEntry->SetEnabled(false);
m_TimeTextEntry->SetText("");
m_TimeTextEntry->SetText("");
 
m_PitchTextEntry->SetText("");
m_VolumeTextEntry->SetEnabled(false);
m_PositionTextEntry->SetText("");
m_VolumeTextEntry->SetText("");
m_VolumeTextEntry->SetText("");


m_SoundLevels->SetEnabled(false);
m_SoundNameTextEntry->SetEnabled(false);
m_TimeTextEntry->SetEnabled(false);
m_PitchTextEntry->SetEnabled(false);
m_PitchTextEntry->SetEnabled(false);
m_PitchTextEntry->SetText("");
m_PositionTextEntry->SetEnabled(false);
m_PositionTextEntry->SetEnabled(false);
m_PositionTextEntry->SetText("");
m_VolumeTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);


m_SoundLevels->SetEnabled(false);
m_pSoundList->Clear();
m_SoundLevels->SetText("");


m_SoundNameTextEntry->SetEnabled(false);
m_kvCurrSound = nullptr;
m_SoundNameTextEntry->SetText("");
m_pSoundList->m_Keyvalues = nullptr;


m_SoundNamePlay->SetEnabled(false);
//bounds checking
if (index >= vec.Count())
index = vec.Count() - 1; // fix bounds more safely


if (g_SoundPanel)
//select the button
{
if (index >= 0)
g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
OnCommand(vec[index]->GetCommand()->GetString("command"));
g_SoundPanel->SetVisible(false);
}
}
else if (m_kvCurrSelected)
 
{
//check for current keyvalues. should never bee nullptr but could be
if (m_KeyValues == m_kvCurrSelected)
if (!m_kvCurrSelected)
{
{
//play an error sound
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");
vgui::surface()->PlaySound("resource/warning.wav");
//show error
char buf[1028];
Q_snprintf(buf, sizeof(buf), "Failed to find KeyValue subkey \"%s\"\nfor current soundscape file!", pszCommand);


//show an error
//show an error
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
vgui::QueryBox* popup = new vgui::QueryBox("Error", "Can not delete base soundscape!", this);
popup->SetOKButtonText("Ok");
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->SetCancelButtonVisible(false);
Line 4,301: Line 4,232:
popup->DoModal(this);
popup->DoModal(this);


//reset vars
m_pCurrentSelected = nullptr;
m_TextEntryName->SetEnabled(false);
m_TextEntryName->SetText("");
m_DspEffects->SetEnabled(false);
m_DspEffects->SetText("");
return;
return;
}
}


if (g_IsPlayingSoundscape)
//find keyvalue with same pointer and get the index
PlaySelectedSoundscape();
int tmpindex = 0;
int index = -1;


m_DeleteCurrentButton->SetEnabled(true);
KeyValues* prev = nullptr;
 
for (KeyValues* keyvalues = m_KeyValues; keyvalues != nullptr; keyvalues = keyvalues->GetNextTrueSubKey())
//set current soundscape name
{
m_TextEntryName->SetText(pszCommand);
if (m_kvCurrSelected == keyvalues)
m_TextEntryName->SetEnabled(true);
{
m_pDataList->m_Keyvalues = m_kvCurrSelected;
//remove it
if (!prev)
break;


//set dsp effect
prev->SetNextKey(keyvalues->GetNextTrueSubKey());
int dsp = Clamp<int>(m_kvCurrSelected->GetInt("dsp"), 0, 29);
keyvalues->SetNextKey(nullptr);
keyvalues->deleteThis();


m_PlaySoundscapeButton->SetEnabled(true);
//get index
index = tmpindex;
break;
}


m_DspEffects->SetEnabled(true);
prev = keyvalues;
m_DspEffects->ActivateItem(dsp);


//clear these
//increment
m_pDataList->Clear();
tmpindex++;
m_pSoundList->Clear();
}
m_pSoundList->m_Keyvalues = nullptr;


//set variables
//error
int RandomNum = 0;
if (index == -1)
int LoopingNum = 0;
return;
int SoundscapeNum = 0;


FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, data)
//store vector
{
auto& vec = m_SoundscapesList->m_MenuButtons;
//store data name
const char* name = data->GetName();


//increment variables based on name
//remove it
if (!Q_strcasecmp(name, "playrandom"))
delete vec[index];
{
vec.Remove(index);
RandomNum++;
m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playrandom%d", RandomNum), this);
}


if (!Q_strcasecmp(name, "playlooping"))
//move everything down
{
m_SoundscapesList->m_iCurrentY = m_SoundscapesList->m_iCurrentY - 22;
LoopingNum++;
m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playlooping%d", LoopingNum), this);
}


if (!Q_strcasecmp(name, "playsoundscape"))
for (int i = index; i < vec.Count(); i++)
{
{
SoundscapeNum++;
//move everything down
m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playsoundscape%d", SoundscapeNum), this);
int x, y = 0;
}
vec[i]->GetPos(x, y);
vec[i]->SetPos(x, y - 22);
}
}
}
}


BaseClass::OnCommand(pszCommand);
if (vec.Count() >= m_SoundscapesList->m_iMax)
}
{
m_SoundscapesList->OnMouseWheeled(1);


//-----------------------------------------------------------------------------
int min, max;
// Purpose: Function to recursivly write keyvalues to keyvalue files. the keyvalues
m_SoundscapesList->m_pSideSlider->GetRange(min, max);
// class does have a function to do this BUT this function writes every single
m_SoundscapesList->m_pSideSlider->SetRange(0, max - 1);
// item one after another. this function does that but writes the keys
}
// first then the subkeys so the order is good.
//-----------------------------------------------------------------------------
void RecursivlyWriteKeyvalues(KeyValues* prev, CUtlBuffer& buffer, int& indent)
{
//write \t indent
for (int i = 0; i < indent; i++)
buffer.PutChar('\t');


//write name
//reset everything
buffer.PutChar('"');
m_DspEffects->SetText("");
buffer.PutString(prev->GetName());
m_SoundLevels->SetText("");
buffer.PutString("\"\n");
m_TextEntryName->SetText("");
m_SoundNameTextEntry->SetText("");
m_TimeTextEntry->SetText("");
m_PitchTextEntry->SetText("");
m_PositionTextEntry->SetText("");
m_VolumeTextEntry->SetText("");


//write {
m_DspEffects->SetEnabled(false);
for (int i = 0; i < indent; i++)
m_SoundLevels->SetEnabled(false);
buffer.PutChar('\t');
m_TextEntryName->SetEnabled(false);
m_SoundNameTextEntry->SetEnabled(false);
m_TimeTextEntry->SetEnabled(false);
m_PitchTextEntry->SetEnabled(false);
m_PositionTextEntry->SetEnabled(false);
m_VolumeTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);


buffer.PutString("{\n");
m_pDataList->Clear();
m_pSoundList->Clear();


//increment indent
m_kvCurrSound = nullptr;
indent++;
m_pDataList->m_Keyvalues = nullptr;


//write all the keys first
//go to next soundscape
FOR_EACH_VALUE(prev, value)
if (!prev)
{
{
for (int i = 0; i < indent; i++)
//restart soundscape
buffer.PutChar('\t');
PlaySelectedSoundscape();
return;
}


//write name and value
if (prev->GetNextTrueSubKey())
buffer.PutChar('"');
OnCommand(prev->GetNextTrueSubKey()->GetName());
buffer.PutString(value->GetName());
else
buffer.PutString("\"\t");
OnCommand(prev->GetName());
}


buffer.PutChar('"');
//restart soundscape
buffer.PutString(value->GetString());
PlaySelectedSoundscape();
buffer.PutString("\"\n");
return;
}
}


//write all the subkeys now
//check for "playrandom", "playsoundscape" or "playlooping"
FOR_EACH_TRUE_SUBKEY(prev, value)
if (Q_stristr(pszCommand, "$playrandom") == pszCommand)
{
{
//increment indent
//get the selected number
RecursivlyWriteKeyvalues(value, buffer, indent);
char* str_number = (char*)(pszCommand + 11);
int number = atoi(str_number);
if (number != 0)
{
//look for button with same command
auto& vec = m_pDataList->m_MenuButtons;
for (int i = 0; i < vec.Count(); i++)
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}


if (value->GetNextTrueSubKey())
buffer.PutChar('\n');
}


//decrement indent
//clear the m_pSoundList
indent--;
m_pSoundList->Clear();
m_pSoundList->m_Keyvalues = nullptr;


//write ending }
//store variables
for (int i = 0; i < indent; i++)
KeyValues* data = nullptr;
buffer.PutChar('\t');
int curr = 0;


buffer.PutString("}\n");
//get subkey
}
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
 
{
//-----------------------------------------------------------------------------
if (Q_strcasecmp(sounds->GetName(), "playrandom"))
// Purpose: Called when a file gets opened/closed
continue;
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnFileSelected(const char* pszFileName)
{
//check for null or empty string
if (!pszFileName || pszFileName[0] == '\0')
return;


//check for file save
if (++curr == number)
if (!m_bWasFileLoad)
{
{
data = sounds;
//save the file
break;
if (m_KeyValues)
}
{
}
//write everything into a buffer
CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
buf.PutString("//------------------------------------------------------------------------------------\n");
buf.PutString("//\n");
buf.PutString("// Auto-generated soundscape file created with modbases soundscape tool'\n");
buf.PutString("//\n");
buf.PutString("//------------------------------------------------------------------------------------\n");


//now write the keyvalues
//no data
KeyValues* pCurrent = m_KeyValues;
if (!data)
while (pCurrent)
return;
{
int indent = 0;
RecursivlyWriteKeyvalues(pCurrent, buf, indent);


//put a newline
m_kvCurrSound = data;
if (pCurrent->GetNextTrueSubKey())
m_kvCurrRndwave = nullptr;
buf.PutChar('\n');


//get next
//set the random times
pCurrent = pCurrent->GetNextTrueSubKey();
m_TimeTextEntry->SetText(data->GetString("time", "10,20"));
}
m_VolumeTextEntry->SetText(data->GetString("volume", "0.5,0.8"));
m_PitchTextEntry->SetText(data->GetString("pitch", "100"));
m_PositionTextEntry->SetText(data->GetString("position", ""));
m_SoundNameTextEntry->SetText("");
 
//get snd level index
int index = 8; //8 = SNDLVL_NORM
const char* name = data->GetString("soundlevel", nullptr);


if (!g_pFullFileSystem->WriteFile(pszFileName, "MOD", buf))
//check for the name
if (name)
{
{
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");


//get the error first
//loop through the sound levels to find the right one
char buf[1028];
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
Q_snprintf(buf, sizeof(buf), "Failed to save soundscape to file \"%s\"", pszFileName);
{
 
if (!Q_strcmp(name, g_SoundLevels[i]))
//show an error
{
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
index = i;
popup->SetOKButtonText("Ok");
break;
popup->SetCancelButtonVisible(false);
}
popup->AddActionSignalTarget(this);
}
popup->DoModal(this);
return;
}
}
}


//select the index
m_SoundLevels->ActivateItem(index);


//store vars
//enable the text entries
const char* last = pszFileName;
m_TimeTextEntry->SetEnabled(true);
const char* tmp = nullptr;
m_VolumeTextEntry->SetEnabled(true);
 
m_PitchTextEntry->SetEnabled(true);
//get the last /
m_PositionTextEntry->SetEnabled(true);
while ((last = Q_strstr(last, "\\")) != nullptr)
m_SoundLevels->SetEnabled(true);
tmp = ++last; //move past the backslash
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(false);


//check tmp
g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
if (!tmp || !*tmp)
g_SoundPanel->SetVisible(false);
tmp = pszFileName;


//set new title
//check for randomwave subkey
char buf[1028];
if ((data = data->FindKey("rndwave")) == nullptr)
Q_snprintf(buf, sizeof(buf), "Soundscape Maker (%s)", tmp);
return;


SetTitle(buf, true);
m_kvCurrRndwave = data;
m_pSoundList->m_Keyvalues = data;


//create copy of pszFileName
//add all the data
char manifest[1028];
int i = 0;
Q_strncpy(manifest, pszFileName, sizeof(manifest));
FOR_EACH_VALUE(data, sound)
{
const char* name = sound->GetName();


//get last /
//get real text
char* lastSlash = Q_strrchr(manifest, '\\');
const char* text = sound->GetString();
if (lastSlash == nullptr || *lastSlash == '\0')
return;


//append 'soundscapes_manifest.txt'
//get last / or \ and make the string be that + 1
lastSlash[1] = '\0';
char* fslash = Q_strrchr(text, '/');
strcat(manifest, "soundscapes_manifest.txt");
char* bslash = Q_strrchr(text, '\\');


//see if we can open manifest file
//no forward slash and no back slash
KeyValues* man_file = new KeyValues("manifest");
if (!fslash && !bslash)
if (!man_file->LoadFromFile(g_pFullFileSystem, manifest))
{
{
text = text;
//cant open manifest file
}
man_file->deleteThis();
else
{
if (fslash > bslash)
text = fslash + 1;
 
else if (bslash > fslash)
text = bslash + 1;
}
 
m_pSoundList->AddButton(name, text, CFmtStr("$rndwave%d", ++i), this, sound, SoundscapeClipboardType::Type_SoundscapeRandomWave);
}
 
m_iSoundscapeMode = SoundscapeMode::Mode_Random;
return;
return;
}
}
 
}
//get real filename
else if (Q_stristr(pszCommand, "$playlooping") == pszCommand)
pszFileName = Q_strrchr(pszFileName, '\\');
{
if (!pszFileName || !*pszFileName)
//get the selected number
char* str_number = (char*)(pszCommand + 12);
int number = atoi(str_number);
if (number != 0)
{
{
man_file->deleteThis();
//look for button with same command
return;
auto& vec = m_pDataList->m_MenuButtons;
}
for (int i = 0; i < vec.Count(); i++)
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}


pszFileName = pszFileName + 1;


//create name to be added to the manifest file
//clear the m_pSoundList
char add_file[1028];
m_pSoundList->Clear();
Q_snprintf(add_file, sizeof(add_file), "scripts/%s", pszFileName);
m_pSoundList->m_Keyvalues = nullptr;


//add filename to manifest file if not found
//store variables
FOR_EACH_VALUE(man_file, value)
KeyValues* data = nullptr;
{
int curr = 0;
if (!Q_strcmp(value->GetString(), add_file))
 
//get subkey
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
{
{
man_file->deleteThis();
if (Q_strcasecmp(sounds->GetName(), "playlooping"))
return;
continue;
 
if (++curr == number)
{
data = sounds;
break;
}
}
}
}


//no data
if (!data)
return;


//add to manifest file
m_kvCurrSound = data;
KeyValues* kv = new KeyValues("file");
m_kvCurrRndwave = nullptr;
kv->SetString(nullptr, add_file);
man_file->AddSubKey(kv);


//write to file
//set the random times
man_file->SaveToFile(g_pFullFileSystem, manifest);
m_TimeTextEntry->SetText("");
m_VolumeTextEntry->SetText(data->GetString("volume", "1"));
m_PitchTextEntry->SetText(data->GetString("pitch", "100"));
m_PositionTextEntry->SetText(data->GetString("position", ""));
m_SoundNameTextEntry->SetText(data->GetString("wave", ""));


man_file->deleteThis();
//get snd level index
return;
int index = 8; //8 = SNDLVL_NORM
}
const char* name = data->GetString("soundlevel", nullptr);


//try and load the keyvalues file first
//check for the name
KeyValues* temp = new KeyValues("SoundscapeFile");
if (name)
if (!temp->LoadFromFile(filesystem, pszFileName))
{
{
 
//play an error sound
//loop through the sound levels to find the right one
vgui::surface()->PlaySound("resource/warning.wav");
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
{
if (!Q_strcmp(name, g_SoundLevels[i]))
{
index = i;
break;
}
}
}


//get the error first
//select the index
char buf[1028];
m_SoundLevels->ActivateItem(index);
Q_snprintf(buf, sizeof(buf), "Failed to open keyvalues file \"%s\"", pszFileName);


//show an error
//enable the text entries
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
m_TimeTextEntry->SetEnabled(false);
popup->SetOKButtonText("Ok");
m_VolumeTextEntry->SetEnabled(true);
popup->SetCancelButtonVisible(false);
m_PitchTextEntry->SetEnabled(true);
popup->AddActionSignalTarget(this);
m_PositionTextEntry->SetEnabled(true);
popup->DoModal(this);
m_SoundLevels->SetEnabled(true);
m_SoundNameTextEntry->SetEnabled(true);
m_SoundNamePlay->SetEnabled(true);
g_SoundPanel->SetVisible(false);


temp->deleteThis();
m_iSoundscapeMode = SoundscapeMode::Mode_Looping;
return;
return;
}
}
}
 
else if (Q_stristr(pszCommand, "$playsoundscape") == pszCommand)
//set the new title
{
{
//store vars
//get the selected number
const char* last = pszFileName;
char* str_number = (char*)(pszCommand + 15);
const char* tmp = nullptr;
int number = atoi(str_number);
if (number != 0)
{
//look for button with same command
auto& vec = m_pDataList->m_MenuButtons;
for (int i = 0; i < vec.Count(); i++)
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}


//get the last /
while ((last = Q_strstr(last, "\\")) != nullptr)
tmp = ++last; //move past the backslash


//check tmp
//clear the m_pSoundList
if (!tmp || !*tmp)
m_pSoundList->Clear();
tmp = pszFileName;
m_pSoundList->m_Keyvalues = nullptr;


//create the new new title
//store variables
char buf[1028];
KeyValues* data = nullptr;
Q_snprintf(buf, sizeof(buf), "Soundscape Maker (%s)", tmp);
int curr = 0;


SetTitle(buf, true);
//get subkey
}
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
{
if (Q_strcasecmp(sounds->GetName(), "playsoundscape"))
continue;


//stop all soundscapes before deleting the old soundscapes
if (++curr == number)
m_kvCurrSelected = nullptr;
{
data = sounds;
break;
}
}


if (g_IsPlayingSoundscape)
//no data
PlaySelectedSoundscape();
if (!data)
return;


//delete and set the old keyvalues
m_kvCurrSound = data;
if (m_KeyValues)
m_kvCurrRndwave = nullptr;
m_KeyValues->deleteThis();


m_KeyValues = temp;
//set the random times
m_TimeTextEntry->SetText("");
m_VolumeTextEntry->SetText(data->GetString("volume", "1"));
m_PositionTextEntry->SetText(data->GetString("positionoverride", ""));
m_SoundNameTextEntry->SetText(data->GetString("name", ""));
m_PitchTextEntry->SetText("");


//load the file
//get snd level index
LoadFile(m_KeyValues);
int index = 8; //8 = SNDLVL_NORM
}
const char* name = data->GetString("soundlevel", nullptr);


//-----------------------------------------------------------------------------
//check for the name
// Purpose: Called when a text thing changes
if (name)
//-----------------------------------------------------------------------------
{
void CSoundscapeMaker::OnTextChanged(KeyValues* keyvalues)
{
//check for these things
if (!m_pCurrentSelected || !m_kvCurrSelected)
return;


//check to see if the current focus is the text text entry
//loop through the sound levels to find the right one
if (m_TextEntryName->HasFocus())
for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
{
{
//get text
if (!Q_strcmp(name, g_SoundLevels[i]))
char buf[50];
{
m_TextEntryName->GetText(buf, sizeof(buf));
index = i;
 
break;
//set current text and keyvalue name
}
m_kvCurrSelected->SetName(buf);
}
m_pCurrentSelected->SetText(buf);
}
m_pCurrentSelected->SetCommand(buf);
return;
}


//set dsp
//select the index
m_kvCurrSelected->SetInt("dsp", Clamp<int>(m_DspEffects->GetActiveItem(), 0, 28));
m_SoundLevels->ActivateItem(index);


//if the m_kvCurrSound is nullptr then dont do the rest of the stuff
//enable the text entries
if (!m_kvCurrSound)
m_TimeTextEntry->SetEnabled(true);
return;
m_VolumeTextEntry->SetEnabled(true);
m_PitchTextEntry->SetEnabled(false);
m_PositionTextEntry->SetEnabled(true);
m_SoundLevels->SetEnabled(true);
m_SoundNameTextEntry->SetEnabled(true);
m_TimeTextEntry->SetEnabled(false);
m_SoundNamePlay->SetEnabled(true);


//set the curr sound and stuff
g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
if (m_TimeTextEntry->HasFocus())
g_SoundPanel->SetVisible(false);
{
//get text
char buf[38];
m_TimeTextEntry->GetText(buf, sizeof(buf));


m_kvCurrSound->SetString("time", buf);
m_iSoundscapeMode = SoundscapeMode::Mode_Soundscape;
return;
return;
}
}
}
else if (m_VolumeTextEntry->HasFocus())
else if (Q_stristr(pszCommand, "$rndwave") == pszCommand)
{
{
//get text
if (!m_kvCurrRndwave)
char buf[38];
return;
m_VolumeTextEntry->GetText(buf, sizeof(buf));


m_kvCurrSound->SetString("volume", buf);
//get the selected number
return;
char* str_number = (char*)(pszCommand + 8);
}
m_iCurrRndWave = atoi(str_number);
else if (m_PitchTextEntry->HasFocus())
if (m_iCurrRndWave != 0)
{
{
//dont add to soundscaep
//look for button with same command
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
auto& vec = m_pSoundList->m_MenuButtons;
return;
for (int i = 0; i < vec.Count(); i++)
{
//if the button doesnt have the same command then de-select it. else select it
if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
vec[i]->m_bIsSelected = true;
else
vec[i]->m_bIsSelected = false;
}


//get text
char buf[38];
m_PitchTextEntry->GetText(buf, sizeof(buf));


m_kvCurrSound->SetString("pitch", buf);
int i = 0;
return;
}
else if (m_PositionTextEntry->HasFocus())
{
//get text
char buf[38];
m_PositionTextEntry->GetText(buf, sizeof(buf));


//if the string is empty then remove the position instead
//get value
if (!buf[0])
KeyValues* curr = nullptr;
{
FOR_EACH_VALUE(m_kvCurrRndwave, wave)
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
{
m_kvCurrSound->RemoveSubKey(m_kvCurrSound->FindKey("positionoverride"));
if (++i == m_iCurrRndWave)
else
{
m_kvCurrSound->RemoveSubKey(m_kvCurrSound->FindKey("position"));
curr = wave;
}
break;
else
}
{
}
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
 
m_kvCurrSound->SetString("positionoverride", buf);
//if no curr then throw an error
else
if (!curr)
m_kvCurrSound->SetString("position", buf);
{
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");
 
//show error
char buf[1028];
Q_snprintf(buf, sizeof(buf), "Failed to get rndwave '%d' for subkey \"%s\"\nfor current soundscape file!", i, m_kvCurrSelected->GetName());


}
//show an error
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);
return;
}


return;
m_SoundNameTextEntry->SetEnabled(true);
}
m_SoundNameTextEntry->SetText(curr->GetString());


//get the sound level amount
m_SoundNamePlay->SetEnabled(true);
int sndlevel = Clamp<int>(m_SoundLevels->GetActiveItem(), 0, 20);
m_kvCurrSound->SetString("soundlevel", g_SoundLevels[sndlevel]);


//set soundscape name/wave
m_iSoundscapeMode = SoundscapeMode::Mode_Random;
if (m_SoundNameTextEntry->HasFocus())
return;
{
}
//get text
}
char buf[512];
m_SoundNameTextEntry->GetText(buf, sizeof(buf));


if (m_iSoundscapeMode == SoundscapeMode::Mode_Looping)
//look for button with the same name as the command
m_kvCurrSound->SetString("wave", buf);
{
else if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
//store vars
m_kvCurrSound->SetString("name", buf);
CUtlVector<CSoundscapeButton*>& array = m_SoundscapesList->m_MenuButtons;
else if (m_iSoundscapeMode == SoundscapeMode::Mode_Random && m_kvCurrRndwave)
 
//de-select button
if (m_pCurrentSelected)
m_pCurrentSelected->m_bIsSelected = false;
 
//check for name
for (int i = 0; i < m_SoundscapesList->m_MenuButtons.Size(); i++)
{
//check button name
if (!Q_strcmp(array[i]->GetCommand()->GetString("command"), pszCommand))
{
//found it
m_pCurrentSelected = array[i];
break;
}
}
 
//set needed stuff
if (m_pCurrentSelected)
{
{
//get value
//select button
int i = 0;
m_pCurrentSelected->m_bIsSelected = true;
m_DeleteCurrentButton->SetEnabled(false);
 
//reset the selected kv
m_kvCurrSelected = nullptr;


FOR_EACH_VALUE(m_kvCurrRndwave, wave)
//find selected keyvalues
for (KeyValues* kv = m_KeyValues; kv != nullptr; kv = kv->GetNextTrueSubKey())
{
{
if (++i == m_iCurrRndWave)
if (!Q_strcmp(kv->GetName(), pszCommand))
{
{
wave->SetStringValue(buf);
m_kvCurrSelected = kv;
break;
}
}


//set text on the sounds panel
//set  
vgui::Button* button = m_pSoundList->m_MenuButtons[i - 1];
m_kvCurrSound = nullptr;
if (button)
m_kvCurrRndwave = nullptr;
{
 
//get last / or \ and make the string be that + 1
m_TimeTextEntry->SetEnabled(false);
char* fslash = Q_strrchr(buf, '/');
m_TimeTextEntry->SetText("");
char* bslash = Q_strrchr(buf, '\\');


//no forward slash and no back slash
m_VolumeTextEntry->SetEnabled(false);
if (!fslash && !bslash)
m_VolumeTextEntry->SetText("");
{
button->SetText(buf);
return;
}


if (fslash > bslash)
m_PitchTextEntry->SetEnabled(false);
{
m_PitchTextEntry->SetText("");
button->SetText(fslash + 1);
return;
}


else if (bslash > fslash)
m_PositionTextEntry->SetEnabled(false);
{
m_PositionTextEntry->SetText("");
button->SetText(bslash + 1);
return;
}
}


break;
m_SoundLevels->SetEnabled(false);
}
m_SoundLevels->SetText("");
}
}


return;
m_SoundNameTextEntry->SetEnabled(false);
}
m_SoundNameTextEntry->SetText("");
}


//-----------------------------------------------------------------------------
m_SoundNamePlay->SetEnabled(false);
// Purpose: Loads the keyvalues
//-----------------------------------------------------------------------------
void CSoundscapeMaker::LoadFile(KeyValues* file)
{
//clear all the text's
m_TextEntryName->SetEnabled(false);
m_TextEntryName->SetText("");


m_DspEffects->SetEnabled(false);
if (g_SoundPanel)
m_DspEffects->SetText("");
{
g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
g_SoundPanel->SetVisible(false);
}


m_TimeTextEntry->SetEnabled(false);
//check for current keyvalues. should never bee nullptr but could be
m_TimeTextEntry->SetText("");
if (!m_kvCurrSelected)
{
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");


m_VolumeTextEntry->SetEnabled(false);
//show error
m_VolumeTextEntry->SetText("");
char buf[1028];
Q_snprintf(buf, sizeof(buf), "Failed to find KeyValue subkey \"%s\"\nfor current soundscape file!", pszCommand);


m_PitchTextEntry->SetEnabled(false);
//show an error
m_PitchTextEntry->SetText("");
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);


m_PositionTextEntry->SetEnabled(false);
//reset vars
m_PositionTextEntry->SetText("");
m_pCurrentSelected = nullptr;


m_SoundNameTextEntry->SetEnabled(false);
m_TextEntryName->SetEnabled(false);
m_SoundNameTextEntry->SetText("");
m_TextEntryName->SetText("");


m_SoundNamePlay->SetEnabled(false);
m_DspEffects->SetEnabled(false);
m_DspEffects->SetText("");
return;
}


if (g_SoundPanel)
if (g_IsPlayingSoundscape)
{
PlaySelectedSoundscape();
g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
g_SoundPanel->SetVisible(false);
}


m_PlaySoundscapeButton->SetEnabled(false);
m_DeleteCurrentButton->SetEnabled(true);
m_PlaySoundscapeButton->SetSelected(false);


m_ResetSoundscapeButton->SetEnabled(false);
//set current soundscape name
m_DeleteCurrentButton->SetEnabled(false);
m_TextEntryName->SetText(pszCommand);
m_TextEntryName->SetEnabled(true);
m_pDataList->m_Keyvalues = m_kvCurrSelected;


//clear current file
//set dsp effect
m_pCurrentSelected = nullptr;
int dsp = Clamp<int>(m_kvCurrSelected->GetInt("dsp"), 0, 29);
m_kvCurrSelected = nullptr;
m_kvCurrSound = nullptr;
m_kvCurrRndwave = nullptr;


//clear the menu items
m_PlaySoundscapeButton->SetEnabled(true);
m_SoundscapesList->Clear();
m_pSoundList->Clear();
m_pDataList->Clear();


m_SoundscapesList->m_Keyvalues = file;
m_DspEffects->SetEnabled(true);
m_pDataList->m_Keyvalues = nullptr;
m_DspEffects->ActivateItem(dsp);
m_pSoundList->m_Keyvalues = nullptr;


g_IsPlayingSoundscape = false;
//clear these
m_pDataList->Clear();
m_pSoundList->Clear();
m_pSoundList->m_Keyvalues = nullptr;


//temp soundscapes list
//set variables
CUtlVector<const char*> Added;
int RandomNum = 0;
int LoopingNum = 0;
int SoundscapeNum = 0;


//add all the menu items
FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, data)
for (KeyValues* soundscape = file; soundscape != nullptr; soundscape = soundscape->GetNextTrueSubKey())
{
{
//store data name
//add the menu buttons
const char* name = data->GetName();
const char* name = soundscape->GetName();
 
//increment variables based on name
if (!Q_strcasecmp(name, "playrandom"))
{
RandomNum++;
m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playrandom%d", RandomNum), this, data, SoundscapeClipboardType::Type_SoundscapeData);
}
 
if (!Q_strcasecmp(name, "playlooping"))
{
LoopingNum++;
m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playlooping%d", LoopingNum), this, data, SoundscapeClipboardType::Type_SoundscapeData);
}


//check for the soundscape first
if (!Q_strcasecmp(name, "playsoundscape"))
if (Added.Find(name) != Added.InvalidIndex())
{
{
SoundscapeNum++;
ConWarning("CSoundscapePanel: Failed to add repeated soundscape '%s'\n", name);
m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playsoundscape%d", SoundscapeNum), this, data, SoundscapeClipboardType::Type_SoundscapeData);
continue;
}
}
}
}
Added.AddToTail(name);
m_SoundscapesList->AddButton(name, name, name, this);
}
}


m_SoundscapesList->m_pSideSlider->SetValue(0);
BaseClass::OnCommand(pszCommand);
m_SoundscapesList->ScrollBarMoved(0);
 
//
OnCommand(file->GetName());
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Sets the sounds text
// Purpose: Paste item from clipboard
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeMaker::SetSoundText(const char* text)
void CSoundscapeMaker::Paste(SoundscapeClipboardType type)
{
{
m_SoundNameTextEntry->SetText(text);
switch (type)
 
//set soundscape name/wave
if (m_iSoundscapeMode != SoundscapeMode::Mode_Random)
{
{
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
case SoundscapeClipboardType::Type_SoundscapeName:
m_kvCurrSound->SetString("name", text);
m_SoundscapesList->OnCommand(PASTE_FROM_CLIBOARD_COMMAND);
else
break;
m_kvCurrSound->SetString("wave", text);
case SoundscapeClipboardType::Type_SoundscapeData:
}
m_pDataList->OnCommand(PASTE_FROM_CLIBOARD_COMMAND);
else
break;
{
case SoundscapeClipboardType::Type_SoundscapeRandomWave:
//get value
m_pSoundList->OnCommand(PASTE_FROM_CLIBOARD_COMMAND);
int i = 0;
break;
}
}
 
//-----------------------------------------------------------------------------
// Purpose: Function to recursivly write keyvalues to keyvalue files. the keyvalues
// class does have a function to do this BUT this function writes every single
// item one after another. this function does that but writes the keys
// first then the subkeys so the order is good.
//-----------------------------------------------------------------------------
void RecursivlyWriteKeyvalues(KeyValues* prev, CUtlBuffer& buffer, int& indent)
{
//write \t indent
for (int i = 0; i < indent; i++)
buffer.PutChar('\t');


FOR_EACH_VALUE(m_kvCurrRndwave, wave)
//write name
{
buffer.PutChar('"');
if (++i == m_iCurrRndWave)
buffer.PutString(prev->GetName());
{
buffer.PutString("\"\n");
wave->SetStringValue(text);


//set text on the sounds panel
//write {
vgui::Button* button = m_pSoundList->m_MenuButtons[i - 1];
for (int i = 0; i < indent; i++)
if (button)
buffer.PutChar('\t');
{
//get last / or \ and make the string be that + 1
char* fslash = Q_strrchr(text, '/');
char* bslash = Q_strrchr(text, '\\');


//no forward slash and no back slash
buffer.PutString("{\n");
if (!fslash && !bslash)
{
button->SetText(text);
return;
}


if (fslash > bslash)
//increment indent
{
indent++;
button->SetText(fslash + 1);
 
return;
//write all the keys first
}
FOR_EACH_VALUE(prev, value)
{
for (int i = 0; i < indent; i++)
buffer.PutChar('\t');
 
//write name and value
buffer.PutChar('"');
buffer.PutString(value->GetName());
buffer.PutString("\"\t");
 
buffer.PutChar('"');
buffer.PutString(value->GetString());
buffer.PutString("\"\n");
}


else if (bslash > fslash)
//write all the subkeys now
{
FOR_EACH_TRUE_SUBKEY(prev, value)
button->SetText(bslash + 1);
{
return;
//increment indent
}
RecursivlyWriteKeyvalues(value, buffer, indent);
}


break;
if (value->GetNextTrueSubKey())
}
buffer.PutChar('\n');
}
}
}


return;
//decrement indent
indent--;
 
//write ending }
for (int i = 0; i < indent; i++)
buffer.PutChar('\t');
 
buffer.PutString("}\n");
}
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: Called when a keyboard key is pressed
// Purpose: Called when a file gets opened/closed
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnKeyCodePressed(vgui::KeyCode code)
void CSoundscapeMaker::OnFileSelected(const char* pszFileName)
{
{
//check for ctrl o or ctrl s
//check for null or empty string
if (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LCONTROL) ||
if (!pszFileName || pszFileName[0] == '\0')
vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RCONTROL))
{
if (code == vgui::KeyCode::KEY_O)
OnCommand(LOAD_BUTTON_COMMAND);
else if (code == vgui::KeyCode::KEY_S)
OnCommand(SAVE_BUTTON_COMMAND);
else if (code == vgui::KeyCode::KEY_N)
OnCommand(NEW_BUTTON_COMMAND);
else if (code == vgui::KeyCode::KEY_P)
{
//show settings
g_SettingsPanel->SetVisible(true);
g_SettingsPanel->RequestFocus();
g_SettingsPanel->MoveToFront();
}
else if (code == vgui::KeyCode::KEY_D)
OnCommand(DELETE_CURRENT_ITEM_COMMAND);
 
//check for ctrl+alt+a
else if (code == vgui::KeyCode::KEY_A && (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LALT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RALT)) && m_pSoundList && m_pSoundList->m_Keyvalues)
m_pSoundList->OnCommand(NEW_RNDWAVE_WAVE_COMMAND);
 
//check for just ctrl+shift+a
else if (code == vgui::KeyCode::KEY_A && (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LSHIFT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RSHIFT)) && m_SoundscapesList)
m_SoundscapesList->OnCommand(ADD_SOUNDSCAPE_COMMAND);
 
return;
return;
}


//check for arrow keys
//check for file save
if (code == KEY_DOWN || code == KEY_UP)
if (!m_bWasFileLoad)
{
{
if (m_kvCurrRndwave)
//save the file
if (m_KeyValues)
{
{
m_pSoundList->OnKeyCodePressed(code);
//write everything into a buffer
}
CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
else if (m_kvCurrSelected && m_pDataList->m_MenuButtons.Count())
buf.PutString("//------------------------------------------------------------------------------------\n");
{
buf.PutString("//\n");
m_pDataList->OnKeyCodePressed(code);
buf.PutString("// Auto-generated soundscape file created with modbases soundscape tool'\n");
}
buf.PutString("//\n");
else
buf.PutString("//------------------------------------------------------------------------------------\n");
{
m_SoundscapesList->OnKeyCodePressed(code);
}


return;
//now write the keyvalues
}
KeyValues* pCurrent = m_KeyValues;
while (pCurrent)
{
int indent = 0;
RecursivlyWriteKeyvalues(pCurrent, buf, indent);


//get key bound to this
//put a newline
const char* key = engine->Key_LookupBinding("modbase_soundscape_panel");
if (pCurrent->GetNextTrueSubKey())
if (!key)
buf.PutChar('\n');
return;


//convert the key to a keyboard code
//get next
const char* keystring = KeyCodeToString(code);
pCurrent = pCurrent->GetNextTrueSubKey();
}


//remove the KEY_ if found
if (!g_pFullFileSystem->WriteFile(pszFileName, "MOD", buf))
if (Q_strstr(keystring, "KEY_") == keystring)
{
keystring = keystring + 4;
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");


//check both strings
//get the error first
if (!Q_strcasecmp(key, keystring))
char buf[1028];
OnClose();
Q_snprintf(buf, sizeof(buf), "Failed to save soundscape to file \"%s\"", pszFileName);
}


//-----------------------------------------------------------------------------
//show an error
// Purpose: Starts the soundscape on map spawn
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
//-----------------------------------------------------------------------------
popup->SetOKButtonText("Ok");
void CSoundscapeMaker::LevelInitPostEntity()
popup->SetCancelButtonVisible(false);
{
popup->AddActionSignalTarget(this);
if (g_IsPlayingSoundscape)
popup->DoModal(this);
PlaySelectedSoundscape();
return;
}
}
}


//-----------------------------------------------------------------------------
// Purpose: Sets keyvalues from text
//-----------------------------------------------------------------------------
void CSoundscapeMaker::Set(const char* buffer)
{
CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
buf.PutString(buffer);


//try and load the keyvalues file first
//store vars
KeyValues* temp = new KeyValues("SoundscapeFile");
const char* last = pszFileName;
if (!temp->LoadFromBuffer("Text Editor Panel Buffer", buf))
const char* tmp = nullptr;
{
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");


//show an error
//get the last /
vgui::QueryBox* popup = new vgui::QueryBox("Error", "Failed to open keyvalues data from \"Text Editor Panel\"", this);
while ((last = Q_strstr(last, "\\")) != nullptr)
popup->SetOKButtonText("Ok");
tmp = ++last; //move past the backslash
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);


temp->deleteThis();
//check tmp
return;
if (!tmp || !*tmp)
}
tmp = pszFileName;


//stop all soundscapes before deleting the old soundscapes
//set new title
m_kvCurrSelected = nullptr;
char buf[1028];
Q_snprintf(buf, sizeof(buf), "Soundscape Maker (%s)", tmp);


if (g_IsPlayingSoundscape)
SetTitle(buf, true);
PlaySelectedSoundscape();


//delete and set the old keyvalues
//create copy of pszFileName
if (m_KeyValues)
char manifest[1028];
m_KeyValues->deleteThis();
Q_strncpy(manifest, pszFileName, sizeof(manifest));


m_KeyValues = temp;
//get last /
char* lastSlash = Q_strrchr(manifest, '\\');
if (lastSlash == nullptr || *lastSlash == '\0')
return;


//load the file
//append 'soundscapes_manifest.txt'
LoadFile(m_KeyValues);
lastSlash[1] = '\0';
}
strcat(manifest, "soundscapes_manifest.txt");


//-----------------------------------------------------------------------------
//see if we can open manifest file
// Purpose: Destructor for soundscape maker panel
KeyValues* man_file = new KeyValues("manifest");
//-----------------------------------------------------------------------------
if (!man_file->LoadFromFile(g_pFullFileSystem, manifest))
CSoundscapeMaker::~CSoundscapeMaker()
{
{
//cant open manifest file
//delete the keyvalue files if needed
man_file->deleteThis();
if (m_KeyValues)
return;
m_KeyValues->deleteThis();
}
}
 
//get real filename
pszFileName = Q_strrchr(pszFileName, '\\');
if (!pszFileName || !*pszFileName)
{
man_file->deleteThis();
return;
}
 
pszFileName = pszFileName + 1;
 
//create name to be added to the manifest file
char add_file[1028];
Q_snprintf(add_file, sizeof(add_file), "scripts/%s", pszFileName);
 
//add filename to manifest file if not found
FOR_EACH_VALUE(man_file, value)
{
if (!Q_strcmp(value->GetString(), add_file))
{
man_file->deleteThis();
return;
}
}


//static panel instance
static CSoundscapeMaker* g_SSMakerPanel = nullptr;


//interface class
//add to manifest file
class CSoundscapeMakerInterface : public ISoundscapeMaker
KeyValues* kv = new KeyValues("file");
{
kv->SetString(nullptr, add_file);
public:
man_file->AddSubKey(kv);
void Create(vgui::VPANEL parent)
 
{
//write to file
g_SSMakerPanel = new CSoundscapeMaker(parent);
man_file->SaveToFile(g_pFullFileSystem, manifest);
g_SoundPanel = new CSoundListPanel(parent, "SoundscapeSoundListPanel");
 
g_SettingsPanel = new CSoundscapeSettingsPanel(parent, "SoundscapeSettingsPanel");
man_file->deleteThis();
g_SoundscapeTextPanel = new CSoundscapeTextPanel(parent, "SoundscapeTextPanel");
return;
g_SoundscapeDebugPanel = new CSoundscapeDebugPanel(parent, "SoundscapeDebugPanel");
}
}


void SetVisible(bool bVisible)
//try and load the keyvalues file first
KeyValues* temp = new KeyValues("SoundscapeFile");
if (!temp->LoadFromFile(filesystem, pszFileName))
{
{
if (g_SSMakerPanel)
//play an error sound
g_SSMakerPanel->SetVisible(bVisible);
vgui::surface()->PlaySound("resource/warning.wav");
 
//get the error first
char buf[1028];
Q_snprintf(buf, sizeof(buf), "Failed to open keyvalues file \"%s\"", pszFileName);
 
//show an error
vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);
 
temp->deleteThis();
return;
}
}


void Destroy()
//set the new title
{
{
if (g_SSMakerPanel)
//store vars
g_SSMakerPanel->DeletePanel();
const char* last = pszFileName;
 
const char* tmp = nullptr;
if (g_SoundPanel)
 
g_SoundPanel->DeletePanel();
//get the last /
 
while ((last = Q_strstr(last, "\\")) != nullptr)
if (g_SettingsPanel)
tmp = ++last; //move past the backslash
g_SettingsPanel->DeletePanel();
 
 
//check tmp
if (g_SoundscapeTextPanel)
if (!tmp || !*tmp)
g_SoundscapeTextPanel->DeletePanel();
tmp = pszFileName;
 
 
if (g_SoundscapeDebugPanel)
//create the new new title
g_SoundscapeDebugPanel->DeletePanel();
char buf[1028];
 
Q_snprintf(buf, sizeof(buf), "Soundscape Maker (%s)", tmp);
g_SSMakerPanel = nullptr;
 
g_SoundPanel = nullptr;
SetTitle(buf, true);
g_SettingsPanel = nullptr;
}
g_SoundscapeTextPanel = nullptr;
 
g_SoundscapeDebugPanel = nullptr;
//stop all soundscapes before deleting the old soundscapes
m_kvCurrSelected = nullptr;
 
if (g_IsPlayingSoundscape)
PlaySelectedSoundscape();
 
//delete and set the old keyvalues
if (m_KeyValues)
m_KeyValues->deleteThis();
 
m_KeyValues = temp;
 
//load the file
LoadFile(m_KeyValues);
}
 
//-----------------------------------------------------------------------------
// Purpose: Called when a text thing changes
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnTextChanged(KeyValues* keyvalues)
{
//check for these things
if (!m_pCurrentSelected || !m_kvCurrSelected)
return;
 
//check to see if the current focus is the text text entry
if (m_TextEntryName->HasFocus())
{
//get text
char buf[50];
m_TextEntryName->GetText(buf, sizeof(buf));
 
//set current text and keyvalue name
m_kvCurrSelected->SetName(buf);
m_pCurrentSelected->SetText(buf);
m_pCurrentSelected->SetCommand(buf);
return;
}
 
//set dsp
m_kvCurrSelected->SetInt("dsp", Clamp<int>(m_DspEffects->GetActiveItem(), 0, 28));
 
//if the m_kvCurrSound is nullptr then dont do the rest of the stuff
if (!m_kvCurrSound)
return;
 
//set the curr sound and stuff
if (m_TimeTextEntry->HasFocus())
{
//get text
char buf[38];
m_TimeTextEntry->GetText(buf, sizeof(buf));
 
m_kvCurrSound->SetString("time", buf);
return;
}
else if (m_VolumeTextEntry->HasFocus())
{
//get text
char buf[38];
m_VolumeTextEntry->GetText(buf, sizeof(buf));
 
m_kvCurrSound->SetString("volume", buf);
return;
}
else if (m_PitchTextEntry->HasFocus())
{
//dont add to soundscaep
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
return;
 
//get text
char buf[38];
m_PitchTextEntry->GetText(buf, sizeof(buf));
 
m_kvCurrSound->SetString("pitch", buf);
return;
}
else if (m_PositionTextEntry->HasFocus())
{
//get text
char buf[38];
m_PositionTextEntry->GetText(buf, sizeof(buf));
 
//if the string is empty then remove the position instead
if (!buf[0])
{
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
m_kvCurrSound->RemoveSubKey(m_kvCurrSound->FindKey("positionoverride"));
else
m_kvCurrSound->RemoveSubKey(m_kvCurrSound->FindKey("position"));
}
else
{
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
m_kvCurrSound->SetString("positionoverride", buf);
else
m_kvCurrSound->SetString("position", buf);
 
}
 
return;
}
 
//get the sound level amount
int sndlevel = Clamp<int>(m_SoundLevels->GetActiveItem(), 0, 20);
m_kvCurrSound->SetString("soundlevel", g_SoundLevels[sndlevel]);
 
//set soundscape name/wave
if (m_SoundNameTextEntry->HasFocus())
{
//get text
char buf[512];
m_SoundNameTextEntry->GetText(buf, sizeof(buf));
 
if (m_iSoundscapeMode == SoundscapeMode::Mode_Looping)
m_kvCurrSound->SetString("wave", buf);
else if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
m_kvCurrSound->SetString("name", buf);
else if (m_iSoundscapeMode == SoundscapeMode::Mode_Random && m_kvCurrRndwave)
{
//get value
int i = 0;
 
FOR_EACH_VALUE(m_kvCurrRndwave, wave)
{
if (++i == m_iCurrRndWave)
{
wave->SetStringValue(buf);
 
//set text on the sounds panel
vgui::Button* button = m_pSoundList->m_MenuButtons[i - 1];
if (button)
{
//get last / or \ and make the string be that + 1
char* fslash = Q_strrchr(buf, '/');
char* bslash = Q_strrchr(buf, '\\');
 
//no forward slash and no back slash
if (!fslash && !bslash)
{
button->SetText(buf);
return;
}
 
if (fslash > bslash)
{
button->SetText(fslash + 1);
return;
}
 
else if (bslash > fslash)
{
button->SetText(bslash + 1);
return;
}
}
 
break;
}
}
}
 
return;
}
}
 
//-----------------------------------------------------------------------------
// Purpose: Loads the keyvalues
//-----------------------------------------------------------------------------
void CSoundscapeMaker::LoadFile(KeyValues* file)
{
//clear all the text's
m_TextEntryName->SetEnabled(false);
m_TextEntryName->SetText("");
 
m_DspEffects->SetEnabled(false);
m_DspEffects->SetText("");
 
m_TimeTextEntry->SetEnabled(false);
m_TimeTextEntry->SetText("");
 
m_VolumeTextEntry->SetEnabled(false);
m_VolumeTextEntry->SetText("");
 
m_PitchTextEntry->SetEnabled(false);
m_PitchTextEntry->SetText("");
 
m_PositionTextEntry->SetEnabled(false);
m_PositionTextEntry->SetText("");
 
m_SoundNameTextEntry->SetEnabled(false);
m_SoundNameTextEntry->SetText("");
 
m_SoundNamePlay->SetEnabled(false);
 
if (g_SoundPanel)
{
g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
g_SoundPanel->SetVisible(false);
}
 
m_PlaySoundscapeButton->SetEnabled(false);
m_PlaySoundscapeButton->SetSelected(false);
 
m_ResetSoundscapeButton->SetEnabled(false);
m_DeleteCurrentButton->SetEnabled(false);
 
//clear current file
m_pCurrentSelected = nullptr;
m_kvCurrSelected = nullptr;
m_kvCurrSound = nullptr;
m_kvCurrRndwave = nullptr;
 
//clear the menu items
m_SoundscapesList->Clear();
m_pSoundList->Clear();
m_pDataList->Clear();
 
m_SoundscapesList->m_Keyvalues = file;
m_pDataList->m_Keyvalues = nullptr;
m_pSoundList->m_Keyvalues = nullptr;
 
g_IsPlayingSoundscape = false;
 
//temp soundscapes list
CUtlVector<const char*> Added;
 
//add all the menu items
for (KeyValues* soundscape = file; soundscape != nullptr; soundscape = soundscape->GetNextTrueSubKey())
{
//add the menu buttons
const char* name = soundscape->GetName();
 
//check for the soundscape first
if (Added.Find(name) != Added.InvalidIndex())
{
ConWarning("CSoundscapePanel: Failed to add repeated soundscape '%s'\n", name);
continue;
}
 
Added.AddToTail(name);
m_SoundscapesList->AddButton(name, name, name, this, soundscape, SoundscapeClipboardType::Type_SoundscapeName);
}
 
m_SoundscapesList->m_pSideSlider->SetValue(0);
m_SoundscapesList->ScrollBarMoved(0);
 
//
OnCommand(file->GetName());
}
 
//-----------------------------------------------------------------------------
// Purpose: Sets the sounds text
//-----------------------------------------------------------------------------
void CSoundscapeMaker::SetSoundText(const char* text)
{
m_SoundNameTextEntry->SetText(text);
 
//set soundscape name/wave
if (m_iSoundscapeMode != SoundscapeMode::Mode_Random)
{
if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
m_kvCurrSound->SetString("name", text);
else
m_kvCurrSound->SetString("wave", text);
}
else
{
//get value
int i = 0;
 
FOR_EACH_VALUE(m_kvCurrRndwave, wave)
{
if (++i == m_iCurrRndWave)
{
wave->SetStringValue(text);
 
//set text on the sounds panel
vgui::Button* button = m_pSoundList->m_MenuButtons[i - 1];
if (button)
{
//get last / or \ and make the string be that + 1
char* fslash = Q_strrchr(text, '/');
char* bslash = Q_strrchr(text, '\\');
 
//no forward slash and no back slash
if (!fslash && !bslash)
{
button->SetText(text);
return;
}
 
if (fslash > bslash)
{
button->SetText(fslash + 1);
return;
}
 
else if (bslash > fslash)
{
button->SetText(bslash + 1);
return;
}
}
 
break;
}
}
}
 
return;
}
 
//-----------------------------------------------------------------------------
// Purpose: Called when a keyboard key is pressed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnKeyCodePressed(vgui::KeyCode code)
{
//check for ctrl o or ctrl s
if (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LCONTROL) ||
vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RCONTROL))
{
if (code == vgui::KeyCode::KEY_O)
OnCommand(LOAD_BUTTON_COMMAND);
else if (code == vgui::KeyCode::KEY_S)
OnCommand(SAVE_BUTTON_COMMAND);
else if (code == vgui::KeyCode::KEY_N)
OnCommand(NEW_BUTTON_COMMAND);
else if (code == vgui::KeyCode::KEY_P)
{
//show settings
g_SettingsPanel->SetVisible(true);
g_SettingsPanel->RequestFocus();
g_SettingsPanel->MoveToFront();
}
 
else if (code == vgui::KeyCode::KEY_D)
OnCommand(DELETE_CURRENT_ITEM_COMMAND);
 
//check for ctrl+alt+a
else if (code == vgui::KeyCode::KEY_A && (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LALT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RALT)) && m_pSoundList && m_pSoundList->m_Keyvalues)
m_pSoundList->OnCommand(NEW_RNDWAVE_WAVE_COMMAND);
 
//check for just ctrl+shift+a
else if (code == vgui::KeyCode::KEY_A && (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LSHIFT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RSHIFT)) && m_SoundscapesList)
m_SoundscapesList->OnCommand(ADD_SOUNDSCAPE_COMMAND);
 
return;
}
 
//check for arrow keys
if (code == KEY_DOWN || code == KEY_UP)
{
if (m_kvCurrRndwave)
{
m_pSoundList->OnKeyCodePressed(code);
}
else if (m_kvCurrSelected && m_pDataList->m_MenuButtons.Count())
{
m_pDataList->OnKeyCodePressed(code);
}
else
{
m_SoundscapesList->OnKeyCodePressed(code);
}
 
return;
}
 
//get key bound to this
const char* key = engine->Key_LookupBinding("modbase_soundscape_panel");
if (!key)
return;
 
//convert the key to a keyboard code
const char* keystring = KeyCodeToString(code);
 
//remove the KEY_ if found
if (Q_strstr(keystring, "KEY_") == keystring)
keystring = keystring + 4;
 
//check both strings
if (!Q_strcasecmp(key, keystring))
OnClose();
}
 
//-----------------------------------------------------------------------------
// Purpose: Starts the soundscape on map spawn
//-----------------------------------------------------------------------------
void CSoundscapeMaker::LevelInitPostEntity()
{
if (g_IsPlayingSoundscape)
PlaySelectedSoundscape();
}
 
//-----------------------------------------------------------------------------
// Purpose: Sets keyvalues from text
//-----------------------------------------------------------------------------
void CSoundscapeMaker::Set(const char* buffer)
{
CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
buf.PutString(buffer);
 
//try and load the keyvalues file first
KeyValues* temp = new KeyValues("SoundscapeFile");
if (!temp->LoadFromBuffer("Text Editor Panel Buffer", buf))
{
//play an error sound
vgui::surface()->PlaySound("resource/warning.wav");
 
//show an error
vgui::QueryBox* popup = new vgui::QueryBox("Error", "Failed to open keyvalues data from \"Text Editor Panel\"", this);
popup->SetOKButtonText("Ok");
popup->SetCancelButtonVisible(false);
popup->AddActionSignalTarget(this);
popup->DoModal(this);
 
temp->deleteThis();
return;
}
 
//stop all soundscapes before deleting the old soundscapes
m_kvCurrSelected = nullptr;
 
if (g_IsPlayingSoundscape)
PlaySelectedSoundscape();
 
//delete and set the old keyvalues
if (m_KeyValues)
m_KeyValues->deleteThis();
 
m_KeyValues = temp;
 
//load the file
LoadFile(m_KeyValues);
}
 
//-----------------------------------------------------------------------------
// Purpose: Destructor for soundscape maker panel
//-----------------------------------------------------------------------------
CSoundscapeMaker::~CSoundscapeMaker()
{
//delete the keyvalue files if needed
if (m_KeyValues)
m_KeyValues->deleteThis();
}
 
//static panel instance
static CSoundscapeMaker* g_SSMakerPanel = nullptr;
 
//interface class
class CSoundscapeMakerInterface : public ISoundscapeMaker
{
public:
void Create(vgui::VPANEL parent)
{
g_SSMakerPanel = new CSoundscapeMaker(parent);
g_SoundPanel = new CSoundListPanel(parent, "SoundscapeSoundListPanel");
g_SettingsPanel = new CSoundscapeSettingsPanel(parent, "SoundscapeSettingsPanel");
g_SoundscapeTextPanel = new CSoundscapeTextPanel(parent, "SoundscapeTextPanel");
g_SoundscapeDebugPanel = new CSoundscapeDebugPanel(parent, "SoundscapeDebugPanel");
}
 
void SetVisible(bool bVisible)
{
if (g_SSMakerPanel)
g_SSMakerPanel->SetVisible(bVisible);
}
 
void Destroy()
{
if (g_SSMakerPanel)
g_SSMakerPanel->DeletePanel();
 
if (g_SoundPanel)
g_SoundPanel->DeletePanel();
 
if (g_SettingsPanel)
g_SettingsPanel->DeletePanel();
 
if (g_SoundscapeTextPanel)
g_SoundscapeTextPanel->DeletePanel();
 
if (g_SoundscapeDebugPanel)
g_SoundscapeDebugPanel->DeletePanel();
 
if (g_SoundscapeClipboard)
g_SoundscapeClipboard->DeletePanel();
 
g_SSMakerPanel = nullptr;
g_SoundPanel = nullptr;
g_SettingsPanel = nullptr;
g_SoundscapeTextPanel = nullptr;
g_SoundscapeDebugPanel = nullptr;
g_SoundscapeClipboard = nullptr;
}
 
void SetSoundText(const char* text)
{
if (!g_SSMakerPanel)
return;
 
g_SSMakerPanel->SetSoundText(text);
g_SSMakerPanel->RequestFocus();
g_SSMakerPanel->MoveToFront();
}
 
void SetAllVisible(bool bVisible)
{
g_ShowSoundscapePanel = false;
 
if (g_SSMakerPanel)
g_SSMakerPanel->SetVisible(bVisible);
 
if (g_SoundPanel)
g_SoundPanel->SetVisible(bVisible);
 
if (g_SettingsPanel)
g_SettingsPanel->SetVisible(bVisible);
 
if (g_SoundscapeTextPanel)
g_SoundscapeTextPanel->SetVisible(bVisible);
 
if (g_SoundscapeDebugPanel)
g_SoundscapeDebugPanel->SetVisible(bVisible);
}
 
void SetBuffer(const char* text)
{
if (g_SSMakerPanel)
g_SSMakerPanel->Set(text);
}
}


void SetSoundText(const char* text)
KeyValues* GetPanelFile()
{
{
if (!g_SSMakerPanel)
return g_SSMakerPanel->m_KeyValues;
return;
 
g_SSMakerPanel->SetSoundText(text);
g_SSMakerPanel->RequestFocus();
g_SSMakerPanel->MoveToFront();
}
}


void SetAllVisible(bool bVisible)
KeyValues* GetPanelSelected()
{
{
g_ShowSoundscapePanel = false;
return g_SSMakerPanel->m_kvCurrSelected;
 
if (g_SSMakerPanel)
g_SSMakerPanel->SetVisible(bVisible);
 
if (g_SoundPanel)
g_SoundPanel->SetVisible(bVisible);
 
if (g_SettingsPanel)
g_SettingsPanel->SetVisible(bVisible);
 
if (g_SoundscapeTextPanel)
g_SoundscapeTextPanel->SetVisible(bVisible);
 
if (g_SoundscapeDebugPanel)
g_SoundscapeDebugPanel->SetVisible(bVisible);
}
}


void SetBuffer(const char* text)
void PasteFromClipboard(int type)
{
{
if (g_SSMakerPanel)
g_SSMakerPanel->Paste((SoundscapeClipboardType)type);
g_SSMakerPanel->Set(text);
}
}
};
};

Latest revision as of 18:40, 25 June 2025

vgui_soundscape_maker.cpp file needed for vgui soundscape maker

//========= Created by Waddelz. https://www.youtube.com/@WadDeIz_Real. ============//
//
// Purpose: a vgui panel that allows you to create and test soundscapes in game
//
// $NoKeywords: $
//
//=================================================================================//
#include "cbase.h"
#include "c_soundscape.h"
#include "vgui_soundscape_maker.h"
#include <vgui_controls/Frame.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/Divider.h>
#include <vgui_controls/FileOpenDialog.h>
#include <vgui_controls/QueryBox.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/ScrollBar.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/TextEntry.h>
#include <vgui_controls/ComboBox.h>
#include <vgui_controls/CheckButton.h>
#include <vgui_controls/Menu.h>
#include <vgui_controls/MenuItem.h>
#include <vgui_controls/RichText.h>
#include <vgui/ISystem.h>
#include <engine/IEngineSound.h>
#include <vgui/IVGui.h>
#include <vgui/IInput.h>
#include <vgui/ISurface.h>
#include <ienginevgui.h>
#include <filesystem.h>
#include <usermessages.h>
#include <fmtstr.h>

//graph panel for debugging

class CGraphPanel : public vgui::Panel
{
public:
	DECLARE_CLASS_SIMPLE(CGraphPanel, vgui::Panel);

	CGraphPanel(Panel* parent, const char* panelName);

	//think and paint
	virtual void OnThink();
	virtual void Paint();

	//start and stop functions
	virtual void Start();
	virtual void Stop();
	virtual void Restart();
	virtual void Clear();

	//Adding/doing stuff to lines functions
	virtual void AddLine(bool ascending, unsigned char r, unsigned char g, unsigned char b, float speed, float flGraphWidthFraction = 1.0f);
	virtual void RemoveLine(int index);

	//set functions
	virtual void SetDuration(float seconds) { m_flDuration = seconds; }
	virtual void SetHorizontalLinesMax(float seconds) { m_nHorizontalLinesMax = seconds; }
	virtual void SetMaxTextValue(float maxvalue) { m_flMaxValue = maxvalue; }

	//other
	virtual void ApplySchemeSettings(vgui::IScheme* scheme);

	//sets the font
	void SetFont(vgui::HFont font) { m_Font = font; }
	inline vgui::HFont GetFont() { return m_Font; };

	//gets the number of lines
	int GetNumLines() { return m_Lines.Count(); }

private:

	//line information
	struct LineInfo
	{
		float startTime;
		float offset;
		float elapsedWhenStopped;
		float m_flGraphWidthFraction;
		bool ascending;
		unsigned char r, g, b;
		float speed;
	};

	//lines and other stuff
	CUtlVector<LineInfo> m_Lines;
	float m_flDuration;
	float m_flTimeOffset;
	bool m_bRunning;

	//mad horizontal lines
	int m_nHorizontalLinesMax = 2;
	float m_flMaxValue = 1.0f;

	vgui::HFont m_Font;
};

extern vgui::ILocalize* g_pVGuiLocalize;

//-----------------------------------------------------------------------------
// Purpose: Graph panel
//-----------------------------------------------------------------------------
CGraphPanel::CGraphPanel(Panel* parent, const char* panelName)
	: BaseClass(parent, panelName)
{
	SetPaintBackgroundEnabled(false);
	m_flDuration = 2.0f;
	m_bRunning = false;
	m_flTimeOffset = 0.0f;
	m_Font = vgui::INVALID_FONT;
	SetBgColor(Color(0, 0, 0, 255));
}

//-----------------------------------------------------------------------------
// Purpose: Called when this panel thinks
//-----------------------------------------------------------------------------
void CGraphPanel::OnThink()
{
	if (m_bRunning)
		Repaint();
}

//-----------------------------------------------------------------------------
// Purpose: Called when this panel paints
//-----------------------------------------------------------------------------
void CGraphPanel::Paint()
{
	//get size
	int w, h;
	GetSize(w, h);

	//set fill background
	vgui::surface()->DrawSetColor(GetBgColor());
	vgui::surface()->DrawFilledRect(0, 0, w, h);

	//draw horizontal grid lines
	if (m_nHorizontalLinesMax > 1)
	{
		vgui::surface()->DrawSetColor(100, 100, 100, 128); //grey lines

		//draw lines
		for (int i = 0; i < m_nHorizontalLinesMax; ++i)
		{
			float frac = (float)i / (m_nHorizontalLinesMax - 1);
			int y = h - (int)(frac * h);

			//draw the line
			vgui::surface()->DrawLine(0, y, w, y);

			float labelValue = frac * m_flMaxValue;

			char buf[32];
			Q_snprintf(buf, sizeof(buf), "%.2f", labelValue);

			vgui::surface()->DrawSetTextFont(GetFont());
			vgui::surface()->DrawSetTextColor(255, 255, 255, 255);

			//set text pos
			if (labelValue == m_flMaxValue)
				vgui::surface()->DrawSetTextPos(5, y);
			else if (labelValue <= 0.0f)
				vgui::surface()->DrawSetTextPos(5, y - 14);
			else
				vgui::surface()->DrawSetTextPos(5, y - 8);

			wchar_t wbuf[32];
			g_pVGuiLocalize->ConvertANSIToUnicode(buf, wbuf, sizeof(wbuf));
			vgui::surface()->DrawPrintText(wbuf, wcslen(wbuf));
		}
	}

	//get now time
	float now = vgui::system()->GetFrameTime();

	//now draw the graphs
	for (int i = 0; i < m_Lines.Count(); ++i)
	{
		//get line info
		const LineInfo& line = m_Lines[i];

		//set color
		vgui::surface()->DrawSetColor(line.r, line.g, line.b, 255);

		//do stuff
		float elapsed = m_bRunning ? (now - line.startTime - line.offset) : line.elapsedWhenStopped;
		if (elapsed < 0.0f)
			continue;

		float effectiveDuration = m_flDuration / line.speed;

		if (elapsed > effectiveDuration)
			elapsed = effectiveDuration;

		float progress = elapsed / effectiveDuration;

		int lastX = -1;
		int lastY = -1;

		// Calculate the maximum possible width fraction for this line considering the offset
		float maxWidthForLine = line.m_flGraphWidthFraction - line.offset;
		if (maxWidthForLine < 0.0f)
			maxWidthForLine = 0.0f; // prevent negative width

		//make 128 line points
		for (int j = 0; j < 128; ++j)
		{
			float t = (float)j / 127.0f;

			// Stop drawing if t > progress for this line
			if (t > progress)
				break;

			float curve;
			if (line.ascending)
				curve = t * t * t;
			else
				curve = 1.0f - t * t;

			// Calculate the X position using offset + scaled max width for this line
			float combinedPos = line.offset + t * maxWidthForLine;

			// Stop if combinedPos is beyond max graph width fraction (to not draw outside graph area)
			if (combinedPos > line.m_flGraphWidthFraction)
				break;

			int x = (int)(w * combinedPos);
			int y = (int)(h - curve * h);

			//draw the line
			if (lastX >= 0 && lastY >= 0)
				vgui::surface()->DrawLine(lastX, lastY, x, y);

			lastX = x;
			lastY = y;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Starts drawing the graphs
//-----------------------------------------------------------------------------
void CGraphPanel::Start()
{
	//check for not running
	if (!m_bRunning)
	{
		float now = vgui::system()->GetFrameTime();
		for (int i = 0; i < m_Lines.Count(); ++i)
		{
			if (m_Lines[i].elapsedWhenStopped < m_flDuration / m_Lines[i].speed)
				m_Lines[i].startTime = now - m_Lines[i].elapsedWhenStopped;
		}

		//start running
		m_bRunning = true;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Stops drawing the graphs
//-----------------------------------------------------------------------------
void CGraphPanel::Stop()
{
	//check for running
	if (m_bRunning)
	{
		float now = vgui::system()->GetFrameTime();
		for (int i = 0; i < m_Lines.Count(); ++i)
			m_Lines[i].elapsedWhenStopped = now - m_Lines[i].startTime;

		//stop running
		m_bRunning = false;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Clears the line graph
//-----------------------------------------------------------------------------
void CGraphPanel::Clear()
{
	m_Lines.RemoveAll();
	m_flTimeOffset = 0.0f;
	m_bRunning = false;
}

//-----------------------------------------------------------------------------
// Purpose: Resets the lines
//-----------------------------------------------------------------------------
void CGraphPanel::Restart()
{
	for (int i = 0; i < m_Lines.Count(); i++)
	{
		m_Lines[i].startTime = vgui::system()->GetFrameTime();
		m_Lines[i].elapsedWhenStopped = 0.0f;
	}

	m_flTimeOffset += 0.1f;
}

//-----------------------------------------------------------------------------
// Purpose: Adds a line to the line graph
//-----------------------------------------------------------------------------
void CGraphPanel::AddLine(bool ascending, unsigned char r, unsigned char g, unsigned char b, float speed, float flGraphWidthFraction)
{
	//check speed
	if (speed <= 0.0f)
		speed = 1.0f;

	//create line info
	LineInfo line;
	line.startTime = vgui::system()->GetFrameTime();
	line.ascending = ascending;
	line.m_flGraphWidthFraction = flGraphWidthFraction;
	line.offset = m_flTimeOffset;
	line.elapsedWhenStopped = 0.0f;
	line.r = r;
	line.g = g;
	line.b = b;
	line.speed = speed;

	//add to lines array
	m_Lines.AddToTail(line);
	m_flTimeOffset += 0.1f;
}

//-----------------------------------------------------------------------------
// Purpose: Removes a line graph
//-----------------------------------------------------------------------------
void CGraphPanel::RemoveLine(int index)
{
	//bounds check
	if (index >= m_Lines.Count() || index < 0)
		return;

	//remove the line
	m_Lines.Remove(index);

	//move everything down
	m_flTimeOffset = 0.0f;
	for (int i = 0; i < m_Lines.Count(); ++i)
	{
		m_Lines[i].offset = m_flTimeOffset;
		m_flTimeOffset += 0.1f;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called when scheme settings are set
//-----------------------------------------------------------------------------
void CGraphPanel::ApplySchemeSettings(vgui::IScheme* scheme)
{
	SetFont(scheme->GetFont("Default", IsProportional()));
}

//selected text mode
enum class SoundscapeMode
{
	Mode_Random,
	Mode_Soundscape,
	Mode_Looping,
};

//soundscape clipboard type
enum class SoundscapeClipboardType
{
	Type_SoundscapeNone,
	Type_SoundscapeName,
	Type_SoundscapeData,
	Type_SoundscapeRandomWave,
};

//dsp effects
static const char* g_DspEffects[] = {
	"Normal (off)",
	"Generic",
	"Metal Small",
	"Metal Medium",
	"Metal Large",
	"Tunnel Small",
	"Tunnel Medium",
	"Tunnel Large",
	"Chamber Small",
	"Chamber Medium",
	"Chamber Large",
	"Bright Small",
	"Bright Medium",
	"Bright Large",
	"Water 1",
	"Water 2",
	"Water 3",
	"Concrete Small",
	"Concrete Medium",
	"Concrete Large",
	"Big 1",
	"Big 2",
	"Big 3",
	"Cavern Small",
	"Cavern Medium",
	"Cavern Large",
	"Weirdo 1",
	"Weirdo 2",
	"Weirdo 3",
};

//sound levels
static const char* g_SoundLevels[] = {
	"SNDLVL_50dB",
	"SNDLVL_55dB",
	"SNDLVL_IDLE",
	"SNDLVL_TALKING",
	"SNDLVL_60dB",
	"SNDLVL_65dB",
	"SNDLVL_STATIC",
	"SNDLVL_70dB",
	"SNDLVL_NORM",
	"SNDLVL_75dB",
	"SNDLVL_80dB",
	"SNDLVL_85dB",
	"SNDLVL_90dB",
	"SNDLVL_95dB",
	"SNDLVL_100dB",
	"SNDLVL_105dB",
	"SNDLVL_120dB",
	"SNDLVL_130dB",
	"SNDLVL_GUNFIRE",
	"SNDLVL_140dB",
	"SNDLVL_150dB"
};

bool g_bSSMHack = false;

//max clipboard size
#define MAX_CLIPBOARD_ITEMS 10

//current clipboard stuff
static CUtlVector<KeyValues*> CurrClipboardName;		//for soundscape name
static CUtlVector<KeyValues*> CurrClipboardData;		//for soundscape data
static CUtlVector<KeyValues*> CurrClipboardRandom;		//for random wave

//-----------------------------------------------------------------------------
// Purpose: Helper funciton to compare vector and string
//-----------------------------------------------------------------------------
int Q_vecstr(const CUtlVector<wchar_t>& vec, int startindex, int endindex, const char* substr)
{
	//check for null substring
	if (!substr || !*substr)
		return -1;

	//store variables
	int substrLen = Q_strlen(substr);

	//search for match
	for (int i = startindex; i <= endindex; ++i)
	{
		bool match = true;
		for (int j = 0; j < substrLen; ++j)
		{
			wchar_t wc = vec[i + j];
			char ch = substr[j];
			if (wc != ch)
			{
				match = false;
				break;
			}
		}

		//found match
		if (match)
			return i;
	}

	//didnt find match
	return -1;
}

//-----------------------------------------------------------------------------
// Purpose: Helper funciton to compare vector and string but reversed
//-----------------------------------------------------------------------------
int Q_vecrstr(const CUtlVector<wchar_t>& vec, int startindex, int endindex, const char* substr)
{
	//check for null substring
	if (!substr || !*substr)
		return -1;

	//store variables
	int substrLen = Q_strlen(substr);

	//search for match
	for (int i = endindex - substrLen + 1; i >= startindex; --i)
	{
		bool match = true;
		for (int j = 0; j < substrLen; ++j)
		{
			wchar_t wc = vec[i + j];
			char ch = substr[j];
			if (wc != ch)
			{
				match = false;
				break;
			}
		}

		//found match
		if (match)
			return i;
	}

	//didnt find match
	return -1;
}

//holds all the sound names
static CUtlVector<char*> g_SoundDirectories;

//-----------------------------------------------------------------------------
// Purpose: Sort function for utl vector
//-----------------------------------------------------------------------------
static int VectorSortFunc(char* const* p1, char* const* p2)
{
	return Q_stricmp(*p1, *p2);
}

//-----------------------------------------------------------------------------
// Purpose: Sort function for utl vector
//-----------------------------------------------------------------------------
static int VectorSortFunc(const char* const* p1, const char* const* p2)
{
	return Q_stricmp(*p1, *p2);
}

//-----------------------------------------------------------------------------
// Purpose: Gets all the sound names and stores them into g_SoundDirectories
//-----------------------------------------------------------------------------
static void GetSoundNames()
{
	//first off clear the sound array first
	for (int i = 0; i < g_SoundDirectories.Count(); i++)
		free(g_SoundDirectories[i]);

	g_SoundDirectories.RemoveAll();

	//directories to search
	CUtlVector<char*> directoriesToSearch;
	directoriesToSearch.AddToTail(strdup("sound"));

	//loop until all directories have been processed
	while (directoriesToSearch.Count() > 0)
	{
		//take the last added directory (depth-first search)
		char* currentDir = directoriesToSearch[directoriesToSearch.Count() - 1];
		directoriesToSearch.Remove(directoriesToSearch.Count() - 1);

		//create a wildcard path to search all files and subdirs
		char searchPath[MAX_PATH];
		Q_snprintf(searchPath, sizeof(searchPath), "%s/*", currentDir);

		FileFindHandle_t findHandle;
		const char* filename = g_pFullFileSystem->FindFirst(searchPath, &findHandle);

		while (filename)
		{
			//ignore special directories
			if (Q_strcmp(filename, ".") != 0 && Q_strcmp(filename, "..") != 0)
			{
				char fullPath[MAX_PATH];
				Q_snprintf(fullPath, sizeof(fullPath), "%s/%s", currentDir, filename);

				//if it's a directory, add it to the list for later processing
				if (g_pFullFileSystem->FindIsDirectory(findHandle))
				{
					directoriesToSearch.AddToTail(strdup(fullPath));
				}
				else
				{
					//check file extension and print if it's .wav or .mp3
					const char* ext = V_GetFileExtension(filename);
					if (ext && (!Q_stricmp(ext, "wav") || !Q_stricmp(ext, "mp3")))
					{
						g_SoundDirectories.AddToTail(strdup(fullPath + 6));
					}
				}
			}

			// Move to next file
			filename = g_pFullFileSystem->FindNext(findHandle);
		}

		//free the memory
		g_pFullFileSystem->FindClose(findHandle);
		free(currentDir);
	}

	//
	g_SoundDirectories.Sort(VectorSortFunc);
}


//text entry for text edit panel


class CTextPanelTextEntry : public vgui::TextEntry
{
public:
	DECLARE_CLASS_SIMPLE(CTextPanelTextEntry, vgui::TextEntry)

	//constructor
	CTextPanelTextEntry(vgui::Panel* parent, const char* panelName)
		: TextEntry(parent, panelName)
	{
		SetMultiline(true);
	}

	//called on keycode typed
	virtual void OnKeyCodeTyped(vgui::KeyCode code)
	{
		//check for enter or enter
		if (code == KEY_ENTER || code == KEY_PAD_ENTER)
			InsertString("\n");

		//check for tab
		else if (code == KEY_TAB)
			InsertString("    ");

		//do other key code
		else
			BaseClass::OnKeyCodeTyped(code);
	}

	//called on keycode pressed
	void OnKeyCodePressed(vgui::KeyCode code)
	{
		if (code == KEY_ENTER || code == KEY_PAD_ENTER
			|| code == KEY_TAB)
			return;

		BaseClass::OnKeyCodePressed(code);
	}

	//called on keycode insert
	void OnKeyTyped(wchar_t c)
	{
		//if (c == '{')
		//{
		//	//insert:
		//	//{
		//	//
		//	//}
		//	//and set cursor in the middle
		//	BaseClass::OnKeyTyped(c);
		//	InsertString("\n\n}    ");
		//	GotoLeft();
		//	GotoUp();
		//	SelectNoText();
		//}
		if (c == '"')
		{
			//check next item
			if (_cursorPos < m_TextStream.Count())
			{

				//check for " so you dont insert string inside string
				if (m_TextStream[_cursorPos] == '"')
				{
					GotoRight();
					SelectNone();
					return;
				}

			}

			//insert:
			//""
			//and set cursor in the middle
			BaseClass::OnKeyTyped(c);
			InsertString("\"");
			GotoLeft();
			SelectNone();
		}
		else
		{
			BaseClass::OnKeyTyped(c);
		}
	}
};



//simple clipboard panel
class CSoundscapeClipboard : public vgui::Frame
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeClipboard, vgui::Frame)

	CSoundscapeClipboard(SoundscapeClipboardType type);

	//creates all the buttons
	void CreateButtons();

	//other
	void OnCommand(const char* pszCommand);
	void OnClose();

private:
	SoundscapeClipboardType m_Type;
};

//static soundscape clipboard panel
static CSoundscapeClipboard* g_SoundscapeClipboard;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSoundscapeClipboard::CSoundscapeClipboard(SoundscapeClipboardType type)
	: BaseClass(nullptr, "SoundscapeMakerClipboard"), m_Type(type)
{
	//get the size of the panel
	int tall = 30;

	switch (type)
	{
	case SoundscapeClipboardType::Type_SoundscapeName:
		tall += 29 * CurrClipboardName.Count();
		break;
	case SoundscapeClipboardType::Type_SoundscapeData:
		tall += 29 * CurrClipboardData.Count();
		break;
	case SoundscapeClipboardType::Type_SoundscapeRandomWave:
		tall += 29 * CurrClipboardRandom.Count();
		break;
	}

	SetParent(enginevgui->GetPanel(VGuiPanel_t::PANEL_TOOLS));
	SetCloseButtonVisible(true);
	SetSize(300, tall);
	MoveToCenterOfScreen();
	SetTitle("Soundscape Clipboard", true);
	SetSizeable(false);
	SetDeleteSelfOnClose(true);

	SetVisible(true);
	RequestFocus();
	MoveToFront();

	CreateButtons();
}

//-----------------------------------------------------------------------------
// Purpose: Creates all the clipboard buttons
//-----------------------------------------------------------------------------
void CSoundscapeClipboard::CreateButtons()
{
	switch (m_Type)
	{
	case SoundscapeClipboardType::Type_SoundscapeName:
	{
		//add all the buttons
		for (int i = 0; i < CurrClipboardName.Count(); i++)
		{
			vgui::Button* button = new vgui::Button(this, CFmtStr("PasteButton%d", i), CFmtStr("%.50s", CurrClipboardName[i]->GetName()));
			button->SetBounds(10, 29 + (i * 27), 280, 25);
			button->SetCommand(CFmtStr("$PASTE%d", i));
		}
		break;
	}
	case SoundscapeClipboardType::Type_SoundscapeData:
	{
		//add all the buttons
		for (int i = 0; i < CurrClipboardData.Count(); i++)
		{
			vgui::Button* button = new vgui::Button(this, CFmtStr("PasteButton%d", i), "");

			//set text
			const char* name = CurrClipboardData[i]->GetName();
			if (!Q_stricmp(name, "playrandom"))
			{
				button->SetText("playrandom");
			}
			else if (!Q_stricmp(name, "playlooping"))
			{
				const char* looping = CurrClipboardData[i]->GetString("wave");
				if (strlen(looping) > 25)
					looping += strlen(looping) - 25;

				button->SetText(CFmtStr("%s : '%s'", name, looping));
			}
			else
			{
				const char* looping = CurrClipboardData[i]->GetString("name");
				if (strlen(looping) > 25)
					looping += strlen(looping) - 25;

				button->SetText(CFmtStr("%s : '%s'", name, looping));
			}

			//set other stuff
			button->SetBounds(10, 29 + (i * 27), 280, 25);
			button->SetCommand(CFmtStr("$PASTE%d", i));
		}
		break;
	}
	case SoundscapeClipboardType::Type_SoundscapeRandomWave:
	{
		//add all the buttons
		for (int i = 0; i < CurrClipboardRandom.Count(); i++)
		{
			vgui::Button* button = new vgui::Button(this, CFmtStr("PasteButton%d", i), CurrClipboardRandom[i]->GetString());
			button->SetBounds(10, 29 + (i * 27), 280, 25);
			button->SetCommand(CFmtStr("$PASTE%d", i));
		}
		break;
	}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called when focus is killed
//-----------------------------------------------------------------------------
void CSoundscapeClipboard::OnClose()
{
	g_SoundscapeClipboard = nullptr;

	BaseClass::OnClose();
}

//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeClipboard::OnCommand(const char* command)
{
	if (Q_stristr(command, "$PASTE") == command)
	{
		//get index
		int index = atoi(command + 6);

		//so this is what i am gonna do:
		//	1. copy KeyValue from index <index> to the top of the clipboard
		//	2. call g_SoundscapeMaker.PasteFromClipboard((int)m_Type);
		//	3. remove keyvalues at last index of clipboard CUtlVector
		switch (m_Type)
		{
		case SoundscapeClipboardType::Type_SoundscapeName:
			if (index >= CurrClipboardName.Count() || CurrClipboardName.Count() <= 0)
				return;

			CurrClipboardName.AddToTail(CurrClipboardName[index]);
			g_SoundscapeMaker->PasteFromClipboard((int)m_Type);
			CurrClipboardName.Remove(CurrClipboardName.Count() - 1);
			break;
		case SoundscapeClipboardType::Type_SoundscapeData:
			if (index >= CurrClipboardData.Count() || CurrClipboardData.Count() <= 0)
				return;

			CurrClipboardData.AddToTail(CurrClipboardData[index]);
			g_SoundscapeMaker->PasteFromClipboard((int)m_Type);
			CurrClipboardData.Remove(CurrClipboardData.Count() - 1);
			break;
		case SoundscapeClipboardType::Type_SoundscapeRandomWave:
			if (index >= CurrClipboardRandom.Count() || CurrClipboardRandom.Count() <= 0)
				return;

			CurrClipboardRandom.AddToTail(CurrClipboardRandom[index]);
			g_SoundscapeMaker->PasteFromClipboard((int)m_Type);
			CurrClipboardRandom.Remove(CurrClipboardRandom.Count() - 1);
			break;
		}
	}

	BaseClass::OnCommand(command);
}



//soundscape maker text editor panel
#define TEXT_PANEL_WIDTH 760
#define TEXT_PANEL_HEIGHT 630

#define TEXT_PANEL_COMMAND_SET "Set"
#define TEXT_PANEL_COMMAND_SET_OK "SetOk"
#define TEXT_PANEL_COMMAND_FIND "FInd"

class CSoundscapeTextPanel : public vgui::Frame
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeTextPanel, vgui::Frame);

	CSoundscapeTextPanel(vgui::VPANEL parent, const char* name);

	//sets the keyvalues
	void Set(KeyValues* keyvalues);
	void RecursiveSetText(KeyValues* keyvalues, CUtlBuffer& buffer, int indent);

	//other
	void OnCommand(const char* pszCommand);
	void PerformLayout();
	void OnClose() { BaseClass::OnClose(); }

private:
	CTextPanelTextEntry* m_Text;
	vgui::Button* m_SetButton;
	vgui::TextEntry* m_FindTextEntry;
	vgui::Button* m_FindButton;
};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSoundscapeTextPanel::CSoundscapeTextPanel(vgui::VPANEL parent, const char* name)
	: BaseClass(nullptr, name)
{
	SetParent(parent);

	SetKeyBoardInputEnabled(true);
	SetMouseInputEnabled(true);

	SetProportional(false);
	SetTitleBarVisible(true);
	SetMinimizeButtonVisible(false);
	SetMaximizeButtonVisible(false);
	SetCloseButtonVisible(true);
	SetSizeable(true);
	SetMoveable(true);
	SetVisible(false);
	SetMinimumSize(575, 120);

	int ScreenWide, ScreenTall;
	vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);

	SetTitle("Soundscape Text Editor", true);
	SetSize(TEXT_PANEL_WIDTH, TEXT_PANEL_HEIGHT);
	SetPos((ScreenWide - TEXT_PANEL_WIDTH) / 2, (ScreenTall - TEXT_PANEL_HEIGHT) / 2);



	//make text entry
	m_Text = new CTextPanelTextEntry(this, "EditBox");
	m_Text->SetBounds(5, 25, TEXT_PANEL_WIDTH - 10, TEXT_PANEL_HEIGHT - 55);
	m_Text->SetEnabled(true);
	m_Text->SetMultiline(true);
	m_Text->SetVerticalScrollbar(true);

	//make set button
	m_SetButton = new vgui::Button(this, "SetButton", "Apply Changes To Keyvalue Maker");
	m_SetButton->SetBounds(5, TEXT_PANEL_HEIGHT - 27, 250, 25);
	m_SetButton->SetCommand(TEXT_PANEL_COMMAND_SET);

	//make find text entry
	m_FindTextEntry = new vgui::TextEntry(this, "FindTextEntry");
	m_FindTextEntry->SetBounds(450, TEXT_PANEL_HEIGHT - 27, 200, 25);

	//make find button
	m_FindButton = new vgui::Button(this, "FindButton", "Find String");
	m_FindButton->SetBounds(655, TEXT_PANEL_HEIGHT - 27, 100, 25);
	m_FindButton->SetCommand(TEXT_PANEL_COMMAND_FIND);
}

//-----------------------------------------------------------------------------
// Purpose: Sets the keyvalues
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::Set(KeyValues* keyvalues)
{
	//write everything into a buffer
	CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);

	//now write the keyvalues
	KeyValues* pCurrent = keyvalues;
	while (pCurrent)
	{
		RecursiveSetText(pCurrent, buf, 0);

		//put a newline
		if (pCurrent->GetNextTrueSubKey())
			buf.PutChar('\n');

		//get next
		pCurrent = pCurrent->GetNextTrueSubKey();
	}

	//write that to the m_Text
	m_Text->SetText((const char*)buf.Base());
}

//-----------------------------------------------------------------------------
// Purpose: Recursively writes to a util buffer
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::RecursiveSetText(KeyValues* keyvalues, CUtlBuffer& buffer, int indent)
{
	//write \t indent
	for (int i = 0; i < indent; i++)
		buffer.PutString("    ");

	//write name
	buffer.PutChar('"');
	buffer.PutString(keyvalues->GetName());
	buffer.PutString("\"\n");

	//write {
	for (int i = 0; i < indent; i++)
		buffer.PutString("    ");

	buffer.PutString("{\n");

	//increment indent
	indent++;

	//write all the keys first
	FOR_EACH_VALUE(keyvalues, value)
	{
		for (int i = 0; i < indent; i++)
			buffer.PutString("    ");

		//write name and value
		buffer.PutChar('"');
		buffer.PutString(value->GetName());
		buffer.PutString("\"    ");

		buffer.PutChar('"');
		buffer.PutString(value->GetString());
		buffer.PutString("\"\n");
	}

	//write all the subkeys now
	FOR_EACH_TRUE_SUBKEY(keyvalues, value)
	{
		//increment indent
		RecursiveSetText(value, buffer, indent);

		if (value->GetNextTrueSubKey())
			buffer.PutChar('\n');
	}

	//decrement indent
	indent--;

	//write ending }
	for (int i = 0; i < indent; i++)
		buffer.PutString("    ");

	buffer.PutString("}\n");
}

//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::OnCommand(const char* pszCommand)
{
	if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_SET))
	{
		//play sound
		vgui::surface()->PlaySound("ui/buttonclickrelease.wav");

		//check first incase you accidentally press it
		vgui::QueryBox* popup = new vgui::QueryBox("Set File?", "Are you sure you want to set the current keyvalues for the keyvalue maker?\nIf there are errors then this could break the keyvalue file.", this);
		popup->SetOKCommand(new KeyValues("Command", "command", TEXT_PANEL_COMMAND_SET_OK));
		popup->SetCancelButtonVisible(false);
		popup->AddActionSignalTarget(this);
		popup->DoModal(this);
		return;
	}

	//set text
	if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_SET_OK))
	{
		//get string
		int len = m_Text->GetTextLength() + 1;

		char* buf = new char[len];
		m_Text->GetText(buf, len);

		g_SoundscapeMaker->SetBuffer(buf);

		//delete string
		delete[] buf;

		//hide this
		SetVisible(false);
		return;
	}

	//find text
	if (!Q_strcmp(pszCommand, TEXT_PANEL_COMMAND_FIND))
	{
		//get buffer
		char buf[128];
		m_FindTextEntry->GetText(buf, sizeof(buf));

		int index = m_Text->_cursorPos + 1;
		int find = -1;

		//go in reversed order if holding shift
		if (vgui::input()->IsKeyDown(KEY_LSHIFT) || vgui::input()->IsKeyDown(KEY_RSHIFT))
		{

			//see if we find index
			find = Q_vecrstr(m_Text->m_TextStream, 0, index - 2, buf);
			if (find == -1)

				//look again
				find = Q_vecrstr(m_Text->m_TextStream, index, m_Text->m_TextStream.Count() - 1, buf);

		}
		else
		{

			//see if we find index
			find = Q_vecstr(m_Text->m_TextStream, index, m_Text->m_TextStream.Count(), buf);
			if (find == -1)

				//look again
				find = Q_vecstr(m_Text->m_TextStream, 0, index, buf);

		}

		//check for invalid index
		if (find == -1)
		{
			//play an error sound
			vgui::surface()->PlaySound("resource/warning.wav");

			//get text
			char error[512];
			Q_snprintf(error, sizeof(error), "Couldnt find any instances of '%s'", buf);

			//show an error
			vgui::QueryBox* popup = new vgui::QueryBox("No Instances Found", error, this);
			popup->SetOKButtonText("Ok");
			popup->SetCancelButtonVisible(false);
			popup->AddActionSignalTarget(this);
			popup->DoModal(this);

			return;
		}

		//get number of newlines
		/*int newline = 0;
		int column = 0;
		for (int i = 0; i < find; i++)
		{
			if (m_Text->m_TextStream[i] == '\n')
			{
				newline++;
				column = 0;
			}
			else
			{
				column++;
			}
		}*/

		//select that
		m_Text->_cursorPos = find;
		m_Text->_select[0] = find;
		m_Text->_select[1] = find + Q_strlen(buf);
		m_Text->LayoutVerticalScrollBarSlider();
		m_Text->Repaint();
		m_Text->RequestFocus();

		return;
	}

	BaseClass::OnCommand(pszCommand);
}

//-----------------------------------------------------------------------------
// Purpose: Called on panel size changed
//-----------------------------------------------------------------------------
void CSoundscapeTextPanel::PerformLayout()
{
	BaseClass::PerformLayout();

	int wide, tall;
	GetSize(wide, tall);

	if (m_Text)
		m_Text->SetBounds(5, 25, wide - 10, tall - 55);

	if (m_SetButton)
		m_SetButton->SetBounds(5, tall - 27, 250, 25);

	if (m_FindTextEntry)
		m_FindTextEntry->SetBounds(wide - 310, tall - 27, 200, 25);

	if (m_FindButton)
		m_FindButton->SetBounds(wide - 105, tall - 27, 100, 25);
}

//soundscape settings panel
static CSoundscapeTextPanel* g_SoundscapeTextPanel = nullptr;




//soundscape maker text editor panel
#define DEBUG_PANEL_WIDTH 725
#define DEBUG_PANEL_HEIGHT 530

#define DEBUG_PANEL_COMMAND_CLEAR "Clear"

class CSoundscapeDebugPanel : public vgui::Frame
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeDebugPanel, vgui::Frame);

	CSoundscapeDebugPanel(vgui::VPANEL parent, const char* name);

	//sets the keyvalues
	void AddMessage(Color color, const char* text);

	//other
	void OnCommand(const char* pszCommand);
	void PerformLayout();
	void OnClose() { BaseClass::OnClose(); }

private:
	vgui::RichText* m_Text;
	vgui::Button* m_ClearButton;
	vgui::Label* m_SoundscapesFadingInText;

public:
	CGraphPanel* m_PanelSoundscapesFadingIn;
};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSoundscapeDebugPanel::CSoundscapeDebugPanel(vgui::VPANEL parent, const char* name)
	: BaseClass(nullptr, name)
{
	SetParent(parent);

	SetKeyBoardInputEnabled(true);
	SetMouseInputEnabled(true);

	SetProportional(false);
	SetTitleBarVisible(true);
	SetMinimizeButtonVisible(false);
	SetMaximizeButtonVisible(false);
	SetCloseButtonVisible(true);
	SetSizeable(true);
	SetMoveable(true);
	SetVisible(false);
	SetMinimumSize(575, 280);

	SetTitle("Soundscape Debug Panel", true);
	SetSize(DEBUG_PANEL_WIDTH, DEBUG_PANEL_HEIGHT);
	SetPos(0, 0);



	//make text entry
	m_Text = new vgui::RichText(this, "DebugText");
	m_Text->SetBounds(5, 25, DEBUG_PANEL_WIDTH - 10, DEBUG_PANEL_HEIGHT - 55);
	m_Text->SetEnabled(true);
	m_Text->SetVerticalScrollbar(true);

	//make clear button
	m_ClearButton = new vgui::Button(this, "ClearButton", "Clear");
	m_ClearButton->SetBounds(5, DEBUG_PANEL_HEIGHT - 215, DEBUG_PANEL_WIDTH - 10, 25);
	m_ClearButton->SetCommand(DEBUG_PANEL_COMMAND_CLEAR);

	//make fading in label
	m_SoundscapesFadingInText = new vgui::Label(this, "LabelFadingIn", "Soundscapes Fading In");
	m_SoundscapesFadingInText->SetBounds(5, DEBUG_PANEL_HEIGHT - 187, DEBUG_PANEL_WIDTH - 10, 20);

	//make soundscapes fading in thing
	m_PanelSoundscapesFadingIn = new CGraphPanel(this, "SoundscapesFadingIn");
	m_PanelSoundscapesFadingIn->SetBounds(5, DEBUG_PANEL_HEIGHT - 165, DEBUG_PANEL_WIDTH - 10, 155);
	m_PanelSoundscapesFadingIn->SetMaxTextValue(1.0f);
	m_PanelSoundscapesFadingIn->SetHorizontalLinesMax(5);
}

//-----------------------------------------------------------------------------
// Purpose: adds a message to the debug panel
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::AddMessage(Color color, const char* text)
{
	m_Text->InsertColorChange(color);
	m_Text->InsertString(text);

	m_Text->SetMaximumCharCount(100000);
}

//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::OnCommand(const char* pszCommand)
{
	if (!Q_strcmp(pszCommand, DEBUG_PANEL_COMMAND_CLEAR))
	{
		//clear the text
		m_Text->SetText("");
		m_Text->GotoTextEnd();
		return;
	}

	BaseClass::OnCommand(pszCommand);
}

//-----------------------------------------------------------------------------
// Purpose: Called on panel size changed
//-----------------------------------------------------------------------------
void CSoundscapeDebugPanel::PerformLayout()
{
	BaseClass::PerformLayout();

	int wide, tall;
	GetSize(wide, tall);

	m_Text->SetBounds(5, 25, wide - 10, tall - 245);
	m_ClearButton->SetBounds(5, tall - 215, wide - 10, 25);
	m_PanelSoundscapesFadingIn->SetBounds(5, tall - 165, wide - 10, 155);
	m_SoundscapesFadingInText->SetBounds(5, tall - 187, wide - 10, 20);
}

//soundscape debug panel
static CSoundscapeDebugPanel* g_SoundscapeDebugPanel = nullptr;

//-----------------------------------------------------------------------------
// Purpose: Function to print text to debug panel
//-----------------------------------------------------------------------------
void SoundscapePrint(Color color, const char* msg, ...)
{
	//format string
	va_list args;
	va_start(args, msg);

	char buf[2048];
	Q_vsnprintf(buf, sizeof(buf), msg, args);
	g_SoundscapeDebugPanel->AddMessage(color, buf);

	va_end(args);
}

//-----------------------------------------------------------------------------
// Purpose: Function to add a line to the soundscape debug panel
//-----------------------------------------------------------------------------
void SoundscapeAddLine(Color color, float speed, float width, bool accending)
{
	if (g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->GetNumLines() <= 6)
		g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->AddLine(accending, color.r(), color.g(), color.b(), speed, width);
}

//-----------------------------------------------------------------------------
// Purpose: Function to get debug line num
//-----------------------------------------------------------------------------
int SoundscapeGetLineNum()
{
	return g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->GetNumLines();
}

//vector positions
Vector g_SoundscapePositions[] = {
	vec3_origin,
	vec3_origin,
	vec3_origin,
	vec3_origin,
	vec3_origin,
	vec3_origin,
	vec3_origin,
	vec3_origin
};

#define SETTINGS_PANEL_WIDTH 350
#define SETTINGS_PANEL_HEIGHT 277

#define SETTINGS_PANEL_COMMAND_POS1 "GetPos0"
#define SETTINGS_PANEL_COMMAND_POS2 "GetPos1"
#define SETTINGS_PANEL_COMMAND_POS3 "GetPos2"
#define SETTINGS_PANEL_COMMAND_POS4 "GetPos3"
#define SETTINGS_PANEL_COMMAND_POS5 "GetPos4"
#define SETTINGS_PANEL_COMMAND_POS6 "GetPos5"
#define SETTINGS_PANEL_COMMAND_POS7 "GetPos6"
#define SETTINGS_PANEL_COMMAND_POS8 "GetPos7"
#define SETTINGS_PANEL_COMMAND_SHOW "ShowPositions"
#define SETTINGS_PANEL_COMMAND_DEBUG "Debug"

#define MAX_SOUNDSCAPES 8

//soundscape maker settings panel
class CSoundscapeSettingsPanel : public vgui::Frame
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeSettingsPanel, vgui::Frame);

	CSoundscapeSettingsPanel(vgui::VPANEL parent, const char* name);

	//other
	void OnCommand(const char* pszCommand);

	//sets the text
	void SetItem(int index, const Vector& value);

	//message funcs
	MESSAGE_FUNC_PARAMS(OnTextChanged, "TextChanged", data);

	~CSoundscapeSettingsPanel();

private:
	//position text entries
	vgui::TextEntry* m_TextEntryPos0;
	vgui::TextEntry* m_TextEntryPos1;
	vgui::TextEntry* m_TextEntryPos2;
	vgui::TextEntry* m_TextEntryPos3;
	vgui::TextEntry* m_TextEntryPos4;
	vgui::TextEntry* m_TextEntryPos5;
	vgui::TextEntry* m_TextEntryPos6;
	vgui::TextEntry* m_TextEntryPos7;
	vgui::CheckButton* m_ShowSoundscapePositions;
	vgui::Button* m_ShowSoundscapeDebug;

	friend class CSoundscapeMaker;
};


//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSoundscapeSettingsPanel::CSoundscapeSettingsPanel(vgui::VPANEL parent, const char* name)
	: BaseClass(nullptr, name)
{
	SetParent(parent);

	SetKeyBoardInputEnabled(true);
	SetMouseInputEnabled(true);

	SetProportional(false);
	SetTitleBarVisible(true);
	SetMinimizeButtonVisible(false);
	SetMaximizeButtonVisible(false);
	SetCloseButtonVisible(true);
	SetSizeable(false);
	SetMoveable(true);
	SetVisible(false);

	//set the size and pos
	int ScreenWide, ScreenTall;
	vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);

	SetTitle("Soundscape Maker Settings", true);
	SetSize(SETTINGS_PANEL_WIDTH, SETTINGS_PANEL_HEIGHT);
	SetPos((ScreenWide - SETTINGS_PANEL_WIDTH) / 2, (ScreenTall - SETTINGS_PANEL_HEIGHT) / 2);



	//load settings
	KeyValues* settings = new KeyValues("settings");
	if (!settings->LoadFromFile(filesystem, "cfg/soundscape_maker.txt", "MOD"))
		ConWarning("Failed to load settings for 'cfg/soundscape_maker.txt'. Using default settings.");

	//get positions
	const char* pos0 = settings->GetString("Position0", "0 0 0");
	const char* pos1 = settings->GetString("Position1", "0 0 0");
	const char* pos2 = settings->GetString("Position2", "0 0 0");
	const char* pos3 = settings->GetString("Position3", "0 0 0");
	const char* pos4 = settings->GetString("Position4", "0 0 0");
	const char* pos5 = settings->GetString("Position5", "0 0 0");
	const char* pos6 = settings->GetString("Position6", "0 0 0");
	const char* pos7 = settings->GetString("Position7", "0 0 0");

	//create position text 1
	m_TextEntryPos0 = new vgui::TextEntry(this, "PosTextEntry0");
	m_TextEntryPos0->SetEnabled(true);
	m_TextEntryPos0->SetText(pos0 ? pos0 : "0 0 0");
	m_TextEntryPos0->SetBounds(5, 30, 230, 20);
	m_TextEntryPos0->SetMaximumCharCount(32);

	//create position 1 button
	vgui::Button* m_ButtonPos0 = new vgui::Button(this, "PosButton0", "Find Position 0", this, SETTINGS_PANEL_COMMAND_POS1);
	m_ButtonPos0->SetBounds(240, 30, 100, 20);

	//create position text 1
	m_TextEntryPos1 = new vgui::TextEntry(this, "PosTextEntry1");
	m_TextEntryPos1->SetEnabled(true);
	m_TextEntryPos1->SetText(pos1 ? pos1 : "0 0 0");
	m_TextEntryPos1->SetBounds(5, 55, 230, 20);
	m_TextEntryPos1->SetMaximumCharCount(32);

	//create position 2 button
	vgui::Button* m_ButtonPos1 = new vgui::Button(this, "PosButton1", "Find Position 1", this, SETTINGS_PANEL_COMMAND_POS2);
	m_ButtonPos1->SetBounds(240, 55, 100, 20);

	//create position text 3
	m_TextEntryPos2 = new vgui::TextEntry(this, "PosTextEntry0");
	m_TextEntryPos2->SetEnabled(true);
	m_TextEntryPos2->SetText(pos2 ? pos2 : "0 0 0");
	m_TextEntryPos2->SetBounds(5, 80, 230, 20);
	m_TextEntryPos2->SetMaximumCharCount(32);

	//create position 1 button
	vgui::Button* m_ButtonPos2 = new vgui::Button(this, "PosButton2", "Find Position 2", this, SETTINGS_PANEL_COMMAND_POS3);
	m_ButtonPos2->SetBounds(240, 80, 100, 20);

	// create position text 4
	m_TextEntryPos3 = new vgui::TextEntry(this, "PosTextEntry3");
	m_TextEntryPos3->SetEnabled(true);
	m_TextEntryPos3->SetText(pos3 ? pos3 : "0 0 0");
	m_TextEntryPos3->SetBounds(5, 105, 230, 20);
	m_TextEntryPos3->SetMaximumCharCount(32);

	// create position 4 button
	vgui::Button* m_ButtonPos3 = new vgui::Button(this, "PosButton3", "Find Position 3", this, SETTINGS_PANEL_COMMAND_POS4);
	m_ButtonPos3->SetBounds(240, 105, 100, 20);

	// create position text 5
	m_TextEntryPos4 = new vgui::TextEntry(this, "PosTextEntry4");
	m_TextEntryPos4->SetEnabled(true);
	m_TextEntryPos4->SetText(pos4 ? pos4 : "0 0 0");
	m_TextEntryPos4->SetBounds(5, 130, 230, 20);
	m_TextEntryPos4->SetMaximumCharCount(32);

	// create position 5 button
	vgui::Button* m_ButtonPos4 = new vgui::Button(this, "PosButton4", "Find Position 4", this, SETTINGS_PANEL_COMMAND_POS5);
	m_ButtonPos4->SetBounds(240, 130, 100, 20);

	// create position text 6
	m_TextEntryPos5 = new vgui::TextEntry(this, "PosTextEntry5");
	m_TextEntryPos5->SetEnabled(true);
	m_TextEntryPos5->SetText(pos5 ? pos5 : "0 0 0");
	m_TextEntryPos5->SetBounds(5, 155, 230, 20);
	m_TextEntryPos5->SetMaximumCharCount(32);

	// create position 6 button
	vgui::Button* m_ButtonPos5 = new vgui::Button(this, "PosButton5", "Find Position 5", this, SETTINGS_PANEL_COMMAND_POS6);
	m_ButtonPos5->SetBounds(240, 155, 100, 20);

	// create position text 7
	m_TextEntryPos6 = new vgui::TextEntry(this, "PosTextEntry6");
	m_TextEntryPos6->SetEnabled(true);
	m_TextEntryPos6->SetText(pos6 ? pos6 : "0 0 0");
	m_TextEntryPos6->SetBounds(5, 180, 230, 20);
	m_TextEntryPos6->SetMaximumCharCount(32);

	// create position 7 button
	vgui::Button* m_ButtonPos6 = new vgui::Button(this, "PosButton6", "Find Position 6", this, SETTINGS_PANEL_COMMAND_POS7);
	m_ButtonPos6->SetBounds(240, 180, 100, 20);

	// create position text 8
	m_TextEntryPos7 = new vgui::TextEntry(this, "PosTextEntry7");
	m_TextEntryPos7->SetEnabled(true);
	m_TextEntryPos7->SetText(pos7 ? pos7 : "0 0 0");
	m_TextEntryPos7->SetBounds(5, 205, 230, 20);
	m_TextEntryPos7->SetMaximumCharCount(32);

	// create position 8 button
	vgui::Button* m_ButtonPos7 = new vgui::Button(this, "PosButton7", "Find Position 7", this, SETTINGS_PANEL_COMMAND_POS8);
	m_ButtonPos7->SetBounds(240, 205, 100, 20);

	// create show soundscape positions checkbox
	m_ShowSoundscapePositions = new vgui::CheckButton(this, "ShowCheckox", "Show Soundscape Positions");
	m_ShowSoundscapePositions->SetBounds(75, 225, 200, 20);
	m_ShowSoundscapePositions->SetCommand(SETTINGS_PANEL_COMMAND_SHOW);
	m_ShowSoundscapePositions->SetSelected(settings->GetBool("ShowSoundscapes", false));

	//set convar value
	ConVar* cv = cvar->FindVar("__ss_draw");
	if (cv)
		cv->SetValue(m_ShowSoundscapePositions->IsSelected());

	//create divider
	vgui::Divider* div = new vgui::Divider(this, "Divider");
	div->SetBounds(-2, 247, SETTINGS_PANEL_WIDTH + 4, 2);

	//create debug thing
	m_ShowSoundscapeDebug = new vgui::Button(this, "DebugInfo", "Show soundscape debug panel");
	m_ShowSoundscapeDebug->SetBounds(20, 254, SETTINGS_PANEL_WIDTH - 40, 20);
	m_ShowSoundscapeDebug->SetCommand(SETTINGS_PANEL_COMMAND_DEBUG);

	//set server positions
	ConCommand* cc = cvar->FindCommand("__ss_maker_set");
	if (cc)
	{
		CCommand args;

		//do pos 0
		if (pos0)
		{
			args.Tokenize(CFmtStr("ssmaker 0 %s 1", pos0));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[0].Base(), pos0);
		}

		//do pos 1
		if (pos1)
		{
			args.Tokenize(CFmtStr("ssmaker 1 %s 1", pos1));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[1].Base(), pos1);
		}

		//do pos 2
		if (pos2)
		{
			args.Tokenize(CFmtStr("ssmaker 2 %s 1", pos2));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[2].Base(), pos2);
		}

		//do pos 3
		if (pos3)
		{
			args.Tokenize(CFmtStr("ssmaker 3 %s 1", pos3));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[3].Base(), pos3);
		}

		//do pos 4
		if (pos4)
		{
			args.Tokenize(CFmtStr("ssmaker 4 %s 1", pos4));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[4].Base(), pos4);
		}

		//do pos 5
		if (pos5)
		{
			args.Tokenize(CFmtStr("ssmaker 5 %s 1", pos5));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[5].Base(), pos5);
		}

		//do pos 6
		if (pos6)
		{
			args.Tokenize(CFmtStr("ssmaker 6 %s 1", pos6));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[6].Base(), pos6);
		}

		//do pos 7
		if (pos7)
		{
			args.Tokenize(CFmtStr("ssmaker 7 %s", pos7));
			cc->Dispatch(args);

			UTIL_StringToVector(g_SoundscapePositions[7].Base(), pos7);
		}
	}

	//delete settings
	settings->deleteThis();
}

//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeSettingsPanel::OnCommand(const char* pszCommand)
{
	if (Q_strstr(pszCommand, "GetPos") == pszCommand)
	{
		//search for number
		pszCommand = pszCommand + 6;

		//execute command
		static ConCommand* cc = cvar->FindCommand("__ss_maker_start");
		if (cc)
		{
			//hide everything first
			g_SoundscapeMaker->SetAllVisible(false);

			CCommand args;
			args.Tokenize(CFmtStr("ssmaker %d", atoi(pszCommand)));
			cc->Dispatch(args);
		}

		return;
	}

	else if (!Q_strcmp(pszCommand, SETTINGS_PANEL_COMMAND_SHOW))
	{
		static ConVar* cv = cvar->FindVar("__ss_draw");
		if (cv)
			cv->SetValue(m_ShowSoundscapePositions->IsSelected());

		return;
	}

	//handle debug thing
	else if (!Q_strcmp(pszCommand, SETTINGS_PANEL_COMMAND_DEBUG))
	{
		g_SoundscapeDebugPanel->SetVisible(true);
		g_SoundscapeDebugPanel->RequestFocus();
		g_SoundscapeDebugPanel->MoveToFront();
		return;
	}

	BaseClass::OnCommand(pszCommand);
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
void CSoundscapeSettingsPanel::SetItem(int index, const Vector& value)
{
	const char* text = CFmtStr("%.3f %.3f %.3f", value.x, value.y, value.z);

	//check index
	switch (index)
	{
	case 0:
		m_TextEntryPos0->RequestFocus();
		m_TextEntryPos0->SetText(text);
		g_SoundscapePositions[0] = value;
		break;

	case 1:
		m_TextEntryPos1->RequestFocus();
		m_TextEntryPos1->SetText(text);
		g_SoundscapePositions[1] = value;
		break;

	case 2:
		m_TextEntryPos2->RequestFocus();
		m_TextEntryPos2->SetText(text);
		g_SoundscapePositions[2] = value;
		break;
	case 3:
		m_TextEntryPos3->RequestFocus();
		m_TextEntryPos3->SetText(text);
		g_SoundscapePositions[3] = value;
		break;

	case 4:
		m_TextEntryPos4->RequestFocus();
		m_TextEntryPos4->SetText(text);
		g_SoundscapePositions[4] = value;
		break;

	case 5:
		m_TextEntryPos5->RequestFocus();
		m_TextEntryPos5->SetText(text);
		g_SoundscapePositions[5] = value;
		break;

	case 6:
		m_TextEntryPos6->RequestFocus();
		m_TextEntryPos6->SetText(text);
		g_SoundscapePositions[6] = value;
		break;

	case 7:
		m_TextEntryPos7->RequestFocus();
		m_TextEntryPos7->SetText(text);
		g_SoundscapePositions[7] = value;
		break;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called on text changed
//-----------------------------------------------------------------------------
void CSoundscapeSettingsPanel::OnTextChanged(KeyValues* kv)
{
	static ConCommand* cc = cvar->FindCommand("__ss_maker_set");

	//check focus
	if (m_TextEntryPos0->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos0->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[0].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 0 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos1->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos1->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[1].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 1 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos2->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos2->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[2].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 2 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos3->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos3->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[3].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 3 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos4->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos4->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[4].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 4 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos5->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos5->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[5].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 5 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos6->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos6->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[6].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 6 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

	//check focus
	if (m_TextEntryPos7->HasFocus())
	{
		//get text
		char buf[512];
		m_TextEntryPos7->GetText(buf, sizeof(buf));

		//convert to vector
		UTIL_StringToVector(g_SoundscapePositions[7].Base(), buf);

		//do command
		if (cc)
		{
			CCommand args;
			args.Tokenize(CFmtStr("ssmaker 7 %s 1", buf));
			cc->Dispatch(args);
		}

		return;
	}

}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CSoundscapeSettingsPanel::~CSoundscapeSettingsPanel()
{
	//save everything
	KeyValues* settings = new KeyValues("settings");

	//get text's
	char text0[64];
	char text1[64];
	char text2[64];
	char text3[64];
	char text4[64];
	char text5[64];
	char text6[64];
	char text7[64];

	m_TextEntryPos0->GetText(text0, sizeof(text0));
	m_TextEntryPos1->GetText(text1, sizeof(text1));
	m_TextEntryPos2->GetText(text2, sizeof(text2));
	m_TextEntryPos3->GetText(text3, sizeof(text3));
	m_TextEntryPos4->GetText(text4, sizeof(text4));
	m_TextEntryPos5->GetText(text5, sizeof(text5));
	m_TextEntryPos6->GetText(text6, sizeof(text6));
	m_TextEntryPos7->GetText(text7, sizeof(text7));

	//save text entries
	settings->SetString("Position0", text0);
	settings->SetString("Position1", text1);
	settings->SetString("Position2", text2);
	settings->SetString("Position3", text3);
	settings->SetString("Position4", text4);
	settings->SetString("Position5", text5);
	settings->SetString("Position6", text6);
	settings->SetString("Position7", text7);

	//save check buttons
	settings->SetBool("ShowSoundscapes", m_ShowSoundscapePositions->IsSelected());

	//save to file
	settings->SaveToFile(filesystem, "cfg/soundscape_maker.txt", "MOD");
	settings->deleteThis();
}

//static soundscape settings panel
static CSoundscapeSettingsPanel* g_SettingsPanel = nullptr;


#define BUTTON_MENU_COMMAND_COPY_CLIPBOARD "CopyClipboard"

//button
class CSoundscapeButton : public vgui::Button
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeButton, vgui::Button)

	CSoundscapeButton(vgui::Panel* parent, const char* name, const char* text, vgui::Panel* target = nullptr, const char* command = nullptr, KeyValues* kv = nullptr, SoundscapeClipboardType type = SoundscapeClipboardType::Type_SoundscapeNone)
		: BaseClass(parent, name, text, target, command), m_bIsSelected(false), m_KeyValues(kv), m_KeyValuesType(type)
	{
		m_ColorSelected = Color(200, 200, 200, 200);
		m_FgColorSelected = Color(0, 0, 0, 255);
	}

	//apply scheme settings
	void ApplySchemeSettings(vgui::IScheme* scheme)
	{
		BaseClass::ApplySchemeSettings(scheme);

		m_ColorNotSelected = GetButtonArmedBgColor();
		m_FgColorNotSelected = GetButtonArmedFgColor();
	}

	//paints the background
	void PaintBackground()
	{
		if (m_bIsSelected)
			SetBgColor(m_ColorSelected);
		else
			SetBgColor(m_ColorNotSelected);

		BaseClass::PaintBackground();
	}

	//paints
	void Paint()
	{
		if (m_bIsSelected)
			SetFgColor(m_FgColorSelected);
		else
			SetFgColor(m_FgColorNotSelected);

		BaseClass::Paint();
	}

	//mouse release
	void OnMouseReleased(vgui::MouseCode code)
	{
		if (code != vgui::MouseCode::MOUSE_RIGHT)
			return BaseClass::OnMouseReleased(code);

		//this should never happen but just in case
		if (!m_KeyValues)
			return;

		//get cursor pos
		int x, y;
		vgui::surface()->SurfaceGetCursorPos(x, y);

		//show menu
		vgui::Menu* menu = new vgui::Menu(this, "Clipboard");
		menu->AddMenuItem("CopyToClipboard", "Copy", BUTTON_MENU_COMMAND_COPY_CLIPBOARD, this);
		menu->SetBounds(x, y, 200, 50);
		menu->SetVisible(true);

		BaseClass::Paint();
	}

	//mouse release
	void OnCommand(const char* pszCommand)
	{
		if (!Q_strcmp(pszCommand, BUTTON_MENU_COMMAND_COPY_CLIPBOARD))
		{
			//create copy of keyvalues
			switch (m_KeyValuesType)
			{
			case SoundscapeClipboardType::Type_SoundscapeName:
			{
				//copy
				if (CurrClipboardName.Count() >= MAX_CLIPBOARD_ITEMS)
				{
					CurrClipboardName[0]->deleteThis();
					CurrClipboardName.Remove(0);
				}

				CurrClipboardName.AddToTail(m_KeyValues->MakeCopy());

				//debug message
				SoundscapePrint(Color(255, 255, 255, 255), "Soundscape: '%s' Coppied to clipboard.\n", m_KeyValues->GetName());
				break;
			}
			case SoundscapeClipboardType::Type_SoundscapeData:
			{
				//copy
				if (CurrClipboardData.Count() >= MAX_CLIPBOARD_ITEMS)
				{
					CurrClipboardData[0]->deleteThis();
					CurrClipboardData.Remove(0);
				}

				//make copy
				CurrClipboardData.AddToTail(m_KeyValues->MakeCopy());

				//debug message
				SoundscapePrint(Color(255, 255, 255, 255), "Soundscape Data: '%s' Coppied to clipboard.\n", m_KeyValues->GetName());
				break;
			}
			case SoundscapeClipboardType::Type_SoundscapeRandomWave:
			{
				//copy
				if (CurrClipboardRandom.Count() >= MAX_CLIPBOARD_ITEMS)
				{
					CurrClipboardRandom[0]->deleteThis();
					CurrClipboardRandom.Remove(0);
				}

				CurrClipboardRandom.AddToTail(m_KeyValues->MakeCopy());

				//debug message
				SoundscapePrint(Color(255, 255, 255, 255), "Soundscape Random Wave: '%s' Coppied to clipboard.\n", m_KeyValues->GetString());
				break;
			}
			}
		}
	}

	//is this selected or not
	bool m_bIsSelected;
	static Color m_ColorSelected;
	static Color m_ColorNotSelected;
	static Color m_FgColorSelected;
	static Color m_FgColorNotSelected;

	KeyValues* m_KeyValues = nullptr;
	SoundscapeClipboardType m_KeyValuesType;
};

Color CSoundscapeButton::m_ColorSelected = Color();
Color CSoundscapeButton::m_ColorNotSelected = Color();
Color CSoundscapeButton::m_FgColorSelected = Color();
Color CSoundscapeButton::m_FgColorNotSelected = Color();


//soundscape combo box

class CSoundListComboBox : public vgui::ComboBox
{
public:
	DECLARE_CLASS_SIMPLE(CSoundListComboBox, vgui::ComboBox);

	CSoundListComboBox(Panel* parent, const char* panelName, int numLines, bool allowEdit) :
		BaseClass(parent, panelName, numLines, allowEdit) {}

	//on key typed. check for menu item with text inside it and if found then
	//select that item.
	void OnKeyTyped(wchar_t unichar)
	{
		//check for ctrl or shift down
		if (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LCONTROL) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RCONTROL) || unichar == '`')
			return;

		//open up this combo box
		if (unichar == 13)
		{
			ShowMenu();
			return;
		}

		BaseClass::OnKeyTyped(unichar);

		//check for backspace
		if (unichar == 8 || unichar == '_')
			return;

		//get text
		char buf[512];
		GetText(buf, sizeof(buf));

		//start from current index + 1
		int start = GetMenu()->GetActiveItem() + 1;

		//look for sound with same name starting from the start first
		for (int i = start; i < g_SoundDirectories.Count(); i++)
		{
			if (Q_stristr(g_SoundDirectories[i], buf))
			{
				GetMenu()->SetCurrentlyHighlightedItem(i);
				return;
			}
		}

		//now cheeck from 0 to the start
		for (int i = 0; i < start; i++)
		{
			if (Q_stristr(g_SoundDirectories[i], buf))
			{
				GetMenu()->SetCurrentlyHighlightedItem(i);
				return;
			}
		}
	}
};


//sounds list panel

#define SOUND_LIST_PANEL_WIDTH 375
#define SOUND_LIST_PANEL_HEIGHT 255
#define SOUND_LIST_PLAY_COMMAND "PlaySound"
#define SOUND_LIST_STOP_COMMAND "StopSound"
#define SOUND_LIST_INSERT_COMMAND "Insert"
#define SOUND_LIST_RELOAD_COMMAND "Reload"
#define SOUND_LIST_SEARCH_COMMAND "Search"

class CSoundListPanel : public vgui::Frame
{
public:
	DECLARE_CLASS_SIMPLE(CSoundListPanel, vgui::Frame);

	CSoundListPanel(vgui::VPANEL parent, const char* name);

	//initalizes sound combo box
	void InitalizeSounds();
	void InitalizeSoundscapes(CUtlVector<const char*>& OtherSoundscapes);

	//sets if this is currently using the soundscape panel or sound panel
	void SetIsUsingSoundPanel(bool bUsing);

	//other
	void OnCommand(const char* pszCommand);
	void OnClose();

private:
	friend class CSoundscapeMaker;

	//are we currently in the 'sound' panel or 'soundscape' panel
	bool bCurrentlyInSoundPanel = true;

	CSoundListComboBox* m_SoundsList;		//for sounds
	CSoundListComboBox* m_SoundscapesList;	//for soundscapes
	vgui::TextEntry* m_SearchText;
	vgui::Button* m_SearchButton;
	vgui::Button* m_PlayButton;
	vgui::Button* m_StopSoundButton;
	vgui::Button* m_InsertButton;
	vgui::Button* m_ReloadSounds;

	//current sound guid
	int m_iSongGuid = -1;
};

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSoundListPanel::CSoundListPanel(vgui::VPANEL parent, const char* name)
	: BaseClass(nullptr, name)
{
	SetParent(parent);

	SetKeyBoardInputEnabled(true);
	SetMouseInputEnabled(true);

	SetProportional(false);
	SetTitleBarVisible(true);
	SetMinimizeButtonVisible(false);
	SetMaximizeButtonVisible(false);
	SetCloseButtonVisible(true);
	SetSizeable(false);
	SetMoveable(true);
	SetVisible(false);

	//set the size and pos
	int ScreenWide, ScreenTall;
	vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);

	SetTitle("Sounds List", true);
	SetSize(SOUND_LIST_PANEL_WIDTH, SOUND_LIST_PANEL_HEIGHT);
	SetPos((ScreenWide - SOUND_LIST_PANEL_WIDTH) / 2, (ScreenTall - SOUND_LIST_PANEL_HEIGHT) / 2);

	//create combo box's
	m_SoundsList = new CSoundListComboBox(this, "SoundsList", 20, true);
	m_SoundsList->SetBounds(5, 25, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_SoundsList->AddActionSignalTarget(this);
	m_SoundsList->SetVisible(true);

	m_SoundscapesList = new CSoundListComboBox(this, "SoundscapesList", 20, true);
	m_SoundscapesList->SetBounds(5, 25, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_SoundscapesList->AddActionSignalTarget(this);
	m_SoundscapesList->SetVisible(false);

	//make divider
	vgui::Divider* divider1 = new vgui::Divider(this, "Divider");
	divider1->SetBounds(-5, 48, SOUND_LIST_PANEL_WIDTH + 10, 2);

	//create text
	vgui::Label* label1 = new vgui::Label(this, "FindSound", "Find Sound");
	label1->SetBounds(147, 51, 120, 20);

	//create text entry
	m_SearchText = new vgui::TextEntry(this, "SearchTextEntry");
	m_SearchText->SetBounds(5, 75, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_SearchText->SetEnabled(true);
	m_SearchText->SetText("");

	//create search for button
	m_SearchButton = new vgui::Button(this, "SearchButton", "Search For");
	m_SearchButton->SetBounds(5, 100, SOUND_LIST_PANEL_WIDTH - 15, 20);;
	m_SearchButton->SetEnabled(true);
	m_SearchButton->SetCommand(SOUND_LIST_SEARCH_COMMAND);

	//make divider
	vgui::Divider* divider2 = new vgui::Divider(this, "Divider");
	divider2->SetBounds(-5, 124, SOUND_LIST_PANEL_WIDTH + 10, 2);

	//create text
	vgui::Label* label2 = new vgui::Label(this, "SoundButtons", "Sound Buttons");
	label2->SetBounds(140, 127, 120, 20);

	//create play button
	m_PlayButton = new vgui::Button(this, "PlayButton", "Play Sound", this);
	m_PlayButton->SetBounds(5, 150, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_PlayButton->SetCommand(SOUND_LIST_PLAY_COMMAND);

	//create stop sound button
	m_StopSoundButton = new vgui::Button(this, "StopSound", "Stop Sound", this);
	m_StopSoundButton->SetBounds(5, 175, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_StopSoundButton->SetCommand(SOUND_LIST_STOP_COMMAND);

	//create sound insert button
	m_InsertButton = new vgui::Button(this, "InsertSound", "Insert Sound", this);
	m_InsertButton->SetBounds(5, 225, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_InsertButton->SetCommand(SOUND_LIST_INSERT_COMMAND);

	//create reload sounds button
	m_ReloadSounds = new vgui::Button(this, "ReloadSounds", "Reload Sounds", this);
	m_ReloadSounds->SetBounds(5, 200, SOUND_LIST_PANEL_WIDTH - 15, 20);
	m_ReloadSounds->SetCommand(SOUND_LIST_RELOAD_COMMAND);
}

//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundListPanel::OnCommand(const char* pszCommand)
{
	if (!Q_strcmp(pszCommand, SOUND_LIST_SEARCH_COMMAND))
	{
		//get text
		char buf[512];
		m_SearchText->GetText(buf, sizeof(buf));

		//check for shift key
		bool shift = (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LSHIFT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RSHIFT));

		//vector of texts
		CUtlVector<char*> SoundNames;
		CSoundListComboBox* SoundList = bCurrentlyInSoundPanel ? m_SoundsList : m_SoundscapesList;

		//if we are in soundscape mode then set the SoundNames to all the soundscapes. else set SoundNames to g_SoundDirectories
		if (!bCurrentlyInSoundPanel)
		{
			for (int i = 0; i < m_SoundscapesList->GetItemCount(); i++)
			{
				//insert
				char* tmpbuf = new char[512];
				m_SoundscapesList->GetItemText(i, tmpbuf, 512);

				SoundNames.AddToTail(tmpbuf);
			}
		}
		else
		{
			SoundNames = g_SoundDirectories;
		}

		if (shift)
		{
			//start from current index - 1
			int start = SoundList->GetMenu()->GetActiveItem() - 1;

			//look for sound with same name starting from the start first and going down
			for (int i = start; i >= 0; i--)
			{
				if (Q_stristr(SoundNames[i], buf))
				{
					//select item
					SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
					SoundList->ActivateItem(i);

					//set text
					SoundList->SetText(SoundNames[i]);

					//delete all soundscapes if we need to
					if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
						delete[] SoundNames[i];

					return;
				}
			}


			//now cheeck from the SoundNames to the start
			for (int i = SoundNames.Count() - 1; i > start; i--)
			{
				if (Q_stristr(SoundNames[i], buf))
				{
					//select item
					SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
					SoundList->ActivateItem(i);

					//set text
					SoundList->SetText(SoundNames[i]);

					//delete all soundscapes if we need to
					if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
						delete[] SoundNames[i];

					return;
				}
			}
		}
		else
		{
			//start from current index + 1
			int start = SoundList->GetMenu()->GetActiveItem() + 1;

			//look for sound with same name starting from the start first
			for (int i = start; i < SoundNames.Count(); i++)
			{
				if (Q_stristr(SoundNames[i], buf))
				{
					//select item
					SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
					SoundList->ActivateItem(i);

					//set text
					SoundList->SetText(SoundNames[i]);

					//delete all soundscapes if we need to
					if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
						delete[] SoundNames[i];

					return;
				}
			}


			//now cheeck from 0 to the start
			for (int i = 0; i < start; i++)
			{
				if (Q_stristr(SoundNames[i], buf))
				{
					//select item
					SoundList->GetMenu()->SetCurrentlyHighlightedItem(i);
					SoundList->ActivateItem(i);

					//set text
					SoundList->SetText(SoundNames[i]);

					//delete all soundscapes if we need to
					if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
						delete[] SoundNames[i];

					return;
				}
			}
		}

		//delete all soundscapes if we need to
		if (!bCurrentlyInSoundPanel) for (int i = 0; i < SoundNames.Count(); i++)
			delete[] SoundNames[i];

		return;
	}
	else if (!Q_strcmp(pszCommand, SOUND_LIST_PLAY_COMMAND))
	{
		//get the sound
		char buf[512];
		m_SoundsList->GetText(buf, sizeof(buf));

		//stop the sound
		if (enginesound->IsSoundStillPlaying(m_iSongGuid))
		{
			enginesound->StopSoundByGuid(m_iSongGuid);
			m_iSongGuid = -1;
		}

		//precache and play the sound
		if (!enginesound->IsSoundPrecached(buf))
			enginesound->PrecacheSound(buf);

		enginesound->EmitAmbientSound(buf, 1, 100);
		m_iSongGuid = enginesound->GetGuidForLastSoundEmitted();
		return;
	}
	else if (!Q_strcmp(pszCommand, SOUND_LIST_STOP_COMMAND))
	{
		//stop the sound
		if (m_iSongGuid != -1 && enginesound->IsSoundStillPlaying(m_iSongGuid))
		{
			enginesound->StopSoundByGuid(m_iSongGuid);
			m_iSongGuid = -1;
		}

		return;
	}
	else if (!Q_strcmp(pszCommand, SOUND_LIST_INSERT_COMMAND))
	{
		//make not visible
		SetVisible(false);

		//stop the sound
		if (enginesound->IsSoundStillPlaying(m_iSongGuid))
		{
			enginesound->StopSoundByGuid(m_iSongGuid);
			m_iSongGuid = -1;
		}

		//get the sound
		char buf[512];

		if (bCurrentlyInSoundPanel)
			m_SoundsList->GetText(buf, sizeof(buf));
		else
			m_SoundscapesList->GetText(buf, sizeof(buf));

		//set the sound text
		g_SoundscapeMaker->SetSoundText(buf);
		return;
	}
	else if (!Q_strcmp(pszCommand, SOUND_LIST_RELOAD_COMMAND))
	{
		if (bCurrentlyInSoundPanel)
		{
			//clear everything for the combo box and reload it
			m_SoundsList->RemoveAll();
			InitalizeSounds();
		}
		else
		{
			//clear everything for the combo box and reload it
			m_SoundscapesList->RemoveAll();

			bool bPrev = g_bSSMHack;
			g_bSSMHack = true;

			//reload all the soundscape files
			enginesound->StopAllSounds(true);

			g_SoundscapeSystem.StartNewSoundscape(nullptr);
			g_SoundscapeSystem.RemoveAll();
			g_SoundscapeSystem. Init();

			g_bSSMHack = bPrev;

			//load all the temporary soundscapes
			CUtlVector<const char*> OtherSoundscapes;
			for (KeyValues* curr = g_SoundscapeMaker->GetPanelFile(); curr; curr = curr->GetNextKey())
			{
				if (curr == g_SoundscapeMaker->GetPanelSelected())
					continue;

				OtherSoundscapes.AddToTail(curr->GetName());
			}

			InitalizeSoundscapes(OtherSoundscapes);
		}

		return;
	}

	BaseClass::OnCommand(pszCommand);
}

//-----------------------------------------------------------------------------
// Purpose: Called on panel close
//-----------------------------------------------------------------------------
void CSoundListPanel::OnClose()
{
	OnCommand(SOUND_LIST_STOP_COMMAND);
	BaseClass::OnClose();
}

//-----------------------------------------------------------------------------
// Purpose: Initalizes the sounds list
//-----------------------------------------------------------------------------
void CSoundListPanel::InitalizeSounds()
{
	//get the sound array
	GetSoundNames();

	//add all the sounds
	for (int i = 0; i < g_SoundDirectories.Size(); i++)
		m_SoundsList->AddItem(g_SoundDirectories[i], nullptr);

	m_SoundsList->ActivateItem(0);
}

//-----------------------------------------------------------------------------
// Purpose: Initalizes the soundscape list
//-----------------------------------------------------------------------------
void CSoundListPanel::InitalizeSoundscapes(CUtlVector<const char*>& OtherSoundscapes)
{
	//remove everything
	m_SoundscapesList->RemoveAll();

	//add all the soundscapes
	for (int i = 0; i < g_SoundscapeSystem.m_soundscapes.Count(); i++)
		OtherSoundscapes.AddToTail(g_SoundscapeSystem.m_soundscapes[i]->GetName());

	OtherSoundscapes.Sort(VectorSortFunc);

	//quickly remove duplicatesd
	for (int i = 1; i < OtherSoundscapes.Count(); )
	{
		if (!Q_strcmp(OtherSoundscapes[i], OtherSoundscapes[i - 1]))
		{
			OtherSoundscapes.Remove(i);
			continue;
		}
		i++;
	}

	for (int i = 0; i < OtherSoundscapes.Size(); i++)
		m_SoundscapesList->AddItem(OtherSoundscapes[i], nullptr);

	m_SoundscapesList->ActivateItem(0);
}

//-----------------------------------------------------------------------------
// Purpose: Sets if this panel is currently the sound panel or soundscape 
//			selector panel.
//-----------------------------------------------------------------------------
void CSoundListPanel::SetIsUsingSoundPanel(bool bUsing)
{
	bCurrentlyInSoundPanel = bUsing;

	//disable stuff
	if (bUsing)
	{
		//set 'reload' text
		m_ReloadSounds->SetText("Reload Sounds");

		m_SoundscapesList->SetVisible(false);
		m_SoundsList->SetVisible(true);

		//enable the play button
		m_PlayButton->SetEnabled(true);
		m_StopSoundButton->SetEnabled(true);

		//set texts
		m_PlayButton->SetText("Play Sound");
		m_StopSoundButton->SetText("Stop Sound");
		m_InsertButton->SetText("Insert Sound");

		//set title
		SetTitle("Sounds List", true);
	}
	else
	{
		//set 'reload' text
		m_ReloadSounds->SetText("Reload Soundscapes");

		m_SoundscapesList->SetVisible(true);
		m_SoundsList->SetVisible(false);

		//disable the play button
		m_PlayButton->SetEnabled(false);
		m_StopSoundButton->SetEnabled(false);

		//set texts
		m_PlayButton->SetText("Play Soundscape");
		m_StopSoundButton->SetText("Stop Soundscape");
		m_InsertButton->SetText("Insert Soundscape");

		//set stuff
		SetTitle("Soundscape List", true);
	}
}

//static sound list instance
static CSoundListPanel* g_SoundPanel = nullptr;


//soundscape list


#define ADD_SOUNDSCAPE_COMMAND "AddSoundscape"
#define PASTE_FROM_CLIBOARD_COMMAND "PasteFromClipboard"
#define OPEN_CLIBOARD_COMMAND "OpenClipboard"


//soundscape list class
class CSoundscapeList : public vgui::Divider
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeList, vgui::Divider);

	//constructor
	CSoundscapeList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height);

	//menu item stuff
	virtual void AddButton(const char* name, const char* text, const char* command, vgui::Panel* parent, KeyValues* add, SoundscapeClipboardType type);
	virtual void Clear();

	//other
	virtual void OnMouseWheeled(int delta);
	virtual void OnMouseReleased(vgui::MouseCode code);

	virtual void OnCommand(const char* pszCommand);
	virtual void PaintBackground();

	virtual void OnKeyCodeReleased(vgui::KeyCode code);

	//message funcs
	MESSAGE_FUNC_INT(ScrollBarMoved, "ScrollBarSliderMoved", position);

protected:
	friend class CSoundscapeMaker;

	//keyvalue list.
	KeyValues* m_Keyvalues = nullptr;

	//says "Soundscapes List"
	vgui::Label* m_pLabel;
	vgui::ScrollBar* m_pSideSlider;

	//menu
	vgui::Menu* menu;

	//menu button stuff
	CUtlVector<CSoundscapeButton*> m_MenuButtons;
	int m_iCurrentY;
	int m_iMax;
	int m_AmtAdded;
};

//-----------------------------------------------------------------------------
// Purpose: Constructor for soundscape list panel
//-----------------------------------------------------------------------------
CSoundscapeList::CSoundscapeList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
	: BaseClass(parent, name)
{
	//create the text
	m_pLabel = new vgui::Label(this, "ListsText", text);
	m_pLabel->SetVisible(true);
	m_pLabel->SetBounds(text_x_pos, 2, 150, 20);

	//create the side slider
	m_pSideSlider = new vgui::ScrollBar(this, "ListsSlider", true);
	m_pSideSlider->SetBounds(width - 20, 0, 20, height - 2);
	m_pSideSlider->SetValue(0);
	m_pSideSlider->SetEnabled(false);
	m_pSideSlider->SetRange(0, 0);
	m_pSideSlider->SetButtonPressedScrollValue(1);
	m_pSideSlider->SetRangeWindow(0);
	m_pSideSlider->AddActionSignalTarget(this);

	m_iCurrentY = 22;
	m_iMax = max;
	m_Keyvalues = nullptr;
}

//-----------------------------------------------------------------------------
// Purpose: adds a button to the soundscape list
//-----------------------------------------------------------------------------
void CSoundscapeList::AddButton(const char* name, const char* text, const char* command, vgui::Panel* parent, KeyValues* add, SoundscapeClipboardType type)
{
	//create a new button
	CSoundscapeButton* button = new CSoundscapeButton(this, name, text, parent, command, add, type);
	button->SetBounds(5, m_iCurrentY, GetWide() - 30, 20);

	//increment current y
	m_iCurrentY = m_iCurrentY + 22;

	//add button to array
	m_MenuButtons.AddToTail(button);

	//if the count is more then m_iMax then set slider value
	if (m_MenuButtons.Count() > m_iMax)
	{
		int max = m_MenuButtons.Count() - m_iMax;

		m_pSideSlider->SetRange(0, max);
		m_pSideSlider->SetRangeWindow(1);
		m_pSideSlider->SetEnabled(true);
	}

	m_AmtAdded++;

	//check to see if we need to scroll down
	if (m_MenuButtons.Count() >= m_iMax)
		OnMouseWheeled(-1);
}

//-----------------------------------------------------------------------------
// Purpose: Clears everything for this list
//-----------------------------------------------------------------------------
void CSoundscapeList::Clear()
{
	//reset the slider
	m_pSideSlider->SetValue(0);
	m_pSideSlider->SetEnabled(false);
	m_pSideSlider->SetRange(0, 0);
	m_pSideSlider->SetButtonPressedScrollValue(1);
	m_pSideSlider->SetRangeWindow(0);

	//delete and clear the buttons
	for (int i = 0; i < m_MenuButtons.Count(); i++)
		m_MenuButtons[i]->DeletePanel();

	m_MenuButtons.RemoveAll();

	//reset current y
	m_iCurrentY = 22;

	m_AmtAdded = 0;
}

//-----------------------------------------------------------------------------
// Purpose: Called when a mouse is wheeled
//-----------------------------------------------------------------------------
void CSoundscapeList::OnMouseWheeled(int delta)
{
	//check for scroll down
	if (delta == -1)
		m_pSideSlider->SetValue(m_pSideSlider->GetValue() + 1);

	//check for scroll up
	else if (delta == 1)
		m_pSideSlider->SetValue(m_pSideSlider->GetValue() - 1);
}

//-----------------------------------------------------------------------------
// Purpose: Called when a mouse code is released
//-----------------------------------------------------------------------------
void CSoundscapeList::OnMouseReleased(vgui::MouseCode code)
{
	if (code != vgui::MouseCode::MOUSE_RIGHT)
		return;

	//get cursor pos
	int x, y;
	vgui::surface()->SurfaceGetCursorPos(x, y);

	//create menu
	menu = new vgui::Menu(this, "Menu");
	menu->AddMenuItem("AddSoundscape", "Add Soundscape", ADD_SOUNDSCAPE_COMMAND, this);

	//check clipboard item
	if (CurrClipboardName.Count() > 0)
	{
		menu->AddSeparator();
		menu->AddMenuItem("PasteFromClipboard", "Paste", PASTE_FROM_CLIBOARD_COMMAND, this);
		menu->AddMenuItem("OpenClipboard", "Open Clipboard", OPEN_CLIBOARD_COMMAND, this);
	}

	menu->SetBounds(x, y, 200, 50);
	menu->SetVisible(true);
}

//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeList::OnCommand(const char* pszCommand)
{
	if (!Q_strcmp(pszCommand, ADD_SOUNDSCAPE_COMMAND))
	{
		const char* name = CFmtStr("New Soundscape %d", m_AmtAdded);

		//add to keyvalues file
		KeyValues* kv = new KeyValues(name);
		KeyValues* tmp = m_Keyvalues;
		KeyValues* tmp2 = tmp;

		AddButton(name, name, name, GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeName);

		//get last subkey
		while (tmp != nullptr)
		{
			tmp2 = tmp;
			tmp = tmp->GetNextTrueSubKey();
		}

		//add to last subkey
		tmp2->SetNextKey(kv);

		GetParent()->OnCommand(name);
		return;
	}
	else if (!Q_strcmp(pszCommand, PASTE_FROM_CLIBOARD_COMMAND))
	{
		int index = CurrClipboardName.Count() - 1;

		const char* name = CFmtStr("%s - (Copy %d)", CurrClipboardName[index]->GetName(), m_AmtAdded);

		//add to keyvalues file
		KeyValues* kv = new KeyValues(name);
		CurrClipboardName[index]->CopySubkeys(kv);

		KeyValues* tmp = m_Keyvalues;
		KeyValues* tmp2 = tmp;

		AddButton(name, name, name, GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeName);

		//get last subkey
		while (tmp != nullptr)
		{
			tmp2 = tmp;
			tmp = tmp->GetNextTrueSubKey();
		}

		//add to last subkey
		tmp2->SetNextKey(kv);

		GetParent()->OnCommand(name);
		return;
	}
	else if (!Q_strcmp(pszCommand, OPEN_CLIBOARD_COMMAND))
	{
		if (g_SoundscapeClipboard)
			g_SoundscapeClipboard->DeletePanel();

		g_SoundscapeClipboard = new CSoundscapeClipboard(SoundscapeClipboardType::Type_SoundscapeName);
		return;
	}

	BaseClass::OnCommand(pszCommand);
}

//-----------------------------------------------------------------------------
// Purpose: Paints the background
//-----------------------------------------------------------------------------
void CSoundscapeList::PaintBackground()
{
	//colors
	static Color EnabledColor = Color(100, 100, 100, 200);
	static Color DisabledColor = Color(60, 60, 60, 200);

	//if m_KeyValues then paint the default color
	if (m_Keyvalues)
		SetBgColor(EnabledColor);
	else
		SetBgColor(DisabledColor);

	BaseClass::PaintBackground();
}

//-----------------------------------------------------------------------------
// Purpose: Called on keyboard code pressed
//-----------------------------------------------------------------------------
void CSoundscapeList::OnKeyCodeReleased(vgui::KeyCode code)
{
	//check for arrow
	if (code == KEY_UP)
	{
		//find selected item
		for (int i = 0; i < m_MenuButtons.Count(); i++)
		{
			if (m_MenuButtons[i]->m_bIsSelected)
			{
				//check for size and to see if we can select item
				if (i - 1 < 0)
					return;

				//select that item
				GetParent()->OnCommand(m_MenuButtons[i - 1]->GetCommand()->GetString("command"));
				return;
			}
		}
	}

	//check for arrow
	if (code == KEY_DOWN)
	{
		//find selected item
		for (int i = 0; i < m_MenuButtons.Count(); i++)
		{
			if (m_MenuButtons[i]->m_bIsSelected)
			{
				//check for size and to see if we can select item
				if (i + 1 >= m_MenuButtons.Count())
					return;

				//select that item
				GetParent()->OnCommand(m_MenuButtons[i + 1]->GetCommand()->GetString("command"));
				return;
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called on scroll bar moved
//-----------------------------------------------------------------------------
void CSoundscapeList::ScrollBarMoved(int delta)
{
	int position = m_pSideSlider->GetValue();

	//move everything down (if needed)
	for (int i = 0; i < m_MenuButtons.Count(); i++)
	{
		//make not visible if i < position
		if (i < position)
		{
			m_MenuButtons[i]->SetVisible(false);
			continue;
		}

		m_MenuButtons[i]->SetPos(5, 22 * ((i - position) + 1));
		m_MenuButtons[i]->SetVisible(true);
	}
}




//soundscape data list


#define NEW_PLAYLOOPING_COMMAND "NewLooping"
#define NEW_SOUNDSCAPE_COMMAND "NewSoundscape"
#define NEW_RANDOM_COMMAND "NewRandom"


class CSoundscapeDataList : public CSoundscapeList
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeDataList, CSoundscapeList);

	CSoundscapeDataList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
		: CSoundscapeList(parent, name, text, text_x_pos, max, width, height)
	{}

	//override right click functionality
	virtual void OnMouseReleased(vgui::MouseCode code);

	void OnCommand(const char* pszCommand);

private:
	friend class CSoundscapeMaker;
};


//-----------------------------------------------------------------------------
// Purpose: Called when a mouse code is released
//-----------------------------------------------------------------------------
void CSoundscapeDataList::OnMouseReleased(vgui::MouseCode code)
{
	//if no soundscape is selected or mouse code != right then return
	if (code != vgui::MouseCode::MOUSE_RIGHT || !m_Keyvalues)
		return;

	//get cursor pos
	int x, y;
	vgui::surface()->SurfaceGetCursorPos(x, y);

	//create menu
	menu = new vgui::Menu(this, "Menu");
	menu->AddMenuItem("AddLooping", "Add Looping Sound", NEW_PLAYLOOPING_COMMAND, this);
	menu->AddMenuItem("AddSoundscape", "Add Soundscape", NEW_SOUNDSCAPE_COMMAND, this);
	menu->AddMenuItem("AddSoundscape", "Add Random Sounds", NEW_RANDOM_COMMAND, this);

	//add clipboard thing
	if (CurrClipboardData.Count() > 0)
	{
		menu->AddSeparator();
		menu->AddMenuItem("PasteFromClipboard", "Paste", PASTE_FROM_CLIBOARD_COMMAND, this);
		menu->AddMenuItem("OpenClipboard", "Open Clipboard", OPEN_CLIBOARD_COMMAND, this);
	}

	menu->SetBounds(x, y, 200, 50);
	menu->SetVisible(true);
}


//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeDataList::OnCommand(const char* pszCommand)
{
	if (!Q_strcmp(pszCommand, NEW_PLAYLOOPING_COMMAND))
	{
		int LoopingNum = 0;
		FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
		{
			//store data name
			const char* name = data->GetName();

			//increment variables based on name
			if (!Q_strcasecmp(name, "playlooping"))
				LoopingNum++;
		}

		//add the keyvalues
		KeyValues* kv = new KeyValues("playlooping");
		kv->SetFloat("volume", 1);
		kv->SetInt("pitch", 100);

		//add the keyvalue to both this and the keyvalues
		AddButton("playlooping", "playlooping", CFmtStr("$playlooping%d", LoopingNum + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);

		m_Keyvalues->AddSubKey(kv);

		GetParent()->OnCommand(CFmtStr("$playlooping%d", LoopingNum + 1));

		return;
	}
	else if (!Q_strcmp(pszCommand, NEW_SOUNDSCAPE_COMMAND))
	{
		int SoundscapeNum = 0;
		FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
		{
			//store data name
			const char* name = data->GetName();

			//increment variables based on name
			if (!Q_strcasecmp(name, "playsoundscape"))
				SoundscapeNum++;
		}

		//add the keyvalues
		KeyValues* kv = new KeyValues("playsoundscape");
		kv->SetFloat("volume", 1);

		AddButton("playsoundscape", "playsoundscape", CFmtStr("$playsoundscape%d", SoundscapeNum + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);

		//add the keyvalue to both this and the keyvalues
		m_Keyvalues->AddSubKey(kv);

		GetParent()->OnCommand(CFmtStr("$playsoundscape%d", SoundscapeNum + 1));

		return;
	}
	else if (!Q_strcmp(pszCommand, NEW_RANDOM_COMMAND))
	{
		int RandomNum = 0;
		FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
		{
			//store data name
			const char* name = data->GetName();

			//increment variables based on name
			if (!Q_strcasecmp(name, "playrandom"))
				RandomNum++;
		}

		//add the keyvalues
		KeyValues* kv = new KeyValues("playrandom");


		kv->SetString("volume", "0.5,0.8");
		kv->SetInt("pitch", 100);
		kv->SetString("time", "10,20");

		AddButton("playrandom", "playrandom", CFmtStr("$playrandom%d", RandomNum + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);

		//make rndwave subkey
		KeyValues* rndwave = new KeyValues("rndwave");
		kv->AddSubKey(rndwave);

		//add the keyvalue to both this and the keyvalues
		m_Keyvalues->AddSubKey(kv);

		//make the parent show the new item
		GetParent()->OnCommand(CFmtStr("$playrandom%d", RandomNum + 1));

		return;
	}
	else if (!Q_strcmp(pszCommand, PASTE_FROM_CLIBOARD_COMMAND))
	{
		int index = CurrClipboardData.Count() - 1;

		const char* type = CurrClipboardData[index]->GetName();

		//get num of that item
		int NumItem = 0;
		FOR_EACH_TRUE_SUBKEY(m_Keyvalues, data)
		{
			//store data name
			const char* name = data->GetName();

			//increment variables based on name
			if (!Q_strcasecmp(name, type))
				NumItem++;
		}

		//add the keyvalues
		KeyValues* kv = new KeyValues(type);
		CurrClipboardData[index]->CopySubkeys(kv);

		AddButton(type, type, CFmtStr("$%s%d", type, NumItem + 1), GetParent(), kv, SoundscapeClipboardType::Type_SoundscapeData);

		//add the keyvalue to both this and the keyvalues
		m_Keyvalues->AddSubKey(kv);

		//make the parent show the new item
		GetParent()->OnCommand(CFmtStr("$%s%d", type, NumItem + 1));
		return;
	}
	else if (!Q_strcmp(pszCommand, OPEN_CLIBOARD_COMMAND))
	{
		if (g_SoundscapeClipboard)
			g_SoundscapeClipboard->DeletePanel();

		g_SoundscapeClipboard = new CSoundscapeClipboard(SoundscapeClipboardType::Type_SoundscapeData);
		return;
	}

	BaseClass::OnCommand(pszCommand);
}


//soundscape rndwave data list


#define NEW_RNDWAVE_WAVE_COMMAND "NewRNDWave"


class CSoundscapeRndwaveList : public CSoundscapeList
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeDataList, CSoundscapeList);

	CSoundscapeRndwaveList(vgui::Panel* parent, const char* name, const char* text, int text_x_pos, int max, int width, int height)
		: CSoundscapeList(parent, name, text, text_x_pos, max, width, height)
	{}

	//override right click functionality
	virtual void OnMouseReleased(vgui::MouseCode code);

	void OnCommand(const char* pszCommand);

private:
	friend class CSoundscapeMaker;
};


//-----------------------------------------------------------------------------
// Purpose: Called when a mouse code is released
//-----------------------------------------------------------------------------
void CSoundscapeRndwaveList::OnMouseReleased(vgui::MouseCode code)
{
	//if no soundscape is selected or mouse code != right then return
	if (code != vgui::MouseCode::MOUSE_RIGHT || !m_Keyvalues)
		return;

	//get cursor pos
	int x, y;
	vgui::surface()->SurfaceGetCursorPos(x, y);

	//create menu
	menu = new vgui::Menu(this, "Menu");
	menu->AddMenuItem("AddRandom", "Add Random Wave", NEW_RNDWAVE_WAVE_COMMAND, this);

	//add clipboard thing
	if (CurrClipboardRandom.Count() > 0)
	{
		menu->AddSeparator();
		menu->AddMenuItem("PasteFromClipboard", "Paste", PASTE_FROM_CLIBOARD_COMMAND, this);
		menu->AddMenuItem("OpenClipboard", "Open Clipboard", OPEN_CLIBOARD_COMMAND, this);
	}

	menu->SetBounds(x, y, 200, 50);
	menu->SetVisible(true);
}


//-----------------------------------------------------------------------------
// Purpose: Called on command
//-----------------------------------------------------------------------------
void CSoundscapeRndwaveList::OnCommand(const char* pszCommand)
{
	if (!Q_strcmp(pszCommand, NEW_RNDWAVE_WAVE_COMMAND) && m_Keyvalues)
	{
		//get number of keyvalues
		int num = 0;

		FOR_EACH_VALUE(m_Keyvalues, kv)
			num++;

		KeyValues* add = new KeyValues("wave");
		add->SetString(nullptr, "");

		//add keyvalues and button
		AddButton("Rndwave", "", CFmtStr("$rndwave%d", num + 1), GetParent(), add, SoundscapeClipboardType::Type_SoundscapeRandomWave);

		m_Keyvalues->AddSubKey(add);

		//forward command to parent
		GetParent()->OnCommand(CFmtStr("$rndwave%d", num + 1));

		return;
	}

	else if (!Q_strcmp(pszCommand, PASTE_FROM_CLIBOARD_COMMAND))
	{
		//get number of keyvalues
		int num = 0;

		FOR_EACH_VALUE(m_Keyvalues, kv)
			num++;

		int index = CurrClipboardRandom.Count() - 1;

		const char* text = CurrClipboardRandom[index]->GetString();

		KeyValues* add = new KeyValues("wave");
		add->SetString(nullptr, text);

		//get last / or \ and make the string be that + 1
		char* fslash = Q_strrchr(text, '/');
		char* bslash = Q_strrchr(text, '\\');

		if (fslash > bslash)
			text = fslash + 1;
		else if (bslash > fslash)
			text = bslash + 1;

		//add keyvalues and button
		AddButton("Rndwave", text, CFmtStr("$rndwave%d", num + 1), GetParent(), add, SoundscapeClipboardType::Type_SoundscapeRandomWave);

		m_Keyvalues->AddSubKey(add);

		//forward command to parent
		GetParent()->OnCommand(CFmtStr("$rndwave%d", num + 1));
		return;
	}
	else if (!Q_strcmp(pszCommand, OPEN_CLIBOARD_COMMAND))
	{
		if (g_SoundscapeClipboard)
			g_SoundscapeClipboard->DeletePanel();

		g_SoundscapeClipboard = new CSoundscapeClipboard(SoundscapeClipboardType::Type_SoundscapeRandomWave);
		return;
	}

	BaseClass::OnCommand(pszCommand);
}


//soundscape panel


#define SOUNDSCAPE_PANEL_WIDTH 760
#define SOUNDSCAPE_PANEL_HEIGHT 630

#define NEW_BUTTON_COMMAND "$NewSoundscape"
#define SAVE_BUTTON_COMMAND "$SaveSoundscape"
#define LOAD_BUTTON_COMMAND "$LoadSoundscape"
#define OPTIONS_BUTTON_COMMAND "$ShowOptions"
#define EDIT_BUTTON_COMMAND "$Edit"
#define RESET_BUTTON_COMMAND "$ResetSoundscapes"
#define SOUNDS_LIST_BUTTON_COMMAND "$ShowSoundsList"
#define PLAY_SOUNDSCAPE_COMMAND "$PlaySoundscape"
#define RESET_SOUNDSCAPE_BUTTON_COMMAND "$ResetSoundscape"
#define DELETE_CURRENT_ITEM_COMMAND "$DeleteItem"

//static bool to determin if the soundscape panel should show or not
bool g_ShowSoundscapePanel = false;
bool g_IsPlayingSoundscape = false;

//soundscape maker panel
class CSoundscapeMaker : public vgui::Frame, CAutoGameSystem
{
public:
	DECLARE_CLASS_SIMPLE(CSoundscapeMaker, vgui::Frame)

	CSoundscapeMaker(vgui::VPANEL parent);

	//tick functions
	void OnTick();

	//other functions
	void OnClose();
	void OnCommand(const char* pszCommand);
	void Paste(SoundscapeClipboardType type);

	void PlaySelectedSoundscape();
	void LoadFile(KeyValues* file);

	void OnKeyCodePressed(vgui::KeyCode code);

	void SetSoundText(const char* text);

	//to play the soundscape on map spawn
	void LevelInitPostEntity();

	//sets the keyvalue file
	void Set(const char* buffer);

	//message pointer funcs
	MESSAGE_FUNC_CHARPTR(OnFileSelected, "FileSelected", fullpath);
	MESSAGE_FUNC_PARAMS(OnTextChanged, "TextChanged", data);

	~CSoundscapeMaker();

public:

	//the soundscape keyvalues file
	KeyValues* m_KeyValues = nullptr;

private:
	void CreateEverything();

private:
	//lists all the soundscapes
	CSoundscapeList* m_SoundscapesList;
	CSoundscapeDataList* m_pDataList;
	CSoundscapeRndwaveList* m_pSoundList;

	//buttons
	vgui::Button* m_ButtonNew = nullptr;
	vgui::Button* m_ButtonSave = nullptr;
	vgui::Button* m_ButtonLoad = nullptr;
	vgui::Button* m_ButtonOptions = nullptr;
	vgui::Button* m_EditButton = nullptr;

	//file load and save dialogs
	vgui::FileOpenDialog* m_FileSave = nullptr;
	vgui::FileOpenDialog* m_FileLoad = nullptr;
	bool m_bWasFileLoad = false;

	//text entry for name
	vgui::TextEntry* m_TextEntryName;

	//combo box for dsp effects
	vgui::ComboBox* m_DspEffects;
	vgui::ComboBox* m_SoundLevels;

	//sound data text entry
	vgui::TextEntry* m_TimeTextEntry;
	vgui::TextEntry* m_VolumeTextEntry;
	vgui::TextEntry* m_PitchTextEntry;
	vgui::TextEntry* m_PositionTextEntry;
	vgui::TextEntry* m_SoundNameTextEntry;

	//play sound button
	vgui::Button* m_SoundNamePlay;

	//play/reset soundscape buttons
	vgui::CheckButton* m_PlaySoundscapeButton;
	vgui::Button* m_ResetSoundscapeButton;
	vgui::Button* m_DeleteCurrentButton;

	//current selected soundscape
	CSoundscapeButton* m_pCurrentSelected = nullptr;

public:
	KeyValues* m_kvCurrSelected = nullptr;

private:
	KeyValues* m_kvCurrSound = nullptr;
	KeyValues* m_kvCurrRndwave = nullptr;

	int m_iCurrRndWave = 0;

	//currently in non randomwave thing
	SoundscapeMode m_iSoundscapeMode = SoundscapeMode::Mode_Random;

	//temporary added soundscapes
	CUtlVector<KeyValues*> m_TmpAddedSoundscapes;
};

//user message hook
void _SoundscapeMaker_Recieve(bf_read& bf);

//-----------------------------------------------------------------------------
// Purpose: Constructor for soundscape maker panel
//-----------------------------------------------------------------------------
CSoundscapeMaker::CSoundscapeMaker(vgui::VPANEL parent)
	: BaseClass(nullptr, "SoundscapeMaker")
{
	static bool bRegistered = false;
	if (!bRegistered)
	{
		usermessages->HookMessage("SoundscapeMaker_Recieve", _SoundscapeMaker_Recieve);
		bRegistered = true;
	}

	//set variables
	m_pCurrentSelected = nullptr;

	SetParent(parent);

	SetKeyBoardInputEnabled(true);
	SetMouseInputEnabled(true);

	SetProportional(false);
	SetTitleBarVisible(true);
	SetMinimizeButtonVisible(false);
	SetMaximizeButtonVisible(false);
	SetCloseButtonVisible(true);
	SetSizeable(false);
	SetMoveable(true);
	SetVisible(g_ShowSoundscapePanel);

	int ScreenWide, ScreenTall;
	vgui::surface()->GetScreenSize(ScreenWide, ScreenTall);

	SetTitle("Soundscape Maker (New File)", true);
	SetSize(SOUNDSCAPE_PANEL_WIDTH, SOUNDSCAPE_PANEL_HEIGHT);
	SetPos((ScreenWide - SOUNDSCAPE_PANEL_WIDTH) / 2, (ScreenTall - SOUNDSCAPE_PANEL_HEIGHT) / 2);



	//add a tick signal for every 50 ms
	vgui::ivgui()->AddTickSignal(GetVPanel(), 50);

	CreateEverything();
}

//-----------------------------------------------------------------------------
// Purpose: Creates everything for this panel
//-----------------------------------------------------------------------------
void CSoundscapeMaker::CreateEverything()
{
	//create the divider that will be the outline for the inside of the panel
	vgui::Divider* PanelOutline = new vgui::Divider(this, "InsideOutline");
	PanelOutline->SetEnabled(false);
	PanelOutline->SetBounds(5, 25, SOUNDSCAPE_PANEL_WIDTH - 10, SOUNDSCAPE_PANEL_HEIGHT - 62);

	//create the buttons
		//create the buttons
	m_ButtonNew = new vgui::Button(this, "NewButton", "New Soundscape File");
	m_ButtonNew->SetVisible(true);
	m_ButtonNew->SetBounds(7, 600, 145, 25);
	m_ButtonNew->SetCommand(NEW_BUTTON_COMMAND);
	m_ButtonNew->SetDepressedSound("ui/buttonclickrelease.wav");

	m_ButtonSave = new vgui::Button(this, "SaveButton", "Save Soundscapes");
	m_ButtonSave->SetVisible(true);
	m_ButtonSave->SetBounds(157, 600, 145, 25);
	m_ButtonSave->SetCommand(SAVE_BUTTON_COMMAND);
	m_ButtonSave->SetDepressedSound("ui/buttonclickrelease.wav");

	m_ButtonLoad = new vgui::Button(this, "LoadButton", "Load Soundscapes");
	m_ButtonLoad->SetVisible(true);
	m_ButtonLoad->SetBounds(307, 600, 145, 25);
	m_ButtonLoad->SetCommand(LOAD_BUTTON_COMMAND);
	m_ButtonLoad->SetDepressedSound("ui/buttonclickrelease.wav");

	m_ButtonOptions = new vgui::Button(this, "OptionsButton", "Show Options Panel");
	m_ButtonOptions->SetVisible(true);
	m_ButtonOptions->SetBounds(457, 600, 145, 25);
	m_ButtonOptions->SetCommand(OPTIONS_BUTTON_COMMAND);
	m_ButtonOptions->SetDepressedSound("ui/buttonclickrelease.wav");

	m_EditButton = new vgui::Button(this, "EditButton", "Show Text Editor");
	m_EditButton->SetVisible(true);
	m_EditButton->SetBounds(607, 600, 145, 25);
	m_EditButton->SetCommand(EDIT_BUTTON_COMMAND);
	m_EditButton->SetDepressedSound("ui/buttonclickrelease.wav");

	//create the soundscapes menu
	m_SoundscapesList = new CSoundscapeList(this, "SoundscapesList", "Soundscapes:", 90, 22, 300, 550);
	m_SoundscapesList->SetBounds(15, 35, 300, 550);
	m_SoundscapesList->SetVisible(true);

	//create data list
	m_pDataList = new CSoundscapeDataList(this, "SoudscapeDataList", "Soundscape Data:", 35, 10, 200, 310);
	m_pDataList->SetBounds(327, 275, 200, 310);
	m_pDataList->SetVisible(true);

	//create sound list
	m_pSoundList = new CSoundscapeRndwaveList(this, "SoudscapeDataList", "Random Sounds:", 40, 10, 200, 310);
	m_pSoundList->SetBounds(542, 275, 200, 310);
	m_pSoundList->SetVisible(true);

	//name text entry
	m_TextEntryName = new vgui::TextEntry(this, "NameTextEntry");
	m_TextEntryName->SetEnabled(false);
	m_TextEntryName->SetBounds(325, 40, 295, 20);
	m_TextEntryName->SetMaximumCharCount(256);

	//dsp effects combo box
	m_DspEffects = new vgui::ComboBox(this, "DspEffects", sizeof(g_DspEffects) / sizeof(g_DspEffects[0]), false);
	m_DspEffects->SetEnabled(false);
	m_DspEffects->SetBounds(325, 65, 295, 20);
	m_DspEffects->SetText("");
	m_DspEffects->AddActionSignalTarget(this);

	for (int i = 0; i < sizeof(g_DspEffects) / sizeof(g_DspEffects[i]); i++)
		m_DspEffects->AddItem(g_DspEffects[i], nullptr);

	//time text entry
	m_TimeTextEntry = new vgui::TextEntry(this, "TimeTextEntry");
	m_TimeTextEntry->SetBounds(325, 90, 295, 20);
	m_TimeTextEntry->SetEnabled(false);
	m_TimeTextEntry->SetVisible(true);

	//volume text entry
	m_VolumeTextEntry = new vgui::TextEntry(this, "VolumeTextEntry");
	m_VolumeTextEntry->SetBounds(325, 115, 295, 20);
	m_VolumeTextEntry->SetEnabled(false);
	m_VolumeTextEntry->SetVisible(true);

	//pitch text entry
	m_PitchTextEntry = new vgui::TextEntry(this, "PitchTextEntry");
	m_PitchTextEntry->SetBounds(325, 140, 295, 20);
	m_PitchTextEntry->SetEnabled(false);
	m_PitchTextEntry->SetVisible(true);

	//position text entry
	m_PositionTextEntry = new vgui::TextEntry(this, "PositionTextEntry");
	m_PositionTextEntry->SetBounds(325, 165, 295, 20);
	m_PositionTextEntry->SetEnabled(false);
	m_PositionTextEntry->SetVisible(true);

	//sound levels
	m_SoundLevels = new vgui::ComboBox(this, "SoundLevels", sizeof(g_SoundLevels) / sizeof(g_SoundLevels[0]), false);
	m_SoundLevels->SetEnabled(false);
	m_SoundLevels->SetBounds(325, 190, 295, 20);
	m_SoundLevels->SetText("");
	m_SoundLevels->AddActionSignalTarget(this);

	for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
		m_SoundLevels->AddItem(g_SoundLevels[i], nullptr);

	//sound name
	m_SoundNameTextEntry = new vgui::TextEntry(this, "SoundName");
	m_SoundNameTextEntry->SetBounds(325, 215, 215, 20);
	m_SoundNameTextEntry->SetEnabled(false);
	m_SoundNameTextEntry->SetVisible(true);

	//sound list button
	m_SoundNamePlay = new vgui::Button(this, "SoundPlayButton", "Sounds List");
	m_SoundNamePlay->SetBounds(545, 215, 75, 20);
	m_SoundNamePlay->SetCommand(SOUNDS_LIST_BUTTON_COMMAND);
	m_SoundNamePlay->SetEnabled(false);

	//starts the soundscape
	m_PlaySoundscapeButton = new vgui::CheckButton(this, "PlaySoundscape", "Play Soundscape");
	m_PlaySoundscapeButton->SetBounds(330, 243, 125, 20);
	m_PlaySoundscapeButton->SetCommand(PLAY_SOUNDSCAPE_COMMAND);
	m_PlaySoundscapeButton->SetEnabled(false);
	m_PlaySoundscapeButton->SetSelected(false);

	//reset soundscape button
	m_ResetSoundscapeButton = new vgui::Button(this, "ResetSoundscape", "Restart Soundscape");
	m_ResetSoundscapeButton->SetBounds(465, 243, 125, 20);
	m_ResetSoundscapeButton->SetCommand(RESET_SOUNDSCAPE_BUTTON_COMMAND);
	m_ResetSoundscapeButton->SetEnabled(false);

	//delete this item
	m_DeleteCurrentButton = new vgui::Button(this, "DeleteItem", "Delete Current Item");
	m_DeleteCurrentButton->SetBounds(595, 243, 135, 20);
	m_DeleteCurrentButton->SetCommand(DELETE_CURRENT_ITEM_COMMAND);
	m_DeleteCurrentButton->SetEnabled(false);

	//create the soundscape name text
	vgui::Label* NameLabel = new vgui::Label(this, "NameLabel", "Soundscape Name");
	NameLabel->SetBounds(635, 40, 125, 20);

	//create the soundscape dsp text
	vgui::Label* DspLabel = new vgui::Label(this, "DspLabel", "Soundscape Dsp");
	DspLabel->SetBounds(635, 65, 125, 20);

	//create the soundscape time text
	vgui::Label* TimeLabel = new vgui::Label(this, "TimeLabel", "Sound Time");
	TimeLabel->SetBounds(635, 90, 125, 20);

	//create the soundscape volumn text
	vgui::Label* VolumeLabel = new vgui::Label(this, "VolumeLabel", "Sound Volume");
	VolumeLabel->SetBounds(635, 115, 125, 20);

	//create the soundscape pitch text
	vgui::Label* PitchLabel = new vgui::Label(this, "PitchLabel", "Sound Pitch");
	PitchLabel->SetBounds(635, 140, 125, 20);

	//create the soundscape position text
	vgui::Label* PositionLabel = new vgui::Label(this, "PositionLabel", "Sound Position");
	PositionLabel->SetBounds(635, 165, 125, 20);

	//create the soundscape sound level text
	vgui::Label* SoundLevelLabel = new vgui::Label(this, "SoundLevelLabel", "Sound Level");
	SoundLevelLabel->SetBounds(635, 190, 125, 20);

	//create the soundscape sound name text
	vgui::Label* SoundName = new vgui::Label(this, "SoundName", "Sound Name");
	SoundName->SetBounds(635, 215, 125, 20);

	//create the soundscape keyvalues and load it
	m_KeyValues = new KeyValues("Empty Soundscape");

	LoadFile(m_KeyValues);
}

//-----------------------------------------------------------------------------
// Purpose: Called every tick for the soundscape maker
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnTick()
{
	//set the visibility
	static bool bPrevVisible = g_ShowSoundscapePanel;
	if (g_ShowSoundscapePanel != bPrevVisible)
		SetVisible(g_ShowSoundscapePanel);

	//set the old visibility
	bPrevVisible = g_ShowSoundscapePanel;
}

//-----------------------------------------------------------------------------
// Purpose: Called when the close button is pressed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnClose()
{
	//hide the other panels
	g_SoundPanel->OnClose();
	g_SettingsPanel->OnClose();
	g_SoundscapeTextPanel->OnClose();

	g_ShowSoundscapePanel = false;
}

//-----------------------------------------------------------------------------
// Purpose: Play the selected soundscape
//-----------------------------------------------------------------------------
void CSoundscapeMaker::PlaySelectedSoundscape()
{
	//set debug stuff
	SoundscapePrint(Color(255, 255, 255, 255), "\n\n\n=============== %s %s =================\n\n", m_kvCurrSelected ? "Starting Soundscape: " : "Stopping Current Soundscape", m_kvCurrSelected ? m_kvCurrSelected->GetName() : "");
	g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->Clear();

	g_IsPlayingSoundscape = true;
	g_bSSMHack = true;

	//remove all the temporary soundscapes from the soundscape system
	for (int i = 0; i < m_TmpAddedSoundscapes.Count(); i++)
	{
		for (int j = 0; j < g_SoundscapeSystem.m_soundscapes.Count(); j++)
		{
			if (g_SoundscapeSystem.m_soundscapes[j] == m_TmpAddedSoundscapes[i])
			{
				g_SoundscapeSystem.m_soundscapes.Remove(j);
				break;
			}
		}
	}

	m_TmpAddedSoundscapes.RemoveAll();

	//change audio params position
	g_SoundscapeSystem.m_params.localBits = 0x7f;
	for (int i = 0; i < MAX_SOUNDSCAPES - 1; i++)
		g_SoundscapeSystem.m_params.localSound.Set(i, g_SoundscapePositions[i]);


	//if m_kvCurrSelected then add all the "playsoundscape" soundscape keyvalues
	//into the g_SoundscapeSystem.m_soundscapes array
	if (m_kvCurrSelected)
	{
		CUtlVector<const char*> SoundscapeNames;
		FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, subkey)
		{
			//look for playsoundscape file
			if (!Q_strcasecmp(subkey->GetName(), "playsoundscape"))
			{
				const char* name = subkey->GetString("name", nullptr);
				if (!name || !name[0] || SoundscapeNames.Find(name) != SoundscapeNames.InvalidIndex())
					continue;

				SoundscapeNames.AddToTail(name);
			}
		}

		//now look for each keyvalue
		for (int i = 0; i < SoundscapeNames.Count(); i++)
		{
			for (KeyValues* subkey = m_KeyValues; subkey != nullptr; subkey = subkey->GetNextTrueSubKey())
			{
				//look for playsoundscape file
				if (!Q_strcmp(subkey->GetName(), SoundscapeNames[i]))
				{
					//add it to the soundscape system
					m_TmpAddedSoundscapes.AddToTail(subkey);
					g_SoundscapeSystem.m_soundscapes.AddToTail(subkey);
				}
			}
		}
	}

	//stop all sounds
	enginesound->StopAllSounds(true);

	//stop the current soundscape and start a new soundscape
	g_SoundscapeSystem.StartNewSoundscape(nullptr);
	g_SoundscapeSystem.StartNewSoundscape(m_kvCurrSelected);

	//start debug graphs
	g_SoundscapeDebugPanel->m_PanelSoundscapesFadingIn->Start();

	g_bSSMHack = false;
}

//-----------------------------------------------------------------------------
// Purpose: Called when a button or something else gets pressed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnCommand(const char* pszCommand)
{
	//check for close command first
	if (!Q_strcmp(pszCommand, "Close"))
	{
		BaseClass::OnCommand(pszCommand);
		return;
	}

	//check for the save button command
	else if (!Q_strcmp(pszCommand, SAVE_BUTTON_COMMAND))
	{
		//initalize the file save dialog
		if (!m_FileSave)
		{
			//get the current game directory
			char buf[512];
			filesystem->RelativePathToFullPath("scripts", "MOD", buf, sizeof(buf));

			//create the save dialog
			m_FileSave = new vgui::FileOpenDialog(this, "Save Soundscape File", false);
			m_FileSave->AddFilter("*.txt", "Soundscape Text File", true);
			m_FileSave->AddFilter("*.*", "All Files (*.*)", false);
			m_FileSave->SetStartDirectory(buf);
			m_FileSave->AddActionSignalTarget(this);
		}

		//show the dialog
		m_FileSave->DoModal(false);
		m_FileSave->Activate();

		//file wasnt loadad
		m_bWasFileLoad = false;

		return;
	}

	//check for load button command
	else if (!Q_strcmp(pszCommand, LOAD_BUTTON_COMMAND))
	{
		//initalize the file save dialog
		if (!m_FileLoad)
		{
			//get the current game directory
			char buf[512];
			filesystem->RelativePathToFullPath("scripts", "MOD", buf, sizeof(buf));

			//create the load dialog
			m_FileLoad = new vgui::FileOpenDialog(this, "Load Soundscape File", true);
			m_FileLoad->AddFilter("*.txt", "Soundscape Text File", true);
			m_FileLoad->AddFilter("*.*", "All Files (*.*)", false);
			m_FileLoad->SetStartDirectory(buf);
			m_FileLoad->AddActionSignalTarget(this);
		}

		//show the file load dialog
		m_FileLoad->DoModal(false);
		m_FileLoad->Activate();

		//file was loadad
		m_bWasFileLoad = true;

		return;
	}

	//check for options panel button
	else if (!Q_strcmp(pszCommand, OPTIONS_BUTTON_COMMAND))
	{
		g_SettingsPanel->SetVisible(true);
		g_SettingsPanel->MoveToFront();
		g_SettingsPanel->RequestFocus();
		return;
	}

	//check for edit panel button
	else if (!Q_strcmp(pszCommand, EDIT_BUTTON_COMMAND))
	{
		g_SoundscapeTextPanel->SetVisible(true);
		g_SoundscapeTextPanel->MoveToFront();
		g_SoundscapeTextPanel->RequestFocus();
		g_SoundscapeTextPanel->Set(m_KeyValues);
		return;
	}

	//check for new soundscape
	else if (!Q_strcmp(pszCommand, NEW_BUTTON_COMMAND))
	{
		//make sure you want to create a new soundscape file
		vgui::QueryBox* popup = new vgui::QueryBox("New File?", "Are you sure you want to create a new soundscape file?", this);
		popup->SetOKCommand(new KeyValues("Command", "command", RESET_BUTTON_COMMAND));
		popup->SetCancelButtonVisible(false);
		popup->AddActionSignalTarget(this);
		popup->DoModal(this);

		return;
	}

	//check for reset soundscape
	else if (!Q_strcmp(pszCommand, RESET_BUTTON_COMMAND))
	{
		m_kvCurrSelected = nullptr;

		//stop all soundscapes before deleting the old soundscapes
		if (g_IsPlayingSoundscape)
			PlaySelectedSoundscape();

		m_KeyValues->deleteThis();
		m_KeyValues = new KeyValues("Empty Soundscape");

		//reset title
		SetTitle("Soundscape Maker (New File)", true);

		LoadFile(m_KeyValues);
		return;
	}

	//check for play sound
	else if (!Q_strcmp(pszCommand, SOUNDS_LIST_BUTTON_COMMAND))
	{
		//initalize the sounds
		static bool g_SoundPanelInitalized = false;
		if (!g_SoundPanelInitalized)
		{
			g_SoundPanelInitalized = true;
			g_SoundPanel->InitalizeSounds();
		}

		//get sound text entry name
		char buf[512];
		m_SoundNameTextEntry->GetText(buf, sizeof(buf));

		//check the current mode
		if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
		{
			g_SoundPanel->SetIsUsingSoundPanel(false);

			//load all the temporary soundscapes
			CUtlVector<const char*> OtherSoundscapes;
			for (KeyValues* curr = m_KeyValues; curr; curr = curr->GetNextKey())
			{
				if (curr == m_kvCurrSelected)
					continue;

				OtherSoundscapes.AddToTail(curr->GetName());
			}

			g_SoundPanel->InitalizeSoundscapes(OtherSoundscapes);
		}
		else
		{
			g_SoundPanel->SetIsUsingSoundPanel(true);

			//look for item with same name
			for (int i = 0; i < g_SoundDirectories.Count(); i++)
			{
				if (!Q_strcmp(buf, g_SoundDirectories[i]))
				{
					//select item
					g_SoundPanel->m_SoundsList->ActivateItem(i);
					g_SoundPanel->m_SoundsList->SetText(buf);

					break;
				}
			}
		}

		g_SoundPanel->SetVisible(true);
		g_SoundPanel->MoveToFront();
		g_SoundPanel->RequestFocus();
		return;
	}

	//check for play soundscape
	else if (!Q_strcmp(pszCommand, PLAY_SOUNDSCAPE_COMMAND))
	{
		if (m_PlaySoundscapeButton->IsSelected())
		{
			//enable the reset soundscape button
			m_ResetSoundscapeButton->SetEnabled(true);

			//play the soundscape
			PlaySelectedSoundscape();
		}
		else
		{
			//disable the reset soundscape button
			m_ResetSoundscapeButton->SetEnabled(false);

			g_IsPlayingSoundscape = false;

			//stop all sounds and soundscapes
			enginesound->StopAllSounds(true);
			g_SoundscapeSystem.StartNewSoundscape(nullptr);
		}

		return;
	}

	//check for play soundscape
	else if (!Q_strcmp(pszCommand, RESET_SOUNDSCAPE_BUTTON_COMMAND))
	{
		PlaySelectedSoundscape();
		return;
	}


	//check for delete item
	else if (!Q_strcmp(pszCommand, DELETE_CURRENT_ITEM_COMMAND))
	{
		//check for current rndwave
		if (m_kvCurrRndwave && m_SoundNameTextEntry->IsEnabled())
		{
			if (!m_kvCurrRndwave || m_iCurrRndWave <= 0)
				return;

			//get the keyvalues by the index
			int curr = 0;
			KeyValues* prev = nullptr;

			FOR_EACH_VALUE(m_kvCurrRndwave, keyvalues)
			{
				if (++curr == m_iCurrRndWave)
				{
					//delete
					if (prev)
						prev->SetNextKey(keyvalues->GetNextValue());
					else
					{
						m_kvCurrRndwave->m_pSub = keyvalues->GetNextValue();
						m_iCurrRndWave = -1;
					}

					curr = curr - 1;

					keyvalues->SetNextKey(nullptr);
					keyvalues->deleteThis();
					break;
				}



				prev = keyvalues;
			}

			//reset everything
			m_SoundNameTextEntry->SetText("");
			m_SoundNameTextEntry->SetEnabled(false);
			m_SoundNamePlay->SetEnabled(false);

			//store vector
			auto& vec = m_pSoundList->m_MenuButtons;

			//remove it
			delete vec[curr];
			vec.Remove(curr);

			//move everything down
			m_pSoundList->m_iCurrentY = m_pSoundList->m_iCurrentY - 22;
			m_pSoundList->m_Keyvalues = m_kvCurrRndwave;

			if (vec.Count() >= m_pSoundList->m_iMax)
			{
				m_pSoundList->OnMouseWheeled(1);

				int min, max;
				m_pSoundList->m_pSideSlider->GetRange(min, max);
				m_pSoundList->m_pSideSlider->SetRange(0, max - 1);
			}

			for (int i = curr; i < vec.Count(); i++)
			{
				//move everything down
				int x, y = 0;
				vec[i]->GetPos(x, y);
				vec[i]->SetPos(x, y - 22);
			}

			//reset every command
			int WaveAmount = 0;
			for (int i = 0; i < vec.Count(); i++)
			{
				//store data name
				const char* name = vec[i]->GetCommand()->GetString("command");

				//increment variables based on name
				if (Q_stristr(name, "$rndwave") == name)
				{
					WaveAmount++;
					vec[i]->SetCommand(CFmtStr("$rndwave%d", WaveAmount));
				}
			}

			//bounds check
			if (vec.Count() <= 0)
			{
				m_kvCurrRndwave = nullptr;
				m_pSoundList->m_Keyvalues = nullptr;

				//restart soundscape
				PlaySelectedSoundscape();

				return;
			}

			//select next item
			if (m_iCurrRndWave <= vec.Count())
				OnCommand(CFmtStr("$rndwave%d", curr + 1));
			else
				OnCommand(CFmtStr("$rndwave%d", curr));
		}
		else if (m_kvCurrSound)
		{
			//find keyvalue with same pointer and get the index
			int tmpindex = 0;
			int index = -1;

			KeyValues* prev = nullptr;
			FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, keyvalues)
			{
				if (m_kvCurrSound == keyvalues)
				{
					//remove it
					if (prev)
						prev->SetNextKey(keyvalues->GetNextTrueSubKey());
					else
						m_kvCurrSelected->m_pSub = keyvalues->GetNextTrueSubKey();

					keyvalues->SetNextKey(nullptr);
					keyvalues->deleteThis();

					//get index
					index = tmpindex;
					break;
				}

				prev = keyvalues;

				//increment
				tmpindex++;
			}

			//error
			if (index == -1)
				return;

			//store vector
			auto& vec = m_pDataList->m_MenuButtons;

			//remove it
			delete vec[index];
			vec.Remove(index);

			//move everything down
			m_pDataList->m_iCurrentY = m_pDataList->m_iCurrentY - 22;
			m_pDataList->m_Keyvalues = m_kvCurrSelected;

			for (int i = index; i < vec.Count(); i++)
			{
				//move everything down
				int x, y = 0;
				vec[i]->GetPos(x, y);
				vec[i]->SetPos(x, y - 22);
			}

			if (vec.Count() >= m_pDataList->m_iMax)
			{
				m_pDataList->OnMouseWheeled(1);

				int min, max;
				m_pDataList->m_pSideSlider->GetRange(min, max);

				if (max > 0)
					m_pDataList->m_pSideSlider->SetRange(0, max - 1);
				else
					m_pDataList->m_pSideSlider->SetRange(0, 0);
			}

			//reset the names of each button
			int RandomNum = 0;
			int LoopingNum = 0;
			int SoundscapeNum = 0;

			//change the commands of the buttons
			for (int i = 0; i < vec.Count(); i++)
			{
				//store data name
				const char* name = vec[i]->GetCommand()->GetString("command");

				//increment variables based on name
				if (Q_stristr(name, "$playrandom") == name)
				{
					RandomNum++;
					vec[i]->SetCommand(CFmtStr("$playrandom%d", RandomNum));
				}

				if (Q_stristr(name, "$playlooping") == name)
				{
					LoopingNum++;
					vec[i]->SetCommand(CFmtStr("$playlooping%d", LoopingNum));
				}

				if (Q_stristr(name, "$playsoundscape") == name)
				{
					SoundscapeNum++;
					vec[i]->SetCommand(CFmtStr("$playsoundscape%d", SoundscapeNum));
				}
			}

			//reset everything
			m_SoundLevels->SetText("");
			m_SoundNameTextEntry->SetText("");
			m_TimeTextEntry->SetText("");
			m_PitchTextEntry->SetText("");
			m_PositionTextEntry->SetText("");
			m_VolumeTextEntry->SetText("");

			m_SoundLevels->SetEnabled(false);
			m_SoundNameTextEntry->SetEnabled(false);
			m_TimeTextEntry->SetEnabled(false);
			m_PitchTextEntry->SetEnabled(false);
			m_PositionTextEntry->SetEnabled(false);
			m_VolumeTextEntry->SetEnabled(false);
			m_SoundNamePlay->SetEnabled(false);

			m_pSoundList->Clear();

			m_kvCurrSound = nullptr;
			m_pSoundList->m_Keyvalues = nullptr;

			//bounds checking
			if (index >= vec.Count())
				index = vec.Count() - 1; // fix bounds more safely

			//select the button
			if (index >= 0)
				OnCommand(vec[index]->GetCommand()->GetString("command"));
		}
		else if (m_kvCurrSelected)
		{
			if (m_KeyValues == m_kvCurrSelected)
			{
				//play an error sound
				vgui::surface()->PlaySound("resource/warning.wav");

				//show an error
				vgui::QueryBox* popup = new vgui::QueryBox("Error", "Can not delete base soundscape!", this);
				popup->SetOKButtonText("Ok");
				popup->SetCancelButtonVisible(false);
				popup->AddActionSignalTarget(this);
				popup->DoModal(this);

				return;
			}

			//find keyvalue with same pointer and get the index
			int tmpindex = 0;
			int index = -1;

			KeyValues* prev = nullptr;
			for (KeyValues* keyvalues = m_KeyValues; keyvalues != nullptr; keyvalues = keyvalues->GetNextTrueSubKey())
			{
				if (m_kvCurrSelected == keyvalues)
				{
					//remove it
					if (!prev)
						break;

					prev->SetNextKey(keyvalues->GetNextTrueSubKey());
					keyvalues->SetNextKey(nullptr);
					keyvalues->deleteThis();

					//get index
					index = tmpindex;
					break;
				}

				prev = keyvalues;

				//increment
				tmpindex++;
			}

			//error
			if (index == -1)
				return;

			//store vector
			auto& vec = m_SoundscapesList->m_MenuButtons;

			//remove it
			delete vec[index];
			vec.Remove(index);

			//move everything down
			m_SoundscapesList->m_iCurrentY = m_SoundscapesList->m_iCurrentY - 22;

			for (int i = index; i < vec.Count(); i++)
			{
				//move everything down
				int x, y = 0;
				vec[i]->GetPos(x, y);
				vec[i]->SetPos(x, y - 22);
			}

			if (vec.Count() >= m_SoundscapesList->m_iMax)
			{
				m_SoundscapesList->OnMouseWheeled(1);

				int min, max;
				m_SoundscapesList->m_pSideSlider->GetRange(min, max);
				m_SoundscapesList->m_pSideSlider->SetRange(0, max - 1);
			}

			//reset everything
			m_DspEffects->SetText("");
			m_SoundLevels->SetText("");
			m_TextEntryName->SetText("");
			m_SoundNameTextEntry->SetText("");
			m_TimeTextEntry->SetText("");
			m_PitchTextEntry->SetText("");
			m_PositionTextEntry->SetText("");
			m_VolumeTextEntry->SetText("");

			m_DspEffects->SetEnabled(false);
			m_SoundLevels->SetEnabled(false);
			m_TextEntryName->SetEnabled(false);
			m_SoundNameTextEntry->SetEnabled(false);
			m_TimeTextEntry->SetEnabled(false);
			m_PitchTextEntry->SetEnabled(false);
			m_PositionTextEntry->SetEnabled(false);
			m_VolumeTextEntry->SetEnabled(false);
			m_SoundNamePlay->SetEnabled(false);

			m_pDataList->Clear();
			m_pSoundList->Clear();

			m_kvCurrSound = nullptr;
			m_pDataList->m_Keyvalues = nullptr;

			//go to next soundscape
			if (!prev)
			{
				//restart soundscape
				PlaySelectedSoundscape();
				return;
			}

			if (prev->GetNextTrueSubKey())
				OnCommand(prev->GetNextTrueSubKey()->GetName());
			else
				OnCommand(prev->GetName());
		}

		//restart soundscape
		PlaySelectedSoundscape();
		return;
	}

	//check for "playrandom", "playsoundscape" or "playlooping"
	if (Q_stristr(pszCommand, "$playrandom") == pszCommand)
	{
		//get the selected number
		char* str_number = (char*)(pszCommand + 11);
		int number = atoi(str_number);
		if (number != 0)
		{
			//look for button with same command
			auto& vec = m_pDataList->m_MenuButtons;
			for (int i = 0; i < vec.Count(); i++)
			{
				//if the button doesnt have the same command then de-select it. else select it
				if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
					vec[i]->m_bIsSelected = true;
				else
					vec[i]->m_bIsSelected = false;
			}


			//clear the m_pSoundList
			m_pSoundList->Clear();
			m_pSoundList->m_Keyvalues = nullptr;

			//store variables
			KeyValues* data = nullptr;
			int curr = 0;

			//get subkey
			FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
			{
				if (Q_strcasecmp(sounds->GetName(), "playrandom"))
					continue;

				if (++curr == number)
				{
					data = sounds;
					break;
				}
			}

			//no data
			if (!data)
				return;

			m_kvCurrSound = data;
			m_kvCurrRndwave = nullptr;

			//set the random times
			m_TimeTextEntry->SetText(data->GetString("time", "10,20"));
			m_VolumeTextEntry->SetText(data->GetString("volume", "0.5,0.8"));
			m_PitchTextEntry->SetText(data->GetString("pitch", "100"));
			m_PositionTextEntry->SetText(data->GetString("position", ""));
			m_SoundNameTextEntry->SetText("");

			//get snd level index
			int index = 8;	//8 = SNDLVL_NORM
			const char* name = data->GetString("soundlevel", nullptr);

			//check for the name
			if (name)
			{

				//loop through the sound levels to find the right one
				for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
				{
					if (!Q_strcmp(name, g_SoundLevels[i]))
					{
						index = i;
						break;
					}
				}
			}

			//select the index
			m_SoundLevels->ActivateItem(index);

			//enable the text entries
			m_TimeTextEntry->SetEnabled(true);
			m_VolumeTextEntry->SetEnabled(true);
			m_PitchTextEntry->SetEnabled(true);
			m_PositionTextEntry->SetEnabled(true);
			m_SoundLevels->SetEnabled(true);
			m_SoundNameTextEntry->SetEnabled(false);
			m_SoundNamePlay->SetEnabled(false);

			g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
			g_SoundPanel->SetVisible(false);

			//check for randomwave subkey
			if ((data = data->FindKey("rndwave")) == nullptr)
				return;

			m_kvCurrRndwave = data;
			m_pSoundList->m_Keyvalues = data;

			//add all the data
			int i = 0;
			FOR_EACH_VALUE(data, sound)
			{
				const char* name = sound->GetName();

				//get real text
				const char* text = sound->GetString();

				//get last / or \ and make the string be that + 1
				char* fslash = Q_strrchr(text, '/');
				char* bslash = Q_strrchr(text, '\\');

				//no forward slash and no back slash
				if (!fslash && !bslash)
				{
					text = text;
				}
				else
				{
					if (fslash > bslash)
						text = fslash + 1;

					else if (bslash > fslash)
						text = bslash + 1;
				}

				m_pSoundList->AddButton(name, text, CFmtStr("$rndwave%d", ++i), this, sound, SoundscapeClipboardType::Type_SoundscapeRandomWave);
			}

			m_iSoundscapeMode = SoundscapeMode::Mode_Random;
			return;
		}
	}
	else if (Q_stristr(pszCommand, "$playlooping") == pszCommand)
	{
		//get the selected number
		char* str_number = (char*)(pszCommand + 12);
		int number = atoi(str_number);
		if (number != 0)
		{
			//look for button with same command
			auto& vec = m_pDataList->m_MenuButtons;
			for (int i = 0; i < vec.Count(); i++)
			{
				//if the button doesnt have the same command then de-select it. else select it
				if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
					vec[i]->m_bIsSelected = true;
				else
					vec[i]->m_bIsSelected = false;
			}


			//clear the m_pSoundList
			m_pSoundList->Clear();
			m_pSoundList->m_Keyvalues = nullptr;

			//store variables
			KeyValues* data = nullptr;
			int curr = 0;

			//get subkey
			FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
			{
				if (Q_strcasecmp(sounds->GetName(), "playlooping"))
					continue;

				if (++curr == number)
				{
					data = sounds;
					break;
				}
			}

			//no data
			if (!data)
				return;

			m_kvCurrSound = data;
			m_kvCurrRndwave = nullptr;

			//set the random times
			m_TimeTextEntry->SetText("");
			m_VolumeTextEntry->SetText(data->GetString("volume", "1"));
			m_PitchTextEntry->SetText(data->GetString("pitch", "100"));
			m_PositionTextEntry->SetText(data->GetString("position", ""));
			m_SoundNameTextEntry->SetText(data->GetString("wave", ""));

			//get snd level index
			int index = 8;	//8 = SNDLVL_NORM
			const char* name = data->GetString("soundlevel", nullptr);

			//check for the name
			if (name)
			{

				//loop through the sound levels to find the right one
				for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
				{
					if (!Q_strcmp(name, g_SoundLevels[i]))
					{
						index = i;
						break;
					}
				}
			}

			//select the index
			m_SoundLevels->ActivateItem(index);

			//enable the text entries
			m_TimeTextEntry->SetEnabled(false);
			m_VolumeTextEntry->SetEnabled(true);
			m_PitchTextEntry->SetEnabled(true);
			m_PositionTextEntry->SetEnabled(true);
			m_SoundLevels->SetEnabled(true);
			m_SoundNameTextEntry->SetEnabled(true);
			m_SoundNamePlay->SetEnabled(true);
			g_SoundPanel->SetVisible(false);

			m_iSoundscapeMode = SoundscapeMode::Mode_Looping;
			return;
		}
	}
	else if (Q_stristr(pszCommand, "$playsoundscape") == pszCommand)
	{
		//get the selected number
		char* str_number = (char*)(pszCommand + 15);
		int number = atoi(str_number);
		if (number != 0)
		{
			//look for button with same command
			auto& vec = m_pDataList->m_MenuButtons;
			for (int i = 0; i < vec.Count(); i++)
			{
				//if the button doesnt have the same command then de-select it. else select it
				if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
					vec[i]->m_bIsSelected = true;
				else
					vec[i]->m_bIsSelected = false;
			}


			//clear the m_pSoundList
			m_pSoundList->Clear();
			m_pSoundList->m_Keyvalues = nullptr;

			//store variables
			KeyValues* data = nullptr;
			int curr = 0;

			//get subkey
			FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, sounds)
			{
				if (Q_strcasecmp(sounds->GetName(), "playsoundscape"))
					continue;

				if (++curr == number)
				{
					data = sounds;
					break;
				}
			}

			//no data
			if (!data)
				return;

			m_kvCurrSound = data;
			m_kvCurrRndwave = nullptr;

			//set the random times
			m_TimeTextEntry->SetText("");
			m_VolumeTextEntry->SetText(data->GetString("volume", "1"));
			m_PositionTextEntry->SetText(data->GetString("positionoverride", ""));
			m_SoundNameTextEntry->SetText(data->GetString("name", ""));
			m_PitchTextEntry->SetText("");

			//get snd level index
			int index = 8;	//8 = SNDLVL_NORM
			const char* name = data->GetString("soundlevel", nullptr);

			//check for the name
			if (name)
			{

				//loop through the sound levels to find the right one
				for (int i = 0; i < sizeof(g_SoundLevels) / sizeof(g_SoundLevels[i]); i++)
				{
					if (!Q_strcmp(name, g_SoundLevels[i]))
					{
						index = i;
						break;
					}
				}
			}

			//select the index
			m_SoundLevels->ActivateItem(index);

			//enable the text entries
			m_TimeTextEntry->SetEnabled(true);
			m_VolumeTextEntry->SetEnabled(true);
			m_PitchTextEntry->SetEnabled(false);
			m_PositionTextEntry->SetEnabled(true);
			m_SoundLevels->SetEnabled(true);
			m_SoundNameTextEntry->SetEnabled(true);
			m_TimeTextEntry->SetEnabled(false);
			m_SoundNamePlay->SetEnabled(true);

			g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
			g_SoundPanel->SetVisible(false);

			m_iSoundscapeMode = SoundscapeMode::Mode_Soundscape;
			return;
		}
	}
	else if (Q_stristr(pszCommand, "$rndwave") == pszCommand)
	{
		if (!m_kvCurrRndwave)
			return;

		//get the selected number
		char* str_number = (char*)(pszCommand + 8);
		m_iCurrRndWave = atoi(str_number);
		if (m_iCurrRndWave != 0)
		{
			//look for button with same command
			auto& vec = m_pSoundList->m_MenuButtons;
			for (int i = 0; i < vec.Count(); i++)
			{
				//if the button doesnt have the same command then de-select it. else select it
				if (!Q_strcmp(vec[i]->GetCommand()->GetString("command"), pszCommand))
					vec[i]->m_bIsSelected = true;
				else
					vec[i]->m_bIsSelected = false;
			}


			int i = 0;

			//get value
			KeyValues* curr = nullptr;
			FOR_EACH_VALUE(m_kvCurrRndwave, wave)
			{
				if (++i == m_iCurrRndWave)
				{
					curr = wave;
					break;
				}
			}

			//if no curr then throw an error
			if (!curr)
			{
				//play an error sound
				vgui::surface()->PlaySound("resource/warning.wav");

				//show error
				char buf[1028];
				Q_snprintf(buf, sizeof(buf), "Failed to get rndwave '%d' for subkey \"%s\"\nfor current soundscape file!", i, m_kvCurrSelected->GetName());

				//show an error
				vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
				popup->SetOKButtonText("Ok");
				popup->SetCancelButtonVisible(false);
				popup->AddActionSignalTarget(this);
				popup->DoModal(this);
				return;
			}

			m_SoundNameTextEntry->SetEnabled(true);
			m_SoundNameTextEntry->SetText(curr->GetString());

			m_SoundNamePlay->SetEnabled(true);

			m_iSoundscapeMode = SoundscapeMode::Mode_Random;
			return;
		}
	}

	//look for button with the same name as the command
	{
		//store vars
		CUtlVector<CSoundscapeButton*>& array = m_SoundscapesList->m_MenuButtons;

		//de-select button
		if (m_pCurrentSelected)
			m_pCurrentSelected->m_bIsSelected = false;

		//check for name
		for (int i = 0; i < m_SoundscapesList->m_MenuButtons.Size(); i++)
		{
			//check button name
			if (!Q_strcmp(array[i]->GetCommand()->GetString("command"), pszCommand))
			{
				//found it
				m_pCurrentSelected = array[i];
				break;
			}
		}

		//set needed stuff
		if (m_pCurrentSelected)
		{
			//select button
			m_pCurrentSelected->m_bIsSelected = true;
			m_DeleteCurrentButton->SetEnabled(false);

			//reset the selected kv
			m_kvCurrSelected = nullptr;

			//find selected keyvalues
			for (KeyValues* kv = m_KeyValues; kv != nullptr; kv = kv->GetNextTrueSubKey())
			{
				if (!Q_strcmp(kv->GetName(), pszCommand))
				{
					m_kvCurrSelected = kv;
					break;
				}
			}

			//set 
			m_kvCurrSound = nullptr;
			m_kvCurrRndwave = nullptr;

			m_TimeTextEntry->SetEnabled(false);
			m_TimeTextEntry->SetText("");

			m_VolumeTextEntry->SetEnabled(false);
			m_VolumeTextEntry->SetText("");

			m_PitchTextEntry->SetEnabled(false);
			m_PitchTextEntry->SetText("");

			m_PositionTextEntry->SetEnabled(false);
			m_PositionTextEntry->SetText("");

			m_SoundLevels->SetEnabled(false);
			m_SoundLevels->SetText("");

			m_SoundNameTextEntry->SetEnabled(false);
			m_SoundNameTextEntry->SetText("");

			m_SoundNamePlay->SetEnabled(false);

			if (g_SoundPanel)
			{
				g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
				g_SoundPanel->SetVisible(false);
			}

			//check for current keyvalues. should never bee nullptr but could be
			if (!m_kvCurrSelected)
			{
				//play an error sound
				vgui::surface()->PlaySound("resource/warning.wav");

				//show error
				char buf[1028];
				Q_snprintf(buf, sizeof(buf), "Failed to find KeyValue subkey \"%s\"\nfor current soundscape file!", pszCommand);

				//show an error
				vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
				popup->SetOKButtonText("Ok");
				popup->SetCancelButtonVisible(false);
				popup->AddActionSignalTarget(this);
				popup->DoModal(this);

				//reset vars
				m_pCurrentSelected = nullptr;

				m_TextEntryName->SetEnabled(false);
				m_TextEntryName->SetText("");

				m_DspEffects->SetEnabled(false);
				m_DspEffects->SetText("");
				return;
			}

			if (g_IsPlayingSoundscape)
				PlaySelectedSoundscape();

			m_DeleteCurrentButton->SetEnabled(true);

			//set current soundscape name
			m_TextEntryName->SetText(pszCommand);
			m_TextEntryName->SetEnabled(true);
			m_pDataList->m_Keyvalues = m_kvCurrSelected;

			//set dsp effect
			int dsp = Clamp<int>(m_kvCurrSelected->GetInt("dsp"), 0, 29);

			m_PlaySoundscapeButton->SetEnabled(true);

			m_DspEffects->SetEnabled(true);
			m_DspEffects->ActivateItem(dsp);

			//clear these
			m_pDataList->Clear();
			m_pSoundList->Clear();
			m_pSoundList->m_Keyvalues = nullptr;

			//set variables
			int RandomNum = 0;
			int LoopingNum = 0;
			int SoundscapeNum = 0;

			FOR_EACH_TRUE_SUBKEY(m_kvCurrSelected, data)
			{
				//store data name
				const char* name = data->GetName();

				//increment variables based on name
				if (!Q_strcasecmp(name, "playrandom"))
				{
					RandomNum++;
					m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playrandom%d", RandomNum), this, data, SoundscapeClipboardType::Type_SoundscapeData);
				}

				if (!Q_strcasecmp(name, "playlooping"))
				{
					LoopingNum++;
					m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playlooping%d", LoopingNum), this, data, SoundscapeClipboardType::Type_SoundscapeData);
				}

				if (!Q_strcasecmp(name, "playsoundscape"))
				{
					SoundscapeNum++;
					m_pDataList->AddButton(data->GetName(), data->GetName(), CFmtStr("$playsoundscape%d", SoundscapeNum), this, data, SoundscapeClipboardType::Type_SoundscapeData);
				}
			}
		}
	}

	BaseClass::OnCommand(pszCommand);
}

//-----------------------------------------------------------------------------
// Purpose: Paste item from clipboard
//-----------------------------------------------------------------------------
void CSoundscapeMaker::Paste(SoundscapeClipboardType type)
{
	switch (type)
	{
	case SoundscapeClipboardType::Type_SoundscapeName:
		m_SoundscapesList->OnCommand(PASTE_FROM_CLIBOARD_COMMAND);
		break;
	case SoundscapeClipboardType::Type_SoundscapeData:
		m_pDataList->OnCommand(PASTE_FROM_CLIBOARD_COMMAND);
		break;
	case SoundscapeClipboardType::Type_SoundscapeRandomWave:
		m_pSoundList->OnCommand(PASTE_FROM_CLIBOARD_COMMAND);
		break;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Function to recursivly write keyvalues to keyvalue files. the keyvalues
//			class does have a function to do this BUT this function writes every single
//			item one after another. this function does that but writes the keys
//			first then the subkeys so the order is good.
//-----------------------------------------------------------------------------
void RecursivlyWriteKeyvalues(KeyValues* prev, CUtlBuffer& buffer, int& indent)
{
	//write \t indent
	for (int i = 0; i < indent; i++)
		buffer.PutChar('\t');

	//write name
	buffer.PutChar('"');
	buffer.PutString(prev->GetName());
	buffer.PutString("\"\n");

	//write {
	for (int i = 0; i < indent; i++)
		buffer.PutChar('\t');

	buffer.PutString("{\n");

	//increment indent
	indent++;

	//write all the keys first
	FOR_EACH_VALUE(prev, value)
	{
		for (int i = 0; i < indent; i++)
			buffer.PutChar('\t');

		//write name and value
		buffer.PutChar('"');
		buffer.PutString(value->GetName());
		buffer.PutString("\"\t");

		buffer.PutChar('"');
		buffer.PutString(value->GetString());
		buffer.PutString("\"\n");
	}

	//write all the subkeys now
	FOR_EACH_TRUE_SUBKEY(prev, value)
	{
		//increment indent
		RecursivlyWriteKeyvalues(value, buffer, indent);

		if (value->GetNextTrueSubKey())
			buffer.PutChar('\n');
	}

	//decrement indent
	indent--;

	//write ending }
	for (int i = 0; i < indent; i++)
		buffer.PutChar('\t');

	buffer.PutString("}\n");
}

//-----------------------------------------------------------------------------
// Purpose: Called when a file gets opened/closed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnFileSelected(const char* pszFileName)
{
	//check for null or empty string
	if (!pszFileName || pszFileName[0] == '\0')
		return;

	//check for file save
	if (!m_bWasFileLoad)
	{
		//save the file
		if (m_KeyValues)
		{
			//write everything into a buffer
			CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
			buf.PutString("//------------------------------------------------------------------------------------\n");
			buf.PutString("//\n");
			buf.PutString("// Auto-generated soundscape file created with modbases soundscape tool'\n");
			buf.PutString("//\n");
			buf.PutString("//------------------------------------------------------------------------------------\n");

			//now write the keyvalues
			KeyValues* pCurrent = m_KeyValues;
			while (pCurrent)
			{
				int indent = 0;
				RecursivlyWriteKeyvalues(pCurrent, buf, indent);

				//put a newline
				if (pCurrent->GetNextTrueSubKey())
					buf.PutChar('\n');

				//get next
				pCurrent = pCurrent->GetNextTrueSubKey();
			}

			if (!g_pFullFileSystem->WriteFile(pszFileName, "MOD", buf))
			{
				//play an error sound
				vgui::surface()->PlaySound("resource/warning.wav");

				//get the error first
				char buf[1028];
				Q_snprintf(buf, sizeof(buf), "Failed to save soundscape to file \"%s\"", pszFileName);

				//show an error
				vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
				popup->SetOKButtonText("Ok");
				popup->SetCancelButtonVisible(false);
				popup->AddActionSignalTarget(this);
				popup->DoModal(this);
				return;
			}
		}


		//store vars
		const char* last = pszFileName;
		const char* tmp = nullptr;

		//get the last /
		while ((last = Q_strstr(last, "\\")) != nullptr)
			tmp = ++last; //move past the backslash

		//check tmp
		if (!tmp || !*tmp)
			tmp = pszFileName;

		//set new title
		char buf[1028];
		Q_snprintf(buf, sizeof(buf), "Soundscape Maker (%s)", tmp);

		SetTitle(buf, true);

		//create copy of pszFileName
		char manifest[1028];
		Q_strncpy(manifest, pszFileName, sizeof(manifest));

		//get last /
		char* lastSlash = Q_strrchr(manifest, '\\');
		if (lastSlash == nullptr || *lastSlash == '\0')
			return;

		//append 'soundscapes_manifest.txt'
		lastSlash[1] = '\0';
		strcat(manifest, "soundscapes_manifest.txt");

		//see if we can open manifest file
		KeyValues* man_file = new KeyValues("manifest");
		if (!man_file->LoadFromFile(g_pFullFileSystem, manifest))
		{
			//cant open manifest file
			man_file->deleteThis();
			return;
		}

		//get real filename
		pszFileName = Q_strrchr(pszFileName, '\\');
		if (!pszFileName || !*pszFileName)
		{
			man_file->deleteThis();
			return;
		}

		pszFileName = pszFileName + 1;

		//create name to be added to the manifest file
		char add_file[1028];
		Q_snprintf(add_file, sizeof(add_file), "scripts/%s", pszFileName);

		//add filename to manifest file if not found
		FOR_EACH_VALUE(man_file, value)
		{
			if (!Q_strcmp(value->GetString(), add_file))
			{
				man_file->deleteThis();
				return;
			}
		}


		//add to manifest file
		KeyValues* kv = new KeyValues("file");
		kv->SetString(nullptr, add_file);
		man_file->AddSubKey(kv);

		//write to file
		man_file->SaveToFile(g_pFullFileSystem, manifest);

		man_file->deleteThis();
		return;
	}

	//try and load the keyvalues file first
	KeyValues* temp = new KeyValues("SoundscapeFile");
	if (!temp->LoadFromFile(filesystem, pszFileName))
	{
		//play an error sound
		vgui::surface()->PlaySound("resource/warning.wav");

		//get the error first
		char buf[1028];
		Q_snprintf(buf, sizeof(buf), "Failed to open keyvalues file \"%s\"", pszFileName);

		//show an error
		vgui::QueryBox* popup = new vgui::QueryBox("Error", buf, this);
		popup->SetOKButtonText("Ok");
		popup->SetCancelButtonVisible(false);
		popup->AddActionSignalTarget(this);
		popup->DoModal(this);

		temp->deleteThis();
		return;
	}

	//set the new title
	{
		//store vars
		const char* last = pszFileName;
		const char* tmp = nullptr;

		//get the last /
		while ((last = Q_strstr(last, "\\")) != nullptr)
			tmp = ++last; //move past the backslash

		//check tmp
		if (!tmp || !*tmp)
			tmp = pszFileName;

		//create the new new title
		char buf[1028];
		Q_snprintf(buf, sizeof(buf), "Soundscape Maker (%s)", tmp);

		SetTitle(buf, true);
	}

	//stop all soundscapes before deleting the old soundscapes
	m_kvCurrSelected = nullptr;

	if (g_IsPlayingSoundscape)
		PlaySelectedSoundscape();

	//delete and set the old keyvalues
	if (m_KeyValues)
		m_KeyValues->deleteThis();

	m_KeyValues = temp;

	//load the file
	LoadFile(m_KeyValues);
}

//-----------------------------------------------------------------------------
// Purpose: Called when a text thing changes
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnTextChanged(KeyValues* keyvalues)
{
	//check for these things
	if (!m_pCurrentSelected || !m_kvCurrSelected)
		return;

	//check to see if the current focus is the text text entry
	if (m_TextEntryName->HasFocus())
	{
		//get text
		char buf[50];
		m_TextEntryName->GetText(buf, sizeof(buf));

		//set current text and keyvalue name
		m_kvCurrSelected->SetName(buf);
		m_pCurrentSelected->SetText(buf);
		m_pCurrentSelected->SetCommand(buf);
		return;
	}

	//set dsp
	m_kvCurrSelected->SetInt("dsp", Clamp<int>(m_DspEffects->GetActiveItem(), 0, 28));

	//if the m_kvCurrSound is nullptr then dont do the rest of the stuff
	if (!m_kvCurrSound)
		return;

	//set the curr sound and stuff
	if (m_TimeTextEntry->HasFocus())
	{
		//get text
		char buf[38];
		m_TimeTextEntry->GetText(buf, sizeof(buf));

		m_kvCurrSound->SetString("time", buf);
		return;
	}
	else if (m_VolumeTextEntry->HasFocus())
	{
		//get text
		char buf[38];
		m_VolumeTextEntry->GetText(buf, sizeof(buf));

		m_kvCurrSound->SetString("volume", buf);
		return;
	}
	else if (m_PitchTextEntry->HasFocus())
	{
		//dont add to soundscaep
		if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
			return;

		//get text
		char buf[38];
		m_PitchTextEntry->GetText(buf, sizeof(buf));

		m_kvCurrSound->SetString("pitch", buf);
		return;
	}
	else if (m_PositionTextEntry->HasFocus())
	{
		//get text
		char buf[38];
		m_PositionTextEntry->GetText(buf, sizeof(buf));

		//if the string is empty then remove the position instead
		if (!buf[0])
		{
			if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
				m_kvCurrSound->RemoveSubKey(m_kvCurrSound->FindKey("positionoverride"));
			else
				m_kvCurrSound->RemoveSubKey(m_kvCurrSound->FindKey("position"));
		}
		else
		{
			if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
				m_kvCurrSound->SetString("positionoverride", buf);
			else
				m_kvCurrSound->SetString("position", buf);

		}

		return;
	}

	//get the sound level amount
	int sndlevel = Clamp<int>(m_SoundLevels->GetActiveItem(), 0, 20);
	m_kvCurrSound->SetString("soundlevel", g_SoundLevels[sndlevel]);

	//set soundscape name/wave
	if (m_SoundNameTextEntry->HasFocus())
	{
		//get text
		char buf[512];
		m_SoundNameTextEntry->GetText(buf, sizeof(buf));

		if (m_iSoundscapeMode == SoundscapeMode::Mode_Looping)
			m_kvCurrSound->SetString("wave", buf);
		else if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
			m_kvCurrSound->SetString("name", buf);
		else if (m_iSoundscapeMode == SoundscapeMode::Mode_Random && m_kvCurrRndwave)
		{
			//get value
			int i = 0;

			FOR_EACH_VALUE(m_kvCurrRndwave, wave)
			{
				if (++i == m_iCurrRndWave)
				{
					wave->SetStringValue(buf);

					//set text on the sounds panel
					vgui::Button* button = m_pSoundList->m_MenuButtons[i - 1];
					if (button)
					{
						//get last / or \ and make the string be that + 1
						char* fslash = Q_strrchr(buf, '/');
						char* bslash = Q_strrchr(buf, '\\');

						//no forward slash and no back slash
						if (!fslash && !bslash)
						{
							button->SetText(buf);
							return;
						}

						if (fslash > bslash)
						{
							button->SetText(fslash + 1);
							return;
						}

						else if (bslash > fslash)
						{
							button->SetText(bslash + 1);
							return;
						}
					}

					break;
				}
			}
		}

		return;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Loads the keyvalues
//-----------------------------------------------------------------------------
void CSoundscapeMaker::LoadFile(KeyValues* file)
{
	//clear all the text's
	m_TextEntryName->SetEnabled(false);
	m_TextEntryName->SetText("");

	m_DspEffects->SetEnabled(false);
	m_DspEffects->SetText("");

	m_TimeTextEntry->SetEnabled(false);
	m_TimeTextEntry->SetText("");

	m_VolumeTextEntry->SetEnabled(false);
	m_VolumeTextEntry->SetText("");

	m_PitchTextEntry->SetEnabled(false);
	m_PitchTextEntry->SetText("");

	m_PositionTextEntry->SetEnabled(false);
	m_PositionTextEntry->SetText("");

	m_SoundNameTextEntry->SetEnabled(false);
	m_SoundNameTextEntry->SetText("");

	m_SoundNamePlay->SetEnabled(false);

	if (g_SoundPanel)
	{
		g_SoundPanel->OnCommand(SOUND_LIST_STOP_COMMAND);
		g_SoundPanel->SetVisible(false);
	}

	m_PlaySoundscapeButton->SetEnabled(false);
	m_PlaySoundscapeButton->SetSelected(false);

	m_ResetSoundscapeButton->SetEnabled(false);
	m_DeleteCurrentButton->SetEnabled(false);

	//clear current file
	m_pCurrentSelected = nullptr;
	m_kvCurrSelected = nullptr;
	m_kvCurrSound = nullptr;
	m_kvCurrRndwave = nullptr;

	//clear the menu items
	m_SoundscapesList->Clear();
	m_pSoundList->Clear();
	m_pDataList->Clear();

	m_SoundscapesList->m_Keyvalues = file;
	m_pDataList->m_Keyvalues = nullptr;
	m_pSoundList->m_Keyvalues = nullptr;

	g_IsPlayingSoundscape = false;

	//temp soundscapes list
	CUtlVector<const char*> Added;

	//add all the menu items
	for (KeyValues* soundscape = file; soundscape != nullptr; soundscape = soundscape->GetNextTrueSubKey())
	{
		//add the menu buttons
		const char* name = soundscape->GetName();

		//check for the soundscape first
		if (Added.Find(name) != Added.InvalidIndex())
		{
			ConWarning("CSoundscapePanel: Failed to add repeated soundscape '%s'\n", name);
			continue;
		}

		Added.AddToTail(name);
		m_SoundscapesList->AddButton(name, name, name, this, soundscape, SoundscapeClipboardType::Type_SoundscapeName);
	}

	m_SoundscapesList->m_pSideSlider->SetValue(0);
	m_SoundscapesList->ScrollBarMoved(0);

	//
	OnCommand(file->GetName());
}

//-----------------------------------------------------------------------------
// Purpose: Sets the sounds text
//-----------------------------------------------------------------------------
void CSoundscapeMaker::SetSoundText(const char* text)
{
	m_SoundNameTextEntry->SetText(text);

	//set soundscape name/wave
	if (m_iSoundscapeMode != SoundscapeMode::Mode_Random)
	{
		if (m_iSoundscapeMode == SoundscapeMode::Mode_Soundscape)
			m_kvCurrSound->SetString("name", text);
		else
			m_kvCurrSound->SetString("wave", text);
	}
	else
	{
		//get value
		int i = 0;

		FOR_EACH_VALUE(m_kvCurrRndwave, wave)
		{
			if (++i == m_iCurrRndWave)
			{
				wave->SetStringValue(text);

				//set text on the sounds panel
				vgui::Button* button = m_pSoundList->m_MenuButtons[i - 1];
				if (button)
				{
					//get last / or \ and make the string be that + 1
					char* fslash = Q_strrchr(text, '/');
					char* bslash = Q_strrchr(text, '\\');

					//no forward slash and no back slash
					if (!fslash && !bslash)
					{
						button->SetText(text);
						return;
					}

					if (fslash > bslash)
					{
						button->SetText(fslash + 1);
						return;
					}

					else if (bslash > fslash)
					{
						button->SetText(bslash + 1);
						return;
					}
				}

				break;
			}
		}
	}

	return;
}

//-----------------------------------------------------------------------------
// Purpose: Called when a keyboard key is pressed
//-----------------------------------------------------------------------------
void CSoundscapeMaker::OnKeyCodePressed(vgui::KeyCode code)
{
	//check for ctrl o or ctrl s
	if (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LCONTROL) ||
		vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RCONTROL))
	{
		if (code == vgui::KeyCode::KEY_O)
			OnCommand(LOAD_BUTTON_COMMAND);
		else if (code == vgui::KeyCode::KEY_S)
			OnCommand(SAVE_BUTTON_COMMAND);
		else if (code == vgui::KeyCode::KEY_N)
			OnCommand(NEW_BUTTON_COMMAND);
		else if (code == vgui::KeyCode::KEY_P)
		{
			//show settings
			g_SettingsPanel->SetVisible(true);
			g_SettingsPanel->RequestFocus();
			g_SettingsPanel->MoveToFront();
		}

		else if (code == vgui::KeyCode::KEY_D)
			OnCommand(DELETE_CURRENT_ITEM_COMMAND);

		//check for ctrl+alt+a
		else if (code == vgui::KeyCode::KEY_A && (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LALT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RALT)) && m_pSoundList && m_pSoundList->m_Keyvalues)
			m_pSoundList->OnCommand(NEW_RNDWAVE_WAVE_COMMAND);

		//check for just ctrl+shift+a
		else if (code == vgui::KeyCode::KEY_A && (vgui::input()->IsKeyDown(vgui::KeyCode::KEY_LSHIFT) || vgui::input()->IsKeyDown(vgui::KeyCode::KEY_RSHIFT)) && m_SoundscapesList)
			m_SoundscapesList->OnCommand(ADD_SOUNDSCAPE_COMMAND);

		return;
	}

	//check for arrow keys
	if (code == KEY_DOWN || code == KEY_UP)
	{
		if (m_kvCurrRndwave)
		{
			m_pSoundList->OnKeyCodePressed(code);
		}
		else if (m_kvCurrSelected && m_pDataList->m_MenuButtons.Count())
		{
			m_pDataList->OnKeyCodePressed(code);
		}
		else
		{
			m_SoundscapesList->OnKeyCodePressed(code);
		}

		return;
	}

	//get key bound to this
	const char* key = engine->Key_LookupBinding("modbase_soundscape_panel");
	if (!key)
		return;

	//convert the key to a keyboard code
	const char* keystring = KeyCodeToString(code);

	//remove the KEY_ if found
	if (Q_strstr(keystring, "KEY_") == keystring)
		keystring = keystring + 4;

	//check both strings
	if (!Q_strcasecmp(key, keystring))
		OnClose();
}

//-----------------------------------------------------------------------------
// Purpose: Starts the soundscape on map spawn
//-----------------------------------------------------------------------------
void CSoundscapeMaker::LevelInitPostEntity()
{
	if (g_IsPlayingSoundscape)
		PlaySelectedSoundscape();
}

//-----------------------------------------------------------------------------
// Purpose: Sets keyvalues from text
//-----------------------------------------------------------------------------
void CSoundscapeMaker::Set(const char* buffer)
{
	CUtlBuffer buf(0, 0, CUtlBuffer::TEXT_BUFFER);
	buf.PutString(buffer);

	//try and load the keyvalues file first
	KeyValues* temp = new KeyValues("SoundscapeFile");
	if (!temp->LoadFromBuffer("Text Editor Panel Buffer", buf))
	{
		//play an error sound
		vgui::surface()->PlaySound("resource/warning.wav");

		//show an error
		vgui::QueryBox* popup = new vgui::QueryBox("Error", "Failed to open keyvalues data from \"Text Editor Panel\"", this);
		popup->SetOKButtonText("Ok");
		popup->SetCancelButtonVisible(false);
		popup->AddActionSignalTarget(this);
		popup->DoModal(this);

		temp->deleteThis();
		return;
	}

	//stop all soundscapes before deleting the old soundscapes
	m_kvCurrSelected = nullptr;

	if (g_IsPlayingSoundscape)
		PlaySelectedSoundscape();

	//delete and set the old keyvalues
	if (m_KeyValues)
		m_KeyValues->deleteThis();

	m_KeyValues = temp;

	//load the file
	LoadFile(m_KeyValues);
}

//-----------------------------------------------------------------------------
// Purpose: Destructor for soundscape maker panel
//-----------------------------------------------------------------------------
CSoundscapeMaker::~CSoundscapeMaker()
{
	//delete the keyvalue files if needed
	if (m_KeyValues)
		m_KeyValues->deleteThis();
}

//static panel instance
static CSoundscapeMaker* g_SSMakerPanel = nullptr;

//interface class
class CSoundscapeMakerInterface : public ISoundscapeMaker
{
public:
	void Create(vgui::VPANEL parent)
	{
		g_SSMakerPanel = new CSoundscapeMaker(parent);
		g_SoundPanel = new CSoundListPanel(parent, "SoundscapeSoundListPanel");
		g_SettingsPanel = new CSoundscapeSettingsPanel(parent, "SoundscapeSettingsPanel");
		g_SoundscapeTextPanel = new CSoundscapeTextPanel(parent, "SoundscapeTextPanel");
		g_SoundscapeDebugPanel = new CSoundscapeDebugPanel(parent, "SoundscapeDebugPanel");
	}

	void SetVisible(bool bVisible)
	{
		if (g_SSMakerPanel)
			g_SSMakerPanel->SetVisible(bVisible);
	}

	void Destroy()
	{
		if (g_SSMakerPanel)
			g_SSMakerPanel->DeletePanel();

		if (g_SoundPanel)
			g_SoundPanel->DeletePanel();

		if (g_SettingsPanel)
			g_SettingsPanel->DeletePanel();

		if (g_SoundscapeTextPanel)
			g_SoundscapeTextPanel->DeletePanel();

		if (g_SoundscapeDebugPanel)
			g_SoundscapeDebugPanel->DeletePanel();

		if (g_SoundscapeClipboard)
			g_SoundscapeClipboard->DeletePanel();

		g_SSMakerPanel = nullptr;
		g_SoundPanel = nullptr;
		g_SettingsPanel = nullptr;
		g_SoundscapeTextPanel = nullptr;
		g_SoundscapeDebugPanel = nullptr;
		g_SoundscapeClipboard = nullptr;
	}

	void SetSoundText(const char* text)
	{
		if (!g_SSMakerPanel)
			return;

		g_SSMakerPanel->SetSoundText(text);
		g_SSMakerPanel->RequestFocus();
		g_SSMakerPanel->MoveToFront();
	}

	void SetAllVisible(bool bVisible)
	{
		g_ShowSoundscapePanel = false;

		if (g_SSMakerPanel)
			g_SSMakerPanel->SetVisible(bVisible);

		if (g_SoundPanel)
			g_SoundPanel->SetVisible(bVisible);

		if (g_SettingsPanel)
			g_SettingsPanel->SetVisible(bVisible);

		if (g_SoundscapeTextPanel)
			g_SoundscapeTextPanel->SetVisible(bVisible);

		if (g_SoundscapeDebugPanel)
			g_SoundscapeDebugPanel->SetVisible(bVisible);
	}

	void SetBuffer(const char* text)
	{
		if (g_SSMakerPanel)
			g_SSMakerPanel->Set(text);
	}

	KeyValues* GetPanelFile()
	{
		return g_SSMakerPanel->m_KeyValues;
	}

	KeyValues* GetPanelSelected()
	{
		return g_SSMakerPanel->m_kvCurrSelected;
	}

	void PasteFromClipboard(int type)
	{
		g_SSMakerPanel->Paste((SoundscapeClipboardType)type);
	}
};

CSoundscapeMakerInterface SoundscapeMaker;
ISoundscapeMaker* g_SoundscapeMaker = &SoundscapeMaker;

//-----------------------------------------------------------------------------
// Purpose: User message hook function for setting/getting soundscape position
//-----------------------------------------------------------------------------
void _SoundscapeMaker_Recieve(bf_read& bf)
{
	//show the soundscape panel
	g_ShowSoundscapePanel = true;
	g_SSMakerPanel->SetVisible(true);

	//show settings panel
	g_SettingsPanel->SetVisible(true);

	//get the stuff
	byte index = bf.ReadByte();
	Vector pos;
	bf.ReadBitVec3Coord(pos);

	//if index == -1 (255) then that means the message was canceled
	if (index == 255)
		return;

	//clamp index and get pos
	index = Clamp<int>(index, 0, MAX_SOUNDSCAPES - 1);

	//send message to settings
	g_SettingsPanel->SetItem(index, pos);
}

//-----------------------------------------------------------------------------
// Purpose: Command to toggle the soundscape panel
//-----------------------------------------------------------------------------
CON_COMMAND(modbase_soundscape_panel, "Toggles the modebase soundscape panel")
{
	g_ShowSoundscapePanel = !g_ShowSoundscapePanel;
	SoundscapeMaker.SetVisible(g_ShowSoundscapePanel);

	//tell player to stop soundscape mode
	static ConCommand* cc = cvar->FindCommand("__ss_maker_stop");
	if (cc)
		cc->Dispatch({});
}