Skip to content

Commit

Permalink
Improved text fields using the new fancy rect shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Sep 20, 2023
1 parent 313a960 commit f3ee4f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/resources/shaders/fancy_rect.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ in float internalAspectRatio;
out vec4 color;

uniform vec4 col;
uniform bool inverted;

void main() {
vec2 distToEdge2d = 0.5 - abs(internalPosition - 0.5);
Expand All @@ -39,6 +40,8 @@ void main() {
topLeftNess = (clamp(((1 - internalPosition.x) - (1 - internalPosition.y) * internalAspectRatio) * 200, -1, 1) + 1) * 0.5;
}

if (inverted) topLeftNess = 1 - topLeftNess;

if (isCloseToEdge) {
color = col + vec4(1, 1, 1, 0) * edgeEffect * mix(-0.3, 0.3, smoothstep(0, 1, topLeftNess));
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/hexacraft/gui/comp/Component.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ object Component:
xoffset: Float,
yoffset: Float,
color: Vector4f,
windowAspectRatio: Float
windowAspectRatio: Float,
inverted: Boolean = false
): Unit =
fancyRectShader.enable()

Expand All @@ -94,6 +95,7 @@ object Component:
.scale(location.w, location.h, 1)
)
fancyRectShader.setColor(color)
fancyRectShader.setInverted(inverted)

fancyRectShader.setWindowAspectRatio(windowAspectRatio)
Component.rectRenderer.render(rectVAO)
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/hexacraft/gui/comp/FancyRectShader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ class FancyRectShader {
def setColor(color: Vector4f): Unit =
shader.setUniform4f("col", color)

def setInverted(inverted: Boolean): Unit =
shader.setUniform1i("inverted", if inverted then 1 else 0)

def enable(): Unit = shader.activate()
}
11 changes: 9 additions & 2 deletions src/main/scala/hexacraft/gui/comp/TextField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TextField(
centered: Boolean = true,
maxFontSize: Float = 4f
) extends Component:
private val bgColor = new Vector4f(0.5f)
private val bgColor = new Vector4f(0.4f, 0.4f, 0.4f, 0.8f)
private val textColor = new Vector3f(1.0f)

private val contentText: Text = makeContentText()
Expand Down Expand Up @@ -71,7 +71,14 @@ class TextField(
cursorTextVisible = false

override def render(transformation: GUITransformation)(using context: RenderContext): Unit =
Component.drawRect(location, transformation.x, transformation.y, bgColor, context.windowAspectRatio)
Component.drawFancyRect(
location,
transformation.x,
transformation.y,
bgColor,
context.windowAspectRatio,
inverted = true
)
super.render(transformation)

override def handleEvent(event: Event): Boolean =
Expand Down

0 comments on commit f3ee4f5

Please sign in to comment.