From fe1efd15c203f09ff72544be8311f6ce82e9f2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 18 Jul 2020 01:40:56 -0300 Subject: [PATCH] Some minor refactor for the UI Models code. --- include/eepp/ui/abstract/uiabstractview.hpp | 12 +- include/eepp/ui/models/model.hpp | 103 ++++++++++++++++++ .../modeleditingdelegate.hpp | 6 +- .../ui/{abstract => models}/modelindex.hpp | 4 +- .../{abstract => models}/modelselection.hpp | 10 +- .../model.hpp => models/variant.hpp} | 97 +---------------- premake4.lua | 1 + premake5.lua | 1 + projects/android-project/app/jni/eepp.mk | 3 +- projects/linux/ee.creator.user | 4 +- projects/linux/ee.files | 22 ++-- src/eepp/ui/{abstract => models}/model.cpp | 6 +- .../{abstract => models}/modelselection.cpp | 6 +- src/eepp/ui/uicodeeditor.cpp | 27 ++--- src/eepp/ui/uitreeview.cpp | 5 +- src/tests/ui_perf_test/ui_perf_test.cpp | 2 +- 16 files changed, 166 insertions(+), 143 deletions(-) create mode 100644 include/eepp/ui/models/model.hpp rename include/eepp/ui/{abstract => models}/modeleditingdelegate.hpp (88%) rename include/eepp/ui/{abstract => models}/modelindex.hpp (93%) rename include/eepp/ui/{abstract => models}/modelselection.hpp (91%) rename include/eepp/ui/{abstract/model.hpp => models/variant.hpp} (50%) rename src/eepp/ui/{abstract => models}/model.cpp (89%) rename src/eepp/ui/{abstract => models}/modelselection.cpp (92%) diff --git a/include/eepp/ui/abstract/uiabstractview.hpp b/include/eepp/ui/abstract/uiabstractview.hpp index 5739af215..f65d1bcb4 100644 --- a/include/eepp/ui/abstract/uiabstractview.hpp +++ b/include/eepp/ui/abstract/uiabstractview.hpp @@ -1,15 +1,17 @@ #ifndef EE_UI_UIABSTRACTVIEW_HPP #define EE_UI_UIABSTRACTVIEW_HPP -#include -#include -#include +#include +#include +#include #include #include +using namespace EE::UI::Models; + namespace EE { namespace UI { namespace Abstract { -class ModelEvent : public Event { +class EE_API ModelEvent : public Event { public: ModelEvent( Model* model, const ModelIndex& index, Node* node ) : Event( node, Event::OnModelEvent ), model( model ), index( index ) {} @@ -52,7 +54,7 @@ class EE_API UIAbstractView : public UIScrollableWidget { void setOnSelection( const std::function& onSelection ); protected: - friend class Model; + friend class EE::UI::Models::Model; virtual void onModelUpdate( unsigned flags ); diff --git a/include/eepp/ui/models/model.hpp b/include/eepp/ui/models/model.hpp new file mode 100644 index 000000000..5e2b4f75f --- /dev/null +++ b/include/eepp/ui/models/model.hpp @@ -0,0 +1,103 @@ +#ifndef EE_UI_MODEL_MODEL_HPP +#define EE_UI_MODEL_MODEL_HPP + +#include +#include +#include +#include +#include + +using namespace EE::Graphics; +using namespace EE::Math; + +namespace EE { namespace UI { namespace Abstract { +class UIAbstractView; +}}} // namespace EE::UI::Abstract + +using namespace EE::UI::Abstract; + +namespace EE { namespace UI { namespace Models { + +enum class SortOrder { None, Ascending, Descending }; + +class EE_API Model { + public: + enum UpdateFlag { + DontInvalidateIndexes = 0, + InvalidateAllIndexes = 1 << 0, + }; + + enum class Role { + Display, + Icon, + }; + + virtual ~Model(){}; + + virtual size_t rowCount( const ModelIndex& = ModelIndex() ) const = 0; + + virtual size_t columnCount( const ModelIndex& = ModelIndex() ) const = 0; + + virtual std::string columnName( const size_t& /*column*/ ) const { return {}; } + + virtual Variant data( const ModelIndex&, Role = Role::Display ) const = 0; + + virtual void update() = 0; + + virtual ModelIndex parentIndex( const ModelIndex& ) const { return {}; } + + virtual ModelIndex index( int row, int column = 0, const ModelIndex& = ModelIndex() ) const { + return createIndex( row, column ); + } + + virtual ModelIndex sibling( int row, int column, const ModelIndex& parent ) const; + + virtual void setData( const ModelIndex&, const Variant& ) {} + + virtual size_t treeColumn() const { return 0; } + + virtual bool acceptsDrag( const ModelIndex&, const std::string& dataType ); + + virtual bool isColumnSortable( const size_t& /*columnIndex*/ ) const { return true; } + + virtual std::string dragDataType() const { return {}; } + + bool isValid( const ModelIndex& index ) const { + auto parentIndex = this->parentIndex( index ); + return index.row() >= 0 && index.row() < (Int64)rowCount( parentIndex ) && + index.column() >= 0 && index.column() < (Int64)columnCount( parentIndex ); + } + + virtual int keyColumn() const { return -1; } + + virtual SortOrder sortOrder() const { return SortOrder::None; } + + virtual void setKeyColumnAndSortOrder( const size_t& /*column*/, const SortOrder& /*order*/ ) {} + + void registerView( UIAbstractView* ); + + void unregisterView( UIAbstractView* ); + + void setOnUpdate( const std::function& onUpdate ); + + protected: + Model(){}; + + void forEachView( std::function ); + + void onModelUpdate( unsigned flags = UpdateFlag::InvalidateAllIndexes ); + + ModelIndex createIndex( int row, int column, const void* data = nullptr ) const; + + private: + std::unordered_set mViews; + std::function mOnUpdate; +}; + +inline ModelIndex ModelIndex::parent() const { + return mModel ? mModel->parentIndex( *this ) : ModelIndex(); +} + +}}} // namespace EE::UI::Model + +#endif // EE_UI_MODEL_MODEL_HPP diff --git a/include/eepp/ui/abstract/modeleditingdelegate.hpp b/include/eepp/ui/models/modeleditingdelegate.hpp similarity index 88% rename from include/eepp/ui/abstract/modeleditingdelegate.hpp rename to include/eepp/ui/models/modeleditingdelegate.hpp index d95e773ec..6205aa61a 100644 --- a/include/eepp/ui/abstract/modeleditingdelegate.hpp +++ b/include/eepp/ui/models/modeleditingdelegate.hpp @@ -1,11 +1,11 @@ #ifndef EE_UI_MODELEDITINGDELEGATE_HPP #define EE_UI_MODELEDITINGDELEGATE_HPP -#include +#include #include #include -namespace EE { namespace UI { namespace Abstract { +namespace EE { namespace UI { namespace Models { class EE_API ModelEditingDelegate { public: @@ -43,6 +43,6 @@ class EE_API ModelEditingDelegate { UIWidget* mWidget; }; -}}} // namespace EE::UI::Abstract +}}} // namespace EE::UI::Model #endif // EE_UI_MODELEDITINGDELEGATE_HPP diff --git a/include/eepp/ui/abstract/modelindex.hpp b/include/eepp/ui/models/modelindex.hpp similarity index 93% rename from include/eepp/ui/abstract/modelindex.hpp rename to include/eepp/ui/models/modelindex.hpp index 36670bb86..b5bb32fea 100644 --- a/include/eepp/ui/abstract/modelindex.hpp +++ b/include/eepp/ui/models/modelindex.hpp @@ -3,7 +3,7 @@ #include -namespace EE { namespace UI { namespace Abstract { +namespace EE { namespace UI { namespace Models { class Model; @@ -45,6 +45,6 @@ class EE_API ModelIndex { mModel( &model ), mRow( row ), mColumn( column ), mData( internalData ) {} }; -}}} // namespace EE::UI::Abstract +}}} // namespace EE::UI::Model #endif // EE_UI_MODEL_MODELINDEX_HPP diff --git a/include/eepp/ui/abstract/modelselection.hpp b/include/eepp/ui/models/modelselection.hpp similarity index 91% rename from include/eepp/ui/abstract/modelselection.hpp rename to include/eepp/ui/models/modelselection.hpp index 2153f64a7..0841fe8ee 100644 --- a/include/eepp/ui/abstract/modelselection.hpp +++ b/include/eepp/ui/models/modelselection.hpp @@ -2,13 +2,17 @@ #define EE_UI_MODEL_MODELSELECTION_HPP #include -#include +#include #include #include namespace EE { namespace UI { namespace Abstract { - class UIAbstractView; +}}} // namespace EE::UI::Abstract + +using namespace EE::UI::Abstract; + +namespace EE { namespace UI { namespace Models { class EE_API ModelSelection { public: @@ -63,6 +67,6 @@ class EE_API ModelSelection { std::vector mIndexes; }; -}}} // namespace EE::UI::Abstract +}}} // namespace EE::UI::Model #endif // EE_UI_MODEL_MODELSELECTION_HPP diff --git a/include/eepp/ui/abstract/model.hpp b/include/eepp/ui/models/variant.hpp similarity index 50% rename from include/eepp/ui/abstract/model.hpp rename to include/eepp/ui/models/variant.hpp index 65ae66ce5..1c8f37870 100644 --- a/include/eepp/ui/abstract/model.hpp +++ b/include/eepp/ui/models/variant.hpp @@ -1,24 +1,17 @@ -#ifndef EE_UI_MODEL_MODEL_HPP -#define EE_UI_MODEL_MODEL_HPP +#ifndef EE_UI_MODEL_VARIANT_HPP +#define EE_UI_MODEL_VARIANT_HPP #include #include #include -#include -#include #include -#include using namespace EE::Graphics; using namespace EE::Math; -namespace EE { namespace UI { namespace Abstract { +namespace EE { namespace UI { namespace Models { -enum class SortOrder { None, Ascending, Descending }; - -class UIAbstractView; - -class Variant { +class EE_API Variant { public: enum class Type { Invalid, @@ -101,84 +94,6 @@ class Variant { bool mOwnsObject{false}; }; -class EE_API Model { - public: - enum UpdateFlag { - DontInvalidateIndexes = 0, - InvalidateAllIndexes = 1 << 0, - }; +}}} - enum class Role { - Display, - Icon, - }; - - virtual ~Model(){}; - - virtual size_t rowCount( const ModelIndex& = ModelIndex() ) const = 0; - - virtual size_t columnCount( const ModelIndex& = ModelIndex() ) const = 0; - - virtual std::string columnName( const size_t& /*column*/ ) const { return {}; } - - virtual Variant data( const ModelIndex&, Role = Role::Display ) const = 0; - - virtual void update() = 0; - - virtual ModelIndex parentIndex( const ModelIndex& ) const { return {}; } - - virtual ModelIndex index( int row, int column = 0, const ModelIndex& = ModelIndex() ) const { - return createIndex( row, column ); - } - - virtual ModelIndex sibling( int row, int column, const ModelIndex& parent ) const; - - virtual void setData( const ModelIndex&, const Variant& ) {} - - virtual size_t treeColumn() const { return 0; } - - virtual bool acceptsDrag( const ModelIndex&, const std::string& dataType ); - - virtual bool isColumnSortable( const size_t& /*columnIndex*/ ) const { return true; } - - virtual std::string dragDataType() const { return {}; } - - bool isValid( const ModelIndex& index ) const { - auto parentIndex = this->parentIndex( index ); - return index.row() >= 0 && index.row() < (Int64)rowCount( parentIndex ) && - index.column() >= 0 && index.column() < (Int64)columnCount( parentIndex ); - } - - virtual int keyColumn() const { return -1; } - - virtual SortOrder sortOrder() const { return SortOrder::None; } - - virtual void setKeyColumnAndSortOrder( const size_t& /*column*/, const SortOrder& /*order*/ ) {} - - void registerView( UIAbstractView* ); - - void unregisterView( UIAbstractView* ); - - void setOnUpdate( const std::function& onUpdate ); - - protected: - Model(){}; - - void forEachView( std::function ); - - void onModelUpdate( unsigned flags = UpdateFlag::InvalidateAllIndexes ); - - ModelIndex createIndex( int row, int column, const void* data = nullptr ) const; - - private: - std::unordered_set mViews; - std::function mOnUpdate; -}; - -inline ModelIndex ModelIndex::parent() const { - return mModel ? mModel->parentIndex( *this ) : ModelIndex(); -} - -}}} // namespace EE::UI::Abstract - -#endif // EE_UI_MODEL_MODEL_HPP +#endif // EE_UI_MODEL_VARIANT_HPP diff --git a/premake4.lua b/premake4.lua index c54c0060e..eb89202af 100644 --- a/premake4.lua +++ b/premake4.lua @@ -749,6 +749,7 @@ function build_eepp( build_name ) "src/eepp/scene/actions/*.cpp", "src/eepp/ui/*.cpp", "src/eepp/ui/abstract/*.cpp", + "src/eepp/ui/models/*.cpp", "src/eepp/ui/css/*.cpp", "src/eepp/ui/doc/*.cpp", "src/eepp/ui/tools/*.cpp", diff --git a/premake5.lua b/premake5.lua index 484a0abfa..af90a8285 100644 --- a/premake5.lua +++ b/premake5.lua @@ -470,6 +470,7 @@ function build_eepp( build_name ) "src/eepp/scene/actions/*.cpp", "src/eepp/ui/*.cpp", "src/eepp/ui/abstract/*.cpp", + "src/eepp/ui/models/*.cpp", "src/eepp/ui/css/*.cpp", "src/eepp/ui/doc/*.cpp", "src/eepp/ui/tools/*.cpp", diff --git a/projects/android-project/app/jni/eepp.mk b/projects/android-project/app/jni/eepp.mk index c59cb45d1..1dcff4de5 100644 --- a/projects/android-project/app/jni/eepp.mk +++ b/projects/android-project/app/jni/eepp.mk @@ -82,7 +82,8 @@ CODE_SRCS := \ ui/*.cpp \ ui/css/*.cpp \ ui/doc/*.cpp \ - ui/model/*.cpp \ + ui/abstract/*.cpp \ + ui/models/*.cpp \ ui/tools/*.cpp \ maps/*.cpp \ maps/mapeditor/*.cpp diff --git a/projects/linux/ee.creator.user b/projects/linux/ee.creator.user index 2f5fb63c6..94228d6c7 100644 --- a/projects/linux/ee.creator.user +++ b/projects/linux/ee.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -89,7 +89,7 @@ {6d057187-158a-4883-8d5b-d470a6b6b025} 10 0 - 19 + 18 ../../make/linux diff --git a/projects/linux/ee.files b/projects/linux/ee.files index 9a7ae73fb..85ef64c3c 100644 --- a/projects/linux/ee.files +++ b/projects/linux/ee.files @@ -295,14 +295,14 @@ ../../include/eepp/thirdparty/chipmunk/cpSpatialIndex.h ../../include/eepp/thirdparty/chipmunk/cpVect.h ../../include/eepp/thirdparty/PlusCallback/callback.hpp -../../include/eepp/ui/abstract/model.hpp +../../include/eepp/ui/models/model.hpp ../../include/eepp/ui/abstract/uiabstracttableview.hpp ../../include/eepp/ui/abstract/uiabstractview.hpp ../../include/eepp/ui/base.hpp ../../include/eepp/ui.hpp -../../include/eepp/ui/abstract/modeleditingdelegate.hpp -../../include/eepp/ui/abstract/modelindex.hpp -../../include/eepp/ui/abstract/modelselection.hpp +../../include/eepp/ui/models/modeleditingdelegate.hpp +../../include/eepp/ui/models/modelindex.hpp +../../include/eepp/ui/models/modelselection.hpp ../../include/eepp/ui/border.hpp ../../include/eepp/ui/css/animationdefinition.hpp ../../include/eepp/ui/css/drawableimageparser.hpp @@ -338,9 +338,9 @@ ../../include/eepp/ui/doc/undostack.hpp ../../include/eepp/ui/keyboardshortcut.hpp ../../include/eepp/ui/marginmove/scale.hpp -../../include/eepp/ui/abstract/model.hpp -../../include/eepp/ui/abstract/modelindex.hpp -../../include/eepp/ui/abstract/modelselection.hpp +../../include/eepp/ui/models/model.hpp +../../include/eepp/ui/models/modelindex.hpp +../../include/eepp/ui/models/modelselection.hpp ../../include/eepp/ui/tools/textureatlaseditor.hpp ../../include/eepp/ui/tools/uicodeeditorsplitter.hpp ../../include/eepp/ui/tools/uicolorpicker.hpp @@ -770,8 +770,8 @@ ../../src/eepp/system/virtualfilesystem.cpp ../../src/eepp/system/zip.cpp ../../src/eepp/ui/abstract/filesystemmodel.hpp -../../src/eepp/ui/abstract/model.cpp -../../src/eepp/ui/abstract/modelselection.cpp +../../src/eepp/ui/models/model.cpp +../../src/eepp/ui/models/modelselection.cpp ../../src/eepp/ui/abstract/uiabstracttableview.cpp ../../src/eepp/ui/abstract/uiabstractview.cpp ../../src/eepp/ui/border.cpp @@ -804,8 +804,8 @@ ../../src/eepp/ui/doc/textdocument.cpp ../../src/eepp/ui/doc/undostack.cpp ../../src/eepp/ui/keyboardshortcut.cpp -../../src/eepp/ui/abstract/model.cpp -../../src/eepp/ui/abstract/modelselection.cpp +../../src/eepp/ui/models/model.cpp +../../src/eepp/ui/models/modelselection.cpp ../../src/eepp/ui/tools/textureatlaseditor.cpp ../../src/eepp/ui/tools/textureatlasnew.cpp ../../src/eepp/ui/tools/textureatlasnew.hpp diff --git a/src/eepp/ui/abstract/model.cpp b/src/eepp/ui/models/model.cpp similarity index 89% rename from src/eepp/ui/abstract/model.cpp rename to src/eepp/ui/models/model.cpp index bafd03d22..c0b3c3ce0 100644 --- a/src/eepp/ui/abstract/model.cpp +++ b/src/eepp/ui/models/model.cpp @@ -1,7 +1,7 @@ -#include #include +#include -namespace EE { namespace UI { namespace Abstract { +namespace EE { namespace UI { namespace Models { void Model::onModelUpdate( unsigned flags ) { if ( mOnUpdate ) @@ -43,4 +43,4 @@ bool Model::acceptsDrag( const ModelIndex&, const std::string& ) { return false; } -}}} // namespace EE::UI::Abstract +}}} // namespace EE::UI::Model diff --git a/src/eepp/ui/abstract/modelselection.cpp b/src/eepp/ui/models/modelselection.cpp similarity index 92% rename from src/eepp/ui/abstract/modelselection.cpp rename to src/eepp/ui/models/modelselection.cpp index 5faeec7c2..96664530f 100644 --- a/src/eepp/ui/abstract/modelselection.cpp +++ b/src/eepp/ui/models/modelselection.cpp @@ -1,8 +1,8 @@ #include -#include +#include #include -namespace EE { namespace UI { namespace Abstract { +namespace EE { namespace UI { namespace Models { void ModelSelection::removeMatching( std::function filter ) { std::vector::iterator> toRemove; @@ -59,4 +59,4 @@ void ModelSelection::clear() { mView->notifySelectionChange(); } -}}} // namespace EE::UI::Abstract +}}} // namespace EE::UI::Model diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index 0584d7148..5696ce7dc 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -1839,26 +1839,23 @@ void UICodeEditor::drawWhitespaces( const std::pair& lineRange, cpoint->setColor( color ); for ( int index = lineRange.first; index <= lineRange.second; index++ ) { Vector2f position( {startScroll.x, startScroll.y + lineHeight * index} ); - auto& tokens = mHighlighter.getLine( index ); - for ( auto& token : tokens ) { - Float textWidth = getTextWidth( token.text ); - if ( position.x + textWidth >= mScreenPos.x && + const auto& text = mDoc->line( index ).getText(); + for ( size_t i = 0; i < text.size(); i++ ) { + if ( position.x + glyphW >= mScreenPos.x && position.x <= mScreenPos.x + mSize.getWidth() ) { - for ( size_t i = 0; i < token.text.size(); i++ ) { - if ( ' ' == token.text[i] ) { - cpoint->draw( Vector2f( position.x, position.y ) ); - position.x += glyphW; - } else if ( '\t' == token.text[i] ) { - adv->draw( Vector2f( position.x + tabCenter, position.y ) ); - position.x += tabWidth; - } else { - position.x += glyphW; - } + if ( ' ' == text[i] ) { + cpoint->draw( Vector2f( position.x, position.y ) ); + position.x += glyphW; + } else if ( '\t' == text[i] ) { + adv->draw( Vector2f( position.x + tabCenter, position.y ) ); + position.x += tabWidth; + } else { + position.x += glyphW; } } else if ( position.x > mScreenPos.x + mSize.getWidth() ) { break; } else { - position.x += textWidth; + position.x += glyphW; } } } diff --git a/src/eepp/ui/uitreeview.cpp b/src/eepp/ui/uitreeview.cpp index b8f7a5d2d..6fd8e7cd3 100644 --- a/src/eepp/ui/uitreeview.cpp +++ b/src/eepp/ui/uitreeview.cpp @@ -30,9 +30,8 @@ UITreeView::MetadataForIndex& UITreeView::getIndexMetadata( const ModelIndex& in if ( it != mViewMetadata.end() ) return it->second; auto newMetadata = MetadataForIndex(); - auto* ref = &newMetadata; mViewMetadata.insert( {index.data(), std::move( newMetadata )} ); - return *ref; + return mViewMetadata[index.data()]; } template void UITreeView::traverseTree( Callback callback ) const { @@ -183,8 +182,8 @@ UIWidget* UITreeView::createCell( UIWidget* rowWidget, const ModelIndex&, const if ( pos >= Vector2f::Zero && pos <= icon->getPixelsSize() ) { auto idx = mouseEvent->getNode()->getParent()->asType()->getCurIndex(); - auto& data = getIndexMetadata( idx ); if ( getModel()->rowCount( idx ) ) { + auto& data = getIndexMetadata( idx ); data.open = !data.open; createOrUpdateColumns(); } diff --git a/src/tests/ui_perf_test/ui_perf_test.cpp b/src/tests/ui_perf_test/ui_perf_test.cpp index 860b01701..d9b2893ab 100644 --- a/src/tests/ui_perf_test/ui_perf_test.cpp +++ b/src/tests/ui_perf_test/ui_perf_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include