Fix zooming. Fixes #13.

This commit is contained in:
Robbie Gleichman 2020-11-27 20:58:15 -08:00
parent e7c3c2ff4c
commit 739c1dfaf7
2 changed files with 27 additions and 12 deletions

View File

@ -215,7 +215,7 @@ _drawCircle (x, y) = do
drawNode :: Double -> (Int, Element) -> Render () drawNode :: Double -> (Int, Element) -> Render ()
drawNode zoom (elemId, Element {..}) = do drawNode zoom (elemId, Element {..}) = do
let (x, y) = _elPosition let (x, y) = _elPosition
(width, height) = Tuple.both (* zoom) _elSize (width, height) = _elSize
halfWidth = width / 2 halfWidth = width / 2
Cairo.setSourceRGB 1 0 0 Cairo.setSourceRGB 1 0 0
@ -246,11 +246,11 @@ updateBackground _canvas stateRef = do
traverse_ (drawNode (_asZoom stateVal)) (IntMap.toList (_asElements stateVal)) traverse_ (drawNode (_asZoom stateVal)) (IntMap.toList (_asElements stateVal))
findElementByPosition :: findElementByPosition ::
Double -> IntMap.IntMap Element -> (Double, Double) -> Maybe (Int, Element) IntMap.IntMap Element -> (Double, Double) -> Maybe (Int, Element)
findElementByPosition zoom elements (mouseX, mouseY) = findElementByPosition elements (mouseX, mouseY) =
let mouseInElement (_elementId, Element {_elPosition, _elSize}) = let mouseInElement (_elementId, Element {_elPosition, _elSize}) =
let (x, y) = _elPosition let (x, y) = _elPosition
(width, height) = Tuple.both (* zoom) _elSize (width, height) = _elSize
in mouseX >= x && mouseX <= (x + width) in mouseX >= x && mouseX <= (x + width)
&& mouseY >= y && mouseY >= y
&& mouseY <= (y + height) && mouseY <= (y + height)
@ -283,11 +283,11 @@ addNodeWithId :: ElemId -> (Double, Double) -> AppState -> AppState
addNodeWithId addNodeWithId
nodeId nodeId
addPosition addPosition
state@AppState {_asElements, _asHistory, _asBiggestID} = state@AppState {_asElements, _asHistory, _asBiggestID, _asZoom} =
let newNode = let newNode =
Element Element
{ _elPosition = addPosition, { _elPosition = addPosition,
_elSize = nodeSize, _elSize = Tuple.both (* _asZoom) nodeSize,
_elZ = 0 _elZ = 0
} }
newElements = newElements =
@ -330,11 +330,27 @@ abort :: AppState -> AppState
abort state@AppState {_asHistory, _asUndoPosition} = abort state@AppState {_asHistory, _asUndoPosition} =
state {_asUndoPosition = _asHistory} state {_asUndoPosition = _asHistory}
-- | Adjust the zoom factor and update the node positions with the new
-- zoom factor.
adjustZoom :: Double -> AppState -> AppState adjustZoom :: Double -> AppState -> AppState
adjustZoom zoomAdjustment state@AppState {_asZoom} = adjustZoom zoomAdjustment state@AppState {_asZoom, _asElements} =
state {_asZoom = max minimumZoom newZoom} state
{ _asZoom = newZoom,
_asElements = newElements
}
where where
newZoom = _asZoom * zoomAdjustment adjustedZoom = _asZoom * zoomAdjustment
(newZoom, newElements) =
if adjustedZoom > minimumZoom
then (adjustedZoom, fmap zoomElement _asElements)
else (_asZoom, _asElements)
-- newZoom = max minimumZoom (_asZoom * zoomAdjustment)
zoomElement :: Element -> Element
zoomElement element@Element {_elPosition, _elSize} =
element
{ _elPosition = Tuple.both (* zoomAdjustment) _elPosition,
_elSize = Tuple.both (* zoomAdjustment) _elSize
}
processInput :: InputEvent -> AppState -> AppState processInput :: InputEvent -> AppState -> AppState
processInput inputEvent oldState = processInput inputEvent oldState =
@ -367,7 +383,7 @@ updateState
elementwiseOp elementwiseOp
(-) (-)
_inMouseXandY _inMouseXandY
(Tuple.both (* (_asZoom / 2)) _elSize) (Tuple.both (* 0.5) _elSize)
in oldNode {_elPosition = newPosition} in oldNode {_elPosition = newPosition}
) )
(_unElemId nodeId) (_unElemId nodeId)
@ -417,7 +433,7 @@ leftClickAction inputsRef stateRef eventButton =
mousePosition <- getXandY eventButton mousePosition <- getXandY eventButton
state <- readIORef stateRef state <- readIORef stateRef
let mElem = findElementByPosition (_asZoom state) (_asElements state) mousePosition let mElem = findElementByPosition (_asElements state) mousePosition
addClickEvent inputs@Inputs {_inEvents} = addClickEvent inputs@Inputs {_inEvents} =
case mElem of case mElem of

View File

@ -1,7 +1,6 @@
# Todo # Todo
## GUI Todo Now ## GUI Todo Now
* Add zooming [Bug #13](https://github.com/rgleichman/glance/issues/13)
* Add panning * Add panning
## Non-GUI Todo Now ## Non-GUI Todo Now