mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat: add text class
This commit is contained in:
65
include/Cubed/ui/color.hpp
Normal file
65
include/Cubed/ui/color.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum class Color {
|
||||
BLACK,
|
||||
WHITE,
|
||||
RED,
|
||||
GREEN,
|
||||
BLUE,
|
||||
YELLOW,
|
||||
CYAN,
|
||||
MAGENTA,
|
||||
GRAY,
|
||||
ORANGE,
|
||||
PURPLE,
|
||||
PINK,
|
||||
BROWN
|
||||
};
|
||||
|
||||
inline constexpr glm::vec4 color_value(Color color) {
|
||||
using glm::vec4;
|
||||
|
||||
switch (color) {
|
||||
case Color::BLACK:
|
||||
return vec4{0.0f, 0.0f, 0.0f, 1.0f};
|
||||
case Color::WHITE:
|
||||
return vec4{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
case Color::RED:
|
||||
return vec4{1.0f, 0.0f, 0.0f, 1.0f};
|
||||
case Color::GREEN:
|
||||
return vec4{0.0f, 1.0f, 0.0f, 1.0f};
|
||||
case Color::BLUE:
|
||||
return vec4{0.0f, 0.0f, 1.0f, 1.0f};
|
||||
case Color::YELLOW:
|
||||
return vec4{1.0f, 1.0f, 0.0f, 1.0f};
|
||||
case Color::CYAN:
|
||||
return vec4{0.0f, 1.0f, 1.0f, 1.0f};
|
||||
case Color::MAGENTA:
|
||||
return vec4{1.0f, 0.0f, 1.0f, 1.0f};
|
||||
case Color::GRAY:
|
||||
return vec4{0.5f, 0.5f, 0.5f, 1.0f};
|
||||
case Color::ORANGE:
|
||||
return vec4{1.0f, 0.647f, 0.0f, 1.0f};
|
||||
case Color::PURPLE:
|
||||
return vec4{0.502f, 0.0f, 0.502f, 1.0f};
|
||||
case Color::PINK:
|
||||
return vec4{1.0f, 0.753f, 0.769f, 1.0f};
|
||||
case Color::BROWN:
|
||||
return vec4{0.647f, 0.165f, 0.165f, 1.0f};
|
||||
default:
|
||||
CUBED_ASSERT_MSG(false, "Unknown Color");
|
||||
return vec4{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline glm::vec4 rgb255_to_float(int r, int g, int b, int a) {
|
||||
float nr = static_cast<float>(r) / 255.0f;
|
||||
float ng = static_cast<float>(g) / 255.0f;
|
||||
float nb = static_cast<float>(b) / 255.0f;
|
||||
float na = static_cast<float>(a) / 255.0f;
|
||||
|
||||
return glm::vec4{nr, ng, nb, na};
|
||||
}
|
||||
46
include/Cubed/ui/text.hpp
Normal file
46
include/Cubed/ui/text.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/ui/color.hpp>
|
||||
|
||||
class Shader;
|
||||
|
||||
class Text {
|
||||
public:
|
||||
Text();
|
||||
Text(std::string_view str, glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
|
||||
~Text();
|
||||
|
||||
Text& color(Color color);
|
||||
//Text& color(const glm::vec4& color, int pos);
|
||||
Text& position(float x, float y);
|
||||
Text& scale(float s);
|
||||
static void set_loc(const Shader& shader);
|
||||
Text& text(std::string_view str);
|
||||
|
||||
|
||||
void render();
|
||||
|
||||
private:
|
||||
float m_scale = 1.0f;
|
||||
glm::vec2 m_pos{0.0f, 0.0f};
|
||||
|
||||
std::string m_name;
|
||||
std::string m_text;
|
||||
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
glm::mat4 m_model_matrix;
|
||||
|
||||
std::vector<Vertex2D> m_vertices;
|
||||
GLuint m_vbo = 0;
|
||||
static inline GLuint m_color_loc = 0;
|
||||
static inline GLuint m_mv_loc = 0;
|
||||
|
||||
void update_vertices();
|
||||
void upload_to_gpu();
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user