Skip to content

Commit

Permalink
[SGE] Implemented Column calculation from local coordinates in Text's…
Browse files Browse the repository at this point in the history
… Container
  • Loading branch information
ravi688 committed Aug 6, 2024
1 parent 965a285 commit dd8c69b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sutk/include/sutk/IGfxDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace SUTK
virtual void destroyText(GfxDriverObjectHandleType handle) = 0;
virtual void setTextPosition(GfxDriverObjectHandleType handle, Vec2Df position) = 0;
virtual void setTextData(GfxDriverObjectHandleType handle, const std::string& data) = 0;
virtual LineCountType getTextGlyphIndexFromCoord(GfxDriverObjectHandleType handle, f32 coord) = 0;
virtual f32 getTextCoordFromGlyphIndex(GfxDriverObjectHandleType handle, LineCountType col) = 0;
virtual GfxDriverObjectHandleType getTextObject(GfxDriverObjectHandleType handle) = 0;

virtual GfxDriverObjectHandleType getObject(GfxDriverObjectHandleType handle) = 0;
Expand Down
4 changes: 4 additions & 0 deletions sutk/include/sutk/SGEGfxDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace SUTK
SGE::Shader compileShader(const Geometry& geometry);
// Transforms SUTK coordinates (origin at top-left, and y downwards) to SGE coordinates (origin at center, and y upwards)
vec3_t SUTKToSGECoordTransform(const Vec2Df position);
Vec2Df SGEToSUTKCoordTransform(const vec3_t position);
ObjectType getType(GfxDriverObjectHandleType handle);
void removeIDFromTypeTable(u32 id);

Expand All @@ -105,6 +106,9 @@ namespace SUTK
virtual void setTextPosition(GfxDriverObjectHandleType handle, Vec2Df position) override;
virtual void setTextData(GfxDriverObjectHandleType handle, const std::string& data) override;

virtual LineCountType getTextGlyphIndexFromCoord(GfxDriverObjectHandleType handle, f32 coord) override;
virtual f32 getTextCoordFromGlyphIndex(GfxDriverObjectHandleType handle, LineCountType col) override;

virtual GfxDriverObjectHandleType getTextObject(GfxDriverObjectHandleType handle) override;
virtual GfxDriverObjectHandleType getObject(GfxDriverObjectHandleType handle) override;
virtual void setObjectScissor(GfxDriverObjectHandleType handle, const Rect2Df rect) override;
Expand Down
11 changes: 11 additions & 0 deletions sutk/include/sutk/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string> /* for std::string */
#include <vector> /* for std::vector*/
#include <limits> /* for std::numeric_limits */
#include <ostream> // for std::ostream

namespace SUTK
{
Expand Down Expand Up @@ -84,6 +85,9 @@ namespace SUTK
virtual bool isDirty() override;
virtual void update() override;

// returns column (index of a glyph) given a coordinate (absisca) along the line length.
LineCountType getColPosFromCoord(f32 coord) noexcept;
f32 getCoordFromColPos(LineCountType col) noexcept;
void setData(const std::string& data) noexcept;
void append(const std::string& data) noexcept;
void insert(LineCountType col, const std::string& data) noexcept;
Expand Down Expand Up @@ -136,6 +140,13 @@ namespace SUTK
template<> CursorPosition<LineCountType> CursorPosition<LineCountType>::EndOfLine(LineCountType line);


template<typename T>
std::ostream& operator <<(std::ostream& stream, const CursorPosition<T>& pos)
{
stream << "{ line: " << pos.getLine() << ", col: " << pos.getColumn() << " }";
return stream;
}

// One text object is responsible for rendering a small to medium sized sub-text
// This usually translates to a single Gfx API specific buffer object. For example, it is VkBuffer (and VkDeviceMemory) in vulkan.
// This is to ensure fast manipulation of larget text data consisting of multiple such 'Text' objects.
Expand Down
28 changes: 28 additions & 0 deletions sutk/source/SGEGfxDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ namespace SUTK
return vec3(0.0f, windowSize.height * 0.5f - pixelPosition.height, pixelPosition.width - windowSize.width * 0.5f);
}

