Merge pull request #7719 from archimede67/editor-adjust-special-tiles-shortcut

Editor: add shortcut to adjust special tiles numbers
This commit is contained in:
Dennis Felsing 2023-12-26 22:13:02 +00:00 committed by GitHub
commit 90596e51ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 30 deletions

View file

@ -8158,10 +8158,28 @@ void CEditor::Render()
MapView()->Zoom()->ChangeValue(-50.0f);
if(Input()->KeyPress(KEY_KP_MULTIPLY))
MapView()->ResetZoom();
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
MapView()->Zoom()->ChangeValue(20.0f);
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
MapView()->Zoom()->ChangeValue(-20.0f);
if(m_pBrush->IsEmpty() || !Input()->ShiftIsPressed())
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
MapView()->Zoom()->ChangeValue(20.0f);
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
MapView()->Zoom()->ChangeValue(-20.0f);
}
if(!m_pBrush->IsEmpty())
{
if(Input()->ShiftIsPressed())
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
AdjustBrushSpecialTiles(false, -1);
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
AdjustBrushSpecialTiles(false, 1);
}
// Use ctrl+f to replace number in brush with next free
if(Input()->ModifierIsPressed() && Input()->KeyPress(KEY_F))
AdjustBrushSpecialTiles(true);
}
}
for(CEditorComponent &Component : m_vComponents)
@ -8872,8 +8890,6 @@ bool CEditor::Append(const char *pFileName, int StorageType, bool IgnoreHistory)
return true;
}
IEditor *CreateEditor() { return new CEditor; }
void CEditor::UndoLastAction()
{
if(m_ActiveExtraEditor == EXTRAEDITOR_SERVER_SETTINGS)
@ -8893,3 +8909,111 @@ void CEditor::RedoLastAction()
else
m_EditorHistory.Redo();
}
void CEditor::AdjustBrushSpecialTiles(bool UseNextFree, int Adjust)
{
// Adjust m_Number field of tune, switch and tele tiles by `Adjust` if `UseNextFree` is false
// If true, then use the next free number instead
auto &&AdjustNumber = [Adjust](unsigned char &Number) {
Number = ((Number + Adjust) - 1 + 255) % 255 + 1;
};
for(auto &pLayer : m_pBrush->m_vpLayers)
{
if(pLayer->m_Type != LAYERTYPE_TILES)
continue;
std::shared_ptr<CLayerTiles> pLayerTiles = std::static_pointer_cast<CLayerTiles>(pLayer);
// Only handle tele, switch and tune layers
if(pLayerTiles->m_Tele)
{
int NextFreeNumber = FindNextFreeTileNumber(LAYERTYPE_TELE);
std::shared_ptr<CLayerTele> pTeleLayer = std::static_pointer_cast<CLayerTele>(pLayer);
for(int y = 0; y < pTeleLayer->m_Height; y++)
{
for(int x = 0; x < pTeleLayer->m_Width; x++)
{
int i = y * pTeleLayer->m_Width + x;
if(!IsValidTeleTile(pTeleLayer->m_pTiles[i].m_Index) || (!UseNextFree && !pTeleLayer->m_pTeleTile[i].m_Number))
continue;
if(UseNextFree)
pTeleLayer->m_pTeleTile[i].m_Number = NextFreeNumber;
else
AdjustNumber(pTeleLayer->m_pTeleTile[i].m_Number);
}
}
}
else if(pLayerTiles->m_Tune)
{
if(!UseNextFree)
{
std::shared_ptr<CLayerTune> pTuneLayer = std::static_pointer_cast<CLayerTune>(pLayer);
for(int y = 0; y < pTuneLayer->m_Height; y++)
{
for(int x = 0; x < pTuneLayer->m_Width; x++)
{
int i = y * pTuneLayer->m_Width + x;
if(!IsValidTuneTile(pTuneLayer->m_pTiles[i].m_Index) || !pTuneLayer->m_pTuneTile[i].m_Number)
continue;
AdjustNumber(pTuneLayer->m_pTuneTile[i].m_Number);
}
}
}
}
else if(pLayerTiles->m_Switch)
{
int NextFreeNumber = FindNextFreeTileNumber(LAYERTYPE_SWITCH);
std::shared_ptr<CLayerSwitch> pSwitchLayer = std::static_pointer_cast<CLayerSwitch>(pLayer);
for(int y = 0; y < pSwitchLayer->m_Height; y++)
{
for(int x = 0; x < pSwitchLayer->m_Width; x++)
{
int i = y * pSwitchLayer->m_Width + x;
if(!IsValidSwitchTile(pSwitchLayer->m_pTiles[i].m_Index) || (!UseNextFree && !pSwitchLayer->m_pSwitchTile[i].m_Number))
continue;
if(UseNextFree)
pSwitchLayer->m_pSwitchTile[i].m_Number = NextFreeNumber;
else
AdjustNumber(pSwitchLayer->m_pSwitchTile[i].m_Number);
}
}
}
}
}
int CEditor::FindNextFreeTileNumber(int Type)
{
int Number = -1;
if(Type == LAYERTYPE_TELE)
{
for(int i = 1; i <= 255; i++)
{
if(!m_Map.m_pTeleLayer->ContainsElementWithId(i))
{
Number = i;
break;
}
}
}
else if(Type == LAYERTYPE_SWITCH)
{
for(int i = 1; i <= 255; i++)
{
if(!m_Map.m_pSwitchLayer->ContainsElementWithId(i))
{
Number = i;
break;
}
}
}
return Number;
}
IEditor *CreateEditor() { return new CEditor; }

View file

@ -1061,6 +1061,9 @@ public:
unsigned char m_SwitchNum;
unsigned char m_SwitchDelay;
void AdjustBrushSpecialTiles(bool UseNextFree, int Adjust = 0);
int FindNextFreeTileNumber(int Type);
public:
// Undo/Redo
CEditorHistory m_EditorHistory;

View file

@ -2430,20 +2430,10 @@ CUI::EPopupMenuFunctionResult CEditor::PopupTele(void *pContext, CUIRect View, b
static int s_EmptySlotPid = 0;
if(pEditor->DoButton_Editor(&s_EmptySlotPid, "F", 0, &FindEmptySlot, 0, "[ctrl+f] Find empty slot") || (Active && pEditor->Input()->ModifierIsPressed() && pEditor->Input()->KeyPress(KEY_F)))
{
int number = -1;
for(int i = 1; i <= 255; i++)
{
if(!pEditor->m_Map.m_pTeleLayer->ContainsElementWithId(i))
{
number = i;
break;
}
}
int Number = pEditor->FindNextFreeTileNumber(LAYERTYPE_TELE);
if(number != -1)
{
pEditor->m_TeleNumber = number;
}
if(Number != -1)
pEditor->m_TeleNumber = Number;
}
}
@ -2536,20 +2526,10 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSwitch(void *pContext, CUIRect View,
static int s_EmptySlotPid = 0;
if(pEditor->DoButton_Editor(&s_EmptySlotPid, "F", 0, &FindEmptySlot, 0, "[ctrl+f] Find empty slot") || (Active && pEditor->Input()->ModifierIsPressed() && pEditor->Input()->KeyPress(KEY_F)))
{
int Number = -1;
for(int i = 1; i <= 255; i++)
{
if(!pEditor->m_Map.m_pSwitchLayer->ContainsElementWithId(i))
{
Number = i;
break;
}
}
int Number = pEditor->FindNextFreeTileNumber(LAYERTYPE_SWITCH);
if(Number != -1)
{
pEditor->m_SwitchNum = Number;
}
}
}