Prog ttf
This commit is contained in:
		@@ -14,12 +14,162 @@ UILabelNew::UILabelNew(SceneItem *item) :
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabelNew::onStart() {
 | 
			
		||||
  this->shaderBuffer.init();
 | 
			
		||||
 | 
			
		||||
  auto font = this->getGame()->assetManager.get<NewTrueTypeAsset>("font_arial");
 | 
			
		||||
 | 
			
		||||
  std::vector<struct UILabelText> texts;
 | 
			
		||||
  texts.push_back({
 | 
			
		||||
    .text = "Hello",
 | 
			
		||||
    .style = {
 | 
			
		||||
      .color = COLOR_RED,
 | 
			
		||||
      .style = 0,
 | 
			
		||||
      .size = 32,
 | 
			
		||||
      .font = font
 | 
			
		||||
    }
 | 
			
		||||
  });
 | 
			
		||||
  texts.push_back({
 | 
			
		||||
    .text = "World",
 | 
			
		||||
    .style = {
 | 
			
		||||
      .color = COLOR_BLUE,
 | 
			
		||||
      .style = 1,
 | 
			
		||||
      .size = 64,
 | 
			
		||||
      .font = font
 | 
			
		||||
    }
 | 
			
		||||
  });
 | 
			
		||||
  this->rebufferQuads(texts);
 | 
			
		||||
 | 
			
		||||
  // std::vector<struct UILabelStyle> styleStack;
 | 
			
		||||
  // struct UILabelStyle current;
 | 
			
		||||
  // styleStack.push_back(current);
 | 
			
		||||
  // std::vector<struct UILabelText> texts;
 | 
			
		||||
 | 
			
		||||
  // std::function<void(Xml*)> parseChildren = [&](Xml *node) {
 | 
			
		||||
  //   if(node->children.empty()) {
 | 
			
		||||
  //     struct UILabelText text;
 | 
			
		||||
  //     text.style = current;
 | 
			
		||||
  //     text.text = node->value;
 | 
			
		||||
  //     (node->value)
 | 
			
		||||
  //   } else {
 | 
			
		||||
  //     auto itNode = node->children.begin();
 | 
			
		||||
  //     while(itNode != node->children.end()) {
 | 
			
		||||
  //       auto child = *itNode;
 | 
			
		||||
  //       std::cout << "Node: " << child->node << std::endl;
 | 
			
		||||
 | 
			
		||||
  //       assertTrue(child->node == "font");
 | 
			
		||||
 | 
			
		||||
  //       struct UILabelStyle style;
 | 
			
		||||
  //       if(child->attributes.contains("font")) {
 | 
			
		||||
  //         style.font = this->getGame()->assetManager.get<NewTrueTypeAsset>(child->attributes["font"]);
 | 
			
		||||
  //       } else {
 | 
			
		||||
  //         style.font = current.font;
 | 
			
		||||
  //       }
 | 
			
		||||
 | 
			
		||||
  //       if(child->attributes.contains("size")) {
 | 
			
		||||
  //         style.size = std::stoi(child->attributes["size"]);
 | 
			
		||||
  //       } else {
 | 
			
		||||
  //         style.size = current.size;
 | 
			
		||||
  //       }
 | 
			
		||||
 | 
			
		||||
  //       if(child->attributes.contains("style")) {
 | 
			
		||||
  //         style.style = std::stoi(child->attributes["style"]);
 | 
			
		||||
  //       } else {
 | 
			
		||||
  //         style.style = current.style;
 | 
			
		||||
  //       }
 | 
			
		||||
 | 
			
		||||
  //       if(child->attributes.contains("color")) {
 | 
			
		||||
  //         style.color = std::stoi(child->attributes["color"]);
 | 
			
		||||
  //       } else {
 | 
			
		||||
  //         style.color = current.color;
 | 
			
		||||
  //       }
 | 
			
		||||
 | 
			
		||||
  //       styleStack.push_back(style);
 | 
			
		||||
  //       current = style;
 | 
			
		||||
 | 
			
		||||
  //       parseChildren(child);
 | 
			
		||||
 | 
			
		||||
  //       styleStack.pop_back();
 | 
			
		||||
  //       current = styleStack.back();
 | 
			
		||||
  //       ++itNode;
 | 
			
		||||
  //     }
 | 
			
		||||
  //   }
 | 
			
		||||
  // };
 | 
			
		||||
 | 
			
		||||
  // auto root = Xml::load("<root>" + this->test + "</root>");
 | 
			
		||||
  // parseChildren(&root);
 | 
			
		||||
  // this->rebufferQuads(texts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<struct ShaderPassItem> UILabelNew::getUIRenderPasses() {
 | 
			
		||||
  // if(this->texts.size() == 0) return {};
 | 
			
		||||
 | 
			
		||||
  auto canvas = this->getCanvas();
 | 
			
		||||
  auto shader = getGame()->renderManager.fontShader;
 | 
			
		||||
 | 
			
		||||
  struct ShaderPassItem item;
 | 
			
		||||
  item.shader = shader;
 | 
			
		||||
  item.mesh = &this->mesh;
 | 
			
		||||
  item.matrixValues[shader->paramModel] = transform->getWorldTransform();
 | 
			
		||||
  item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer;
 | 
			
		||||
  item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer;
 | 
			
		||||
  item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND;
 | 
			
		||||
 | 
			
		||||
  // Map texture slots
 | 
			
		||||
  auto it = textureMap.begin();
 | 
			
		||||
  while(it != textureMap.end()) {
 | 
			
		||||
    shaderparameter_t param;
 | 
			
		||||
    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:
 | 
			
		||||
        assertUnreachable();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    item.textureSlots[it->second] = &it->first->texture;
 | 
			
		||||
    item.textureValues[param] = it->second;
 | 
			
		||||
    ++it;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return { item };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getWidth() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getHeight() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getContentWidth() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getContentHeight() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabelNew::rebufferQuads(std::vector<struct UILabelText> texts) {
 | 
			
		||||
  std::cout << "Rebuffering" << std::endl;
 | 
			
		||||
  auto oldTexts = this->texts;
 | 
			
		||||
 | 
			
		||||
  textureMap.clear();
 | 
			
		||||
  glm::vec2 position(0, 0);
 | 
			
		||||
  glm::vec2 position(32, 32);
 | 
			
		||||
  struct FontShaderBufferData fontData;
 | 
			
		||||
  int32_t quadIndex = 0;
 | 
			
		||||
  int32_t partIndex = 0;
 | 
			
		||||
@@ -32,13 +182,13 @@ void UILabelNew::rebufferQuads(std::vector<struct UILabelText> texts) {
 | 
			
		||||
    quadCount += itText->text.length();
 | 
			
		||||
 | 
			
		||||
    // Determine font and lock it.
 | 
			
		||||
    assertNotNull(itText->font);
 | 
			
		||||
    itText->lockId = itText->font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
      itText->size,
 | 
			
		||||
      itText->style
 | 
			
		||||
    assertNotNull(itText->style.font);
 | 
			
		||||
    itText->lockId = itText->style.font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
      itText->style.size,
 | 
			
		||||
      itText->style.style
 | 
			
		||||
    });
 | 
			
		||||
    assertTrue(itText->lockId != -1);
 | 
			
		||||
    itText->texture = itText->font->getTexture(itText->lockId);
 | 
			
		||||
    itText->texture = itText->style.font->getTexture(itText->lockId);
 | 
			
		||||
 | 
			
		||||
    // Check for existing texture, if not, map it.
 | 
			
		||||
    if(textureMap.find(itText->texture) == textureMap.end()) {
 | 
			
		||||
@@ -47,7 +197,7 @@ void UILabelNew::rebufferQuads(std::vector<struct UILabelText> texts) {
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
    // Set initial line height
 | 
			
		||||
    position.y = mathMax<float_t>(itText->size, position.y);
 | 
			
		||||
    position.y = mathMax<float_t>(itText->style.size, position.y);
 | 
			
		||||
 | 
			
		||||
    ++itText;
 | 
			
		||||
  }
 | 
			
		||||
@@ -57,8 +207,8 @@ void UILabelNew::rebufferQuads(std::vector<struct UILabelText> texts) {
 | 
			
		||||
  itText = oldTexts.begin();
 | 
			
		||||
  while(itText != oldTexts.end()) {
 | 
			
		||||
    assertTrue(itText->lockId != -1);
 | 
			
		||||
    assertNotNull(itText->font);
 | 
			
		||||
    itText->font->unlock(itText->lockId);
 | 
			
		||||
    assertNotNull(itText->style.font);
 | 
			
		||||
    itText->style.font->unlock(itText->lockId);
 | 
			
		||||
    ++itText;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -82,138 +232,13 @@ void UILabelNew::rebufferQuads(std::vector<struct UILabelText> texts) {
 | 
			
		||||
      quadIndex,
 | 
			
		||||
      partIndex
 | 
			
		||||
    );
 | 
			
		||||
    partIndex++;
 | 
			
		||||
    ++partIndex;
 | 
			
		||||
    ++itText;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  shaderBuffer.buffer(&fontData);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabelNew::onStart() {
 | 
			
		||||
  this->shaderBuffer.init();
 | 
			
		||||
 | 
			
		||||
  auto font = this->getGame()->assetManager.get<NewTrueTypeAsset>("font_arial");
 | 
			
		||||
 | 
			
		||||
  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);
 | 
			
		||||
 | 
			
		||||
  // this->texts.clear();
 | 
			
		||||
 | 
			
		||||
  // useEffect([&]{
 | 
			
		||||
  //   usagelockid_t newLock = -1;
 | 
			
		||||
 | 
			
		||||
  //   if(font != nullptr) {
 | 
			
		||||
  //     newLock = font->lock(NewTrueTypeFaceTextureStyle{
 | 
			
		||||
  //       fontSize,
 | 
			
		||||
  //       style
 | 
			
		||||
  //     });
 | 
			
		||||
  //   }
 | 
			
		||||
 | 
			
		||||
  //   if(fontLock != -1) {
 | 
			
		||||
  //     font.previous->unlock(fontLock);
 | 
			
		||||
  //     fontLock = -1;
 | 
			
		||||
  //   }
 | 
			
		||||
 | 
			
		||||
  //   if(font == nullptr) return;
 | 
			
		||||
 | 
			
		||||
  //   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->texts.size() == 0) return {};
 | 
			
		||||
 | 
			
		||||
  auto canvas = this->getCanvas();
 | 
			
		||||
  auto shader = getGame()->renderManager.fontShader;
 | 
			
		||||
 | 
			
		||||
  struct ShaderPassItem item;
 | 
			
		||||
  item.shader = shader;
 | 
			
		||||
  item.mesh = &this->mesh;
 | 
			
		||||
  item.matrixValues[shader->paramModel] = transform->getWorldTransform();
 | 
			
		||||
  item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer;
 | 
			
		||||
  item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer;
 | 
			
		||||
  item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND;
 | 
			
		||||
 | 
			
		||||
  // 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:
 | 
			
		||||
        assertUnreachable();
 | 
			
		||||
    }
 | 
			
		||||
    item.textureValues[param] = it->second;
 | 
			
		||||
    ++it;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return { item };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getWidth() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getHeight() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getContentWidth() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t UILabelNew::getContentHeight() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t UILabelNew::bufferQuads(
 | 
			
		||||
  struct UILabelText text,
 | 
			
		||||
  struct FontShaderBufferData &bufferData,
 | 
			
		||||
@@ -258,11 +283,10 @@ int32_t UILabelNew::bufferQuads(
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Map texture level values
 | 
			
		||||
  bufferData.colors[partIndex] = text.color;
 | 
			
		||||
 | 
			
		||||
  auto textureId = textureMap.find(text.texture);
 | 
			
		||||
  assertTrue(textureId != textureMap.end());
 | 
			
		||||
  bufferData.textures[partIndex] = textureId->second;
 | 
			
		||||
  bufferData.colors[partIndex] = text.style.color;
 | 
			
		||||
 | 
			
		||||
  return len;
 | 
			
		||||
}
 | 
			
		||||
@@ -7,14 +7,19 @@
 | 
			
		||||
#include "scene/components/ui/UIComponentRenderable.hpp"
 | 
			
		||||
#include "display/mesh/QuadMesh.hpp"
 | 
			
		||||
#include "asset/assets/NewTrueTypeAsset.hpp"
 | 
			
		||||
#include "util/Xml.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  struct UILabelText {
 | 
			
		||||
    std::string text;
 | 
			
		||||
  struct UILabelStyle {
 | 
			
		||||
    struct Color color = COLOR_MAGENTA;
 | 
			
		||||
    flag_t style = 0;
 | 
			
		||||
    uint32_t size = 16;
 | 
			
		||||
    NewTrueTypeAsset *font = nullptr;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  struct UILabelText {
 | 
			
		||||
    std::string text;
 | 
			
		||||
    struct UILabelStyle style;
 | 
			
		||||
 | 
			
		||||
    // Part index
 | 
			
		||||
    // Quad start
 | 
			
		||||
@@ -56,6 +61,8 @@ namespace Dawn {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      std::string test;
 | 
			
		||||
 | 
			
		||||
      UILabelNew(SceneItem *item);
 | 
			
		||||
 | 
			
		||||
      void onStart() override;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user