Vec2Df SGEGfxDriver::SGEToSUTKCoordTransform(const vec3_t position)
{
auto windowSize = getSizeInPixels();
Vec2Df sutkPosInPixels = { windowSize.height * 0.5f - position.y, windowSize.width * 0.5f + position.x };
extent2d_t sutkPosInInches = SGE::Display::ConvertPixelsToInches({ sutkPosInPixels.x, sutkPosInPixels.y });
return { sutkPosInInches.x * CENTIMETERS_PER_INCH, sutkPosInInches.y * CENTIMETERS_PER_INCH };
}

void SGEGfxDriver::setTextPosition(GfxDriverObjectHandleType handle, Vec2Df position)
{
getText(handle).setPosition(SUTKToSGECoordTransform(position));
Expand All @@ -244,6 +252,26 @@ namespace SUTK
bitmapTextData.charCount += textString.getLength();
}

LineCountType SGEGfxDriver::getTextGlyphIndexFromCoord(GfxDriverObjectHandleType handle, f32 coord)
{
auto it = getSubTextIterator(handle);
SGE::BitmapTextString& textString = it->second.textString;
// 'coord' is in centimeters, so convert it into pixels along the width of the monitor/window
f32 zCoord = SGE::Display::ConvertInchesToPixels({ coord * INCHES_PER_CENTIMETER, 0 }).x;
return static_cast<LineCountType>(textString.getGlyphIndexFromZCoord(zCoord));
}

f32 SGEGfxDriver::getTextCoordFromGlyphIndex(GfxDriverObjectHandleType handle, LineCountType col)
{
auto it = getSubTextIterator(handle);
SGE::BitmapTextString& textString = it->second.textString;
// get the offset along horizontal axis in pixel coordinates
f32 zCoord = textString.getZCoordFromGlyphIndex(static_cast<u32>(col));
// convert it into centimeters
f32 xCoord = SGE::Display::ConvertPixelsToInches({ zCoord, 0 }).x * CENTIMETERS_PER_INCH;
return xCoord;
}

GfxDriverObjectHandleType SGEGfxDriver::getTextObject(GfxDriverObjectHandleType handle)
{
auto it = getSubTextIterator(handle);
Expand Down
14 changes: 12 additions & 2 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;
}
}
LineCountType LineText::getColPosFromCoord(f32 coord) noexcept
{
return getGfxDriver().getTextGlyphIndexFromCoord(getGfxDriverObjectHandle(), coord);
}
f32 LineText::getCoordFromColPos(LineCountType col) noexcept
{
return getGfxDriver().getTextCoordFromGlyphIndex(getGfxDriverObjectHandle(), col);
}
void LineText::setData(const std::string& data) noexcept
{
m_data = std::string(data);
Expand Down Expand Up @@ -115,7 +123,9 @@ namespace SUTK

Vec2Df Text::getLocalPositionFromCursorPosition(const CursorPosition<LineCountType>& cursor) noexcept
{
return { 0, m_baselineHeight * cursor.getLine() + m_scrollDelta.y };
LineCountType line = cursor.getLine();
_assert((line >= 0) && (line < m_lines.size()));
return { m_lines[line]->getCoordFromColPos(cursor.getColumn()), m_baselineHeight * cursor.getLine() + m_scrollDelta.y };
}

std::pair<s32, s32> Text::getUnclippedLineRange() noexcept
Expand All @@ -138,8 +148,8 @@ namespace SUTK
auto lineNo = static_cast<s32>((coords - m_scrollDelta).y / m_baselineHeight);
lineNo = std::min(lineNo, static_cast<s32>(m_lines.size()) - 1);
lineNo = std::max(lineNo, 0);
LineCountType colNo = 0;
_assert(lineNo >= 0);
LineCountType colNo = m_lines[lineNo]->getColPosFromCoord(coords.x);
return { lineNo, colNo };
}

Expand Down

0 comments on commit dd8c69b

Please sign in to comment.