5428: Separate editor update and render to fix slow joystick input r=def- a=Robyt3

Split `IEditor::UpdateAndEditor` into `OnUpdate` and `OnRender` and call the various input and UI methods in the same order as in the gameclient. This fixes the joystick input being slow in the editor. Closes #5422.

## 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 if it works standalone, system.c especially
- [ ] 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-06-15 16:56:58 +00:00 committed by GitHub
commit 3920b48f5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 43 deletions

View file

@ -2816,8 +2816,10 @@ void CClient::Update()
m_ServerBrowser.Update(m_ResortServerBrowser);
m_ResortServerBrowser = false;
// update gameclient
if(!m_EditorActive)
// update editor/gameclient
if(m_EditorActive)
m_pEditor->OnUpdate();
else
GameClient()->OnUpdate();
Discord()->Update();
@ -3224,7 +3226,7 @@ void CClient::Run()
Render();
else
{
m_pEditor->UpdateAndRender();
m_pEditor->OnRender();
DebugRender();
}
m_pGraphics->Swap();
@ -3237,7 +3239,7 @@ void CClient::Run()
Render();
else
{
m_pEditor->UpdateAndRender();
m_pEditor->OnRender();
DebugRender();
}
m_pGraphics->Swap();

View file

@ -10,7 +10,8 @@ class IEditor : public IInterface
public:
virtual ~IEditor() {}
virtual void Init() = 0;
virtual void UpdateAndRender() = 0;
virtual void OnUpdate() = 0;
virtual void OnRender() = 0;
virtual bool HasUnsavedData() const = 0;
virtual int Load(const char *pFilename, int StorageType) = 0;
virtual int Save(const char *pFilename) = 0;

View file

@ -6386,67 +6386,53 @@ void CEditor::PlaceBorderTiles()
pT->m_pTiles[i].m_Index = 1;
}
void CEditor::UpdateAndRender()
void CEditor::OnUpdate()
{
static float s_MouseX = 0.0f;
static float s_MouseY = 0.0f;
if(!m_EditorWasUsedBefore)
{
m_EditorWasUsedBefore = true;
Reset();
}
if(m_Animate)
m_AnimateTime = (time_get() - m_AnimateStart) / (float)time_freq();
else
m_AnimateTime = 0;
ms_pUiGotContext = nullptr;
UI()->StartCheck();
// handle mouse movement
float mx, my, Mwx, Mwy;
float rx = 0, ry = 0;
{
IInput::ECursorType CursorType = Input()->CursorRelative(&rx, &ry);
if(CursorType != IInput::CURSOR_NONE)
UIEx()->ConvertMouseMove(&rx, &ry, CursorType);
UIEx()->ResetMouseSlow();
static double s_MouseX = 0.0f;
static double s_MouseY = 0.0f;
m_MouseDeltaX = rx;
m_MouseDeltaY = ry;
float MouseRelX = 0.0f, MouseRelY = 0.0f;
IInput::ECursorType CursorType = Input()->CursorRelative(&MouseRelX, &MouseRelY);
if(CursorType != IInput::CURSOR_NONE)
UIEx()->ConvertMouseMove(&MouseRelX, &MouseRelY, CursorType);
UIEx()->ResetMouseSlow();
if(!m_LockMouse)
{
s_MouseX = clamp<float>(s_MouseX + rx, 0.0f, Graphics()->WindowWidth());
s_MouseY = clamp<float>(s_MouseY + ry, 0.0f, Graphics()->WindowHeight());
s_MouseX = clamp<float>(s_MouseX + MouseRelX, 0.0f, Graphics()->WindowWidth());
s_MouseY = clamp<float>(s_MouseY + MouseRelY, 0.0f, Graphics()->WindowHeight());
}
// update the ui
mx = UI()->Screen()->w * ((float)s_MouseX / Graphics()->WindowWidth());
my = UI()->Screen()->h * ((float)s_MouseY / Graphics()->WindowHeight());
Mwx = 0;
Mwy = 0;
// update positions for ui, but only update ui when rendering
m_MouseX = UI()->Screen()->w * ((float)s_MouseX / Graphics()->WindowWidth());
m_MouseY = UI()->Screen()->h * ((float)s_MouseY / Graphics()->WindowHeight());
// fix correct world x and y
CLayerGroup *g = GetSelectedGroup();
if(g)
CLayerGroup *pGroup = GetSelectedGroup();
if(pGroup)
{
float aPoints[4];
g->Mapping(aPoints);
pGroup->Mapping(aPoints);
float WorldWidth = aPoints[2] - aPoints[0];
float WorldHeight = aPoints[3] - aPoints[1];
Mwx = aPoints[0] + WorldWidth * ((float)s_MouseX / Graphics()->WindowWidth());
Mwy = aPoints[1] + WorldHeight * ((float)s_MouseY / Graphics()->WindowHeight());
m_MouseDeltaWx = m_MouseDeltaX * (WorldWidth / Graphics()->ScreenWidth());
m_MouseDeltaWy = m_MouseDeltaY * (WorldHeight / Graphics()->ScreenHeight());
m_MouseWorldX = aPoints[0] + WorldWidth * (s_MouseX / Graphics()->WindowWidth());
m_MouseWorldY = aPoints[1] + WorldHeight * (s_MouseY / Graphics()->WindowHeight());
}
else
{
m_MouseWorldX = 0.0f;
m_MouseWorldY = 0.0f;
}
UI()->Update(mx, my, Mwx, Mwy);
}
// toggle gui
@ -6455,6 +6441,23 @@ void CEditor::UpdateAndRender()
if(Input()->KeyPress(KEY_F10))
m_ShowMousePointer = false;
}
void CEditor::OnRender()
{
if(m_Animate)
m_AnimateTime = (time_get() - m_AnimateStart) / (float)time_freq();
else
m_AnimateTime = 0;
ms_pUiGotContext = nullptr;
UI()->StartCheck();
m_MouseDeltaX = m_MouseX - UI()->MouseX();
m_MouseDeltaY = m_MouseY - UI()->MouseY();
m_MouseDeltaWx = m_MouseWorldX - UI()->MouseWorldX();
m_MouseDeltaWy = m_MouseWorldY - UI()->MouseWorldY();
UI()->Update(m_MouseX, m_MouseY, m_MouseWorldX, m_MouseWorldY);
Render();

View file

@ -811,7 +811,8 @@ public:
}
void Init() override;
void UpdateAndRender() override;
void OnUpdate() override;
void OnRender() override;
bool HasUnsavedData() const override { return m_Map.m_Modified; }
void UpdateMentions() override { m_Mentions++; }
void ResetMentions() override { m_Mentions = 0; }
@ -942,6 +943,10 @@ public:
bool m_ShowMousePointer;
bool m_GuiActive;
bool m_ProofBorders;
float m_MouseX = 0.0f;
float m_MouseY = 0.0f;
float m_MouseWorldX = 0.0f;
float m_MouseWorldY = 0.0f;
float m_MouseDeltaX;
float m_MouseDeltaY;
float m_MouseDeltaWx;