From 1f91f612de4c2c22ae8e6c4ced80122474bf3d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 21 May 2020 02:31:34 -0300 Subject: [PATCH] More editor improvements. Also changed the editor name, now is UICodeEditor. --- include/eepp/graphics/fontstyleconfig.hpp | 4 +- include/eepp/ui.hpp | 2 +- include/eepp/ui/doc/textdocument.hpp | 22 ++ .../ui/{uicodeedit.hpp => uicodeeditor.hpp} | 19 +- projects/linux/ee.creator.user | 2 +- projects/linux/ee.files | 4 +- src/eepp/ui/doc/textdocument.cpp | 76 +++++- .../ui/{uicodeedit.cpp => uicodeeditor.cpp} | 227 +++++++++++++----- src/eepp/ui/uiwidgetcreator.cpp | 4 +- .../ui_hello_world/ui_hello_world.cpp | 12 +- 10 files changed, 291 insertions(+), 81 deletions(-) rename include/eepp/ui/{uicodeedit.hpp => uicodeeditor.hpp} (86%) rename src/eepp/ui/{uicodeedit.cpp => uicodeeditor.cpp} (54%) diff --git a/include/eepp/graphics/fontstyleconfig.hpp b/include/eepp/graphics/fontstyleconfig.hpp index fd77b5c9e..89a4a4e8e 100644 --- a/include/eepp/graphics/fontstyleconfig.hpp +++ b/include/eepp/graphics/fontstyleconfig.hpp @@ -17,7 +17,7 @@ class FontStyleConfig { const Color& getFontShadowColor() const { return ShadowColor; } - const Uint32& getFontCharacterSize() const { return CharacterSize; } + const Float& getFontCharacterSize() const { return CharacterSize; } const Uint32& getFontStyle() const { return Style; } @@ -47,7 +47,7 @@ class FontStyleConfig { } Graphics::Font* Font = NULL; - Uint32 CharacterSize = 12; + Float CharacterSize = 12; Uint32 Style = 0; Color FontColor = Color( 255, 255, 255, 255 ); Color ShadowColor = Color( 50, 50, 50, 230 ); diff --git a/include/eepp/ui.hpp b/include/eepp/ui.hpp index d45fae173..dcc104fe6 100644 --- a/include/eepp/ui.hpp +++ b/include/eepp/ui.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index b7c7a24f7..4c091cfd6 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -55,6 +55,8 @@ class EE_API TextDocument { String getText( const TextRange& range ) const; + String getSelectedText() const; + String::StringBaseType getChar( const TextPosition& position ) const; TextPosition insert( const TextPosition& position, const String& text ); @@ -100,8 +102,12 @@ class EE_API TextDocument { void deleteTo( int offset ); + void deleteSelection(); + void selectTo( TextPosition offset ); + void selectTo( int offset ); + void moveTo( TextPosition offset ); void moveTo( int columnOffset ); @@ -116,6 +122,10 @@ class EE_API TextDocument { void moveToNextChar(); + void moveToPreviousWord(); + + void moveToNextWord(); + void moveToPreviousLine( Int64 lastColIndex = 0 ); void moveToNextLine( Int64 lastColIndex = 0 ); @@ -128,6 +138,18 @@ class EE_API TextDocument { void deleteToNextChar(); + void selectToPreviousChar(); + + void selectToNextChar(); + + void selectToPreviousWord(); + + void selectToNextWord(); + + void selectToPreviousLine( Int64 lastColIndex ); + + void selectToNextLine( Int64 lastColIndex ); + void newLine(); void indent(); diff --git a/include/eepp/ui/uicodeedit.hpp b/include/eepp/ui/uicodeeditor.hpp similarity index 86% rename from include/eepp/ui/uicodeedit.hpp rename to include/eepp/ui/uicodeeditor.hpp index 1d5eeb584..c7a647294 100644 --- a/include/eepp/ui/uicodeedit.hpp +++ b/include/eepp/ui/uicodeeditor.hpp @@ -14,13 +14,13 @@ class Font; namespace EE { namespace UI { -class EE_API UICodeEdit : public UIWidget, public TextDocument::Client { +class EE_API UICodeEditor : public UIWidget, public TextDocument::Client { public: - static UICodeEdit* New(); + static UICodeEditor* New(); - UICodeEdit(); + UICodeEditor(); - virtual ~UICodeEdit(); + virtual ~UICodeEditor(); virtual Uint32 getType() const; @@ -40,6 +40,8 @@ class EE_API UICodeEdit : public UIWidget, public TextDocument::Client { void setFont( Font* font ); + void setFontSize( Float dpSize ); + const Uint32& getTabWidth() const; void setTabWidth( const Uint32& tabWidth ); @@ -55,6 +57,7 @@ class EE_API UICodeEdit : public UIWidget, public TextDocument::Client { Clock mBlinkTimer; bool mDirtyEditor; bool mCursorVisible; + bool mMouseDown; Uint32 mTabWidth; Int64 mLastColOffset; Vector2f mScroll; @@ -62,12 +65,16 @@ class EE_API UICodeEdit : public UIWidget, public TextDocument::Client { void invalidateEditor(); + virtual Uint32 onFocusLoss(); + virtual Uint32 onTextInput( const TextInputEvent& event ); virtual Uint32 onKeyDown( const KeyEvent& Event ); virtual Uint32 onMouseDown( const Vector2i& position, const Uint32& flags ); + virtual Uint32 onMouseMove( const Vector2i& position, const Uint32& flags ); + virtual Uint32 onMouseUp( const Vector2i& position, const Uint32& flags ); virtual void onSizeChange(); @@ -77,7 +84,9 @@ class EE_API UICodeEdit : public UIWidget, public TextDocument::Client { void updateEditor(); void onDocumentTextChanged(); + void onDocumentCursorChange( const TextPosition& ); + void onDocumentSelectionChange( const TextRange& ); std::pair getVisibleLineRange(); @@ -98,6 +107,8 @@ class EE_API UICodeEdit : public UIWidget, public TextDocument::Client { void updateLastColumnOffset(); + void resetCursor(); + TextPosition resolveScreenPosition( const Vector2f& position ) const; Vector2f getViewPortLineCount() const; diff --git a/projects/linux/ee.creator.user b/projects/linux/ee.creator.user index f3f62f2b9..af8c507a1 100644 --- a/projects/linux/ee.creator.user +++ b/projects/linux/ee.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/projects/linux/ee.files b/projects/linux/ee.files index 84929ac8c..0acdd54b4 100644 --- a/projects/linux/ee.files +++ b/projects/linux/ee.files @@ -325,7 +325,7 @@ ../../include/eepp/ui/uibackgrounddrawable.hpp ../../include/eepp/ui/uiborderdrawable.hpp ../../include/eepp/ui/uicheckbox.hpp -../../include/eepp/ui/uicodeedit.hpp +../../include/eepp/ui/uicodeeditor.hpp ../../include/eepp/ui/uicombobox.hpp ../../include/eepp/ui/uicommondialog.hpp ../../include/eepp/ui/uidropdownlist.hpp @@ -766,7 +766,7 @@ ../../src/eepp/ui/uibackgrounddrawable.cpp ../../src/eepp/ui/uiborderdrawable.cpp ../../src/eepp/ui/uicheckbox.cpp -../../src/eepp/ui/uicodeedit.cpp +../../src/eepp/ui/uicodeeditor.cpp ../../src/eepp/ui/uicombobox.cpp ../../src/eepp/ui/uicommondialog.cpp ../../src/eepp/ui/uidropdownlist.cpp diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index ad0f9b2f2..c49957d92 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -131,6 +131,10 @@ String TextDocument::getText( const TextRange& range ) const { return String::join( lines, -1 ); } +String TextDocument::getSelectedText() const { + return getText( getSelection() ); +} + String::StringBaseType TextDocument::getChar( const TextPosition& position ) const { auto pos = sanitizePosition( position ); return mLines[pos.line()][pos.column()]; @@ -270,7 +274,7 @@ TextPosition TextDocument::previousWordBoundary( TextPosition position ) const { break; } nextChar = getChar( positionOffset( position, -1 ) ); - } while ( ( inWord && isNonWord( nextChar ) ) || ( !inWord && nextChar != ch ) ); + } while ( ( inWord && !isNonWord( nextChar ) ) || ( !inWord && nextChar == ch ) ); return position; } @@ -285,7 +289,7 @@ TextPosition TextDocument::nextWordBoundary( TextPosition position ) const { break; } nextChar = getChar( position ); - } while ( ( inWord && isNonWord( nextChar ) ) || ( !inWord && nextChar != ch ) ); + } while ( ( inWord && !isNonWord( nextChar ) ) || ( !inWord && nextChar == ch ) ); return position; } @@ -369,10 +373,22 @@ void TextDocument::deleteTo( int offset ) { setSelection( cursorPos ); } +void TextDocument::deleteSelection() { + TextPosition cursorPos = getSelection( true ).start(); + remove( getSelection() ); + setSelection( cursorPos ); +} + void TextDocument::selectTo( TextPosition offset ) { TextRange range = getSelection(); TextPosition posOffset = positionOffset( range.start(), offset ); - setSelection( TextRange( range.start(), posOffset ) ); + setSelection( TextRange( posOffset, range.end() ) ); +} + +void TextDocument::selectTo( int offset ) { + TextRange range = getSelection(); + TextPosition posOffset = positionOffset( range.start(), offset ); + setSelection( TextRange( posOffset, range.end() ) ); } void TextDocument::moveTo( TextPosition offset ) { @@ -416,6 +432,24 @@ void TextDocument::moveToNextChar() { } } +void TextDocument::moveToPreviousWord() { + if ( hasSelection() ) { + TextRange selection = getSelection( true ); + setSelection( selection.end() ); + } else { + setSelection( previousWordBoundary( getSelection().start() ) ); + } +} + +void TextDocument::moveToNextWord() { + if ( hasSelection() ) { + TextRange selection = getSelection( true ); + setSelection( selection.start() ); + } else { + setSelection( nextWordBoundary( getSelection().start() ) ); + } +} + void TextDocument::moveToPreviousLine( Int64 lastColIndex ) { TextPosition pos = getSelection().start(); pos.setLine( pos.line() - 1 ); @@ -456,6 +490,42 @@ void TextDocument::deleteToNextChar() { deleteTo( 1 ); } +void TextDocument::selectToPreviousChar() { + selectTo( -1 ); +} + +void TextDocument::selectToNextChar() { + selectTo( 1 ); +} + +void TextDocument::selectToPreviousWord() { + setSelection( {previousWordBoundary( getSelection().start() ), getSelection().end()} ); +} + +void TextDocument::selectToNextWord() { + setSelection( {nextWordBoundary( getSelection().start() ), getSelection().end()} ); +} + +void TextDocument::selectToPreviousLine( Int64 lastColIndex ) { + TextPosition pos = getSelection().start(); + pos.setLine( pos.line() - 1 ); + if ( pos.line() >= 0 ) { + lastColIndex = getRelativeColumnOffset( TextPosition( pos.line(), lastColIndex ) ); + } + pos.setColumn( lastColIndex ); + setSelection( TextRange( pos, getSelection().end() ) ); +} + +void TextDocument::selectToNextLine( Int64 lastColIndex ) { + TextPosition pos = getSelection().start(); + pos.setLine( pos.line() + 1 ); + if ( pos.line() < (Int64)mLines.size() ) { + lastColIndex = getRelativeColumnOffset( TextPosition( pos.line(), lastColIndex ) ); + } + pos.setColumn( lastColIndex ); + setSelection( TextRange( pos, getSelection().end() ) ); +} + void TextDocument::newLine() { String input( "\n" ); TextPosition start = getSelection().start(); diff --git a/src/eepp/ui/uicodeedit.cpp b/src/eepp/ui/uicodeeditor.cpp similarity index 54% rename from src/eepp/ui/uicodeedit.cpp rename to src/eepp/ui/uicodeeditor.cpp index 5957d591a..7cc6c27dc 100644 --- a/src/eepp/ui/uicodeedit.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -2,22 +2,24 @@ #include #include #include -#include +#include #include +#include #include #include namespace EE { namespace UI { -UICodeEdit* UICodeEdit::New() { - return eeNew( UICodeEdit, () ); +UICodeEditor* UICodeEditor::New() { + return eeNew( UICodeEditor, () ); } -UICodeEdit::UICodeEdit() : - UIWidget( "codeedit" ), +UICodeEditor::UICodeEditor() : + UIWidget( "codeeditor" ), mFont( FontManager::instance()->getByName( "monospace" ) ), mDirtyEditor( false ), mCursorVisible( false ), + mMouseDown( false ), mTabWidth( 4 ), mLastColOffset( 0 ), mMouseWheelScroll( 50 ) { @@ -29,24 +31,24 @@ UICodeEdit::UICodeEdit() : subscribeScheduledUpdate(); } -UICodeEdit::~UICodeEdit() { +UICodeEditor::~UICodeEditor() { mDoc.unregisterClient( *this ); } -Uint32 UICodeEdit::getType() const { +Uint32 UICodeEditor::getType() const { return UI_TYPE_CODEEDIT; } -bool UICodeEdit::isType( const Uint32& type ) const { +bool UICodeEditor::isType( const Uint32& type ) const { return type == getType() || UIWidget::isType( type ); } -void UICodeEdit::setTheme( UITheme* Theme ) { +void UICodeEditor::setTheme( UITheme* Theme ) { UIWidget::setTheme( Theme ); setThemeSkin( Theme, "textedit" ); } -void UICodeEdit::draw() { +void UICodeEditor::draw() { if ( mDirtyEditor ) { updateEditor(); } @@ -54,25 +56,53 @@ void UICodeEdit::draw() { if ( mFont == NULL ) return; - Float charSize = getCharacterSize(); + Float charSize = PixelDensity::pxToDp( getCharacterSize() ); Float lineHeight = getLineHeight(); Vector2f start( mScreenPos.x + mRealPadding.Left, mScreenPos.y + mRealPadding.Top ); Vector2f startScroll( start - mScroll ); - std::pair lineRange = getVisibleLineRange(); - for ( int i = lineRange.first; i <= lineRange.second; i++ ) { - Text line( mDoc.line( i ), mFont, charSize ); - line.draw( startScroll.x, startScroll.y + lineHeight * i ); - } - Primitives primitives; TextPosition cursor( mDoc.getSelection().start() ); - primitives.setColor( Color( 255, 255, 255, 50 ) ); + primitives.setColor( Color( 25, 25, 25, 255 ) ); primitives.drawRectangle( Rectf( Vector2f( startScroll.x, startScroll.y + cursor.line() * lineHeight ), Sizef( mSize.getWidth(), lineHeight ) ) ); if ( mDoc.hasSelection() ) { + primitives.setColor( Color( 50, 50, 50, 255 ) ); + + TextRange selection = mDoc.getSelection( true ); + + for ( auto ln = selection.start().line(); ln <= selection.end().line(); ln++ ) { + const String& line = mDoc.line( ln ); + Rectf selRect; + selRect.Top = startScroll.y + ln * lineHeight; + selRect.Bottom = selRect.Top + lineHeight; + if ( selection.start().line() == ln ) { + selRect.Left = startScroll.x + getXOffsetCol( {ln, selection.start().column()} ); + if ( selection.end().line() == ln ) { + selRect.Right = startScroll.x + getXOffsetCol( {ln, selection.end().column()} ); + } else { + selRect.Right = + startScroll.x + getXOffsetCol( {ln, static_cast( line.length() )} ); + } + } else if ( selection.end().line() == ln ) { + selRect.Left = startScroll.x + getXOffsetCol( {ln, 0} ); + selRect.Right = startScroll.x + getXOffsetCol( {ln, selection.end().column()} ); + } else { + selRect.Left = startScroll.x + getXOffsetCol( {ln, 0} ); + selRect.Right = + startScroll.x + getXOffsetCol( {ln, static_cast( line.length() )} ); + } + + primitives.drawRectangle( selRect ); + } + } + + std::pair lineRange = getVisibleLineRange(); + for ( int i = lineRange.first; i <= lineRange.second; i++ ) { + Text line( mDoc.line( i ), mFont, charSize ); + line.draw( startScroll.x, startScroll.y + lineHeight * i ); } if ( mCursorVisible ) { @@ -85,7 +115,7 @@ void UICodeEdit::draw() { } } -void UICodeEdit::scheduledUpdate( const Time& ) { +void UICodeEditor::scheduledUpdate( const Time& ) { if ( hasFocus() ) { if ( mBlinkTimer.getElapsedTime().asSeconds() > 0.5f ) { mCursorVisible = !mCursorVisible; @@ -95,18 +125,18 @@ void UICodeEdit::scheduledUpdate( const Time& ) { } } -void UICodeEdit::reset() {} +void UICodeEditor::reset() {} -void UICodeEdit::loadFromFile( const std::string& path ) { +void UICodeEditor::loadFromFile( const std::string& path ) { mDoc.loadFromPath( path ); invalidateEditor(); } -Font* UICodeEdit::getFont() const { +Font* UICodeEditor::getFont() const { return mFont; } -void UICodeEdit::setFont( Font* font ) { +void UICodeEditor::setFont( Font* font ) { if ( mFont != font ) { mFont = font; invalidateDraw(); @@ -114,35 +144,48 @@ void UICodeEdit::setFont( Font* font ) { } } -const Uint32& UICodeEdit::getTabWidth() const { +void UICodeEditor::setFontSize( Float dpSize ) { + if ( mFontStyleConfig.CharacterSize != dpSize ) { + mFontStyleConfig.CharacterSize = dpSize; + invalidateDraw(); + } +} + +const Uint32& UICodeEditor::getTabWidth() const { return mTabWidth; } -void UICodeEdit::setTabWidth( const Uint32& tabWidth ) { +void UICodeEditor::setTabWidth( const Uint32& tabWidth ) { if ( mTabWidth != tabWidth ) { mTabWidth = tabWidth; mDoc.setTabWidth( mTabWidth ); } } -const Float& UICodeEdit::getMouseWheelScroll() const { +const Float& UICodeEditor::getMouseWheelScroll() const { return mMouseWheelScroll; } -void UICodeEdit::setMouseWheelScroll( const Float& mouseWheelScroll ) { +void UICodeEditor::setMouseWheelScroll( const Float& mouseWheelScroll ) { mMouseWheelScroll = mouseWheelScroll; } -void UICodeEdit::invalidateEditor() { +void UICodeEditor::invalidateEditor() { mDirtyEditor = true; } -Uint32 UICodeEdit::onTextInput( const TextInputEvent& event ) { +Uint32 UICodeEditor::onFocusLoss() { + mMouseDown = false; + mCursorVisible = false; + return UIWidget::onFocusLoss(); +} + +Uint32 UICodeEditor::onTextInput( const TextInputEvent& event ) { mDoc.textInput( event.getText() ); return 1; } -Uint32 UICodeEdit::onKeyDown( const KeyEvent& event ) { +Uint32 UICodeEditor::onKeyDown( const KeyEvent& event ) { switch ( event.getKeyCode() ) { case KEY_BACKSPACE: { mDoc.deleteToPreviousChar(); @@ -160,21 +203,45 @@ Uint32 UICodeEdit::onKeyDown( const KeyEvent& event ) { break; } case KEY_UP: { - mDoc.moveToPreviousLine( mLastColOffset ); + if ( event.getMod() & KEYMOD_LSHIFT ) { + mDoc.selectToPreviousLine( mLastColOffset ); + } else { + mDoc.moveToPreviousLine( mLastColOffset ); + } break; } case KEY_DOWN: { - mDoc.moveToNextLine( mLastColOffset ); + if ( event.getMod() & KEYMOD_LSHIFT ) { + mDoc.selectToNextLine( mLastColOffset ); + } else { + mDoc.moveToNextLine( mLastColOffset ); + } break; } case KEY_LEFT: { - mDoc.moveToPreviousChar(); - updateLastColumnOffset(); + if ( ( event.getMod() & KEYMOD_LSHIFT ) && ( event.getMod() & KEYMOD_LCTRL ) ) { + mDoc.selectToPreviousWord(); + } else if ( event.getMod() & KEYMOD_LSHIFT ) { + mDoc.selectToPreviousChar(); + } else if ( event.getMod() & KEYMOD_LCTRL ) { + mDoc.moveToPreviousWord(); + } else { + mDoc.moveToPreviousChar(); + updateLastColumnOffset(); + } break; } case KEY_RIGHT: { - mDoc.moveToNextChar(); - updateLastColumnOffset(); + if ( ( event.getMod() & KEYMOD_LSHIFT ) && ( event.getMod() & KEYMOD_LCTRL ) ) { + mDoc.selectToNextWord(); + } else if ( event.getMod() & KEYMOD_LSHIFT ) { + mDoc.selectToNextChar(); + } else if ( event.getMod() & KEYMOD_LCTRL ) { + mDoc.moveToNextWord(); + } else { + mDoc.moveToNextChar(); + updateLastColumnOffset(); + } break; } case KEY_HOME: { @@ -207,6 +274,24 @@ Uint32 UICodeEdit::onKeyDown( const KeyEvent& event ) { } break; } + case KEY_V: { + if ( event.getMod() & KEYMOD_LCTRL ) { + mDoc.textInput( getUISceneNode()->getWindow()->getClipboard()->getText() ); + } + break; + } + case KEY_C: { + if ( event.getMod() & KEYMOD_LCTRL ) { + getUISceneNode()->getWindow()->getClipboard()->setText( mDoc.getSelectedText() ); + } + break; + } + case KEY_X: { + if ( event.getMod() & KEYMOD_LCTRL ) { + mDoc.deleteSelection(); + } + break; + } default: break; } @@ -214,7 +299,7 @@ Uint32 UICodeEdit::onKeyDown( const KeyEvent& event ) { return 1; } -TextPosition UICodeEdit::resolveScreenPosition( const Vector2f& position ) const { +TextPosition UICodeEditor::resolveScreenPosition( const Vector2f& position ) const { Vector2f localPos( position ); worldToNode( localPos ); localPos = PixelDensity::dpToPx( localPos ); @@ -226,12 +311,12 @@ TextPosition UICodeEdit::resolveScreenPosition( const Vector2f& position ) const return TextPosition( line, getColFromXOffset( line, localPos.x ) ); } -Vector2f UICodeEdit::getViewPortLineCount() const { +Vector2f UICodeEditor::getViewPortLineCount() const { return Vector2f( eefloor( mSize.getWidth() / getGlyphWidth() ), eefloor( mSize.getHeight() / getLineHeight() ) ); } -Sizef UICodeEdit::getMaxScroll() const { +Sizef UICodeEditor::getMaxScroll() const { Vector2f vplc( getViewPortLineCount() ); return Sizef( 0 /*TODO: Implement*/, vplc.y > mDoc.lineCount() - 1 @@ -239,15 +324,28 @@ Sizef UICodeEdit::getMaxScroll() const { : ( mDoc.lineCount() - getViewPortLineCount().y ) * getLineHeight() ); } -Uint32 UICodeEdit::onMouseDown( const Vector2i& position, const Uint32& flags ) { - if ( flags & EE_BUTTON_LMASK ) { +Uint32 UICodeEditor::onMouseDown( const Vector2i& position, const Uint32& flags ) { + if ( !mMouseDown && ( flags & EE_BUTTON_LMASK ) ) { + mMouseDown = true; mDoc.setSelection( resolveScreenPosition( position.asFloat() ) ); } return UIWidget::onMouseDown( position, flags ); } -Uint32 UICodeEdit::onMouseUp( const Vector2i& position, const Uint32& flags ) { - if ( flags & EE_BUTTON_WDMASK ) { +Uint32 UICodeEditor::onMouseMove( const Vector2i& position, const Uint32& flags ) { + if ( ( flags & EE_BUTTON_LMASK ) ) { + mMouseDown = true; + TextRange selection = mDoc.getSelection(); + selection.setStart( resolveScreenPosition( position.asFloat() ) ); + mDoc.setSelection( selection ); + } + return UIWidget::onMouseMove( position, flags ); +} + +Uint32 UICodeEditor::onMouseUp( const Vector2i& position, const Uint32& flags ) { + if ( flags & EE_BUTTON_LMASK ) { + mMouseDown = false; + } else if ( flags & EE_BUTTON_WDMASK ) { mScroll.y += PixelDensity::dpToPx( mMouseWheelScroll ); mScroll.y = eemin( mScroll.y, getMaxScroll().y ); invalidateDraw(); @@ -259,40 +357,38 @@ Uint32 UICodeEdit::onMouseUp( const Vector2i& position, const Uint32& flags ) { return UIWidget::onMouseUp( position, flags ); } -void UICodeEdit::onSizeChange() { +void UICodeEditor::onSizeChange() { UIWidget::onSizeChange(); mScroll = {0, 0}; invalidateEditor(); } -void UICodeEdit::onPaddingChange() { +void UICodeEditor::onPaddingChange() { UIWidget::onPaddingChange(); invalidateEditor(); } -void UICodeEdit::updateEditor() { +void UICodeEditor::updateEditor() { scrollToMakeVisible( mDoc.getSelection().start() ); mDirtyEditor = false; } -void UICodeEdit::onDocumentTextChanged() { +void UICodeEditor::onDocumentTextChanged() { invalidateDraw(); } -void UICodeEdit::onDocumentCursorChange( const Doc::TextPosition& ) { - mCursorVisible = true; - mBlinkTimer.restart(); +void UICodeEditor::onDocumentCursorChange( const Doc::TextPosition& ) { + resetCursor(); invalidateEditor(); invalidateDraw(); } -void UICodeEdit::onDocumentSelectionChange( const Doc::TextRange& ) { - mCursorVisible = true; - mBlinkTimer.restart(); +void UICodeEditor::onDocumentSelectionChange( const Doc::TextRange& ) { + resetCursor(); invalidateDraw(); } -std::pair UICodeEdit::getVisibleLineRange() { +std::pair UICodeEditor::getVisibleLineRange() { Float lineHeight = getLineHeight(); Float minLine = eemax( 0.f, eefloor( mScroll.y / lineHeight ) ); Float maxLine = eemin( mDoc.lineCount() - 1.f, @@ -300,12 +396,12 @@ std::pair UICodeEdit::getVisibleLineRange() { return std::make_pair( (int)minLine, (int)maxLine ); } -int UICodeEdit::getVisibleLinesCount() { +int UICodeEditor::getVisibleLinesCount() { auto lines = getVisibleLineRange(); return lines.second - lines.first; } -void UICodeEdit::scrollToMakeVisible( const TextPosition& position ) { +void UICodeEditor::scrollToMakeVisible( const TextPosition& position ) { Float lineHeight = getLineHeight(); Float min = lineHeight * ( eemax( 0, position.line() - 1 ) ); Float max = lineHeight * ( position.line() + 2 ) - mSize.getHeight(); @@ -314,7 +410,7 @@ void UICodeEdit::scrollToMakeVisible( const TextPosition& position ) { invalidateDraw(); } -Float UICodeEdit::getXOffsetCol( const TextPosition& position ) const { +Float UICodeEditor::getXOffsetCol( const TextPosition& position ) const { const String& line = mDoc.line( position.line() ); Float glyphWidth = getGlyphWidth(); Float x = 0; @@ -328,7 +424,7 @@ Float UICodeEdit::getXOffsetCol( const TextPosition& position ) const { return x; } -Int64 UICodeEdit::getColFromXOffset( Int64 _line, const Float& offset ) const { +Int64 UICodeEditor::getColFromXOffset( Int64 _line, const Float& offset ) const { if ( offset <= 0 ) return 0; TextPosition pos = mDoc.sanitizePosition( TextPosition( _line, 0 ) ); @@ -348,20 +444,25 @@ Int64 UICodeEdit::getColFromXOffset( Int64 _line, const Float& offset ) const { return line.size() - 1; } -Float UICodeEdit::getLineHeight() const { +Float UICodeEditor::getLineHeight() const { return mFont->getLineSpacing( getCharacterSize() ); } -Float UICodeEdit::getCharacterSize() const { - return PixelDensity::pxToDp( mFontStyleConfig.getFontCharacterSize() ); +Float UICodeEditor::getCharacterSize() const { + return PixelDensity::dpToPx( mFontStyleConfig.getFontCharacterSize() ); } -Float UICodeEdit::getGlyphWidth() const { +Float UICodeEditor::getGlyphWidth() const { return mFont->getGlyph( KEY_SPACE, getCharacterSize(), false ).advance; } -void UICodeEdit::updateLastColumnOffset() { +void UICodeEditor::updateLastColumnOffset() { mLastColOffset = mDoc.getAbsolutePosition( mDoc.getSelection().start() ).column(); } +void UICodeEditor::resetCursor() { + mCursorVisible = true; + mBlinkTimer.restart(); +} + }} // namespace EE::UI diff --git a/src/eepp/ui/uiwidgetcreator.cpp b/src/eepp/ui/uiwidgetcreator.cpp index 94a767dd8..541810496 100644 --- a/src/eepp/ui/uiwidgetcreator.cpp +++ b/src/eepp/ui/uiwidgetcreator.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -77,7 +77,7 @@ void UIWidgetCreator::createBaseWidgetList() { registeredWidget["gridlayout"] = UIGridLayout::New; registeredWidget["layout"] = UILayout::New; registeredWidget["viewpager"] = UIViewPager::New; - registeredWidget["codeedit"] = UICodeEdit ::New; + registeredWidget["codeeditor"] = UICodeEditor ::New; registeredWidget["hbox"] = UILinearLayout::NewHorizontal; registeredWidget["vbox"] = UILinearLayout::NewVertical; diff --git a/src/examples/ui_hello_world/ui_hello_world.cpp b/src/examples/ui_hello_world/ui_hello_world.cpp index f073d094c..abdf58007 100644 --- a/src/examples/ui_hello_world/ui_hello_world.cpp +++ b/src/examples/ui_hello_world/ui_hello_world.cpp @@ -37,10 +37,15 @@ EE_MAIN_FUNC int main( int, char** ) { doc.insert( TextPosition( 31, 2 ), String( "hexNumber" ) ); doc.print();*/ // return EXIT_SUCCESS; + //Display* currentDisplay = Engine::instance()->getDisplayManager()->getDisplayIndex( 0 ); + Float pixelDensity = 1; // currentDisplay->getPixelDensity(); + win = Engine::instance()->createWindow( WindowSettings( 640, 480, "eepp - UI Hello World" ), ContextSettings( true ) ); if ( win->isOpen() ) { + PixelDensity::setPixelDensity( eemax( win->getScale(), pixelDensity ) ); + // Load a font to use as the default font in our UI. FontTrueType* font = FontTrueType::New( "NotoSans-Regular", "assets/fonts/NotoSans-Regular.ttf" ); @@ -68,10 +73,11 @@ EE_MAIN_FUNC int main( int, char** ) { layout_width="match_parent" layout_height="wrap_content" text="Hello, I am a PushButton" /> - + layout_weight="1" + /> )xml" ); @@ -93,7 +99,7 @@ EE_MAIN_FUNC int main( int, char** ) { } )css" ); - uiSceneNode->find( "code_edit" ) + uiSceneNode->find( "code_edit" ) ->loadFromFile( "/home/downloads/textposition.hpp" ); win->runMainLoop( &mainLoop );