create new rect which is adapted to new text, and use it in text-builder.sml

This commit is contained in:
2025-08-18 02:39:56 +01:00
parent af15b97400
commit 41153c32a1
2 changed files with 43 additions and 39 deletions

View File

@@ -1,27 +1,35 @@
structure Rect = structure Rect =
struct struct
fun lerp (startX, startY, drawWidth, drawHeight, windowWidth, windowHeight, r, g, b) : Real32.real vector = fun xToNdc (xOffset, xpos, scale, halfWidth) =
((xpos * scale + xOffset) - halfWidth) / halfWidth
fun yToNdc (yOffset, ypos, scale, halfHeight) =
~(((ypos * scale + yOffset) - halfHeight) / halfHeight)
fun lerp (xOffset: Real32.real, yOffset, scale, windowWidth, windowHeight, r, g, b) =
let let
val startX = Real32.fromInt startX val halfWidth = windowWidth / 2.0
val startY = Real32.fromInt startY val halfHeight = windowHeight / 2.0
val endY = windowHeight - startY
val startY = windowHeight - (startY + drawHeight)
val endX = startX + drawWidth
val windowHeight = windowHeight / 2.0
val windowWidth = windowWidth / 2.0
in in
#[ (((startX * (1.0 - 0.733333349228)) + (endX * 0.733333349228)) / windowWidth) - 1.0, #[
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0, r, g, b, xToNdc (xOffset, 0.000000000000000, scale, halfWidth),
(((startX * (1.0 - 0.733333349228)) + (endX * 0.733333349228)) / windowWidth) - 1.0, yToNdc (yOffset, 13.000000000000000, scale, halfHeight),
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0, r, g, b, r, g, b,
(((startX * (1.0 - 0.266666650772)) + (endX * 0.266666650772)) / windowWidth) - 1.0, xToNdc (xOffset, 6.000000000000000, scale, halfWidth),
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0, r, g, b, yToNdc (yOffset, 13.000000000000000, scale, halfHeight),
(((startX * (1.0 - 0.266666650772)) + (endX * 0.266666650772)) / windowWidth) - 1.0, r, g, b,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0, r, g, b, xToNdc (xOffset, 0.000000000000000, scale, halfWidth),
(((startX * (1.0 - 0.266666650772)) + (endX * 0.266666650772)) / windowWidth) - 1.0, yToNdc (yOffset, 0.000000000000000, scale, halfHeight),
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0, r, g, b, r, g, b,
(((startX * (1.0 - 0.733333349228)) + (endX * 0.733333349228)) / windowWidth) - 1.0, xToNdc (xOffset, 0.000000000000000, scale, halfWidth),
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0, r, g, b yToNdc (yOffset, 0.000000000000000, scale, halfHeight),
r, g, b,
xToNdc (xOffset, 6.000000000000000, scale, halfWidth),
yToNdc (yOffset, 13.000000000000000, scale, halfHeight),
r, g, b,
xToNdc (xOffset, 6.000000000000000, scale, halfWidth),
yToNdc (yOffset, 0.000000000000000, scale, halfHeight),
r, g, b
] ]
end end
end end

View File

@@ -71,6 +71,10 @@ struct
(* same as buildTextStringAfterCursor, except this keeps track of absolute (* same as buildTextStringAfterCursor, except this keeps track of absolute
* index and cursor pos too *) * index and cursor pos too *)
fun makeRect (posX, posY, fw, fh, r, g, b) =
Rect.lerp
(Real32.fromInt (posX - 1), Real32.fromInt posY, scale, fw, fh, r, g, b)
fun makeChr (chr, posX, posY, windowWidth, windowHeight, r, g, b) = fun makeChr (chr, posX, posY, windowWidth, windowHeight, r, g, b) =
CozetteAscii.make CozetteAscii.make
( chr ( chr
@@ -124,8 +128,7 @@ struct
let let
val {r, g, b, fw, fh, ...} = env val {r, g, b, fw, fh, ...} = env
val cursorAcc = Rect.lerp val cursorAcc = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
in in
buildTextString buildTextString
( pos + 1 ( pos + 1
@@ -165,8 +168,7 @@ struct
let let
val {r, g, b, fw, fh, ...} = env val {r, g, b, fw, fh, ...} = env
val cursorAcc = Rect.lerp val cursorAcc = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
in in
buildTextString buildTextString
( pos + 1 ( pos + 1
@@ -241,8 +243,7 @@ struct
let let
val {fw, fh, r, g, b, hr, hg, hb, ...} = env val {fw, fh, r, g, b, hr, hg, hb, ...} = env
val cursorAcc = Rect.lerp val cursorAcc = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
in in
if posX + xSpace < #w env then if posX + xSpace < #w env then
let let
@@ -372,8 +373,7 @@ struct
val b: Real32.real = 0.1 val b: Real32.real = 0.1
val {fw, fh, ...} = env val {fw, fh, ...} = env
val space = Rect.lerp val space = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
val bgAcc = space :: bgAcc val bgAcc = space :: bgAcc
in in
buildTextStringSearch buildTextStringSearch
@@ -417,8 +417,7 @@ struct
let let
val {fw, fh, r, g, b, ...} = env val {fw, fh, r, g, b, ...} = env
val cursorAcc = Rect.lerp val cursorAcc = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
in in
buildTextStringSearch buildTextStringSearch
( pos + 1 ( pos + 1
@@ -464,8 +463,7 @@ struct
let let
val {fw, fh, r, g, b, ...} = env val {fw, fh, r, g, b, ...} = env
val cursorAcc = Rect.lerp val cursorAcc = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
in in
buildTextStringSearch buildTextStringSearch
( pos + 1 ( pos + 1
@@ -512,8 +510,7 @@ struct
val g: Real32.real = 0.1 val g: Real32.real = 0.1
val b: Real32.real = 0.1 val b: Real32.real = 0.1
val space = Rect.lerp val space = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
val bgAcc = space :: bgAcc val bgAcc = space :: bgAcc
in in
buildTextStringSearch buildTextStringSearch
@@ -597,8 +594,7 @@ struct
(* equal to cursor *) (* equal to cursor *)
let let
val {fw, fh, r, g, b, hr, hg, hb, ...} = env val {fw, fh, r, g, b, hr, hg, hb, ...} = env
val cursorAcc = Rect.lerp val cursorAcc = makeRect (posX, posY, fw, fh, r, g, b)
(posX, posY, fontSize, fontSize, fw, fh, r, g, b)
in in
if posX + xSpace < #w env then if posX + xSpace < #w env then
let let