Fix the windows

This commit is contained in:
LeshaInc 2019-09-21 13:38:49 +03:00
parent b558a24b0f
commit f8ab31a437
No known key found for this signature in database
GPG Key ID: B4855290FC36DE72
12 changed files with 109 additions and 64 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 B

View File

Before

Width:  |  Height:  |  Size: 306 B

After

Width:  |  Height:  |  Size: 306 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -1,5 +1,5 @@
BackgroundPattern 0 0 304 304 BackgroundPattern 0 0 304 304
Empty 340 197 1 1 Empty 337 197 1 1
ShadowBorder 305 197 1 24 ShadowBorder 305 197 1 24
ShadowCorner 424 0 24 24 ShadowCorner 424 0 24 24
buttons/PowerOff 441 25 18 18 buttons/PowerOff 441 25 18 18
@ -85,8 +85,8 @@ screen/CornerBL 459 180 8 8
screen/CornerBR 468 180 8 8 screen/CornerBR 468 180 8 8
screen/CornerTL 441 180 8 10 screen/CornerTL 441 180 8 10
screen/CornerTR 450 180 8 10 screen/CornerTR 450 180 8 10
window/BorderH 333 197 1 4 window/BorderDark 333 197 1 4
window/BorderV 335 197 4 1 window/BorderLight 335 197 1 4
window/CloseButton 477 180 7 6 window/CloseButton 477 180 7 6
window/CornerBL 313 197 4 4 window/CornerBL 313 197 4 4
window/CornerBR 318 197 4 4 window/CornerBR 318 197 4 4

View File

@ -26,9 +26,9 @@ trait BasicWindow extends Window {
state.goto(0.2f) state.goto(0.2f)
} }
protected def backgroundAlpha: Float = state.value.min(0.5f) * 2 protected def backgroundAlpha: Float = state.value
protected def shadowAlpha: Float = (state.value - 0.5f).max(0) protected def shadowAlpha: Float = state.value * 0.5f
override def update(): Unit = { override def update(): Unit = {
super.update() super.update()

View File

@ -15,7 +15,7 @@ class ComputerWindow extends BasicWindow {
val y = position.y val y = position.y
val w = size.width val w = size.width
val h = size.height val h = size.height
DrawUtils.shadow(g, x - 22, y - 22, w + 44, h + 52, shadowAlpha) // DrawUtils.shadow(g, x - 22, y - 22, w + 44, h + 52, shadowAlpha)
DrawUtils.screenBorder(g, x, y, w, h, RGBAColorNorm(1, 1, 1, backgroundAlpha)) DrawUtils.windowBorder(g, x, y, w, h, RGBAColorNorm(1, 1, 1, backgroundAlpha))
} }
} }

View File

