Skip to content

Commit

Permalink
[SUTK] Added Text::removeRange
Browse files Browse the repository at this point in the history
 - It also merges adjacent lines if the caret was at zero'th column position
  • Loading branch information
ravi688 committed Aug 12, 2024
1 parent aaa3187 commit e11fe6c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
5 changes: 5 additions & 0 deletions sutk/include/sutk/Renderable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace SUTK

public:
Renderable(UIDriver& driver, RenderableContainer* container = NULL) noexcept;
virtual ~Renderable() = default;

// returns true, if GPU side data is out of sync with respect to the CPU side data, otherwise false
virtual bool isDirty() = 0;
Expand Down Expand Up @@ -59,11 +60,14 @@ namespace SUTK

public:
GfxDriverRenderable(UIDriver& driver, RenderableContainer* container = NULL) noexcept : Renderable(driver, container), m_handle(GFX_DRIVER_OBJECT_NULL_HANDLE) { }
virtual ~GfxDriverRenderable() = default;

// Implementation of Renderable
virtual bool isDirty() = 0;
virtual void update() = 0;

virtual void destroy() { }

void setClipRectGlobalCoords(const Rect2Df rect) noexcept;

// sets the clip rect in the local coordinates of the parent container of its renderable container
Expand All @@ -77,6 +81,7 @@ namespace SUTK
Geometry m_geometry;
protected:
GeometryRenderable(UIDriver& driver, RenderableContainer* container = NULL) noexcept : GfxDriverRenderable(driver, container), m_geometry(driver) { }
virtual ~GeometryRenderable() = default;
Geometry& getGeometry() noexcept { return m_geometry; }

// Implementation of GfxDriverRenderable
Expand Down
5 changes: 4 additions & 1 deletion sutk/include/sutk/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ namespace SUTK
// Implementation of Renderable::isDirty() and Renderable::update()
virtual bool isDirty() override;
virtual void update() override;

virtual void destroy() override;

// returns column (index of a glyph) given a coordinate (absisca) along the line length.
LineCountType getColPosFromCoord(f32 coord) noexcept;
Expand Down Expand Up @@ -215,11 +217,12 @@ namespace SUTK
void clear() noexcept;

LineText* createNewLine(Flags flags = Flags::After, LineCountType line = END_OF_TEXT) noexcept;
void removeLine(LineCountType line) noexcept;
LineText* getOrCreateLastLine() noexcept;
void append(const std::string& str) noexcept { insert(CursorPosition<LineCountType>::EndOfText(), str); }
LineText* getLine(LineCountType line) noexcept;
void insert(const CursorPosition<LineCountType>& position, const std::string& str) noexcept;
void remove(const CursorPosition<LineCountType>& position, LineCountType numChars) noexcept;
void removeRange(const CursorPosition<LineCountType>& start, const CursorPosition<LineCountType>& end) noexcept;
void set(const std::string& str) noexcept;
void enableClipping(bool isEnable = true) noexcept;
void setScrollDelta(Vec2Df delta) noexcept;
Expand Down
68 changes: 61 additions & 7 deletions sutk/source/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ namespace SUTK
m_isDataDirty = false;
}
}
void LineText::destroy()
{
_com_assert(getGfxDriverObjectHandle() != GFX_DRIVER_OBJECT_NULL_HANDLE);
getGfxDriver().destroyText(getGfxDriverObjectHandle());
setGfxDriverObjectHandle(GFX_DRIVER_OBJECT_NULL_HANDLE);
m_isPosDirty = false;
m_isDataDirty = false;
}
LineCountType LineText::getColPosFromCoord(f32 coord) noexcept
{
// LineText::getCoordFromColPos function does all the calculations based on the most recent data in SGE
Expand Down Expand Up @@ -196,6 +204,23 @@ namespace SUTK
return lineText;
}

void Text::removeLine(LineCountType line) noexcept
{
if((line + 1) >= m_lines.size())
return;

// destroy the Gfx driver side objects
// m_lines[line]->destroy();
// delete m_lines[line];
m_lines[line]->setData("");
// remove the line at index 'line'
m_lines.erase(com::GetIteratorFromIndex<std::vector, LineText*>(m_lines, line));

// shift the line at which the cursor points to and the lines succeeding it
for(std::size_t i = line; i < m_lines.size(); i++)
m_lines[i]->addPosition({ 0, -m_baselineHeight });
}

LineText* Text::getOrCreateLastLine() noexcept
{
// if there are no lines
Expand Down Expand Up @@ -265,16 +290,45 @@ namespace SUTK
}
}

void Text::remove(const CursorPosition<LineCountType>& position, LineCountType numChars) noexcept
void Text::removeRange(const CursorPosition<LineCountType>& start, const CursorPosition<LineCountType>& end) noexcept
{
if(position.getLine() >= m_lines.size())
return;
_com_assert(start.getLine() < m_lines.size());
_com_assert(end.getLine() < m_lines.size());
// start line ----- lower line number
// ---------------- <-- intermediate line
// ---------------- <-- intermediate line
/// ---------- end line higher line number
_com_assert(start.getLine() <= end.getLine());

LineText* lineText = m_lines[position.getLine()];
if((position.getColumn() + numChars) > lineText->getData().size())
return;
// remove the intermediate lines if any
LineCountType lineDiff = end.getLine() - start.getLine();
for(LineCountType i = 1; i < lineDiff; i++)
removeLine(start.getLine() + i);

// if start and end are straddled across different lines
if(start.getLine() != end.getLine())
{
// remove characters from start.getColumn() to m_lines[start.getLine()].getColumnCount() exclusive
LineText* startLineText = m_lines[start.getLine()];
startLineText->removeRange(start.getColumn(), std::string::npos);

// remove characters from 0 to end.getColumn()
LineText* endLineText = m_lines[end.getLine()];
endLineText->removeRange(0, std::min(static_cast<std::string::size_type>(end.getColumn()), endLineText->getColumnCount()));

// merge
startLineText->append(endLineText->getData());

lineText->removeRange(position.getColumn(), numChars);
// remove the last line (end line)
removeLine(end.getLine());
}
// if start and end are in the same line
else
{
LineText* lineText = m_lines[end.getLine()];
std::size_t colDiff = end.getColumn() - start.getColumn();
lineText->removeRange(start.getColumn(), colDiff);
}
}

void Text::set(const std::string& str) noexcept
Expand Down

0 comments on commit e11fe6c

Please sign in to comment.