From 501cbea8c19f4edf049fabb675888ff11354e0ff Mon Sep 17 00:00:00 2001 From: Zwelf Date: Mon, 19 Jun 2017 11:30:35 +0200 Subject: [PATCH 1/2] added clipboard to CInput --- src/engine/client/input.cpp | 21 +++++++++++++++++++++ src/engine/client/input.h | 4 ++++ src/engine/input.h | 3 +++ 3 files changed, 28 insertions(+) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 575ee39aa..b9402d970 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -39,12 +39,18 @@ CInput::CInput() m_InputCounter = 1; m_InputGrabbed = 0; + m_pClipboardText = 0; m_MouseDoubleClick = false; m_NumEvents = 0; } +CInput::~CInput() +{ + free(m_pClipboardText); +} + void CInput::Init() { m_pGraphics = Kernel()->RequestInterface(); @@ -103,6 +109,21 @@ int CInput::MouseDoubleClick() return 0; } +const char *CInput::GetClipboardText() +{ + if(m_pClipboardText) + { + free(m_pClipboardText); + } + m_pClipboardText = SDL_GetClipboardText(); + return m_pClipboardText; +} + +void CInput::SetClipboardText(const char *pText) +{ + SDL_SetClipboardText(pText); +} + void CInput::Clear() { mem_zero(m_aInputState, sizeof(m_aInputState)); diff --git a/src/engine/client/input.h b/src/engine/client/input.h index d71ade5e7..6d6febffc 100644 --- a/src/engine/client/input.h +++ b/src/engine/client/input.h @@ -9,6 +9,7 @@ class CInput : public IEngineInput IConsole *m_pConsole; int m_InputGrabbed; + char *m_pClipboardText; bool m_MouseDoubleClick; @@ -28,6 +29,7 @@ class CInput : public IEngineInput public: CInput(); + ~CInput(); virtual void Init(); @@ -38,6 +40,8 @@ public: virtual void MouseModeAbsolute(); virtual void MouseModeRelative(); virtual int MouseDoubleClick(); + virtual const char *GetClipboardText(); + virtual void SetClipboardText(const char *pText); virtual int Update(); }; diff --git a/src/engine/input.h b/src/engine/input.h index 256dfd831..d26d859fd 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -63,6 +63,9 @@ public: virtual void MouseModeRelative() = 0; virtual void MouseModeAbsolute() = 0; virtual int MouseDoubleClick() = 0; + virtual const char* GetClipboardText() = 0; + virtual void SetClipboardText(const char *Text) = 0; + virtual void MouseRelative(float *x, float *y) = 0; }; From 1020896fac2373a74ab87373507183be5c90be28 Mon Sep 17 00:00:00 2001 From: Zwelf Date: Mon, 19 Jun 2017 12:28:50 +0200 Subject: [PATCH 2/2] added paste to text fields --- src/game/client/lineinput.cpp | 50 +++++++++++++++++++++++++++++------ src/game/client/lineinput.h | 2 +- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/game/client/lineinput.cpp b/src/game/client/lineinput.cpp index 50f9f82db..9e2a1042e 100644 --- a/src/game/client/lineinput.cpp +++ b/src/game/client/lineinput.cpp @@ -37,7 +37,7 @@ void CLineInput::Set(const char *pString) } } -bool CLineInput::CtrlStop(char c) +bool CLineInput::MoveWordStop(char c) { // jump to spaces and special ASCII characters return ((32 <= c && c <= 47) || // !"#$%&'()*+,-./ @@ -55,7 +55,8 @@ bool CLineInput::Manipulate(IInput::CEvent Event, char *pStr, int StrMaxSize, in if(CursorPos > Len) CursorPos = Len; - if(Event.m_Flags&IInput::FLAG_TEXT) + if(Event.m_Flags&IInput::FLAG_TEXT && + !(KEY_LCTRL <= Event.m_Key && Event.m_Key <= KEY_RGUI)) { // gather string stats int CharCount = 0; @@ -89,13 +90,13 @@ bool CLineInput::Manipulate(IInput::CEvent Event, char *pStr, int StrMaxSize, in if(Event.m_Flags&IInput::FLAG_PRESS) { int Key = Event.m_Key; - bool Ctrl = false; + bool MoveWord = false; #ifdef CONF_PLATFORM_MACOSX if(pInput && (pInput->KeyIsPressed(KEY_LALT) || pInput->KeyIsPressed(KEY_RALT))) #else if(pInput && (pInput->KeyIsPressed(KEY_LCTRL) || pInput->KeyIsPressed(KEY_RCTRL))) #endif - Ctrl = true; + MoveWord = true; if(Key == KEY_BACKSPACE && CursorPos > 0) { int NewCursorPos = CursorPos; @@ -103,7 +104,7 @@ bool CLineInput::Manipulate(IInput::CEvent Event, char *pStr, int StrMaxSize, in { NewCursorPos = str_utf8_rewind(pStr, NewCursorPos); NumChars -= 1; - } while(Ctrl && NewCursorPos > 0 && !CtrlStop(pStr[NewCursorPos - 1])); + } while(MoveWord && NewCursorPos > 0 && !MoveWordStop(pStr[NewCursorPos - 1])); int CharSize = CursorPos-NewCursorPos; mem_move(pStr+NewCursorPos, pStr+CursorPos, Len - NewCursorPos - CharSize + 1); // +1 == null term CursorPos = NewCursorPos; @@ -117,7 +118,7 @@ bool CLineInput::Manipulate(IInput::CEvent Event, char *pStr, int StrMaxSize, in { EndCursorPos = str_utf8_forward(pStr, EndCursorPos); NumChars -= 1; - } while(Ctrl && EndCursorPos < Len && !CtrlStop(pStr[EndCursorPos - 1])); + } while(MoveWord && EndCursorPos < Len && !MoveWordStop(pStr[EndCursorPos - 1])); int CharSize = EndCursorPos - CursorPos; mem_move(pStr + CursorPos, pStr + CursorPos + CharSize, Len - CursorPos - CharSize + 1); // +1 == null term Len -= CharSize; @@ -128,19 +129,52 @@ bool CLineInput::Manipulate(IInput::CEvent Event, char *pStr, int StrMaxSize, in do { CursorPos = str_utf8_rewind(pStr, CursorPos); - } while(Ctrl && CursorPos > 0 && !CtrlStop(pStr[CursorPos - 1])); + } while(MoveWord && CursorPos > 0 && !MoveWordStop(pStr[CursorPos - 1])); } else if(Key == KEY_RIGHT && CursorPos < Len) { do { CursorPos = str_utf8_forward(pStr, CursorPos); - } while(Ctrl && CursorPos < Len && !CtrlStop(pStr[CursorPos - 1])); + } while(MoveWord && CursorPos < Len && !MoveWordStop(pStr[CursorPos - 1])); } else if(Key == KEY_HOME) CursorPos = 0; else if(Key == KEY_END) CursorPos = Len; + else if((pInput->KeyIsPressed(KEY_LCTRL) || pInput->KeyIsPressed(KEY_RCTRL)) && Key == KEY_V) + { + // paste clipboard to cursor + const char *pClipboardText = pInput->GetClipboardText(); + + // gather string stats + int CharCount = 0; + int CharSize = 0; + while(pClipboardText[CharSize]) + { + int NewCharSize = str_utf8_forward(pClipboardText, CharSize); + if(NewCharSize != CharSize) + { + ++CharCount; + CharSize = NewCharSize; + } + } + + // add new string + if(CharCount) + { + if(Len+CharSize < StrMaxSize && CursorPos+CharSize < StrMaxSize && NumChars+CharCount < StrMaxChars) + { + mem_move(pStr + CursorPos + CharSize, pStr + CursorPos, Len-CursorPos+1); // +1 == null term + for(int i = 0; i < CharSize; i++) + pStr[CursorPos+i] = pClipboardText[i]; + CursorPos += CharSize; + Len += CharSize; + NumChars += CharCount; + Changes = true; + } + } + } } *pNumCharsPtr = NumChars; diff --git a/src/game/client/lineinput.h b/src/game/client/lineinput.h index fae3a5ef4..526b25183 100644 --- a/src/game/client/lineinput.h +++ b/src/game/client/lineinput.h @@ -19,7 +19,7 @@ class CLineInput int m_NumChars; IInput *m_pInput; public: - static bool CtrlStop(char c); + static bool MoveWordStop(char c); static bool Manipulate(IInput::CEvent e, char *pStr, int StrMaxSize, int StrMaxChars, int *pStrLenPtr, int *pCursorPosPtr, int *pNumCharsPtr, IInput *pInput); class CCallback