| 76 | } |
| 77 | |
| 78 | int Entry::GetPositionFromMouseX( int mouse_pos_x ) { |
| 79 | const std::string& font_name( Context::Get().GetEngine().GetProperty<std::string>( "FontName", shared_from_this() ) ); |
| 80 | unsigned int font_size( Context::Get().GetEngine().GetProperty<unsigned int>( "FontSize", shared_from_this() ) ); |
| 81 | const sf::Font& font( *Context::Get().GetEngine().GetResourceManager().GetFont( font_name ) ); |
| 82 | float text_padding( Context::Get().GetEngine().GetProperty<float>( "Padding", shared_from_this() ) ); |
| 83 | |
| 84 | std::u32string string( m_visible_string.begin(), m_visible_string.end() ); |
| 85 | |
| 86 | auto text_start = GetAllocation().position.x + text_padding; |
| 87 | auto last_delta = std::fabs( text_start - static_cast<float>( mouse_pos_x ) ); |
| 88 | int cursor_position = 0; |
| 89 | auto length = static_cast<int>( string.size() ); |
| 90 | |
| 91 | for( cursor_position = 0; cursor_position < length; cursor_position++ ) { |
| 92 | auto text_length = Context::Get().GetEngine().GetTextStringMetrics( string.substr( 0, static_cast<std::size_t>( cursor_position + 1 ) ), font, font_size ).x; |
| 93 | auto new_delta = std::fabs( text_start + text_length - static_cast<float>( mouse_pos_x ) ); |
| 94 | if( new_delta < last_delta ) { |
| 95 | last_delta = new_delta; |
| 96 | } |
| 97 | else { |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return m_visible_offset + cursor_position; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void Entry::RecalculateVisibleString() const { |
nothing calls this directly
no test coverage detected