@ -4,7 +4,7 @@ import ocelot.desktop.geometry.Rect2D
import ocelot.desktop.ui.widget.Widget import ocelot.desktop.ui.widget.Widget
trait Window extends Widget { trait Window extends Widget {
def dragRegions: Iterator[Rect2D] = Iterator() def dragRegions: Iterator[Rect2D] = Iterator(Rect2D(position, size))
def onOpened(): Unit = {} def onOpened(): Unit = {}

View File

@ -18,51 +18,74 @@ class WindowPool extends Widget with DragHandler {
window.size = window.minimumSize window.size = window.minimumSize
window.position = (size / 2 - window.size / 2).toVector.round window.position = (size / 2 - window.size / 2).toVector.round
window.show() window.show()
window.focus() children +:= window
windows.lastOption.foreach(_.unfocus()) changeFocus(0)
children :+= window
} }
case CloseWindowEvent(window) => case CloseWindowEvent(window) =>
val idx = windows.indexOf(window) val idx = windows.indexOf(window)
children = children.patch(idx, Nil, 1) children = children.patch(idx, Nil, 1)
changeFocus(windows.length - 1)
case MouseEvent(MouseEvent.State.Press, _) => case MouseEvent(MouseEvent.State.Press, _) =>
val mousePos = UiHandler.mousePosition val mousePos = UiHandler.mousePosition
findWindowUnderCursor(mousePos).foreach(pair => { findWindowUnderCursor(mousePos).foreach(changeFocus)
val (window, idx) = pair
// if (!window.isFocused) {
window.focus()
windows.lastOption.foreach(_.unfocus())
children = children.patch(idx, Nil, 1) :+ window
// }
})
case DragEvent(DragEvent.State.Start, MouseEvent.Button.Left, mousePos) => case DragEvent(DragEvent.State.Start, MouseEvent.Button.Left, mousePos) => startDragging(mousePos)
movingWindow = findWindowUnderCursor(mousePos) case DragEvent(DragEvent.State.Drag, MouseEvent.Button.Left, mousePos) => continueDragging(mousePos)
.map(_._1) case DragEvent(DragEvent.State.Stop, MouseEvent.Button.Left, _) => endDragging()
.filter(_.dragRegions.forall(_.contains(mousePos)))
grabPoint = movingWindow.map(mousePos - _.position).getOrElse(grabPoint)
if (movingWindow.isDefined) UiHandler.cursor = Cursor.Hand
case DragEvent(DragEvent.State.Drag, MouseEvent.Button.Left, mousePos) =>
movingWindow.foreach(_.position = mousePos - grabPoint)
if (movingWindow.isDefined) UiHandler.cursor = Cursor.Hand
case DragEvent(DragEvent.State.Stop, MouseEvent.Button.Left, _) =>
movingWindow = None
case _ => case _ =>
} }
private def windows: Array[Window] = children.map(_.asInstanceOf[Window]) private def windows: Array[Window] = children.map(_.asInstanceOf[Window])
private def findWindowUnderCursor(pos: Vector2D): Option[(Window, Int)] = private def findWindowUnderCursor(pos: Vector2D): Option[Int] =
windows windows
.reverseIterator .reverseIterator
.zip((children.length - 1 to 0 by -1).iterator) .zip((children.length - 1 to 0 by -1).iterator)
.find(_._1.bounds.contains(pos)) .find(_._1.bounds.contains(pos))
.map(_._2)
private def changeFocus(idx: Int): Unit = {
if (idx < 0 || idx >= children.length) return
val window = windows(idx)
windows.lastOption.foreach(_.unfocus())
window.focus()
children = children.patch(idx, Nil, 1) :+ window
}
private def startDragging(mousePos: Vector2D): Unit = {
val idx = findWindowUnderCursor(mousePos) match {
case Some(value) => value
case None => return
}
println(idx)
val window = windows(idx)
if (window.dragRegions.exists(_.contains(mousePos))) {
movingWindow = Some(window)
grabPoint = mousePos - window.position
changeFocus(idx)
}
}
private def continueDragging(mousePos: Vector2D): Unit = {
val window = movingWindow match {
case Some(value) => value
case None => return
}
window.position = mousePos - grabPoint
UiHandler.cursor = Cursor.Hand
}
private def endDragging(): Unit = {
movingWindow = None
}
override def handleEvent(event: Event): Unit = { override def handleEvent(event: Event): Unit = {
eventHandlers(event) eventHandlers(event)

View File

@ -65,8 +65,7 @@ class WorkspaceView extends Widget with DragHandler with ClickHandler {
windowPool.handleEvent(OpenWindowEvent(window)) windowPool.handleEvent(OpenWindowEvent(window))
case None => case None =>
println(nodeSelector) // windowPool.handleEvent(OpenWindowEvent(nodeSelector))
windowPool.handleEvent(OpenWindowEvent(nodeSelector))
} }
} }

View File

