metaforce/specter/include/Specter/TextView.hpp

111 lines
3.3 KiB
C++
Raw Normal View History

2015-11-21 23:45:02 +00:00
#ifndef SPECTER_TEXTVIEW_HPP
#define SPECTER_TEXTVIEW_HPP
#include "View.hpp"
2015-11-25 01:46:30 +00:00
#include <boo/graphicsdev/GL.hpp>
#include <boo/graphicsdev/D3D.hpp>
#include <boo/graphicsdev/Metal.hpp>
#include "FontCache.hpp"
2015-11-21 23:45:02 +00:00
namespace Specter
{
class ViewResources;
2015-11-21 23:45:02 +00:00
class TextView : public View
{
2015-12-13 21:00:30 +00:00
public:
enum class Alignment
{
Left,
Center,
Right
};
private:
2015-11-26 07:35:43 +00:00
size_t m_capacity;
2015-11-25 01:46:30 +00:00
boo::IGraphicsBufferD* m_glyphBuf;
2015-11-26 07:35:43 +00:00
boo::IVertexFormat* m_vtxFmt = nullptr; /* OpenGL only */
boo::IShaderDataBinding* m_shaderBinding;
2015-11-25 01:46:30 +00:00
const FontAtlas& m_fontAtlas;
2015-12-13 21:00:30 +00:00
Alignment m_align;
2015-12-02 21:11:50 +00:00
bool m_valid = false;
2015-12-05 00:42:46 +00:00
int m_width = 0;
2015-11-25 01:46:30 +00:00
2015-12-07 00:52:07 +00:00
friend class MultiLineTextView;
static int DoKern(FT_Pos val, const FontAtlas& atlas);
2015-11-25 01:46:30 +00:00
public:
class Resources
2015-11-25 01:46:30 +00:00
{
friend class ViewResources;
2015-11-25 01:46:30 +00:00
friend class TextView;
2015-11-29 02:55:30 +00:00
friend class MultiLineTextView;
2015-11-26 00:24:01 +00:00
FontCache* m_fcache = nullptr;
boo::IShaderPipeline* m_regular = nullptr;
boo::IShaderPipeline* m_subpixel = nullptr;
2015-11-25 01:46:30 +00:00
boo::IVertexFormat* m_vtxFmt = nullptr; /* Not OpenGL */
2015-11-26 00:24:01 +00:00
void init(boo::GLDataFactory* factory, FontCache* fcache);
2015-11-25 01:46:30 +00:00
#if _WIN32
2015-11-26 00:24:01 +00:00
void init(boo::ID3DDataFactory* factory, FontCache* fcache);
2015-11-25 01:46:30 +00:00
#elif BOO_HAS_METAL
2015-11-26 00:24:01 +00:00
void init(boo::MetalDataFactory* factory, FontCache* fcache);
2015-11-25 01:46:30 +00:00
#endif
2015-11-26 00:24:01 +00:00
};
2015-11-25 01:46:30 +00:00
2015-12-13 21:00:30 +00:00
TextView(ViewResources& res, View& parentView, const FontAtlas& font, Alignment align=Alignment::Left, size_t capacity=256);
TextView(ViewResources& res, View& parentView, FontTag font, Alignment align=Alignment::Left, size_t capacity=256);
2015-11-25 01:46:30 +00:00
struct RenderGlyph
{
2015-11-26 00:24:01 +00:00
Zeus::CVector3f m_pos[4];
Zeus::CMatrix4f m_mv;
2015-11-25 01:46:30 +00:00
Zeus::CVector3f m_uv[4];
Zeus::CColor m_color;
2015-11-26 00:24:01 +00:00
2015-11-26 07:35:43 +00:00
RenderGlyph(int& adv, const FontAtlas::Glyph& glyph, const Zeus::CColor& defaultColor);
2015-11-25 01:46:30 +00:00
};
2015-12-20 21:59:23 +00:00
struct RenderGlyphInfo
{
uint32_t m_char;
std::pair<int,int> m_dims;
int m_adv;
bool m_space = false;
RenderGlyphInfo(uint32_t ch, int width, int height, int adv)
: m_char(ch), m_dims(width, height), m_adv(adv), m_space(iswspace(ch)) {}
};
2015-11-25 01:46:30 +00:00
std::vector<RenderGlyph>& accessGlyphs() {return m_glyphs;}
2015-12-20 04:39:09 +00:00
const std::vector<RenderGlyph>& accessGlyphs() const {return m_glyphs;}
2015-12-02 21:11:50 +00:00
void updateGlyphs() {m_valid = false;}
2015-11-25 01:46:30 +00:00
void typesetGlyphs(const std::string& str,
2015-11-26 07:35:43 +00:00
const Zeus::CColor& defaultColor=Zeus::CColor::skWhite);
2015-11-25 01:46:30 +00:00
void typesetGlyphs(const std::wstring& str,
2015-11-26 07:35:43 +00:00
const Zeus::CColor& defaultColor=Zeus::CColor::skWhite);
2015-11-25 01:46:30 +00:00
2015-11-26 07:35:43 +00:00
void colorGlyphs(const Zeus::CColor& newColor);
void colorGlyphsTypeOn(const Zeus::CColor& newColor, float startInterval=0.2, float fadeTime=0.5);
2015-11-25 01:46:30 +00:00
void think();
2015-11-21 23:45:02 +00:00
void resized(const boo::SWindowRect& root, const boo::SWindowRect& sub);
2015-11-25 01:46:30 +00:00
void draw(boo::IGraphicsCommandQueue* gfxQ);
2015-11-21 23:45:02 +00:00
2015-12-05 00:42:46 +00:00
int nominalWidth() const {return m_width;}
int nominalHeight() const {return m_fontAtlas.FT_LineHeight() >> 6;}
std::pair<int,int> queryGlyphDimensions(size_t pos) const;
2015-12-20 04:39:09 +00:00
size_t reverseSelectGlyph(int x) const;
int queryReverseAdvance(size_t idx) const;
2015-12-20 21:59:23 +00:00
std::pair<size_t,size_t> queryWholeWordRange(size_t idx) const;
2015-11-25 01:46:30 +00:00
private:
std::vector<RenderGlyph> m_glyphs;
2015-12-20 21:59:23 +00:00
std::vector<RenderGlyphInfo> m_glyphInfo;
2015-11-21 23:45:02 +00:00
};
}
#endif // SPECTER_TEXTVIEW_HPP