Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Import "<std>"
  2. #Import "<mojo>"
  3. #Import "<mojox>"
  4.  
  5. Using std..
  6. Using mojo..
  7. Using mojox..
  8.  
  9. Class MyWindow Extends Window
  10.     Global pathPlaceStep:Int   
  11.    
  12.     Method New()
  13.         Super.New("Simple Mojo Gui App",640,480,WindowFlags.Resizable)
  14.        
  15.         Node.camera.X=640/2
  16.         Node.camera.Y=480/2
  17.         For Local i:Int=0 Until 25
  18.             New Node(Rnd(-640,640),Rnd(-480,480))
  19.         Next
  20.        
  21.         ClearColor=Color.DarkGrey
  22.         ClearEnabled=True
  23.     End
  24.    
  25.     Method OnRender(canvas:Canvas) Override
  26.         App.RequestRender()
  27.        
  28.         canvas.DrawText("Click & Drag to Move node",0,0)
  29.         canvas.DrawText("Ctrl & Drag to Link node",0,15)
  30.         canvas.DrawText("Shift & Click to Insta-Link node",0,15*2)
  31.         canvas.DrawText("Alt & Click to Remove node",0,15*3)
  32.         canvas.DrawText("W,A,S,D to Move camera",0,15*4)
  33.         canvas.DrawText("Numpad Minus to Remove node",0,15*5)
  34.         canvas.DrawText("Numpad Plus to Add node",0,15*6)
  35.        
  36.         If Self.pathPlaceStep Then
  37.             canvas.DrawText("Space to Place path end",0,15*7)
  38.         Else
  39.             canvas.DrawText("Space to Place path start",0,15*7)
  40.         Endif
  41.        
  42.         If Keyboard.KeyDown(Key.W) Then Node.camera.Y+=2
  43.         If Keyboard.KeyDown(Key.S) Then Node.camera.Y-=2
  44.         If Keyboard.KeyDown(Key.A) Then Node.camera.X+=2
  45.         If Keyboard.KeyDown(Key.D) Then Node.camera.X-=2
  46.        
  47.         If Keyboard.KeyHit(Key.KeypadPlus) Then
  48.             New Node(Mouse.X-Node.camera.X,Mouse.Y-Node.camera.Y,Node.selectedNode)
  49.             Node.selectedNode=Node.lastAddedNode
  50.         Endif
  51.        
  52.         If Keyboard.KeyHit(Key.KeypadMinus) Then
  53.             If Node.selectedNode Then
  54.                 Node.selectedNode.Remove()
  55.             Else
  56.                 Node.MouseNode()
  57.                 If Node.lastMouseNode Then Node.lastMouseNode.Remove()
  58.             Endif
  59.         Endif
  60.        
  61.         If Keyboard.KeyHit(Key.Space) Then
  62.             If Self.pathPlaceStep then
  63.                 Self.pathPlaceStep=0
  64.                 Node.FindPath(Node.pathStartPos.x,Node.pathStartPos.y,Mouse.X-Node.camera.X,Mouse.Y-Node.camera.Y)
  65.             Else
  66.                 Node.ClearPath()
  67.                 Self.pathPlaceStep=1
  68.                 Node.pathStartPos=New Vec2f(Mouse.X-Node.camera.X,Mouse.Y-Node.camera.Y)
  69.                 Node.pathEndPos=New Vec2f(Mouse.X-Node.camera.X,Mouse.Y-Node.camera.Y)
  70.             endif
  71.         Endif
  72.        
  73.         Node.Update()
  74.         Node.Draw(canvas)
  75.        
  76.         If Node.dragLink Or Node.moveNode Then
  77.             If SwapInterval<>0 Then SwapInterval=0
  78.         Else
  79.             If SwapInterval<>1 Then SwapInterval=1
  80.         Endif
  81.     End
  82. End
  83.  
  84. Function Main()
  85.     New AppInstance
  86.     New MyWindow
  87.    
  88.     App.Run()
  89. End
  90.  
  91. Class Node
  92.     Global list:List<Node>
  93.     Global path:List<Node>
  94.     Global camera:Vec2f
  95.     Global selectedNode:Node
  96.     Global lastMouseNode:Node
  97.     Global lastAddedNode:Node
  98.     Global lastCloseNode:Node
  99.     Global dragLink:Bool
  100.     Global moveNode:Bool
  101.     Global pathStartNode:Node
  102.     Global pathEndNode:Node
  103.     Global pathStartPos:Vec2f
  104.     Global pathEndPos:Vec2f
  105.    
  106.     Field id:Int
  107.     Field x:Float
  108.     Field y:Float
  109.     Field links:List<Node>
  110.     Field color:Color
  111.     Field size:Int=12
  112.    
  113.     Method New(x:Float,y:Float,link:Node=Null)
  114.         If Not Node.list Then Node.list=New List<Node>
  115.         If Not Node.path Then Node.path=New List<Node>
  116.        
  117.         'Find unique ID for node
  118.         Local n:Node
  119.         Local freeId:Int
  120.         Local checkId:Int
  121.         Repeat
  122.             freeId=checkId
  123.             For n=Eachin Node.list
  124.                 If n.id=checkId Then
  125.                     'RETRY!!
  126.                     freeId=-1
  127.                     Exit
  128.                 Endif
  129.             Next
  130.             checkId+=1
  131.         Until freeId>=0
  132.        
  133.         Self.id=freeId
  134.         Self.x=x
  135.         Self.y=y
  136.         If Not Self.links Then Self.links=New List<Node>
  137.        
  138.         If link Then Self.AddLink(link)
  139.        
  140.         Node.lastAddedNode=Self
  141.         Node.list.AddLast(self)
  142.     End
  143.    
  144.     Method MouseHover:Bool()
  145.         If Mouse.X-Node.camera.X>=Self.x-Self.size/2 And Mouse.Y-Node.camera.Y>=Self.y-Self.size/2 Then
  146.             If Mouse.X-Node.camera.X<=Self.x+Self.size/2 And Mouse.Y-Node.camera.Y<=Self.y+Self.size/2
  147.                 Return True
  148.             Endif
  149.         End
  150.        
  151.         Return False
  152.     End
  153.    
  154.     Method AddLink(node:Node)
  155.         If Not node Then
  156.             Print "No link node!"
  157.             Return
  158.         Endif
  159.        
  160.         'Check if self
  161.         If Self=node Then
  162.             Print "Link node is self!"
  163.             Return
  164.         Endif
  165.        
  166.         'Check if link already exists
  167.         Local n:Node
  168.         For n=Eachin node.links
  169.             If n=Self Then
  170.                 Print "Link already exists!"
  171.                 Return
  172.             Endif
  173.         Next
  174.        
  175.         For n=Eachin Self.links
  176.             If n=node Then
  177.                 Print "Link already exists!"
  178.                 Return
  179.             Endif
  180.         Next
  181.        
  182.         node.links.AddLast(Self)
  183.         Self.links.AddLast(node)
  184.     End
  185.    
  186.     Method RemoveLink(node:Node)
  187.         Self.links.Remove(node)
  188.         node.links.Remove(Self)
  189.     End
  190.    
  191.     Method Remove()
  192.         If Node.selectedNode=Self Then Node.selectedNode=Null
  193.         If Node.lastMouseNode=Self Then Node.lastMouseNode=Null
  194.         If Node.lastAddedNode=Self Then Node.lastAddedNode=Null
  195.         If Node.lastCloseNode=Self Then Node.lastCloseNode=Null
  196.        
  197.         If Self.links Then
  198.             For Local n:Node=Eachin Self.links
  199.                 n.RemoveLink(Self)
  200.             Next
  201.         Endif
  202.        
  203.         Node.list.Remove(Self)
  204.     End
  205.    
  206.     Function MouseNode:Node()
  207.         Node.lastMouseNode=Null
  208.         For Local n:Node=Eachin Node.list
  209.             If n.MouseHover() Then
  210.                 Node.lastMouseNode=n
  211.                 Return Node.lastMouseNode
  212.             Endif
  213.         Next
  214.         Return Null
  215.     End
  216.    
  217.     Function NodeClose:Node(x:Float,y:Float)
  218.         Node.lastCloseNode=Null
  219.         Local dx:Float
  220.         Local dy:Float
  221.         Local dist:Float
  222.         Local bestDist:Float
  223.         For Local n:Node=Eachin Node.list
  224.             dx=x-n.x
  225.             dy=y-n.y
  226.             dist=Sqrt(dx*dx + dy*dy)
  227.             If Not Node.lastCloseNode Or dist<bestDist Then
  228.                 bestDist=dist
  229.                 Node.lastCloseNode=n
  230.             Endif
  231.         Next
  232.         Return Node.lastCloseNode
  233.     End
  234.    
  235.     Function NodeId:Node(id:int)
  236.         For Local n:Node=Eachin Node.list
  237.             If n.id=id Then Return n
  238.         Next
  239.         Return Null
  240.     End
  241.    
  242.     Function Update()
  243.         For Local n:Node=Eachin Node.list
  244.             If Node.selectedNode = n Then
  245.                 n.color=Color.Green
  246.             Else
  247.                 n.color=Color.Orange
  248.             Endif
  249.         Next
  250.        
  251.         If Mouse.ButtonHit(MouseButton.Left) Then
  252.             Node.MouseNode()
  253.            
  254.             If Node.selectedNode Then
  255.                 If Not Node.lastMouseNode Then
  256.                     Node.selectedNode=Null
  257.                 Else
  258.                     If Node.selectedNode=Node.lastMouseNode Then
  259.                         If Keyboard.KeyDown(Key.LeftControl) Or Keyboard.KeyDown(Key.RightControl) Then
  260.                             Node.dragLink=True
  261.                         Else
  262.                             Node.moveNode=True
  263.                         Endif
  264.                     Else
  265.                         If Keyboard.KeyDown(Key.LeftShift) Or Keyboard.KeyDown(Key.RightShift) Then
  266.                             Node.selectedNode.AddLink(Node.lastMouseNode)
  267.                         Else
  268.                             If Keyboard.KeyDown(Key.LeftAlt) Or Keyboard.KeyDown(Key.RightAlt) Then
  269.                                 Node.selectedNode.RemoveLink(Node.lastMouseNode)
  270.                             Else
  271.                                 Node.selectedNode=Node.lastMouseNode
  272.                             Endif
  273.                         Endif
  274.                     Endif
  275.                 Endif
  276.             Else
  277.                 Node.selectedNode=Node.lastMouseNode
  278.             Endif
  279.         Else
  280.             If Mouse.ButtonDown(MouseButton.Left) Then
  281.                 If Node.dragLink Then
  282.                     Node.MouseNode()
  283.                     If Node.lastMouseNode And Node.lastMouseNode<>Node.selectedNode Then
  284.                         Node.lastMouseNode.color=Color.Blue
  285.                     Endif
  286.                 Endif
  287.                
  288.                 If Node.moveNode Then
  289.                     Node.selectedNode.x=Mouse.X-Node.camera.X
  290.                     Node.selectedNode.y=Mouse.Y-Node.camera.Y
  291.                 Endif
  292.             Else
  293.                 If Node.dragLink Then
  294.                     If Node.lastMouseNode Then Node.selectedNode.AddLink(Node.lastMouseNode)
  295.                     Node.dragLink=False
  296.                 Endif
  297.                
  298.                 If Node.moveNode Then
  299.                     Node.moveNode=False
  300.                 Endif
  301.             Endif
  302.         Endif
  303.     End
  304.    
  305.     Function Draw(canvas:Canvas)
  306.         canvas.Translate(Node.camera.X,Node.camera.Y)
  307.         Local n:Node
  308.         Local n2:Node
  309.        
  310.         canvas.LineSmoothing=True
  311.         canvas.LineWidth=3
  312.        
  313.         'Draw links
  314.         For n=Eachin Node.list
  315.             canvas.Color=Color.Blue
  316.            
  317.             For n2=Eachin n.links
  318.                 canvas.DrawLine(n.x,n.y,n2.x,n2.y)
  319.             Next
  320.         Next
  321.        
  322.         'Draw nodes
  323.         For n=Eachin Node.list
  324.             canvas.Color=n.color
  325.             canvas.DrawOval(Int(n.x-n.size/2),Int(n.y-n.size/2),n.size,n.size)
  326.             canvas.Color=Color.White
  327.             canvas.DrawText(n.id,n.x,n.y,0.5,0.5)
  328.         Next
  329.        
  330.         'Draw dragging line
  331.         If Node.dragLink Then
  332.             canvas.Color=Color.Blue
  333.             canvas.DrawLine(selectedNode.x,selectedNode.y,Mouse.X-Node.camera.X,Mouse.Y-Node.camera.Y)
  334.         Endif
  335.        
  336.         'Draw path
  337.         canvas.Color=Color.Puce
  338.         If Node.path.Count()<=0 Then
  339.             canvas.DrawLine(Node.pathStartPos.X,Node.pathStartPos.Y,Node.pathEndPos.X,Node.pathEndPos.Y)
  340.         Else
  341.             canvas.DrawLine(Node.pathStartPos.X,Node.pathStartPos.Y,Node.path.First.x,Node.path.First.y)
  342.            
  343.             Local lastNode:Node=Node.path.First
  344.             For n=Eachin Node.path
  345.                 If lastNode=n Then Continue
  346.                 canvas.DrawLine(n.x,n.y,lastNode.x,lastNode.y)
  347.                 lastNode=n
  348.             Next
  349.            
  350.             canvas.DrawLine(Node.pathEndPos.X,Node.pathEndPos.Y,Node.path.Last.x,Node.path.Last.y)
  351.         Endif
  352.        
  353.         canvas.Color=Color.Pink
  354.         canvas.DrawText("S",Int(Node.pathStartPos.X),Int(Node.pathStartPos.Y),0.5,0.5)
  355.         If Int(Node.pathStartPos.X)<>Int(Node.pathEndPos.X) And Int(Node.pathStartPos.Y)<>Int(Node.pathEndPos.Y) then
  356.             canvas.DrawText("E",Int(Node.pathEndPos.X),Int(Node.pathEndPos.Y),0.5,0.5)
  357.         Endif
  358.     End
  359.    
  360.     Function ClearPath()
  361.         Node.path.Clear()
  362.     End
  363.    
  364.     Function FindPath(startX:Float,startY:Float,endX:Float,endY:Float)
  365.         ClearPath()
  366.        
  367.         Node.pathStartNode=Node.NodeClose(startX,startY)
  368.         Node.pathEndNode=Node.NodeClose(endX,endX)
  369.        
  370.         Node.pathStartPos.X=startX
  371.         Node.pathStartPos.Y=startY
  372.         Node.pathEndPos.X=endX
  373.         Node.pathEndPos.Y=endY
  374.        
  375.         'Super pathfinding code here...
  376.         'Just add some random path for now
  377.         Node.path.AddLast(Node.NodeId(8))
  378.         Node.path.AddLast(Node.NodeId(5))
  379.         Node.path.AddLast(Node.NodeId(11))
  380.         Node.path.AddLast(Node.NodeId(17))
  381.         Node.path.AddLast(Node.NodeId(0))
  382.        
  383.         'Print path
  384.         For Local n:Node=Eachin Node.path
  385.             Print n.id
  386.         Next
  387.     End
  388. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement