Code-style consistency improvement:

Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files.
make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type.
This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
erwincoumans
2018-09-23 14:17:31 -07:00
parent b73b05e9fb
commit ab8f16961e
1773 changed files with 1081087 additions and 474249 deletions

View File

@@ -4,7 +4,6 @@
See license in Gwen.h
*/
#include "Gwen/Gwen.h"
#include "Gwen/Controls/TextBox.h"
#include "Gwen/Skin.h"
@@ -13,91 +12,89 @@
#include <math.h>
using namespace Gwen;
using namespace Gwen::Controls;
GWEN_CONTROL_CONSTRUCTOR( TextBox )
GWEN_CONTROL_CONSTRUCTOR(TextBox)
{
SetSize( 200, 20 );
SetSize(200, 20);
SetMouseInputEnabled( true );
SetKeyboardInputEnabled( true );
SetMouseInputEnabled(true);
SetKeyboardInputEnabled(true);
SetAlignment( Pos::Left | Pos::CenterV );
SetTextPadding( Padding( 4, 2, 4, 2 ) );
SetAlignment(Pos::Left | Pos::CenterV);
SetTextPadding(Padding(4, 2, 4, 2));
m_iCursorPos = 0;
m_iCursorEnd = 0;
m_bSelectAll = false;
SetTextColor( Gwen::Color( 50, 50, 50, 255 ) ); // TODO: From Skin
SetTextColor(Gwen::Color(50, 50, 50, 255)); // TODO: From Skin
SetTabable( true );
SetTabable(true);
AddAccelerator( L"Ctrl + c", &TextBox::OnCopy );
AddAccelerator( L"Ctrl + x", &TextBox::OnCut );
AddAccelerator( L"Ctrl + v", &TextBox::OnPaste );
AddAccelerator( L"Ctrl + a", &TextBox::OnSelectAll );
AddAccelerator(L"Ctrl + c", &TextBox::OnCopy);
AddAccelerator(L"Ctrl + x", &TextBox::OnCut);
AddAccelerator(L"Ctrl + v", &TextBox::OnPaste);
AddAccelerator(L"Ctrl + a", &TextBox::OnSelectAll);
}
bool TextBox::OnChar( Gwen::UnicodeChar c )
bool TextBox::OnChar(Gwen::UnicodeChar c)
{
if ( c == '\t' ) return false;
if (c == '\t') return false;
Gwen::UnicodeString str;
str += c;
InsertText( str );
InsertText(str);
return true;
}
void TextBox::InsertText( const Gwen::UnicodeString& strInsert )
void TextBox::InsertText(const Gwen::UnicodeString& strInsert)
{
// TODO: Make sure fits (implement maxlength)
if ( HasSelection() )
if (HasSelection())
{
EraseSelection();
}
if ( m_iCursorPos > TextLength() ) m_iCursorPos = TextLength();
if (m_iCursorPos > TextLength()) m_iCursorPos = TextLength();
if ( !IsTextAllowed( strInsert, m_iCursorPos ) )
if (!IsTextAllowed(strInsert, m_iCursorPos))
return;
UnicodeString str = GetText();
str.insert( m_iCursorPos, strInsert );
SetText( str );
str.insert(m_iCursorPos, strInsert);
SetText(str);
m_iCursorPos += (int) strInsert.size();
m_iCursorPos += (int)strInsert.size();
m_iCursorEnd = m_iCursorPos;
RefreshCursorBounds();
}
void TextBox::Render( Skin::Base* skin )
void TextBox::Render(Skin::Base* skin)
{
if ( ShouldDrawBackground() )
skin->DrawTextBox( this );
if (ShouldDrawBackground())
skin->DrawTextBox(this);
if ( !HasFocus() ) return;
if (!HasFocus()) return;
// Draw selection.. if selected..
if ( m_iCursorPos != m_iCursorEnd )
if (m_iCursorPos != m_iCursorEnd)
{
skin->GetRender()->SetDrawColor( Gwen::Color( 50, 170, 255, 200 ) );
skin->GetRender()->DrawFilledRect( m_rectSelectionBounds );
skin->GetRender()->SetDrawColor(Gwen::Color(50, 170, 255, 200));
skin->GetRender()->DrawFilledRect(m_rectSelectionBounds);
}
// Draw caret
if ( fmod( Gwen::Platform::GetTimeInSeconds()-m_fLastInputTime, 1.0f ) > 0.5f )
skin->GetRender()->SetDrawColor( Gwen::Color( 255, 255, 255, 255 ) );
if (fmod(Gwen::Platform::GetTimeInSeconds() - m_fLastInputTime, 1.0f) > 0.5f)
skin->GetRender()->SetDrawColor(Gwen::Color(255, 255, 255, 255));
else
skin->GetRender()->SetDrawColor( Gwen::Color( 0, 0, 0, 255 ) );
skin->GetRender()->SetDrawColor(Gwen::Color(0, 0, 0, 255));
skin->GetRender()->DrawFilledRect( m_rectCaretBounds );
skin->GetRender()->DrawFilledRect(m_rectCaretBounds);
}
void TextBox::RefreshCursorBounds()
@@ -106,44 +103,43 @@ void TextBox::RefreshCursorBounds()
MakeCaratVisible();
Gwen::Point pA = GetCharacterPosition( m_iCursorPos );
Gwen::Point pB = GetCharacterPosition( m_iCursorEnd );
Gwen::Point pA = GetCharacterPosition(m_iCursorPos);
Gwen::Point pB = GetCharacterPosition(m_iCursorEnd);
m_rectSelectionBounds.x = Utility::Min( pA.x, pB.x );
m_rectSelectionBounds.x = Utility::Min(pA.x, pB.x);
m_rectSelectionBounds.y = m_Text->Y() - 1;
m_rectSelectionBounds.w = Utility::Max( pA.x, pB.x ) - m_rectSelectionBounds.x;
m_rectSelectionBounds.w = Utility::Max(pA.x, pB.x) - m_rectSelectionBounds.x;
m_rectSelectionBounds.h = m_Text->Height() + 2;
m_rectCaretBounds.x = pA.x;
m_rectCaretBounds.y = m_Text->Y() - 1;
m_rectCaretBounds.w = 1;
m_rectCaretBounds.h = m_Text->Height() + 2;
Redraw();
}
void TextBox::OnPaste( Gwen::Controls::Base* /*pCtrl*/ )
void TextBox::OnPaste(Gwen::Controls::Base* /*pCtrl*/)
{
InsertText( Platform::GetClipboardText() );
InsertText(Platform::GetClipboardText());
}
void TextBox::OnCopy( Gwen::Controls::Base* /*pCtrl*/ )
void TextBox::OnCopy(Gwen::Controls::Base* /*pCtrl*/)
{
if ( !HasSelection() ) return;
if (!HasSelection()) return;
Platform::SetClipboardText( GetSelection() );
Platform::SetClipboardText(GetSelection());
}
void TextBox::OnCut( Gwen::Controls::Base* /*pCtrl*/ )
void TextBox::OnCut(Gwen::Controls::Base* /*pCtrl*/)
{
if ( !HasSelection() ) return;
if (!HasSelection()) return;
Platform::SetClipboardText( GetSelection() );
Platform::SetClipboardText(GetSelection());
EraseSelection();
}
void TextBox::OnSelectAll( Gwen::Controls::Base* /*pCtrl*/ )
void TextBox::OnSelectAll(Gwen::Controls::Base* /*pCtrl*/)
{
m_iCursorEnd = 0;
m_iCursorPos = TextLength();
@@ -151,33 +147,33 @@ void TextBox::OnSelectAll( Gwen::Controls::Base* /*pCtrl*/ )
RefreshCursorBounds();
}
void TextBox::OnMouseDoubleClickLeft( int /*x*/, int /*y*/ )
{
OnSelectAll( this );
void TextBox::OnMouseDoubleClickLeft(int /*x*/, int /*y*/)
{
OnSelectAll(this);
}
UnicodeString TextBox::GetSelection()
{
if ( !HasSelection() ) return L"";
if (!HasSelection()) return L"";
int iStart = Utility::Min( m_iCursorPos, m_iCursorEnd );
int iEnd = Utility::Max( m_iCursorPos, m_iCursorEnd );
int iStart = Utility::Min(m_iCursorPos, m_iCursorEnd);
int iEnd = Utility::Max(m_iCursorPos, m_iCursorEnd);
const UnicodeString& str = GetText();
return str.substr( iStart, iEnd - iStart );
return str.substr(iStart, iEnd - iStart);
}
bool TextBox::OnKeyReturn( bool bDown )
bool TextBox::OnKeyReturn(bool bDown)
{
if ( bDown ) return true;
if (bDown) return true;
OnEnter();
// Try to move to the next control, as if tab had been pressed
OnKeyTab( true );
OnKeyTab(true);
// If we still have focus, blur it.
if ( HasFocus() )
if (HasFocus())
{
Blur();
}
@@ -185,46 +181,46 @@ bool TextBox::OnKeyReturn( bool bDown )
return true;
}
bool TextBox::OnKeyBackspace( bool bDown )
bool TextBox::OnKeyBackspace(bool bDown)
{
if ( !bDown ) return true;
if ( HasSelection() )
if (!bDown) return true;
if (HasSelection())
{
EraseSelection();
return true;
}
if ( m_iCursorPos == 0 ) return true;
if (m_iCursorPos == 0) return true;
DeleteText( m_iCursorPos-1, 1 );
DeleteText(m_iCursorPos - 1, 1);
return true;
}
bool TextBox::OnKeyDelete( bool bDown )
bool TextBox::OnKeyDelete(bool bDown)
{
if ( !bDown ) return true;
if ( HasSelection() )
if (!bDown) return true;
if (HasSelection())
{
EraseSelection();
return true;
}
if ( m_iCursorPos >= TextLength() ) return true;
if (m_iCursorPos >= TextLength()) return true;
DeleteText( m_iCursorPos, 1 );
DeleteText(m_iCursorPos, 1);
return true;
}
bool TextBox::OnKeyLeft( bool bDown )
bool TextBox::OnKeyLeft(bool bDown)
{
if ( !bDown ) return true;
if (!bDown) return true;
if ( m_iCursorPos > 0 )
if (m_iCursorPos > 0)
m_iCursorPos--;
if ( !Gwen::Input::IsShiftDown() )
if (!Gwen::Input::IsShiftDown())
{
m_iCursorEnd = m_iCursorPos;
}
@@ -233,14 +229,14 @@ bool TextBox::OnKeyLeft( bool bDown )
return true;
}
bool TextBox::OnKeyRight( bool bDown )
bool TextBox::OnKeyRight(bool bDown)
{
if ( !bDown ) return true;
if (!bDown) return true;
if ( m_iCursorPos < TextLength() )
if (m_iCursorPos < TextLength())
m_iCursorPos++;
if ( !Gwen::Input::IsShiftDown() )
if (!Gwen::Input::IsShiftDown())
{
m_iCursorEnd = m_iCursorPos;
}
@@ -249,12 +245,12 @@ bool TextBox::OnKeyRight( bool bDown )
return true;
}
bool TextBox::OnKeyHome( bool bDown )
bool TextBox::OnKeyHome(bool bDown)
{
if ( !bDown ) return true;
if (!bDown) return true;
m_iCursorPos = 0;
if ( !Gwen::Input::IsShiftDown() )
if (!Gwen::Input::IsShiftDown())
{
m_iCursorEnd = m_iCursorPos;
}
@@ -263,11 +259,11 @@ bool TextBox::OnKeyHome( bool bDown )
return true;
}
bool TextBox::OnKeyEnd( bool /*bDown*/ )
bool TextBox::OnKeyEnd(bool /*bDown*/)
{
m_iCursorPos = TextLength();
if ( !Gwen::Input::IsShiftDown() )
if (!Gwen::Input::IsShiftDown())
{
m_iCursorEnd = m_iCursorPos;
}
@@ -276,35 +272,34 @@ bool TextBox::OnKeyEnd( bool /*bDown*/ )
return true;
}
void TextBox::SetCursorPos( int i )
void TextBox::SetCursorPos(int i)
{
if ( m_iCursorPos == i ) return;
if (m_iCursorPos == i) return;
m_iCursorPos = i;
RefreshCursorBounds();
}
void TextBox::SetCursorEnd( int i )
void TextBox::SetCursorEnd(int i)
{
if ( m_iCursorEnd == i ) return;
if (m_iCursorEnd == i) return;
m_iCursorEnd = i;
RefreshCursorBounds();
}
void TextBox::DeleteText( int iStartPos, int iLength )
void TextBox::DeleteText(int iStartPos, int iLength)
{
UnicodeString str = GetText();
str.erase( iStartPos, iLength );
SetText( str );
str.erase(iStartPos, iLength);
SetText(str);
if ( m_iCursorPos > iStartPos )
if (m_iCursorPos > iStartPos)
{
SetCursorPos( m_iCursorPos - iLength );
SetCursorPos(m_iCursorPos - iLength);
}
SetCursorEnd( m_iCursorPos );
SetCursorEnd(m_iCursorPos);
}
bool TextBox::HasSelection()
@@ -314,98 +309,98 @@ bool TextBox::HasSelection()
void TextBox::EraseSelection()
{
int iStart = Utility::Min( m_iCursorPos, m_iCursorEnd );
int iEnd = Utility::Max( m_iCursorPos, m_iCursorEnd );
int iStart = Utility::Min(m_iCursorPos, m_iCursorEnd);
int iEnd = Utility::Max(m_iCursorPos, m_iCursorEnd);
DeleteText( iStart, iEnd - iStart );
DeleteText(iStart, iEnd - iStart);
// Move the cursor to the start of the selection,
// Move the cursor to the start of the selection,
// since the end is probably outside of the string now.
m_iCursorPos = iStart;
m_iCursorEnd = iStart;
}
void TextBox::OnMouseClickLeft( int x, int y, bool bDown )
void TextBox::OnMouseClickLeft(int x, int y, bool bDown)
{
if ( m_bSelectAll )
if (m_bSelectAll)
{
OnSelectAll( this );
OnSelectAll(this);
m_bSelectAll = false;
return;
}
int iChar = m_Text->GetClosestCharacter( m_Text->CanvasPosToLocal( Gwen::Point( x, y ) ) );
int iChar = m_Text->GetClosestCharacter(m_Text->CanvasPosToLocal(Gwen::Point(x, y)));
if ( bDown )
if (bDown)
{
SetCursorPos( iChar );
SetCursorPos(iChar);
if ( !Gwen::Input::IsShiftDown() )
SetCursorEnd( iChar );
if (!Gwen::Input::IsShiftDown())
SetCursorEnd(iChar);
Gwen::MouseFocus = this;
}
else
{
if ( Gwen::MouseFocus == this )
if (Gwen::MouseFocus == this)
{
SetCursorPos( iChar );
SetCursorPos(iChar);
Gwen::MouseFocus = NULL;
}
}
}
void TextBox::OnMouseMoved( int x, int y, int /*deltaX*/, int /*deltaY*/ )
void TextBox::OnMouseMoved(int x, int y, int /*deltaX*/, int /*deltaY*/)
{
if ( Gwen::MouseFocus != this ) return;
if (Gwen::MouseFocus != this) return;
int iChar = m_Text->GetClosestCharacter( m_Text->CanvasPosToLocal( Gwen::Point( x, y ) ) );
int iChar = m_Text->GetClosestCharacter(m_Text->CanvasPosToLocal(Gwen::Point(x, y)));
SetCursorPos( iChar );
SetCursorPos(iChar);
}
void TextBox::MakeCaratVisible()
{
int iCaratPos = m_Text->GetCharacterPosition( m_iCursorPos ).x;
int iCaratPos = m_Text->GetCharacterPosition(m_iCursorPos).x;
// If the carat is already in a semi-good position, leave it.
{
int iRealCaratPos = iCaratPos + m_Text->X();
if ( iRealCaratPos > Width() * 0.1f && iRealCaratPos < Width() * 0.9f )
if (iRealCaratPos > Width() * 0.1f && iRealCaratPos < Width() * 0.9f)
return;
}
// The ideal position is for the carat to be right in the middle
int idealx = -iCaratPos + Width() * 0.5f;;
int idealx = -iCaratPos + Width() * 0.5f;
;
// Don't show too much whitespace to the right
if ( idealx + m_Text->Width() < Width() - m_rTextPadding.right )
idealx = -m_Text->Width() + (Width() - m_rTextPadding.right );
if (idealx + m_Text->Width() < Width() - m_rTextPadding.right)
idealx = -m_Text->Width() + (Width() - m_rTextPadding.right);
// Or the left
if ( idealx > m_rTextPadding.left )
if (idealx > m_rTextPadding.left)
idealx = m_rTextPadding.left;
m_Text->SetPos( idealx, m_Text->Y() );
m_Text->SetPos(idealx, m_Text->Y());
}
void TextBox::Layout( Skin::Base* skin )
void TextBox::Layout(Skin::Base* skin)
{
BaseClass::Layout( skin );
BaseClass::Layout(skin);
RefreshCursorBounds();
}
void TextBox::OnTextChanged()
{
if ( m_iCursorPos > TextLength() ) m_iCursorPos = TextLength();
if ( m_iCursorEnd > TextLength() ) m_iCursorEnd = TextLength();
if (m_iCursorPos > TextLength()) m_iCursorPos = TextLength();
if (m_iCursorEnd > TextLength()) m_iCursorEnd = TextLength();
onTextChanged.Call( this );
onTextChanged.Call(this);
}
void TextBox::OnEnter()
{
onReturnPressed.Call( this );
onReturnPressed.Call(this);
}