API Almost finished
This commit is contained in:
		@@ -9,82 +9,147 @@
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
UILabelNew::UILabelNew(SceneItem *item) :
 | 
			
		||||
  UIComponentRenderable(item),
 | 
			
		||||
  font(nullptr),
 | 
			
		||||
  fontSize(16),
 | 
			
		||||
  style(0)
 | 
			
		||||
  UIComponentRenderable(item)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabelNew::rebufferQuads() {
 | 
			
		||||
void UILabelNew::rebufferQuads(std::vector<struct UILabelText> texts) {
 | 
			
		||||
  std::cout << "Rebuffering" << std::endl;
 | 
			
		||||
  this->mesh.createBuffers(QUAD_VERTICE_COUNT * 128, QUAD_INDICE_COUNT * 128);
 | 
			
		||||
  auto oldTexts = this->texts;
 | 
			
		||||
 | 
			
		||||
  textureMap.clear();
 | 
			
		||||
  glm::vec2 position(32, 32);
 | 
			
		||||
 | 
			
		||||
  struct FontShaderBufferData fontData;
 | 
			
		||||
  auto x = this->bufferQuads(
 | 
			
		||||
    "Hello World",
 | 
			
		||||
    fontData,
 | 
			
		||||
    this->font->getTexture(this->fontLock),
 | 
			
		||||
    position,
 | 
			
		||||
    0,
 | 
			
		||||
    0
 | 
			
		||||
  int32_t quadIndex = 0;
 | 
			
		||||
  int32_t partIndex = 0;
 | 
			
		||||
  int32_t quadCount = 0;
 | 
			
		||||
  int32_t nextTexture = 0;
 | 
			
		||||
 | 
			
		||||
  // Determine how many quads there are, and the texture indexes.
 | 
			
		||||
  auto itText = texts.begin();
 | 
			
		||||
  while(itText != texts.end()) {
 | 
			
		||||
    quadCount += itText->text.length();
 | 
			
		||||
 | 
			
		||||
    // Determine font and lock it.
 | 
			
		||||
    assertNotNull(itText->font);
 | 
			
		||||
    itText->lockId = itText->font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
      itText->size,
 | 
			
		||||
      itText->style
 | 
			
		||||
    });
 | 
			
		||||
    assertTrue(itText->lockId != -1);
 | 
			
		||||
    itText->texture = itText->font->getTexture(itText->lockId);
 | 
			
		||||
 | 
			
		||||
    // Check for existing texture, if not, map it.
 | 
			
		||||
    if(textureMap.find(itText->texture) == textureMap.end()) {
 | 
			
		||||
      assertTrue(nextTexture < FONT_SHADER_TEXTURE_MAX);
 | 
			
		||||
      textureMap[itText->texture] = nextTexture++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ++itText;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Cleanup old texst, we do this second so we don't unlock, cleanup, and then
 | 
			
		||||
  // lock the same font, causing it to have to re-load.
 | 
			
		||||
  itText = oldTexts.begin();
 | 
			
		||||
  while(itText != oldTexts.end()) {
 | 
			
		||||
    assertTrue(itText->lockId != -1);
 | 
			
		||||
    assertNotNull(itText->font);
 | 
			
		||||
    itText->font->unlock(itText->lockId);
 | 
			
		||||
    ++itText;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Update texts.
 | 
			
		||||
  this->texts = texts;
 | 
			
		||||
  
 | 
			
		||||
  // Create mesh
 | 
			
		||||
  this->mesh.createBuffers(
 | 
			
		||||
    QUAD_VERTICE_COUNT * quadCount,
 | 
			
		||||
    QUAD_INDICE_COUNT * quadCount
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  fontData.colors[0] = COLOR_MAGENTA;
 | 
			
		||||
  fontData.textures[0] = 0;
 | 
			
		||||
  // fontData.colors[1] = COLOR_RED;
 | 
			
		||||
  // fontData.textures[1] = 1;
 | 
			
		||||
  // fontData.colors[2] = COLOR_GREEN;
 | 
			
		||||
  // fontData.textures[2] = 2;
 | 
			
		||||
  // Buffer the text quads
 | 
			
		||||
  itText = texts.begin();
 | 
			
		||||
  while(itText != texts.end()) {
 | 
			
		||||
    quadIndex += this->bufferQuads(
 | 
			
		||||
      *itText,
 | 
			
		||||
      fontData,
 | 
			
		||||
      textureMap,
 | 
			
		||||
      position,
 | 
			
		||||
      quadIndex,
 | 
			
		||||
      partIndex
 | 
			
		||||
    );
 | 
			
		||||
    partIndex++;
 | 
			
		||||
    ++itText;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  shaderBuffer.buffer(&fontData);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabelNew::onStart() {
 | 
			
		||||
  this->shaderBuffer.init();
 | 
			
		||||
 | 
			
		||||
  useEffect([&]{
 | 
			
		||||
    usagelockid_t newLock = -1;
 | 
			
		||||
  auto font = this->getGame()->assetManager.get<NewTrueTypeAsset>("font_arial");
 | 
			
		||||
 | 
			
		||||
    if(font != nullptr) {
 | 
			
		||||
      newLock = font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
        fontSize,
 | 
			
		||||
        style
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  std::vector<struct UILabelText> texts;
 | 
			
		||||
  texts.push_back({
 | 
			
		||||
    .text = "Hello",
 | 
			
		||||
    .color = COLOR_RED,
 | 
			
		||||
    .style = 0,
 | 
			
		||||
    .size = 16,
 | 
			
		||||
    .font = font
 | 
			
		||||
  });
 | 
			
		||||
  texts.push_back({
 | 
			
		||||
    .text = " World",
 | 
			
		||||
    .color = COLOR_BLUE,
 | 
			
		||||
    .style = 1,
 | 
			
		||||
    .size = 32,
 | 
			
		||||
    .font = font
 | 
			
		||||
  });
 | 
			
		||||
  this->rebufferQuads(texts);
 | 
			
		||||
 | 
			
		||||
    if(fontLock != -1) {
 | 
			
		||||
      font.previous->unlock(fontLock);
 | 
			
		||||
      fontLock = -1;
 | 
			
		||||
    }
 | 
			
		||||
  // this->texts.clear();
 | 
			
		||||
 | 
			
		||||
    if(font == nullptr) return;
 | 
			
		||||
  // useEffect([&]{
 | 
			
		||||
  //   usagelockid_t newLock = -1;
 | 
			
		||||
 | 
			
		||||
    fontLock = newLock;
 | 
			
		||||
    this->rebufferQuads();
 | 
			
		||||
  }, font)();
 | 
			
		||||
  //   if(font != nullptr) {
 | 
			
		||||
  //     newLock = font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
  //       fontSize,
 | 
			
		||||
  //       style
 | 
			
		||||
  //     });
 | 
			
		||||
  //   }
 | 
			
		||||
 | 
			
		||||
  useEffect([&]{
 | 
			
		||||
    if(font == nullptr) return;
 | 
			
		||||
    auto newLock = font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
      fontSize,
 | 
			
		||||
      style
 | 
			
		||||
    });
 | 
			
		||||
  //   if(fontLock != -1) {
 | 
			
		||||
  //     font.previous->unlock(fontLock);
 | 
			
		||||
  //     fontLock = -1;
 | 
			
		||||
  //   }
 | 
			
		||||
 | 
			
		||||
    if(fontLock != -1) {
 | 
			
		||||
      font->unlock(fontLock);
 | 
			
		||||
      fontLock = -1;
 | 
			
		||||
    }
 | 
			
		||||
  //   if(font == nullptr) return;
 | 
			
		||||
 | 
			
		||||
    fontLock = newLock;
 | 
			
		||||
    this->rebufferQuads();
 | 
			
		||||
  }, { &fontSize, &style })();
 | 
			
		||||
  //   fontLock = newLock;
 | 
			
		||||
  //   this->rebufferQuads();
 | 
			
		||||
  // }, font)();
 | 
			
		||||
 | 
			
		||||
  // useEffect([&]{
 | 
			
		||||
  //   if(font == nullptr) return;
 | 
			
		||||
  //   auto newLock = font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
  //     fontSize,
 | 
			
		||||
  //     style
 | 
			
		||||
  //   });
 | 
			
		||||
 | 
			
		||||
  //   if(fontLock != -1) {
 | 
			
		||||
  //     font->unlock(fontLock);
 | 
			
		||||
  //     fontLock = -1;
 | 
			
		||||
  //   }
 | 
			
		||||
 | 
			
		||||
  //   fontLock = newLock;
 | 
			
		||||
  //   this->rebufferQuads();
 | 
			
		||||
  // }, { &fontSize, &style })();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<struct ShaderPassItem> UILabelNew::getUIRenderPasses() {
 | 
			
		||||
  if(this->fontLock == -1) return {};
 | 
			
		||||
  if(this->texts.size() == 0) return {};
 | 
			
		||||
 | 
			
		||||
  auto canvas = this->getCanvas();
 | 
			
		||||
  auto shader = getGame()->renderManager.fontShader;
 | 
			
		||||
@@ -97,17 +162,40 @@ std::vector<struct ShaderPassItem> UILabelNew::getUIRenderPasses() {
 | 
			
		||||
  item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer;
 | 
			
		||||
  item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND;
 | 
			
		||||
 | 
			
		||||
  item.textureSlots[0] = &this->font->getTexture(this->fontLock)->texture;
 | 
			
		||||
  item.textureValues[shader->paramTexture0] = 0;
 | 
			
		||||
  // item.textureSlots[1] = &defFaceItalics.texture;
 | 
			
		||||
  // item.textureSlots[2] = &defFaceBold.texture;
 | 
			
		||||
  // item.textureValues[shader->paramTexture1] = 1;
 | 
			
		||||
  // item.textureValues[shader->paramTexture2] = 2;
 | 
			
		||||
  // Map texture slots
 | 
			
		||||
  auto it = textureMap.begin();
 | 
			
		||||
  while(it != textureMap.end()) {
 | 
			
		||||
    item.textureSlots[it->second] = &it->first->texture;
 | 
			
		||||
    
 | 
			
		||||
    shaderparameter_t param = FONT_SHADER_TEXTURE_MAX;
 | 
			
		||||
    switch(it->second) {
 | 
			
		||||
      case 0:
 | 
			
		||||
        param = shader->paramTexture0;
 | 
			
		||||
        break;
 | 
			
		||||
      
 | 
			
		||||
      case 1:
 | 
			
		||||
        param = shader->paramTexture1;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      case 2:
 | 
			
		||||
        param = shader->paramTexture2;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      case 3:
 | 
			
		||||
        param = shader->paramTexture3;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    assertTrue(param >= 0 && param < FONT_SHADER_TEXTURE_MAX);
 | 
			
		||||
    item.textureValues[param] = it->second;
 | 
			
		||||
    ++it;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return { item };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getWidth() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -125,24 +213,27 @@ float_t UILabelNew::getContentHeight() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t UILabelNew::bufferQuads(
 | 
			
		||||
  std::string text,
 | 
			
		||||
  struct UILabelText text,
 | 
			
		||||
  struct FontShaderBufferData &bufferData,
 | 
			
		||||
  struct NewTrueTypeFaceTexture *texture,
 | 
			
		||||
  std::map<NewTrueTypeFaceTexture*, int32_t> &textureMap,
 | 
			
		||||
  glm::vec2 &position,
 | 
			
		||||
  int32_t quadStart,
 | 
			
		||||
  int32_t partIndex
 | 
			
		||||
) {
 | 
			
		||||
  // Get string length
 | 
			
		||||
  int32_t len = text.length();
 | 
			
		||||
  int32_t len = text.text.length();
 | 
			
		||||
  
 | 
			
		||||
  glm::vec2 wh = glm::vec2(texture->texture.getWidth(), texture->texture.getHeight());
 | 
			
		||||
  glm::vec2 wh = glm::vec2(
 | 
			
		||||
    text.texture->texture.getWidth(),
 | 
			
		||||
    text.texture->texture.getHeight()
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  // For each char
 | 
			
		||||
  for(int32_t i = 0; i < len; i++) {
 | 
			
		||||
    char ch = text[i];
 | 
			
		||||
    char ch = text.text[i];
 | 
			
		||||
    int32_t j = quadStart + i;
 | 
			
		||||
    FT_ULong c = ch;
 | 
			
		||||
    auto charInfo = texture->getCharacterData(c);
 | 
			
		||||
    auto charInfo = text.texture->getCharacterData(c);
 | 
			
		||||
 | 
			
		||||
    // Determine texture coordinates.
 | 
			
		||||
    glm::vec2 uv0 = glm::vec2(0.0f, charInfo.textureY) / wh;
 | 
			
		||||
@@ -164,5 +255,12 @@ int32_t UILabelNew::bufferQuads(
 | 
			
		||||
    bufferData.fontQuadMappings[j] = partIndex;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Map texture level values
 | 
			
		||||
  bufferData.colors[partIndex] = text.color;
 | 
			
		||||
 | 
			
		||||
  auto textureId = textureMap.find(text.texture);
 | 
			
		||||
  assertTrue(textureId != textureMap.end());
 | 
			
		||||
  bufferData.textures[partIndex] = textureId->second;
 | 
			
		||||
 | 
			
		||||
  return len;
 | 
			
		||||
}
 | 
			
		||||
@@ -9,54 +9,53 @@
 | 
			
		||||
#include "asset/assets/NewTrueTypeAsset.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  struct UILabelStyle {
 | 
			
		||||
    struct Color;
 | 
			
		||||
    // UILabelFontDef *fontDef;
 | 
			
		||||
  struct UILabelText {
 | 
			
		||||
    std::string text;
 | 
			
		||||
    struct Color color = COLOR_MAGENTA;
 | 
			
		||||
    flag_t style = 0;
 | 
			
		||||
    uint32_t size = 16;
 | 
			
		||||
    NewTrueTypeAsset *font = nullptr;
 | 
			
		||||
 | 
			
		||||
    // Part index
 | 
			
		||||
    // Quad start
 | 
			
		||||
    // position
 | 
			
		||||
    // size
 | 
			
		||||
    // some kind of custom data e.g. wobble or shake?
 | 
			
		||||
 | 
			
		||||
    usagelockid_t lockId = -1;
 | 
			
		||||
    struct NewTrueTypeFaceTexture *texture = nullptr;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  class UILabelNew : public UIComponentRenderable {
 | 
			
		||||
    private:
 | 
			
		||||
      Mesh mesh;
 | 
			
		||||
      FontShaderBuffer shaderBuffer;
 | 
			
		||||
 | 
			
		||||
      usagelockid_t fontLock = -1;
 | 
			
		||||
 | 
			
		||||
      // FT_Face face;
 | 
			
		||||
      // FT_Face faceBold;
 | 
			
		||||
      // FT_Face faceItalics;
 | 
			
		||||
 | 
			
		||||
      // struct UILabelFontDef defFace;
 | 
			
		||||
      // struct UILabelFontDef defFaceBold;
 | 
			
		||||
      // struct UILabelFontDef defFaceItalics;
 | 
			
		||||
      std::vector<struct UILabelText> texts;
 | 
			
		||||
      std::map<NewTrueTypeFaceTexture*, int32_t> textureMap;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Buffers the quads for the given text and updates the progressing values
 | 
			
		||||
       * as the buffer process continues.
 | 
			
		||||
       * 
 | 
			
		||||
       * @param text Text to buffer.
 | 
			
		||||
       * @param text Text information to buffer.
 | 
			
		||||
       * @param bufferData The output quad mappings for the text.
 | 
			
		||||
       * @param texture The font texture definition to use.
 | 
			
		||||
       * @param textureMap Texture map for the textures to map.
 | 
			
		||||
       * @param position The 2D position to buffer the quads at.
 | 
			
		||||
       * @param quadStart The starting quad index.
 | 
			
		||||
       * @param partIndex The part index to store for each quad buffered.
 | 
			
		||||
       * @return The number of quads buffered, not the string length.
 | 
			
		||||
       */
 | 
			
		||||
      int32_t bufferQuads(
 | 
			
		||||
        std::string text,
 | 
			
		||||
        struct UILabelText text,
 | 
			
		||||
        struct FontShaderBufferData &bufferData,
 | 
			
		||||
        struct NewTrueTypeFaceTexture *texture,
 | 
			
		||||
        std::map<NewTrueTypeFaceTexture*, int32_t> &textureMap,
 | 
			
		||||
        glm::vec2 &position,
 | 
			
		||||
        int32_t quadStart,
 | 
			
		||||
        int32_t partIndex
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      void rebufferQuads();
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      StateProperty<NewTrueTypeAsset*> font;
 | 
			
		||||
      StateProperty<uint32_t> fontSize;
 | 
			
		||||
      StateProperty<flag_t> style;
 | 
			
		||||
 | 
			
		||||
      UILabelNew(SceneItem *item);
 | 
			
		||||
 | 
			
		||||
      void onStart() override;
 | 
			
		||||
@@ -65,5 +64,7 @@ namespace Dawn {
 | 
			
		||||
      float_t getHeight() override;
 | 
			
		||||
      float_t getContentWidth() override;
 | 
			
		||||
      float_t getContentHeight() override;
 | 
			
		||||
 | 
			
		||||
      void rebufferQuads(std::vector<struct UILabelText> texts);
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user