@ -29,10 +29,24 @@ object DrawUtils {
def windowBorder(g: Graphics, x: Float, y: Float, w: Float, h: Float, def windowBorder(g: Graphics, x: Float, y: Float, w: Float, h: Float,
color: Color = RGBAColor(255, 255, 255)): Unit = { color: Color = RGBAColor(255, 255, 255)): Unit = {
g.sprite("window/CornerTL", x - 16, y - 20, 16, 20, color) g.sprite("window/CornerTL", x, y, 8, 8, color)
g.sprite("window/CornerTR", x + w, y - 20, 16, 20, color) g.sprite("window/CornerTR", x + w - 8, y, 8, 8, color)
g.sprite("window/CornerBL", x - 16, y + h, 16, 16, color) g.sprite("window/CornerBL", x, y + h - 8, 8, 8, color)
g.sprite("window/CornerBR", x + w, y + h, 16, 16, color) g.sprite("window/CornerBR", x + w - 8, y + h - 8, 8, 8, color)
g.sprite("window/BorderLight", x + 8, y, w - 16, 8, color)
g.sprite("window/BorderDark", x + 8, y + h - 8, w - 16, 8, color)
g.save()
g.translate(x, y + 8)
g.rotate(270.toRadians)
g.sprite("window/BorderLight", 0, 0, -h + 16, 8, color)
g.sprite("window/BorderDark", 0, w - 8, -h + 16, 8, color)
g.restore()
g.sprite = "Empty"
g.foreground = RGBAColor(198, 198, 198, color.toRGBA.a)
g.rect(x + 8, y + 8, w - 16, h - 16)
} }
def shadow(g: Graphics, x: Float, y: Float, w: Float, h: Float, a: Float = 0.8f): Unit = { def shadow(g: Graphics, x: Float, y: Float, w: Float, h: Float, a: Float = 0.8f): Unit = {
@ -64,7 +78,7 @@ object DrawUtils {
g.save() g.save()
g.translate(x + w / 2f, y + h / 2f) g.translate(x + w / 2f, y + h / 2f)
g.rotate(angle) g.rotate(angle)
g.sprite("ShadowCorner", -w / 2f, -h / 2f, w, h, col) g.sprite(sprite, -w / 2f, -h / 2f, w, h, col)
g.restore() g.restore()
} }

View File

@ -3,51 +3,60 @@ package ocelot.desktop.util.animation
import ocelot.desktop.ui.UiHandler import ocelot.desktop.ui.UiHandler
import ocelot.desktop.util.animation.easing.{Easing, EasingFunction} import ocelot.desktop.util.animation.easing.{Easing, EasingFunction}
class ValueAnimation(init: Float = 0f, speed: Float = 2f) { class ValueAnimation(init: Float = 0f, speed: Float = 7f) {
// broken private var _value = init
private var start = init
private var _value: Float = init private var end = init
private var target = 0f private var time = 0f
private var timeToTarget = 0f
private var totalDistance = 0f
private var reached = false private var reached = false
private var _easing = Easing.linear private var _easing: EasingFunction = Easing.easeInOutQuad
private var nextEasing = easing private var nextEasing = _easing
def easing: EasingFunction = _easing def easing: EasingFunction = _easing
def easing_=(value: EasingFunction): Unit = { def easing_=(value: EasingFunction): Unit = {
nextEasing = value nextEasing = value
updateEasing()
} }
def value: Float = _value def value: Float = _value
def jump(to: Float): Unit = { def jump(to: Float): Unit = {
_value = to _value = to
timeToTarget = 0 start = to
target = to end = to
reached = true updateReached()
} }
def goto(to: Float): Unit = { def goto(to: Float): Unit = {
target = to start = _value
totalDistance = (_value - target).abs end = to
timeToTarget = totalDistance / speed time = 0
reached = (_value - target).abs < 0.00001 updateReached()
} }
def update(): Unit = { def update(): Unit = {
// if (reached) return if (reached) return
val dt = UiHandler.dt val dist = (end - start).abs
timeToTarget = (timeToTarget - dt / speed).max(0) val dt = (UiHandler.dt * speed) / dist
time = (time + dt).min(1).max(0)
if (_value > target) _value = if (start > end)
_value = target + easing(timeToTarget) * totalDistance start - time * dist
else else
_value = target - easing(timeToTarget) * totalDistance start + time * dist
reached = (_value - target).abs < 0.00001 updateReached()
if (reached) _easing = nextEasing
}
private def updateReached(): Unit = {
reached = (_value - end).abs < 0.001
}
private def updateEasing(): Unit = {
updateReached()
if (reached) _easing = nextEasing if (reached) _easing = nextEasing
} }
} }