Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
fix: now new functions start with an output node
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Apr 26, 2020
1 parent 403931f commit 3576566
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 34 deletions.
30 changes: 9 additions & 21 deletions src/Component/Editor.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Control.Monad.State (get, gets, modify_, put)
import Control.MonadZero (guard)
import Data.Array (foldr, (..))
import Data.Default (def)
import Data.Foldable (for_, sequence_)
import Data.Foldable (for_)
import Data.Lens (over, preview, set, view)
import Data.List.Lazy as List
import Data.Map as Map
Expand All @@ -27,7 +27,7 @@ import Lunarbox.Component.Editor.Tree as TreeC
import Lunarbox.Component.Icon (icon)
import Lunarbox.Component.Utils (container)
import Lunarbox.Config (Config)
import Lunarbox.Control.Monad.Effect (printString)
import Lunarbox.Control.Monad.Effect (print, printString)
import Lunarbox.Data.Dataflow.Class.Expressible (nullExpr)
import Lunarbox.Data.Dataflow.Expression (printSource)
import Lunarbox.Data.Dataflow.Native.Prelude (loadPrelude)
Expand All @@ -39,8 +39,8 @@ import Lunarbox.Data.Editor.Node.NodeData (NodeData(..), _NodeDataPosition, _Nod
import Lunarbox.Data.Editor.Node.NodeDescriptor (onlyEditable)
import Lunarbox.Data.Editor.Node.NodeId (NodeId(..))
import Lunarbox.Data.Editor.Node.PinLocation (Pin(..))
import Lunarbox.Data.Editor.Project (_projectNodeGroup, createFunction, emptyProject)
import Lunarbox.Data.Editor.State (State, Tab(..), _atColorMap, _atNode, _atNodeData, _currentFunction, _currentTab, _expression, _function, _functions, _isSelected, _lastMousePosition, _nextId, _nodeData, _panelIsOpen, _partialFrom, _partialTo, _project, _typeMap, compile, tabIcon, tryConnecting)
import Lunarbox.Data.Editor.Project (_projectNodeGroup, emptyProject)
import Lunarbox.Data.Editor.State (State, Tab(..), _atColorMap, _atNode, _atNodeData, _currentFunction, _currentTab, _expression, _function, _functionData, _functions, _isSelected, _lastMousePosition, _nextId, _nodeData, _panelIsOpen, _partialFrom, _partialTo, _typeMap, compile, initializeFunction, setCurrentFunction, tabIcon, tryConnecting)
import Lunarbox.Data.Graph as G
import Lunarbox.Data.Vector (Vec2)
import Lunarbox.Page.Editor.EmptyEditor (emptyEditor)
Expand Down Expand Up @@ -147,25 +147,13 @@ component =
else
set _currentTab newTab
CreateFunction name -> do
Tuple id setId <- createId
let
setFunction = over _project $ createFunction name id
modify_ $ setId <<< setFunction
modify_ $ initializeFunction name
s <- gets $ view _functionData
print s
print name
SelectFunction name -> modify_ $ setCurrentFunction name
StartFunctionCreation -> do
void $ query (SProxy :: _ "tree") unit $ tell TreeC.StartCreation
SelectFunction name -> do
-- we need the current function to lookup the function in the function graph
oldName <- gets $ view _currentFunction
functions <- gets $ view _functions
-- this is here to update the function the Scene component renders
when (name /= oldName)
$ sequence_ do
currentFunction <- name
function <-
G.lookup currentFunction functions
pure do
-- And finally, save the selected function in the state
modify_ $ set _currentFunction name <<< compile
SceneMouseDown position -> do
modify_ $ set _lastMousePosition $ Just position
SceneMouseMove position -> do
Expand Down
3 changes: 2 additions & 1 deletion src/Component/Editor/Add.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Halogen.HTML.Events (onClick)
import Halogen.HTML.Properties as HP
import Lunarbox.Capability.Editor.Type (generateTypeMap, prettify)
import Lunarbox.Component.Editor.HighlightedType (highlightTypeToHTML)
import Lunarbox.Component.Editor.Node (renderNode)
import Lunarbox.Component.Editor.Node (SelectionStatus(..), renderNode)
import Lunarbox.Component.Editor.Node as NodeC
import Lunarbox.Component.Icon (icon)
import Lunarbox.Component.Utils (className, container)
Expand Down Expand Up @@ -61,6 +61,7 @@ nodeInput typeMap name functionData =
, labels: mempty
, hasOutput: hasOutput node
, nodeDataMap: mempty
, selectionStatus: NothingSelected
, colorMap:
either (const mempty) identity
$ generateTypeMap
Expand Down
13 changes: 11 additions & 2 deletions src/Component/Editor/Node.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Lunarbox.Component.Editor.Node
( renderNode
, Input
( Input
, SelectionStatus(..)
, renderNode
) where

import Prelude
Expand Down Expand Up @@ -35,6 +36,12 @@ import Svg.Attributes (Color)
import Svg.Attributes as SA
import Svg.Elements as SE

-- A node can either have one of it's inputs, it's output or nothing selected
data SelectionStatus
= InputSelected Int
| OutputSelected
| NothingSelected

type Input h a
= { nodeData :: NodeData
, node :: Node
Expand All @@ -43,6 +50,7 @@ type Input h a
, colorMap :: Map Pin SA.Color
, hasOutput :: Boolean
, nodeDataMap :: Map NodeId NodeData
, selectionStatus :: SelectionStatus
}

type Actions a
Expand Down Expand Up @@ -81,6 +89,7 @@ renderNode { nodeData: nodeData
, hasOutput
, node
, nodeDataMap
, selectionStatus
} { select
, selectOutput
, selectInput
Expand Down
3 changes: 2 additions & 1 deletion src/Component/Editor/Scene.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Halogen.HTML as HH
import Halogen.HTML.Events (onMouseDown, onMouseMove, onMouseUp)
import Lunarbox.Capability.Editor.Type (ColoringError, generateTypeMap, prettify)
import Lunarbox.Component.Editor.HighlightedType (highlightTypeToSvg)
import Lunarbox.Component.Editor.Node (renderNode)
import Lunarbox.Component.Editor.Node (SelectionStatus(..), renderNode)
import Lunarbox.Component.Editor.Node.Label (labelText, label)
import Lunarbox.Data.Dataflow.Expression (Expression, sumarizeExpression)
import Lunarbox.Data.Dataflow.Expression as Expression
Expand Down Expand Up @@ -137,6 +137,7 @@ createNodeComponent { functionName
, labelText $ sumarizeExpression nodeExpression
]
, hasOutput: not $ is _OutputNode node
, selectionStatus: NothingSelected
}
{ select: selectNode id
, selectInput: selectInput id
Expand Down
12 changes: 5 additions & 7 deletions src/Component/Editor/Tree.purs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ component =
{ initialState: (\{ functions, selected } -> { functions, selected, creating: false, validationError: Nothing })
, render
, eval:
mkEval
$ defaultEval
{ handleAction = handleAction
, handleQuery = handleQuery
}
mkEval
$ defaultEval
{ handleAction = handleAction
, handleQuery = handleQuery
}
}
where
-- we need a ref to access the content of the input element
Expand Down Expand Up @@ -139,8 +139,6 @@ component =
-- this notifies the parent element we just created a new function
-- the parent ususally has to add the function to the graph
raise $ CreatedFunction functionName
-- we also need to make the parent select this function too
raise $ SelectedFunction $ Just functionName

handleQuery :: forall a. Query a -> HalogenM State Action ChildSlots Output m (Maybe a)
handleQuery = case _ of
Expand Down
7 changes: 7 additions & 0 deletions src/Data/Editor/FunctionData.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Lunarbox.Data.Editor.FunctionData

import Prelude
import Data.Default (class Default, def)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Lens (Lens', iso, set)
import Data.Lens.Record (prop)
import Data.Newtype (class Newtype, unwrap, wrap)
Expand All @@ -25,8 +27,13 @@ newtype FunctionData
}
}

derive instance genericFunctionData :: Generic FunctionData _

derive instance newtypeFunctionData :: Newtype FunctionData _

instance showFunctionData :: Show FunctionData where
show = genericShow

instance defaultFunctionData :: Default FunctionData where
def =
FunctionData
Expand Down
7 changes: 7 additions & 0 deletions src/Data/Editor/Node/NodeData.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Lunarbox.Data.Editor.Node.NodeData

import Prelude
import Data.Default (class Default)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Lens (Lens', iso)
import Data.Lens.Record (prop)
import Data.Newtype (class Newtype, unwrap, wrap)
Expand All @@ -25,6 +27,11 @@ derive instance newtypeNodeData :: Newtype NodeData _

derive instance eqNodeData :: Eq NodeData

derive instance genericNodeData :: Generic NodeData _

instance showNodeData :: Show NodeData where
show = genericShow

instance ordNodeData :: Ord NodeData where
compare (NodeData { zPosition }) (NodeData ({ zPosition: zPosition' })) = compare zPosition zPosition'

Expand Down
42 changes: 40 additions & 2 deletions src/Data/Editor/State.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Lunarbox.Data.Editor.State
, tabIcon
, tryConnecting
, compile
, setCurrentFunction
, initializeFunction
, _nodeData
, _atNodeData
, _project
Expand Down Expand Up @@ -32,6 +34,7 @@ module Lunarbox.Data.Editor.State
) where

import Prelude
import Data.Default (def)
import Data.Either (Either(..))
import Data.Lens (Lens', Traversal', _Just, lens, over, preview, set, view)
import Data.Lens.At (at)
Expand All @@ -52,10 +55,10 @@ import Lunarbox.Data.Editor.FunctionName (FunctionName)
import Lunarbox.Data.Editor.Location (Location)
import Lunarbox.Data.Editor.Node (Node, _nodeInputs)
import Lunarbox.Data.Editor.Node.NodeData (NodeData, _NodeDataSelected)
import Lunarbox.Data.Editor.Node.NodeId (NodeId)
import Lunarbox.Data.Editor.Node.NodeId (NodeId(..))
import Lunarbox.Data.Editor.NodeGroup (NodeGroup, _NodeGroupNodes)
import Lunarbox.Data.Editor.PartialConnection (PartialConnection, _from, _to)
import Lunarbox.Data.Editor.Project (Project, _ProjectFunctions, _atProjectFunction, _atProjectNode, _projectNodeGroup, compileProject)
import Lunarbox.Data.Editor.Project (Project, _ProjectFunctions, _atProjectFunction, _atProjectNode, _projectNodeGroup, compileProject, createFunction)
import Lunarbox.Data.Graph as G
import Lunarbox.Data.Lens (listToArrayIso)
import Lunarbox.Data.Vector (Vec2)
Expand Down Expand Up @@ -176,6 +179,19 @@ _currentNodeGroup =
)
)

_atCurrentNodeData :: NodeId -> Traversal' State (Maybe NodeData)
_atCurrentNodeData id =
lens
( \state -> do
currentFunction <- view _currentFunction state
view (_atNodeData currentFunction id) state
)
( \state value ->
fromMaybe state do
currentFunction <- view _currentFunction state
pure $ set (_atNodeData currentFunction id) value state
)

_currentNodes :: Traversal' State (G.Graph NodeId Node)
_currentNodes = _currentNodeGroup <<< _Just <<< _NodeGroupNodes

Expand Down Expand Up @@ -223,3 +239,25 @@ tryConnecting state =

state''' = set _partialTo Nothing $ set _partialFrom Nothing state''
pure $ compile state'''

-- Set the function the user is editing at the moment
setCurrentFunction :: Maybe FunctionName -> State -> State
setCurrentFunction = set _currentFunction

-- Creates a function, adds an output node and set it as the current edited function
initializeFunction :: FunctionName -> State -> State
initializeFunction name state =
let
id = NodeId $ show name <> "-output"

function = createFunction name id

state' = over _project function state

state''' = set (_atNodeData name id) (Just def) state''

state'''' = set (_atFunctionData name) (Just def) state'''

state'' = setCurrentFunction (Just name) state'
in
compile state''''

0 comments on commit 3576566

Please sign in to comment.