Advertisement
Guest User

Untitled

a guest
May 27th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.89 KB | None | 0 0
  1. import Prelude
  2. import Data.Maybe
  3. import Data.Char (isDigit)
  4. import Data.List ((\\), delete)
  5.  
  6.  
  7. import Eventloop.EventloopCore
  8. import Eventloop.DefaultConfiguration
  9. import Eventloop.Types.EventTypes
  10.  
  11. import qualified Eventloop.Module.Websocket.Canvas as C
  12. import qualified Eventloop.Module.BasicShapes as B
  13. import qualified Eventloop.Module.Websocket.Mouse as M
  14. import qualified Eventloop.Module.Websocket.Keyboard as K
  15. import qualified Eventloop.Module.StdOut as S
  16. import Eventloop.Module.Graphs
  17.  
  18.  
  19.  
  20.  
  21. {- | Start
  22. This function will start the eventloop system using the eventloopConfig
  23. -}
  24. start = startMainloop eventloopConfig
  25.  
  26. {- | The configuration of the Eventloop system
  27. Uses the graphs module to display graphs. This module
  28. depends on the Keyboard, Mouse, Canvas and BasicShapes modules
  29. -}
  30. eventloopConfig = defaultConfig { moduleConfigurations=[ defaultGraphsModuleConfiguration
  31. , B.defaultBasicShapesModuleConfiguration
  32. , C.defaultCanvasModuleConfiguration
  33. , M.defaultMouseModuleConfiguration
  34. , K.defaultKeyboardModuleConfiguration
  35. , S.defaultStdOutModuleConfiguration
  36. ]}
  37. where
  38. defaultConfig = allModulesEventloopConfiguration directedProgramState eventloop -- Uses beginProgramState and eventloop to build config
  39.  
  40.  
  41. {- | ProgramState
  42. This datatype shows which variables are kept
  43. -}
  44. data ProgramState
  45. = ProgramState { pressedKey :: [Char]
  46. , node1Select :: Maybe Node
  47. , node2Select :: Maybe Node
  48. , graph :: Graph
  49. , pathList :: [[Edge]]
  50. }
  51.  
  52.  
  53. getPathList (ProgramState _ _ _ _ pathList) = pathList !! 0
  54. {- | Begingraph
  55. This is the start state of the graph
  56. -}
  57. beginGraph = Graph allNodes allEdges Undirected Unweighted
  58. where
  59. allNodes = [ ('a', (50, 50), Red)
  60. , ('b', (150, 50), Blue)
  61. , ('c', (200, 200), Orange)
  62. , ('d', (300, 50), Green)
  63. , ('e', (300, 120), Purple)
  64. , ('f', (400, 80), Orange)
  65. ]
  66. allEdges = [ ('a', 'b', Green, 5, Thick)
  67. , ('c', 'b', Orange, 3, Thin)
  68. , ('c', 'a', Purple, 2, Thin)
  69. , ('d', 'f', Red, 4, Thin)
  70. , ('e', 'f', Blue, 3, Thin)
  71. , ('e', 'd', Purple, 2, Thin)
  72. ]
  73.  
  74. directedGraph = Graph allNodes allEdges Directed Weighted
  75. where
  76. allNodes = [ ('a', (50, 50), Red)
  77. , ('b', (150, 50), Blue)
  78. , ('c', (200, 200), Orange)
  79. , ('d', (300, 50), Green)
  80. , ('e', (300, 120), Purple)
  81. , ('f', (400, 80), Orange)
  82. ]
  83. allEdges = [ ('a', 'b', Green, 1, Thick)
  84. , ('a', 'c', Orange, 5, Thin)
  85. , ('b', 'd', Purple, 5, Thin)
  86. , ('b', 'c', Grey, 1, Thin)
  87. , ('c', 'e', Red, 2, Thin)
  88. , ('d', 'f', Blue, 5, Thin)
  89. , ('e', 'f', Purple, 1, Thin)
  90. ]
  91.  
  92. endGraph = Graph [] [] Undirected Unweighted
  93.  
  94.  
  95. {-| The beginstate of the ProgramState
  96. -}
  97. beginProgramState = ProgramState [] Nothing Nothing beginGraph []
  98. directedProgramState = ProgramState [] Nothing Nothing directedGraph []
  99.  
  100.  
  101. {- | Instructions
  102. This is the list of all possible instructions
  103. Feel free to add your own
  104. -}
  105. instructions = [ "Instructions"
  106. , "Press 'n' and click on the screen to create a new node"
  107. , "Press 'r', click on a node and press a letter to rename the node"
  108. , "Press 'e', click on two nodes to create an edge"
  109. , "Press 'd', click on a node to delete the node"
  110. , "Press 'w', click on two nodes and press a number to weight the edge in between"
  111. , "Press 'f', click on two nodes to delete an edge"
  112. , "Press 'c', click on a node to change its color to Red"
  113. , "Press 'u' to undo all coloring and set every node to Orange"
  114. , "Press 'esc' to abort the current operation and start another"
  115. ]
  116.  
  117.  
  118. {- | A variable showing which labels are used for visually added nodes
  119. -}
  120. automaticPossibleLabels :: [Label]
  121. automaticPossibleLabels = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9']
  122.  
  123.  
  124. {- | A function to determine which label can be used next
  125. -}
  126. nextLabel :: [Node] -> Label
  127. nextLabel nodes
  128. | null leftOverLabels = error "Did not have a leftover label to give to a node. Please do not create so many nodes!"
  129. | otherwise = head leftOverLabels
  130. where
  131. currentLabels = map (\(l, _, _) -> l) nodes
  132. leftOverLabels = automaticPossibleLabels \\ currentLabels
  133.  
  134.  
  135. {- | Add a node to the graph
  136. -}
  137. addNode :: Graph -> Node -> Graph
  138. addNode g@(Graph{nodes=ns}) n = g {nodes=(n:ns)}
  139.  
  140.  
  141. {- | Add an edge to the graph
  142. -}
  143. addEdge :: Graph -> Edge -> Graph
  144. addEdge g@(Graph{edges=es}) e = g {edges=(e:es)}
  145.  
  146.  
  147. {- | Create an edge based on two nodes
  148. Is drawn from node1 to node2
  149. -}
  150. createEdge :: Node -> Node -> Edge
  151. createEdge (l1, _, c) (l2, _, _)
  152. = (l1, l2, c, 0, Thin)
  153.  
  154.  
  155. {- | Finds the edge directed from the first to the second node
  156. -}
  157. findEdgeFromNodeToNode :: Node -> Node -> Graph -> Maybe Edge
  158. findEdgeFromNodeToNode n1 n2 g
  159. | null possibleEdges = Nothing
  160. | otherwise = Just $ head possibleEdges
  161. where
  162. allEdges = edges g
  163. possibleEdges = filter (edgeRunsFromNodeToNode n1 n2) allEdges
  164.  
  165.  
  166. {- | Finds all edges connected to this node
  167. -}
  168. findEdgesAtNode :: Node -> Graph -> [Edge]
  169. findEdgesAtNode (l, _, _) g
  170. = filter (\(el1, el2, _, _, _) -> el1 == l || el2 == l) allEdges
  171. where
  172. allEdges = edges g
  173.  
  174.  
  175. {- | Finds all edges that are between two nodes
  176. -}
  177. findEdgesBetweenNodes :: Node -> Node -> Graph -> [Edge]
  178. findEdgesBetweenNodes n1 n2 g
  179. = filter (edgeIsBetweenNodes n1 n2) allEdges
  180. where
  181. allEdges = edges g
  182.  
  183.  
  184. {- | Conditional to check if an edge is connected to both nodes
  185. -}
  186. edgeIsBetweenNodes :: Node -> Node -> Edge -> Bool
  187. edgeIsBetweenNodes (l1, _, _) (l2, _, _) (el1, el2, _, _, _)
  188. = (el1 == l1 && el2 == l2) || (el1 == l2 && el2 == l1)
  189.  
  190.  
  191. {- | Conditional to check if the runs is directed from the first
  192. to the second node
  193. -}
  194. edgeRunsFromNodeToNode :: Node -> Node -> Edge -> Bool
  195. edgeRunsFromNodeToNode (l1, _, _) (l2, _, _) (el1, el2, _, _, _)
  196. = (l1 == el1) && (l2 == el2)
  197.  
  198.  
  199. {- | Removes the node from the graph
  200. -}
  201. removeNode :: Node -> Graph -> Graph
  202. removeNode n g
  203. = g {nodes = allNodes'}
  204. where
  205. allNodes = nodes g
  206. allNodes' = delete n allNodes
  207.  
  208. {- | Removes the edge from the graph
  209. -}
  210. removeEdge :: Edge -> Graph -> Graph
  211. removeEdge e g
  212. = g {edges = allEdges'}
  213. where
  214. allEdges = edges g
  215. allEdges' = delete e allEdges
  216.  
  217. {- | Removes a node, and all edges connected to it,
  218. from the graph
  219. -}
  220. removeNodeWithAdjoiningEdges :: Node -> Graph -> Graph
  221. removeNodeWithAdjoiningEdges n g
  222. = g''
  223. where
  224. g' = removeNode n g
  225. g'' = foldr removeEdge g' (findEdgesAtNode n g)
  226.  
  227. {- | Rename a node in the edge to the new label
  228. if the node is connected to that edge
  229. -}
  230. renameNodeInEdge :: Node -> Label -> Edge -> Edge
  231. renameNodeInEdge (oldL, _, _) newL (el1, el2, color, weight, thickness)
  232. | oldL == el1 = (newL, el2, color, weight, thickness)
  233. | oldL == el2 = (el1, newL, color, weight, thickness)
  234. | otherwise = (el1, el2, color, weight, thickness)
  235.  
  236. getNeighbours :: Node -> Graph -> [Node]
  237. getNeighbours node g
  238. = getNB node allNodes g
  239. where
  240. allNodes = nodes g
  241.  
  242. getNB :: Node -> [Node] -> Graph -> [Node]
  243. getNB node [] g = []
  244. getNB node (x:xs) g
  245. | length (findEdgesBetweenNodes node x g) /= 0 = x: getNB node xs g
  246. | otherwise = getNB node xs g
  247.  
  248. getDirectedNeighbours :: Graph -> Node -> [Node]
  249. getDirectedNeighbours g node
  250. = getDNB g node allEdges
  251. where
  252. allEdges = findEdgesAtNode node g
  253.  
  254. getDNB :: Graph -> Node -> [Edge] -> [Node]
  255. getDNB g _ [] = []
  256. getDNB g (l,p,c) ((l1,l2,_,_,_):xs)
  257. | l1 == l = (getNode nodez l2): (getDNB g (l,p,c) xs)
  258. | isDirected g == Directed = getDNB g (l,p,c) xs
  259. | otherwise = (getNode nodez l2): (getDNB g (l,p,c) xs)
  260. where
  261. nodez = nodes g
  262.  
  263. getNode :: [Node] -> Label -> Node
  264. getNode [x] l1 = x
  265. getNode ((l,p,c):xs) l1 | l == l1 = (l,p,c)
  266. | otherwise = getNode xs l1
  267.  
  268.  
  269. isDirected (Graph _ _ d _) = d
  270.  
  271. colorNode :: Color -> Node -> Node
  272. colorNode col (l, p, _)
  273. = (l, p, col)
  274.  
  275. replaceNodes :: Graph -> [Node] -> [Node] -> Graph
  276. replaceNodes g [] _ = g
  277. replaceNodes g _ [] = g
  278. replaceNodes g (x:xs) (y:ys)
  279. = (flip addNode) x $ removeNode y (replaceNodes g xs ys)
  280.  
  281. checkPath :: Node -> Node -> Graph -> Bool
  282. checkPath n1 n2 g
  283. | length (findEdgesBetweenNodes n1 n2 g) /= 0 = True
  284. | otherwise = checkPathHelper n2 g' (getNeighbours n1 g)
  285. where
  286. g' = removeNodeWithAdjoiningEdges n1 g
  287.  
  288. checkP g n1 n2 = checkPath n1 n2 g
  289.  
  290. checkPathHelper :: Node -> Graph -> [Node] -> Bool
  291. checkPathHelper _ _ [] = False
  292. checkPathHelper n2 g (x:xs)
  293. | x == n2 = True
  294. | otherwise = checkPathHelper n2 g' xs && checkPathHelper n2 g' (getNeighbours x g')
  295. where
  296. g' = removeNodeWithAdjoiningEdges x g
  297.  
  298. findPaths :: Graph -> Node -> Node -> [[Node]]
  299. findPaths g end start | end == start = [[start]]
  300. | otherwise = map (start:) paths
  301. where
  302. nodes = getDirectedNeighbours g start
  303. emptyPaths = map (findPaths g' end) nodes
  304. paths' = removeEmpty emptyPaths
  305. paths = foldr (++) [] paths'
  306. g' = removeNodeWithAdjoiningEdges start g
  307.  
  308. removeEmpty [] = []
  309. removeEmpty ([]:xs) = removeEmpty xs
  310. removeEmpty (x:xs) = x: removeEmpty xs
  311.  
  312. colorPathRed :: [Edge] -> [Edge] -> [Edge]
  313. colorPathRed edgez colEdgez
  314. = colorE black blackPath Red
  315. where
  316. black = colorEdges edgez
  317. blackPath = colorEdges colEdgez
  318.  
  319. colorEdges :: [Edge] -> [Edge]
  320. colorEdges [] = []
  321. colorEdges (x:xs)
  322. = (colorEdge Black x): (colorEdges xs)
  323.  
  324. colE :: [[Edge]] -> Color -> [Edge]
  325. colE [] _ = []
  326. colE (x:xs) col
  327. = map (colorEdge col) x ++ (colE xs (nextCol col))
  328.  
  329. colorE :: [Edge] -> [Edge] -> Color -> [Edge]
  330. colorE alledges [] _ = alledges
  331. colorE alledges (y:ys) col
  332. = (colorEdge col y): (colorE (alledges\\[y]) ys col)
  333.  
  334. colorEdge col (l1, l2, c, l, t) = (l1, l2, col, l, t)
  335.  
  336. getEdgesList :: Graph -> [[Node]] -> [[Edge]]
  337. getEdgesList g [] = []
  338. getEdgesList g (x:xs)
  339. = (getEdges g x): (getEdgesList g xs)
  340.  
  341. getEdges :: Graph -> [Node] -> [Edge]
  342. getEdges g [x] = []
  343. getEdges g (x:x':xs)
  344. = (findEdgesBetweenNodes x x' g) ++ getEdges g (x':xs)
  345.  
  346. findSubGraph :: Graph -> [[Node]]
  347. findSubGraph g
  348. = findAllSubGraph g (nodes g)
  349.  
  350. findAllSubGraph :: Graph -> [Node] -> [[Node]]
  351. findAllSubGraph g [] = []
  352. findAllSubGraph g (x:xs)
  353. = subGraph : (findAllSubGraph g' others)
  354. where
  355. (subGraph, others) = getSubGraph g x
  356. g' = g {nodes = others}
  357.  
  358. getSubGraph :: Graph -> Node -> ([Node],[Node])
  359. getSubGraph g x
  360. = ([nodez | nodez <- nodes g,
  361. checkPath x nodez g && checkPath nodez x g || nodez == x],
  362. [nodes2 | nodes2 <- nodes g,
  363. (not (checkPath x nodes2 g && checkPath nodes2 x g || nodes2 == x))]
  364. )
  365.  
  366. colorSubGraph :: [[Node]] -> [Node]
  367. colorSubGraph nodes
  368. = colSG nodes Orange
  369.  
  370. colSG :: [[Node]] -> Color -> [Node]
  371. colSG [] col = []
  372. colSG (x:xs) col
  373. = (map (colorNode col) x) ++ colSG xs (nextCol col)
  374.  
  375. nextCol col
  376. | col == Orange = Red
  377. | col == Red = Blue
  378. | col == Blue = Green
  379. | col == Green = Yellow
  380. | col == Yellow = Purple
  381. | col == Purple = Black
  382. | col == Black = Grey
  383. | col == Grey = White
  384. | col == White = Orange
  385. | otherwise = Orange
  386.  
  387. getShortest :: [[Edge]] -> [Edge]
  388. getShortest [x] = x
  389. getShortest (x:xs)
  390. | calculateValue x < calculateValue (getShortest xs) = x
  391. | otherwise = getShortest xs
  392.  
  393. calculateValue :: [Edge] -> Float
  394. calculateValue [] = 0
  395. calculateValue ((_,_,_,l,_):xs)
  396. = l + calculateValue xs
  397.  
  398. {- | The eventloop
  399. This function uses the current state and an In event to determine
  400. the new state and what changes should be made as a list of Out events.
  401. -}
  402. eventloop :: ProgramState -> In -> (ProgramState, [Out])
  403.  
  404. eventloop ps Start
  405. = (ps, [OutGraphs SetupGraphs, OutGraphs $ DrawGraph (graph ps), OutGraphs $ Instructions instructions])
  406.  
  407. eventloop ps@(ProgramState "f" (Just node1s) _ g d) (InGraphs (Mouse (Click _) p))
  408. | nodeAtPosM == Nothing = (ps, [])
  409. | edgeM == Nothing = (ProgramState [] Nothing Nothing g d, [])
  410. | otherwise = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Deleted edge from '" ++ [l1] ++ "' to '" ++ [l2] ++ "'\n"])
  411. where
  412. nodeAtPosM = onNode allNodes p
  413. (Just nodeAtPos) = nodeAtPosM
  414. allNodes = nodes g
  415. edgeM = findEdgeFromNodeToNode node1s nodeAtPos g
  416. (Just edge) = edgeM
  417. (l1, l2, _, _, _) = edge
  418. g' = removeEdge edge g
  419.  
  420. {- | If 'w' has been pressed, two nodes are selected and the next key
  421. is a digit, the edge running from node1s to node2s is weighted as that
  422. digit
  423. -}
  424. eventloop ps@(ProgramState "w" (Just node1s) (Just node2s) g d) (InGraphs (Key [key]))
  425. | isDigit key && edgeM /= Nothing = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Weighted edge from '" ++ [l1] ++ "' to '" ++ [l2] ++ "' with " ++ (show weight) ++ "\n"])
  426. | otherwise = (ProgramState [] Nothing Nothing g d, [])
  427. where
  428. edgeM = findEdgeFromNodeToNode node1s node2s g
  429. (Just edge@(l1, l2, col, w, thick)) = edgeM
  430. weight = read [key] :: Weight
  431. edge' = (l1, l2, col, weight, thick)
  432. g' = (flip addEdge) edge' $ removeEdge edge g
  433.  
  434. {- | If 'd' has been pressed and a node is selected
  435. , the node is deleted from the graph
  436. -}
  437. eventloop ps@(ProgramState "d" _ _ g d) (InGraphs (Mouse (Click _) p))
  438. | nodeAtPosM == Nothing = (ps, [])
  439. | otherwise = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Deleted node '" ++ [l] ++ "'\n"])
  440. where
  441. (l, _, _) = nodeAtPos
  442. nodeAtPosM = onNode allNodes p
  443. (Just nodeAtPos) = nodeAtPosM
  444. allNodes = nodes g
  445. g' = removeNodeWithAdjoiningEdges nodeAtPos g
  446.  
  447.  
  448. {- | If 'e' has been pressed, a node selected and a new node is selected
  449. an edge is drawn between the two nodes
  450. -}
  451. eventloop ps@(ProgramState "e" (Just node1s) _ g d) (InGraphs (Mouse (Click _) p))
  452. | nodeAtPosM == Nothing = (ps, [])
  453. | otherwise = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Created edge from '" ++ [l1] ++ "' to '" ++ [l2] ++ "'\n"])
  454. where
  455. (l1, _, _) = node1s
  456. (l2, _, _) = nodeAtPos
  457. nodeAtPosM = onNode allNodes p
  458. allNodes = nodes g
  459. (Just nodeAtPos) = nodeAtPosM
  460. g' = addEdge g $ createEdge node1s nodeAtPos
  461.  
  462.  
  463. {- | If 'r' has been pressed, a node selected and a new key stroke
  464. comes in, the label of the selected node is changed
  465. -}
  466. eventloop ps@(ProgramState "r" (Just node1s) _ g d) (InGraphs (Key [l]))
  467. = (ProgramState [] Nothing Nothing g'' d, [OutGraphs $ DrawGraph g'', OutStdOut $ S.StdOutMessage $ "Renamed node '" ++ [oldL] ++ "' to '" ++ [l] ++ "'\n"])
  468. where
  469. allNodes = nodes g
  470. allEdges = edges g
  471. (oldL, p, color) = node1s
  472. node' = (l, p, color)
  473. allEdges' = map (renameNodeInEdge node1s l) allEdges :: [Edge]
  474. g' = (flip addNode) node' $ removeNode node1s g
  475. g'' = g' {edges = allEdges'}
  476.  
  477. eventloop ps@(ProgramState "p" (Just node1s) _ g d) (InGraphs (Mouse (Click _) p))
  478. = (ProgramState [] Nothing Nothing g d, [OutStdOut $ S.StdOutMessage $ "Checked for path between '" ++ [l1] ++ "' and '" ++ [l2] ++ "'\n Found? " ++ show(result) ++ "\n"])
  479. where
  480. (l1, _, _) = node1s
  481. (l2, _, _) = nodeAtPos
  482. nodeAtPosM = onNode allNodes p
  483. allNodes = nodes g
  484. (Just nodeAtPos) = nodeAtPosM
  485. result = checkPath node1s nodeAtPos g
  486.  
  487.  
  488.  
  489. eventloop ps@(ProgramState "j" (Just node1s) _ g d) (InGraphs (Mouse (Click _) p))
  490. = (ProgramState [] Nothing Nothing g' newPaths, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Checked for path between '" ++ [l1] ++ "' and '" ++ [l2] ++ "'\n Found? " ++ show(result) ++ "\n"])
  491. where
  492. (l1, _, _) = node1s
  493. (l2, _, _) = nodeAtPos
  494. nodeAtPosM = onNode allNodes p
  495. allNodes = nodes g
  496. (Just nodeAtPos) = nodeAtPosM
  497. result = findPaths g nodeAtPos node1s
  498. edges' = colorPathRed (edges g) ((getEdgesList g result) !! 0)
  499. g' = g {edges = edges'}
  500. newPaths = tail $ getEdgesList g result
  501.  
  502.  
  503. eventloop ps (InGraphs (Key "k"))
  504. | path == [] = (ps, [])
  505. | otherwise = (ProgramState [] Nothing Nothing g' newPaths, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Next path'\n"])
  506. where
  507. path = (pathList ps) !! 0
  508. newPaths = tail (pathList ps)
  509. g = (graph ps)
  510. edges' = colorPathRed (edges g) path
  511. g' = g {edges=edges'}
  512.  
  513. eventloop ps@(ProgramState "l" (Just node1s) _ g d) (InGraphs (Mouse (Click _) p))
  514. = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Shortest path between '" ++ [l1] ++ "' and '" ++ [l2] ++ "'\n Found? " ++ show(short) ++ "\n"])
  515. where
  516. (l1, _, _) = node1s
  517. (l2, _, _) = nodeAtPos
  518. nodeAtPosM = onNode allNodes p
  519. allNodes = nodes g
  520. (Just nodeAtPos) = nodeAtPosM
  521. result = findPaths g nodeAtPos node1s
  522. short = getShortest (getEdgesList g result)
  523. edges' = colorPathRed (edges g) short
  524. g' = g {edges = edges'}
  525.  
  526. eventloop ps@(ProgramState _ _ _ g d) (InGraphs (Key "i"))
  527. = (ProgramState [] Nothing Nothing g d, [OutStdOut $ S.StdOutMessage $ "Strongly Connected? " ++ show(result) ++ "\n"])
  528. where
  529. (x:xs) = nodes g
  530. result = all (== True) (map (checkP g x) xs)
  531.  
  532. eventloop ps@(ProgramState _ _ _ g d) (InGraphs (Key "t"))
  533. = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Subgraphs?" ++ show(result) ++ "\n"])
  534. where
  535. result = colorSubGraph (findSubGraph g)
  536. g' = g {nodes = result}
  537.  
  538.  
  539. {- | If 'n' has been pressed and the mouse has
  540. clicked at a position where there is no node yet,
  541. a new node is inserted at that point
  542. -}
  543. eventloop ps@(ProgramState "n" _ _ g d) (InGraphs (Mouse (Click _) p))
  544. | nodeAtPosM == Nothing = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Inserted node '" ++ [nextlabel] ++ "'\n"])
  545. | otherwise = (ps, [OutStdOut $ S.StdOutMessage "Tried to insert a node on another node"])
  546. where
  547. nodeAtPosM = onNode allNodes p
  548. allNodes = nodes g
  549. nextlabel = nextLabel allNodes
  550. newNode = (nextlabel, p, Orange)
  551. g' = g {nodes=(newNode:allNodes)}
  552.  
  553.  
  554. eventloop ps@(ProgramState "c" _ _ g d) (InGraphs (Mouse (Click _) p))
  555. | nodeAtPosM == Nothing = (ps, [OutStdOut $ S.StdOutMessage "That's no node!"])
  556. | otherwise = (ProgramState [] Nothing Nothing g'' d, [OutGraphs $ DrawGraph g'', OutStdOut $ S.StdOutMessage $ "Colored node '" ++ [l] ++ "' red\n"])
  557. where
  558. nodeAtPosM = onNode allNodes p
  559. (Just nodeAtPos) = nodeAtPosM
  560. (l, p1, _) = nodeAtPos
  561. allNodes = nodes g
  562. neighbours = getNeighbours nodeAtPos g
  563. newNeighbours = map (colorNode Blue) neighbours
  564. g' = (flip addNode) (l, p1, Red) $ removeNode nodeAtPos g
  565. g'' = replaceNodes g' newNeighbours neighbours
  566.  
  567.  
  568. {- | Buffer the last node selected if it doesn't
  569. trigger an event on first spot
  570. -}
  571. eventloop ps@(ProgramState _ Nothing _ g d) (InGraphs (Mouse (Click _) p))
  572. | nodeAtPosM == Nothing = (ps, [])
  573. | otherwise = (ps {node1Select = Just nodeAtPos}, [OutStdOut $ S.StdOutMessage $ "[1st Select] Click on node '" ++ [l] ++ "'\n"])
  574. where
  575. (l, _, _) = nodeAtPos
  576. (Just nodeAtPos) = nodeAtPosM
  577. nodeAtPosM = onNode allNodes p
  578. allNodes = nodes g
  579.  
  580.  
  581. eventloop ps@(ProgramState _ _ _ g d) (InGraphs (Key "u"))
  582. = (ProgramState [] Nothing Nothing g' d, [OutGraphs $ DrawGraph g', OutStdOut $ S.StdOutMessage $ "Removed Colors\n"])
  583. where
  584. newNodes = map (colorNode Orange) allNodes
  585. allNodes = nodes g
  586. g' = replaceNodes g newNodes allNodes
  587.  
  588. {- | Buffer the last node selected if it doesn't trigger an event on second spot -}
  589. eventloop ps@(ProgramState _ (Just _) Nothing g d) (InGraphs (Mouse (Click _) p))
  590. | nodeAtPosM == Nothing = (ps, [OutStdOut $ S.StdOutMessage "Clicked on not a node\n"])
  591. | otherwise = (ps {node2Select = Just nodeAtPos}, [OutStdOut $ S.StdOutMessage $ "[2nd Select] Click on node '" ++ [l] ++ "'\n"])
  592. where
  593. (l, _, _) = nodeAtPos
  594. (Just nodeAtPos) = nodeAtPosM
  595. nodeAtPosM = onNode allNodes p
  596. allNodes = nodes g
  597.  
  598. {- | Abort current operation and reset start on "esc" -}
  599. eventloop ps (InGraphs (Key "esc"))
  600. = (ProgramState [] Nothing Nothing (graph ps) (pathList ps), [OutStdOut $ S.StdOutMessage "Aborted current operation\n"])
  601.  
  602.  
  603. {- | Stop the system on "s" -}
  604. eventloop ps (InGraphs (Key "s"))
  605. = (ps, [OutStdOut $ S.StdOutMessage "Stopping system...\n", Stop])
  606.  
  607.  
  608. {- | Buffer the last press key if it doesn't trigger an event -}
  609. eventloop ps@(ProgramState _ _ _ _ _) (InGraphs (Key key))
  610. = (ps {pressedKey = key}, [OutStdOut $ S.StdOutMessage $ "Buffered keystroke '" ++ key ++ "'\n" ])
  611.  
  612.  
  613. {- | For all other In events, do nothing -}
  614. eventloop ps _ = (ps, [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement