gdxapp4d/gdxapp4d-lib-unicodezd/src/main/java/love/distributedrebirth/unicode4d/draw/ImCharacter.java

83 lines
2.6 KiB
Java
Raw Normal View History

2022-03-18 02:10:02 +01:00
package love.distributedrebirth.unicode4d.draw;
import imgui.ImColor;
import imgui.ImDrawList;
import imgui.ImGui;
import imgui.ImVec2;
2022-10-15 02:34:58 +02:00
import love.distributedrebirth.bassboonyd.info.BãßBȍőnAuthor注;
2022-03-18 02:10:02 +01:00
import love.distributedrebirth.unicode4d.draw.DrawGlyphPath.ImGlyphLineTo;
import love.distributedrebirth.unicode4d.draw.DrawGlyphPath.ImGlyphPathCommand;
import love.distributedrebirth.unicode4d.draw.DrawGlyphPath.ImGlyphQuadCurveTo;
2022-10-15 02:34:58 +02:00
@BãßBȍőnAuthor注(name = "للَّٰهِilLצسُو", copyright = "©Δ∞ 仙上主天")
2022-03-18 02:10:02 +01:00
public class ImCharacter {
public static final float HEIGHT = 26f;
2022-10-10 18:04:27 +02:00
public static final float WIDTH = 22f;
2022-04-04 23:05:17 +02:00
public static final float MARGIN_MENUBAR = 4f;
2022-10-10 18:04:27 +02:00
private static final ImVec2 SIZE = new ImVec2(WIDTH, HEIGHT);
private static final int DEFAULT_COLOR = ImColor.intToColor(255, 255, 255, 255);
2022-03-18 16:05:38 +01:00
public static void render(DrawCharacter drawChar) {
2022-03-30 20:19:15 +02:00
ImGui.invisibleButton("canvas", SIZE.x, SIZE.y);
2022-03-18 02:10:02 +01:00
ImVec2 p0 = ImGui.getItemRectMin();
ImVec2 p1 = ImGui.getItemRectMax(); // p1 = p0 + size
ImDrawList drawList = ImGui.getWindowDrawList();
drawList.pushClipRect(p0.x, p0.y, p1.x, p1.y);
2022-10-10 18:04:27 +02:00
drawUnicode4D(drawChar, p0.x, p0.y, DEFAULT_COLOR, drawList);
drawList.popClipRect();
}
public static void drawUnicode4D(DrawCharacter drawChar, float posX, float posY, int color, ImDrawList drawList) {
float xOff = posX;
float yOff = posY + 19f;
2022-03-18 02:10:02 +01:00
float yFlip = -1f;
2022-03-30 20:19:15 +02:00
float scale = 0.0199f;
2022-04-11 20:17:32 +02:00
if (drawChar.getyMax() > 900) {
2022-03-30 20:19:15 +02:00
scale = 0.0100f;
}
2022-03-18 02:10:02 +01:00
ImGlyphPathCommand first = null;
ImGlyphPathCommand prev = null;
for (ImGlyphPathCommand cmd: drawChar.getGlyphPath().getPath()) {
if (cmd.isImGlyphMoveTo()) {
first = cmd;
prev = cmd;
continue;
}
if (cmd.isImGlyphLineTo()) {
ImGlyphLineTo lineTo = cmd.toImGlyphLineTo();
drawList.addLine(
xOff+prev.getX()*scale,
yOff+prev.getY()*scale*yFlip,
xOff+lineTo.getX()*scale,
yOff+lineTo.getY()*scale*yFlip,
2022-10-10 18:04:27 +02:00
color);
2022-03-18 02:10:02 +01:00
prev = cmd;
continue;
}
if (cmd.isImGlyphQuadCurveTo()) {
ImGlyphQuadCurveTo quadCurveTo = cmd.toImGlyphQuadCurveTo();
drawList.addBezierQuadratic(
xOff+prev.getX()*scale,
yOff+prev.getY()*scale*yFlip,
xOff+quadCurveTo.getX1()*scale,
yOff+quadCurveTo.getY1()*scale*yFlip,
xOff+quadCurveTo.getX()*scale,
yOff+quadCurveTo.getY()*scale*yFlip,
2022-10-10 18:04:27 +02:00
color,
2022-03-18 02:10:02 +01:00
1);
prev = cmd;
continue;
}
if (cmd.isImGlyphClosePath()) {
drawList.addLine(
xOff+prev.getX()*scale,
yOff+prev.getY()*scale*yFlip,
xOff+first.getX()*scale,
yOff+first.getY()*scale*yFlip,
2022-10-10 18:04:27 +02:00
color);
2022-03-18 02:10:02 +01:00
}
}
}
2022-10-10 18:04:27 +02:00
}