5967: Fix crashes in editor when scrolling with up/down keys on empty sounds/images lists r=heinrich5991 a=Robyt3

The crashes were already present before #5751.

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-10-21 18:47:13 +00:00 committed by GitHub
commit 529bbcefe9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3987,7 +3987,9 @@ void CEditor::RenderImagesList(CUIRect ToolBox)
ToolBox.y += ScrollOffset.y; ToolBox.y += ScrollOffset.y;
bool ScrollToSelection = false; bool ScrollToSelection = false;
if(Input()->KeyPress(KEY_DOWN) && m_Dialog == DIALOG_NONE) if(m_Dialog == DIALOG_NONE && !m_Map.m_vpImages.empty())
{
if(Input()->KeyPress(KEY_DOWN))
{ {
int OldImage = m_SelectedImage; int OldImage = m_SelectedImage;
m_SelectedImage = clamp(m_SelectedImage, 0, (int)m_Map.m_vpImages.size() - 1); m_SelectedImage = clamp(m_SelectedImage, 0, (int)m_Map.m_vpImages.size() - 1);
@ -4012,7 +4014,7 @@ void CEditor::RenderImagesList(CUIRect ToolBox)
} }
ScrollToSelection = OldImage != m_SelectedImage; ScrollToSelection = OldImage != m_SelectedImage;
} }
if(Input()->KeyPress(KEY_UP) && m_Dialog == DIALOG_NONE) else if(Input()->KeyPress(KEY_UP))
{ {
int OldImage = m_SelectedImage; int OldImage = m_SelectedImage;
m_SelectedImage = clamp(m_SelectedImage, 0, (int)m_Map.m_vpImages.size() - 1); m_SelectedImage = clamp(m_SelectedImage, 0, (int)m_Map.m_vpImages.size() - 1);
@ -4037,6 +4039,7 @@ void CEditor::RenderImagesList(CUIRect ToolBox)
} }
ScrollToSelection = OldImage != m_SelectedImage; ScrollToSelection = OldImage != m_SelectedImage;
} }
}
for(int e = 0; e < 2; e++) // two passes, first embedded, then external for(int e = 0; e < 2; e++) // two passes, first embedded, then external
{ {
@ -4166,16 +4169,19 @@ void CEditor::RenderSounds(CUIRect ToolBox)
ToolBox.y += ScrollOffset.y; ToolBox.y += ScrollOffset.y;
bool ScrollToSelection = false; bool ScrollToSelection = false;
if(Input()->KeyPress(KEY_DOWN) && m_Dialog == DIALOG_NONE) if(m_Dialog == DIALOG_NONE && !m_Map.m_vpSounds.empty())
{
if(Input()->KeyPress(KEY_DOWN))
{ {
m_SelectedSound = (m_SelectedSound + 1) % m_Map.m_vpSounds.size(); m_SelectedSound = (m_SelectedSound + 1) % m_Map.m_vpSounds.size();
ScrollToSelection = true; ScrollToSelection = true;
} }
if(Input()->KeyPress(KEY_UP) && m_Dialog == DIALOG_NONE) else if(Input()->KeyPress(KEY_UP))
{ {
m_SelectedSound = (m_SelectedSound + m_Map.m_vpSounds.size() - 1) % m_Map.m_vpSounds.size(); m_SelectedSound = (m_SelectedSound + m_Map.m_vpSounds.size() - 1) % m_Map.m_vpSounds.size();
ScrollToSelection = true; ScrollToSelection = true;
} }
}
CUIRect Slot; CUIRect Slot;
ToolBox.HSplitTop(RowHeight + 3.0f, &Slot, &ToolBox); ToolBox.HSplitTop(RowHeight + 3.0f, &Slot, &ToolBox);