Fix scroll & layouts

This commit is contained in:
LeshaInc 2019-03-09 14:14:19 +02:00
parent 2782ae253c
commit fd592a9b9d
No known key found for this signature in database
GPG Key ID: B4855290FC36DE72
7 changed files with 45 additions and 24 deletions

View File

@ -70,6 +70,7 @@ class Graphics extends Logging {
def setScissor(x: Int, y: Int, width: Int, height: Int): Unit = {
flush()
stack.head.scissor = Some((x, y, width, height))
GL11.glScissor(x, this.height - height - y, width, height)
GL11.glEnable(GL11.GL_SCISSOR_TEST)
}
@ -80,6 +81,7 @@ class Graphics extends Logging {
def setScissor(): Unit = {
flush()
stack.head.scissor = None
GL11.glDisable(GL11.GL_SCISSOR_TEST)
}
@ -145,8 +147,12 @@ class Graphics extends Logging {
// ^ dirty hack to avoid edge bleeding, somehow works
)
rectRenderer.schedule(MeshInstance(stack.head.background, z, Transform2D.translate(x, y) >> Transform2D.scale(width, height), emptySpriteTrans))
textRenderer.schedule(MeshInstance(stack.head.foreground, z + 1, Transform2D.translate(x, y) >> Transform2D.scale(width, height), uvTransform))
val transform =
Transform2D.translate(x, y) >>
Transform2D.scale(width, height)
rectRenderer.schedule(MeshInstance(stack.head.background, z, transform, emptySpriteTrans))
textRenderer.schedule(MeshInstance(stack.head.foreground, z + 1, transform, uvTransform))
z += 2
}

View File

@ -43,7 +43,7 @@ object UiHandler extends Logging {
def init(root: Widget): Unit = {
this.root = root
root.size = Size2D.Zero
root.size = Size2D(800, 600)
sizeLimits = SizeLimits(root.minimumSize, root.maximumSize)
GLFWErrorCallback.createPrint(System.err).set

View File

@ -92,7 +92,7 @@ class LinearLayout(widget: Widget, orientation: Orientation.Value = Orientation.
for (child <- widget.children) {
orientation match {
case Orientation.Vertical =>
child.position.x = pos
child.position.y = pos
pos += child.size.height
case Orientation.Horizontal =>

View File

@ -8,11 +8,15 @@ class RootWidget(screen: Screen) extends Widget {
val screenWidget = new ScreenWidget(screen)
// children += new ScrollView(new SomeOtherWidget {
// children += new SomeWidget
// children += new SomeWidget
// }
// )
children += new ScrollView(new SomeOtherWidget {
children += new SomeWidget
children += new SomeWidget
children += new SomeWidget
children += new SomeWidget
children += new SomeWidget
children += new SomeWidget
}
)
children += new ScrollView(screenWidget)
}

View File

@ -87,7 +87,6 @@ class ScreenWidget(screen: Screen) extends Widget with Logging {
val w = math.round(fontSize * width / 2f) + 32
val h = math.round(fontSize * height) + 32
g.clear(sx, sy, w, h)
g.foreground = IntColor(0x333333)
g.sprite = "Empty"
g.rect(sx, sy, w, h)

View File

@ -39,8 +39,11 @@ class ScrollView(inner: Widget) extends Widget with Logging {
override def draw(g: Graphics): Unit = {
g.setScissor(position.x, position.y, size.width, size.height)
inner.position = position - Vector2D(xOffset, yOffset)
inner.relayout() // FIXME
inner.draw(g)
g.setScissor()
g.sprite = "Empty"
@ -65,8 +68,8 @@ class ScrollView(inner: Widget) extends Widget with Logging {
override def update(): Unit = {
val mousePos = UiHandler.mousePosition
vAnimDir = if (mousePos.x > size.width - 12 || dragging == 1) 1 else -1
hAnimDir = if (mousePos.y > size.height - 12 || dragging == 2) 1 else -1
vAnimDir = if (vThumbHoverArea.contains(mousePos) || dragging == 1) 1 else -1
hAnimDir = if (hThumbHoverArea.contains(mousePos) || dragging == 2) 1 else -1
vAnim = math.max(0f, math.min(1f, vAnim + UiHandler.dt / 0.2f * vAnimDir))
hAnim = math.max(0f, math.min(1f, hAnim + UiHandler.dt / 0.2f * hAnimDir))
@ -88,17 +91,17 @@ class ScrollView(inner: Widget) extends Widget with Logging {
private def drawVThumb(g: Graphics): Unit = {
val b = vThumbBounds
g.foreground = RGBAColorNorm(0.9f, 0.9f, 0.9f, vAnim * 0.15f)
g.rect(position.x + size.width - 12, position.y, 12, size.height)
g.rect(position.x + size.width - 11, position.y, 10, size.height)
g.foreground = RGBAColorNorm(0.6f, 0.15f, 0.35f, vAnim * 0.5f + 0.4f)
g.rect(b.x, b.y, b.w, b.h)
g.rect(b.x + 3, b.y, b.w - 6, b.h)
}
private def drawHThumb(g: Graphics): Unit = {
val b = hThumbBounds
g.foreground = RGBAColorNorm(0.9f, 0.9f, 0.9f, hAnim * 0.15f)
g.rect(position.x, position.y + size.height - 12, size.width - 12, 12)
g.rect(position.x, position.y + size.height - 11, size.width - 12, 10)
g.foreground = RGBAColorNorm(0.6f, 0.15f, 0.35f, hAnim * 0.5f + 0.4f)
g.rect(b.x, b.y, b.w, b.h)
g.rect(b.x, b.y + 3, b.w, b.h - 6)
}
private def clampOffsets(): Unit = {
@ -106,18 +109,26 @@ class ScrollView(inner: Widget) extends Widget with Logging {
yOffset = math.max(0, math.min(inner.size.height - size.height, yOffset))
}
private def vThumbBounds: Rect2D = {
val x = position.x + size.width - 10
val y = yOffset * vThumbCoeff
private def vThumbHoverArea: Rect2D = {
Rect2D(position.x + size.width - 12, position.y, 12, size.height)
}
Rect2D(x, y + 2, 8, vThumbCoeff * size.height)
private def vThumbBounds: Rect2D = {
val x = position.x + size.width - 12
val y = position.y + yOffset * vThumbCoeff
Rect2D(x, y + 2, 12, vThumbCoeff * size.height)
}
private def hThumbHoverArea: Rect2D = {
Rect2D(position.x, position.y + size.height - 12, size.width, 12)
}
private def hThumbBounds: Rect2D = {
val x = xOffset * hThumbCoeff
val y = position.y + size.height - 10
val x = position.x + xOffset * hThumbCoeff
val y = position.y + size.height - 12
Rect2D(x + 2, y, hThumbCoeff * size.width, 8)
Rect2D(x + 2, y, hThumbCoeff * size.width, 12)
}
private def vThumbVisible: Boolean = inner.size.height > size.height

View File

@ -3,9 +3,10 @@ package ocelot.desktop.ui.widget
import ocelot.desktop.color.IntColor
import ocelot.desktop.graphics.Graphics
import ocelot.desktop.ui.layout.LinearLayout
import ocelot.desktop.util.Orientation
class SomeOtherWidget extends Widget {
override val layout = new LinearLayout(this)
override val layout = new LinearLayout(this, orientation = Orientation.Vertical)
override def draw(g: Graphics): Unit = {
g.sprite = "Empty"