Skip to content

Commit

Permalink
Improved ui context design
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Oct 30, 2024
1 parent f6c8a25 commit bf0b5d4
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 64 deletions.
6 changes: 3 additions & 3 deletions client/src/main/scala/hexacraft/client/DebugOverlay.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hexacraft.client

import hexacraft.gui.{LocationInfo, RenderContext}
import hexacraft.gui.comp.{Component, GUITransformation}
import hexacraft.gui.comp.Component
import hexacraft.text.{Text, TextMaster}
import hexacraft.world.{Camera, CylinderSize}
import hexacraft.world.coord.{ChunkRelWorld, CylCoords}
Expand Down Expand Up @@ -127,10 +127,10 @@ class DebugOverlay {
)
}

def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
def render(context: RenderContext): Unit = {
texts.foreach(t => t.setPosition(-context.windowAspectRatio + 0.01f * 2 * 16 / 9, t.position.y))
textMaster.setWindowAspectRatio(context.windowAspectRatio)
textMaster.render(transformation.x, transformation.y)
textMaster.render(context.offset.x, context.offset.y)
}

def unload(): Unit = {
Expand Down
16 changes: 8 additions & 8 deletions client/src/main/scala/hexacraft/client/GameClient.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package hexacraft.client

import hexacraft.game.*
import hexacraft.gui.{Event, LocationInfo, MousePosition, RenderContext, TickContext, WindowSize}
import hexacraft.gui.comp.{Component, GUITransformation}
import hexacraft.gui.*
import hexacraft.gui.comp.Component
import hexacraft.infra.audio.AudioSystem
import hexacraft.infra.window.{KeyAction, KeyboardKey, MouseAction, MouseButton}
import hexacraft.renderer.{PixelArray, Renderer, TextureArray, VAO}
Expand All @@ -11,7 +11,7 @@ import hexacraft.util.{Channel, NamedThreadFactory, Result, TickableTimer}
import hexacraft.world.*
import hexacraft.world.block.{Block, BlockSpec, BlockState}
import hexacraft.world.chunk.{Chunk, ChunkColumnData, ChunkColumnHeightMap, ChunkColumnTerrain}
import hexacraft.world.coord.{BlockCoords, BlockRelWorld, ChunkRelWorld, CoordUtils, CylCoords, NeighborOffsets}
import hexacraft.world.coord.*

import com.martomate.nbt.Nbt
import org.joml.{Matrix4f, Vector2f, Vector3d, Vector3f}
Expand Down Expand Up @@ -441,20 +441,20 @@ class GameClient(
worldRenderer.frameBufferResized(width, height)
}

def render(transformation: GUITransformation)(using RenderContext): Unit = {
def render(context: RenderContext): Unit = {
worldRenderer.render(camera, new Vector3f(0, 1, -1), selectedBlockAndSide)

renderCrosshair()

blockInHandRenderer.render(transformation)
toolbar.render(transformation)
blockInHandRenderer.render()
toolbar.render(context)

if debugOverlay.isDefined then {
debugOverlay.get.render(transformation)
debugOverlay.get.render(context)
}

for s <- overlays do {
s.render(transformation)
s.render(context)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hexacraft.client

import hexacraft.client.render.BlockRenderer
import hexacraft.gui.comp.GUITransformation
import hexacraft.renderer.*
import hexacraft.shaders.GuiBlockShader
import hexacraft.world.CameraProjection
Expand Down Expand Up @@ -44,7 +43,7 @@ class GuiBlockRenderer(w: Int, h: Int, separation: Float = 0.2f)(blockTextureInd
guiBlockSideShader.setWindowAspectRatio(aspectRatio)
}

def render(transformation: GUITransformation): Unit = {
def render(): Unit = {
guiBlockShader.setViewMatrix(viewMatrix)
guiBlockSideShader.setViewMatrix(viewMatrix)

Expand Down
14 changes: 7 additions & 7 deletions client/src/main/scala/hexacraft/client/InventoryBox.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hexacraft.client

import hexacraft.gui.{Event, LocationInfo, RenderContext}
import hexacraft.gui.comp.{Component, GUITransformation}
import hexacraft.gui.comp.Component
import hexacraft.infra.window.{KeyAction, KeyboardKey, MouseAction}
import hexacraft.util.Channel
import hexacraft.world.Inventory
Expand Down Expand Up @@ -117,7 +117,7 @@ class InventoryBox private (
}
}

override def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
override def render(context: RenderContext): Unit = {
gridRenderer.setWindowAspectRatio(context.windowAspectRatio)
floatingBlockRenderer.setWindowAspectRatio(context.windowAspectRatio)

Expand All @@ -128,18 +128,18 @@ class InventoryBox private (
floatingBlockRenderer.updateContent(mousePos.x, mousePos.y, Seq(floatingBlock.get))
}

Component.drawRect(bounds, transformation.x, transformation.y, backgroundColor, context.windowAspectRatio)
Component.drawRect(bounds, context.offset.x, context.offset.y, backgroundColor, context.windowAspectRatio)

if hoverIndex.isDefined then {
val xOffset = transformation.x + (hoverIndex.get % 9) * 0.2f
val yOffset = transformation.y + (hoverIndex.get / 9) * 0.2f
val xOffset = context.offset.x + (hoverIndex.get % 9) * 0.2f
val yOffset = context.offset.y + (hoverIndex.get / 9) * 0.2f
Component.drawRect(selectedBox, xOffset, yOffset, selectedColor, context.windowAspectRatio)
}

gridRenderer.render(transformation)
gridRenderer.render()

if floatingBlock.isDefined then {
floatingBlockRenderer.render(transformation)
floatingBlockRenderer.render()
}
}

Expand Down
12 changes: 6 additions & 6 deletions client/src/main/scala/hexacraft/client/Toolbar.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hexacraft.client

import hexacraft.gui.{LocationInfo, RenderContext}
import hexacraft.gui.comp.{Component, GUITransformation}
import hexacraft.gui.comp.Component
import hexacraft.world.Inventory

import org.joml.{Matrix4f, Vector4f}
Expand Down Expand Up @@ -46,17 +46,17 @@ class Toolbar(location: LocationInfo, private var inventory: Inventory)(
guiBlockRenderer.updateContent(-4 * 0.2f, -0.83f, (0 until 9).map(i => inventory(i)))
}

override def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
Component.drawRect(location, transformation.x, transformation.y, backgroundColor, context.windowAspectRatio)
override def render(context: RenderContext): Unit = {
Component.drawRect(location, context.offset.x, context.offset.y, backgroundColor, context.windowAspectRatio)
Component.drawRect(
selectedBox,
transformation.x + selectedIndex * location.w / 9,
transformation.y,
context.offset.x + selectedIndex * location.w / 9,
context.offset.y,
selectedColor,
context.windowAspectRatio
)

guiBlockRenderer.render(transformation)
guiBlockRenderer.render()
}

override def unload(): Unit = {
Expand Down
11 changes: 8 additions & 3 deletions client/src/main/scala/hexacraft/gui/RenderContext.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package hexacraft.gui

import org.joml.{Vector2fc, Vector2ic}
import org.joml.{Vector2f, Vector2fc, Vector2ic}

case class RenderContext(
windowAspectRatio: Float,
frameBufferSize: Vector2ic,
heightNormalizedMousePos: Vector2fc
)
heightNormalizedMousePos: Vector2fc,
offset: Vector2fc
) {
def withMoreOffset(dx: Float, dy: Float): RenderContext = {
this.copy(offset = offset.add(dx, dy, new Vector2f))
}
}
8 changes: 4 additions & 4 deletions client/src/main/scala/hexacraft/gui/comp/Button.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ object Button {
class Button(text: String, val bounds: LocationInfo, clickAction: => Unit) extends Component with Boundable {
addText(Component.makeText(text, bounds, 4.0f, shadow = true).setTextAndFitSize(text, 4.0f))

override def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
override def render(context: RenderContext): Unit = {
val mousePos = context.heightNormalizedMousePos
val containsMouse = bounds.containsPoint(mousePos.x - transformation.x, mousePos.y - transformation.y)
val containsMouse = bounds.containsPoint(mousePos.x - context.offset.x, mousePos.y - context.offset.y)

val color =
if containsMouse then {
Expand All @@ -25,8 +25,8 @@ class Button(text: String, val bounds: LocationInfo, clickAction: => Unit) exten
new Vector4f(0.4f, 0.4f, 0.4f, 0.8f)
}

Component.drawFancyRect(bounds, transformation.x, transformation.y, color, context.windowAspectRatio)
super.render(transformation)
Component.drawFancyRect(bounds, context.offset.x, context.offset.y, color, context.windowAspectRatio)
super.render(context)
}

override def handleEvent(event: Event): Boolean = event match {
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/scala/hexacraft/gui/comp/Component.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ abstract class Component {

def tick(ctx: TickContext): Unit = ()

def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
def render(context: RenderContext): Unit = {
textMaster.setWindowAspectRatio(context.windowAspectRatio)
textMaster.render(transformation.x, transformation.y)
textMaster.render(context.offset.x, context.offset.y)
}

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

This file was deleted.

10 changes: 6 additions & 4 deletions client/src/main/scala/hexacraft/gui/comp/ScrollPane.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ class ScrollPane(

def addComponent(comp: Component & Boundable): Unit = components.append(comp)

override def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
override def render(context: RenderContext): Unit = {
Component.drawRect(
location,
transformation.x,
transformation.y,
context.offset.x,
context.offset.y,
new Vector4f(0, 0, 0, 0.4f),
context.windowAspectRatio
)

val contentTransformation = transformation.offset(this.xOffset, this.yOffset)
val contentTransformation = context.withMoreOffset(this.xOffset, this.yOffset)
val loc = location.inScaledScreenCoordinates(context.frameBufferSize)

OpenGL.glScissor(loc.x, loc.y, loc.w, loc.h)
OpenGL.glEnable(OpenGL.State.ScissorTest)
components.foreach(_.render(contentTransformation))
OpenGL.glDisable(OpenGL.State.ScissorTest)

super.render(contentTransformation)
}

Expand Down
6 changes: 3 additions & 3 deletions client/src/main/scala/hexacraft/gui/comp/SubComponents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ trait SubComponents extends Component {
comps.exists(_.handleEvent(event)) || super.handleEvent(event)
}

override def render(transformation: GUITransformation)(using RenderContext): Unit = {
super.render(transformation)
comps.foreach(_.render(transformation))
override def render(context: RenderContext): Unit = {
super.render(context)
comps.foreach(_.render(context))
}

override def tick(ctx: TickContext): Unit = {
Expand Down
8 changes: 4 additions & 4 deletions client/src/main/scala/hexacraft/gui/comp/TextField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ class TextField(
}
}

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

override def handleEvent(event: Event): Boolean = {
Expand Down
7 changes: 3 additions & 4 deletions main/src/main/scala/hexacraft/main/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package hexacraft.main

import hexacraft.client.{BlockTextureLoader, GameClient}
import hexacraft.game.GameKeyboard
import hexacraft.gui.{Event, RenderContext, Scene, TickContext, WindowSize}
import hexacraft.gui.comp.GUITransformation
import hexacraft.gui.*
import hexacraft.infra.audio.AudioSystem
import hexacraft.main.GameScene.Event.{CursorCaptured, CursorReleased, GameQuit}
import hexacraft.server.GameServer
Expand Down Expand Up @@ -84,8 +83,8 @@ class GameScene private (val client: GameClient, server: Option[GameServer]) ext
client.frameBufferResized(width, height)
}

override def render(transformation: GUITransformation)(using RenderContext): Unit = {
client.render(transformation)
override def render(context: RenderContext): Unit = {
client.render(context)
}

override def tick(ctx: TickContext): Unit = {
Expand Down
8 changes: 4 additions & 4 deletions main/src/main/scala/hexacraft/main/MainWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hexacraft.main

import hexacraft.game.GameKeyboard
import hexacraft.gui.*
import hexacraft.gui.comp.GUITransformation
import hexacraft.infra.audio.AudioSystem
import hexacraft.infra.fs.FileSystem
import hexacraft.infra.gpu.OpenGL
Expand All @@ -11,7 +10,7 @@ import hexacraft.renderer.VAO
import hexacraft.server.ServerWorld
import hexacraft.util.{Resource, Result}

import org.joml.Vector2i
import org.joml.{Vector2f, Vector2i}

import java.io.File
import scala.collection.mutable
Expand Down Expand Up @@ -215,11 +214,12 @@ class MainWindow(

private def render(): Unit = {
if scene.isDefined then {
scene.get.render(GUITransformation(0, 0))(using
scene.get.render(
RenderContext(
this._windowSize.logicalAspectRatio,
this._windowSize.physicalSize,
mouse.currentPos.heightNormalizedPos(this.windowSize.logicalSize)
mouse.currentPos.heightNormalizedPos(this.windowSize.logicalSize),
Vector2f(0, 0)
)
)
}
Expand Down
11 changes: 6 additions & 5 deletions main/src/main/scala/hexacraft/main/Menus.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package hexacraft.main

import com.martomate.nbt.Nbt
import hexacraft.gui.{LocationInfo, RenderContext, Scene}
import hexacraft.gui.comp.*
import hexacraft.infra.fs.{FileSystem, NbtIO}
import hexacraft.renderer.TextureSingle
import hexacraft.util.Channel
import hexacraft.world.WorldSettings

import com.martomate.nbt.Nbt

import java.io.File
import java.nio.file.Path
import scala.util.Random

object Menus {

abstract class MenuScene extends Scene with SubComponents {
override def render(transformation: GUITransformation)(using context: RenderContext): Unit = {
override def render(context: RenderContext): Unit = {
Component.drawImage(
LocationInfo(-context.windowAspectRatio, -1, context.windowAspectRatio * 2, 2),
transformation.x,
transformation.y,
context.offset.x,
context.offset.y,
TextureSingle.getTexture("textures/gui/menu/background"),
context.windowAspectRatio
)
super.render(transformation)
super.render(context)
}
}

Expand Down

0 comments on commit bf0b5d4

Please sign in to comment.