Merge pull request #7946 from ChillerDragon/pr_ed_switch

Add "View" button to switch drop down in editor
This commit is contained in:
Robert Müller 2024-02-09 16:43:25 +00:00 committed by GitHub
commit 837ecb8708
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 107 additions and 14 deletions

View file

@ -1310,7 +1310,7 @@ void CEditor::DoToolbarLayers(CUIRect ToolBar)
{
pButtonName = "Switch";
pfnPopupFunc = PopupSwitch;
Rows = 2;
Rows = 3;
}
else if(pS == m_Map.m_pSpeedupLayer)
{

View file

@ -1114,6 +1114,7 @@ public:
unsigned char m_SwitchNum;
unsigned char m_SwitchDelay;
unsigned char m_ViewSwitch;
void AdjustBrushSpecialTiles(bool UseNextFree, int Adjust = 0);
int FindNextFreeSwitchNumber();

View file

@ -10,6 +10,8 @@ CLayerSwitch::CLayerSwitch(CEditor *pEditor, int w, int h) :
m_pSwitchTile = new CSwitchTile[w * h];
mem_zero(m_pSwitchTile, (size_t)w * h * sizeof(CSwitchTile));
m_GotoSwitchLastPos = ivec2(-1, -1);
m_GotoSwitchOffset = 0;
}
CLayerSwitch::CLayerSwitch(const CLayerSwitch &Other) :
@ -328,6 +330,58 @@ bool CLayerSwitch::ContainsElementWithId(int Id)
return false;
}
void CLayerSwitch::GetPos(int Number, int Offset, ivec2 &SwitchPos)
{
int Match = -1;
ivec2 MatchPos = ivec2(-1, -1);
SwitchPos = ivec2(-1, -1);
auto FindTile = [this, &Match, &MatchPos, &Number, &Offset]() {
for(int x = 0; x < m_Width; x++)
{
for(int y = 0; y < m_Height; y++)
{
int i = y * m_Width + x;
int Switch = m_pSwitchTile[i].m_Number;
if(Number == Switch)
{
Match++;
if(Offset != -1)
{
if(Match == Offset)
{
MatchPos = ivec2(x, y);
m_GotoSwitchOffset = Match;
return;
}
continue;
}
MatchPos = ivec2(x, y);
if(m_GotoSwitchLastPos != ivec2(-1, -1))
{
if(distance(m_GotoSwitchLastPos, MatchPos) < 10.0f)
{
m_GotoSwitchOffset++;
continue;
}
}
m_GotoSwitchLastPos = MatchPos;
if(Match == m_GotoSwitchOffset)
return;
}
}
}
};
FindTile();
if(MatchPos == ivec2(-1, -1))
return;
if(Match < m_GotoSwitchOffset)
m_GotoSwitchOffset = -1;
SwitchPos = MatchPos;
m_GotoSwitchOffset++;
}
std::shared_ptr<CLayer> CLayerSwitch::Duplicate() const
{
return std::make_shared<CLayerSwitch>(*this);

View file

@ -36,6 +36,10 @@ public:
void BrushRotate(float Amount) override;
void FillSelection(bool Empty, std::shared_ptr<CLayer> pBrush, CUIRect Rect) override;
virtual bool ContainsElementWithId(int Id);
virtual void GetPos(int Number, int Offset, ivec2 &SwitchPos);
int m_GotoSwitchOffset;
ivec2 m_GotoSwitchLastPos;
EditorTileStateChangeHistory<SSwitchTileStateChange> m_History;
inline void ClearHistory() override

View file

@ -2590,12 +2590,43 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSwitch(void *pContext, CUIRect View,
{
CEditor *pEditor = static_cast<CEditor *>(pContext);
CUIRect NumberPicker, FindEmptySlot;
CUIRect NumberPicker, FindEmptySlot, ViewEmptySlot;
View.VSplitRight(15.0f, &NumberPicker, &FindEmptySlot);
NumberPicker.VSplitRight(2.0f, &NumberPicker, nullptr);
FindEmptySlot.HSplitTop(13.0f, &FindEmptySlot, nullptr);
FindEmptySlot.HSplitTop(13.0f, &FindEmptySlot, &ViewEmptySlot);
ViewEmptySlot.HSplitTop(13.0f, nullptr, &ViewEmptySlot);
FindEmptySlot.HMargin(1.0f, &FindEmptySlot);
ViewEmptySlot.HMargin(1.0f, &ViewEmptySlot);
auto ViewSwitch = [pEditor]() -> bool {
if(!pEditor->m_ViewSwitch)
return false;
ivec2 SwitchPos;
pEditor->m_Map.m_pSwitchLayer->GetPos(pEditor->m_ViewSwitch, -1, SwitchPos);
if(SwitchPos != ivec2(-1, -1))
{
pEditor->MapView()->SetWorldOffset({32.0f * SwitchPos.x + 0.5f, 32.0f * SwitchPos.y + 0.5f});
return true;
}
return false;
};
static std::vector<ColorRGBA> s_vColors = {
ColorRGBA(1, 1, 1, 0.5f),
ColorRGBA(1, 1, 1, 0.5f),
ColorRGBA(1, 1, 1, 0.5f),
};
enum
{
PROP_SWITCH_NUMBER = 0,
PROP_SWITCH_DELAY,
PROP_SWITCH_VIEW,
NUM_PROPS,
};
// find empty number button
{
@ -2607,25 +2638,21 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSwitch(void *pContext, CUIRect View,
if(Number != -1)
pEditor->m_SwitchNum = Number;
}
static int s_NextViewPid = 0;
int ButtonResult = pEditor->DoButton_Editor(&s_NextViewPid, "N", 0, &ViewEmptySlot, 0, "[n] Show next switcher with this number");
if(ButtonResult || (Active && pEditor->Input()->KeyPress(KEY_N)))
s_vColors[PROP_SWITCH_VIEW] = ViewSwitch() ? ColorRGBA(0.5f, 1, 0.5f, 0.5f) : ColorRGBA(1, 0.5f, 0.5f, 0.5f);
}
// number picker
static int s_PreviousNumber = -1;
static int s_PreviousView = -1;
{
static std::vector<ColorRGBA> s_vColors = {
ColorRGBA(1, 1, 1, 0.5f),
};
enum
{
PROP_SWITCH_NUMBER = 0,
PROP_SWITCH_DELAY,
NUM_PROPS,
};
CProperty aProps[] = {
{"Number", pEditor->m_SwitchNum, PROPTYPE_INT, 0, 255},
{"Delay", pEditor->m_SwitchDelay, PROPTYPE_INT, 0, 255},
{"View", pEditor->m_ViewSwitch, PROPTYPE_INT, 0, 255},
{nullptr},
};
@ -2641,12 +2668,19 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSwitch(void *pContext, CUIRect View,
{
pEditor->m_SwitchDelay = (NewVal + 256) % 256;
}
else if(Prop == PROP_SWITCH_VIEW)
{
pEditor->m_ViewSwitch = (NewVal + 256) % 256;
}
if(s_PreviousNumber == 1 || s_PreviousNumber != pEditor->m_SwitchNum)
s_vColors[PROP_SWITCH_NUMBER] = pEditor->m_Map.m_pSwitchLayer->ContainsElementWithId(pEditor->m_SwitchNum) ? ColorRGBA(1, 0.5f, 0.5f, 0.5f) : ColorRGBA(0.5f, 1, 0.5f, 0.5f);
if(s_PreviousView != pEditor->m_ViewSwitch)
s_vColors[PROP_SWITCH_VIEW] = ViewSwitch() ? ColorRGBA(0.5f, 1, 0.5f, 0.5f) : ColorRGBA(1, 0.5f, 0.5f, 0.5f);
}
s_PreviousNumber = pEditor->m_SwitchNum;
s_PreviousView = pEditor->m_ViewSwitch;
return CUI::POPUP_KEEP_OPEN;
}