public static List <Wall2D> ContinueToDiscrete(Vector2[] points) { bool isClose = false; if (points[0] == points[points.Length - 1]) { isClose = true; } List <Wall2D> list = new List <Wall2D>(); Wall2D wall = null; for (int i = 0; i < points.Length - 1; i++) { Wall2D newWall = new Wall2D(points[i], points[i + 1]); list.Add(newWall); if (wall != null) { wall.WallAtEnd = newWall; } wall = newWall; } if (isClose) { wall.WallAtEnd = list[0]; } return(list); }
public bool Select(Vector2 wordPos, out Wall2D wall) { IntPoint mousePos = new IntPoint(wordPos.x * DrawHelper.ClipScalling, wordPos.y * DrawHelper.ClipScalling); List <IntPoint> path = new List <IntPoint>(); //Debug.Log("wall count: " + this.mSize); for (int i = 0; i < this.mSize; i++) { path.Clear(); for (int j = 0; j < 4; j++) { UIVertex p = vertlist[i * 4 + j]; //IntPoint ip = new IntPoint(p.position.x * DrawHelper.ClipScalling, p.position.y * DrawHelper.ClipScalling); Vector2 pos = DrawHelper.Vector3To2(p.position); IntPoint ip = new IntPoint(pos.x * DrawHelper.ClipScalling, pos.y * DrawHelper.ClipScalling); path.Add(ip); } if (Clipper.PointInPolygon(mousePos, path) >= 1) { wall = walls[i]; return(true); } } wall = null; return(false); }
public static List<Wall2D> ContinueToDiscrete(Vector2[] points) { bool isClose = false; if(points[0] == points[points.Length-1]){ isClose = true; } List<Wall2D> list = new List<Wall2D>(); Wall2D wall = null; for(int i=0; i < points.Length-1; i++){ Wall2D newWall = new Wall2D(points[i], points[i+1]); list.Add(newWall); if(wall != null){ wall.WallAtEnd = newWall; } wall = newWall; } if(isClose){ wall.WallAtEnd = list[0]; } return list; }
public Line GetOffsetLine(Wall2D wall, Vector2 realDelta) { Vector2 rightVector = GetOffset(wall.StartPos, wall.EndPos, true, 1.0f); float dist = Vector2.Dot(realDelta, rightVector); Vector2 offset = rightVector * dist; return(GetParallelLine(wall.StartPos, wall.EndPos, offset)); }
private void RenderWall2D(Wall2D objWall2D, Graphics objGraphics, Pen objPen, bool RenderNormals) { objGraphics.DrawLine(objPen, (PointF)objWall2D.From, (PointF)objWall2D.To); //render the normals if rqd if (RenderNormals) { Vector2D middle = objWall2D.Center(); objGraphics.DrawLine(objPen, (int)middle.X, (int)middle.Y, (int)(middle.X + (objWall2D.Normal.X * 10)), (int)(middle.Y + (objWall2D.Normal.Y * 10))); } }
void AddWallsJoined(List<Wall2D> list, Wall2D origin, bool searchEnd = true) { Wall2D wall = origin; if(searchEnd){ while(wall.WallAtEnd != null && wall.WallAtEnd != origin){ wall = wall.WallAtEnd; list.Add(wall); } } else{ while(wall.WallAtStart != null && wall.WallAtStart != origin){ wall = wall.WallAtStart; list.Add(wall); } } }
void AddWallsJoined(List <Wall2D> list, Wall2D origin, bool searchEnd = true) { Wall2D wall = origin; if (searchEnd) { while (wall.WallAtEnd != null && wall.WallAtEnd != origin) { wall = wall.WallAtEnd; list.Add(wall); } } else { while (wall.WallAtStart != null && wall.WallAtStart != origin) { wall = wall.WallAtStart; list.Add(wall); } } }
public bool Select(Vector2 wordPos, out Wall2D wall) { IntPoint mousePos = new IntPoint(wordPos.x * DrawHelper.ClipScalling, wordPos.y * DrawHelper.ClipScalling); List<IntPoint> path = new List<IntPoint>(); //Debug.Log("wall count: " + this.mSize); for(int i=0; i < this.mSize; i++){ path.Clear(); for(int j=0; j < 4; j++){ UIVertex p = vertlist[i*4 + j]; //IntPoint ip = new IntPoint(p.position.x * DrawHelper.ClipScalling, p.position.y * DrawHelper.ClipScalling); Vector2 pos = DrawHelper.Vector3To2(p.position); IntPoint ip = new IntPoint(pos.x * DrawHelper.ClipScalling, pos.y * DrawHelper.ClipScalling); path.Add(ip); } if(Clipper.PointInPolygon(mousePos, path) >= 1){ wall = walls[i]; return true; } } wall = null; return false; }
void Update() { ShowWallFill(); if(mState != State2D.Draw){ return; } Rect rect = camera2d.pixelRect; Vector2 current = Input.mousePosition; if(!rect.Contains(current)){ return; } if(Input.GetMouseButtonDown(0)){ if(drawBreak){ drawBreak = false; wall[0] = wall[1] = DrawHelper.PixelToReal(camera2d, current); walls.Clear(); drawStartIndex = walls.Count; } else{ if(isContinue){ Wall2D newWall = new Wall2D(wall[0], wall[1]); //////////////// if(walls.Count >= 1){ newWall.WallAtStart = walls[walls.Count-1]; Vector2 target = walls[0].StartPos; Vector2 delta = SnapPoint(newWall.EndPos, target); newWall.EndPos += delta; if(delta != Vector2.zero){ newWall.WallAtEnd = walls[0]; drawBreak = true; } } Home.Get().AddWall(newWall); /////////////// walls.Add(newWall); wall[0] = wall[1]; } else{ wall[1] = DrawHelper.PixelToReal(camera2d, current); Wall2D newWall = new Wall2D(wall[0], wall[1]); if(walls.Count >= 1){ newWall.WallAtStart = walls[walls.Count-1]; } Home.Get().AddWall(newWall); walls.Add(newWall); wall[0] = wall[1]; } } } if(Input.GetMouseButtonDown(1)){ drawBreak = true; // Vector3[] room = DiscreteToContinue(walls, drawStartIndex); // if(room != null){ // DrawWall(room); // } } if(Input.GetMouseButton(0) || Input.GetMouseButton(1)){ Vector2 delta = current - lastMousePos; // if(mState == State2D.Idle){ // OnDrag(delta); // } // if(mState == State2D.Draw){ // wall[1] = PixelToReal(camera2d, current); // walls.Add(wall); // } lastMousePos = current; } //stop draw line; if(Input.GetMouseButtonDown(2)){ drawBreak = true; } if(!drawBreak){ wall[1] = DrawHelper.PixelToReal(camera2d, current); } if(!isContinue){ var wallSaved = Home.Get().Walls; drawLine.Resize(wallSaved.Count*2 + 2); widths.Clear(); int i = 0; for(; i < wallSaved.Count; i++){ Wall2D line = wallSaved[i]; drawLine.points3[i*2] = DrawHelper.Vector2To3(line.StartPos); drawLine.points3[i*2+1] = DrawHelper.Vector2To3(line.EndPos); widths.Add(wallThick * DrawHelper.unitPixels); } //walls[walls.Count-1][1] = wall[1]; drawLine.points3[i*2] = DrawHelper.Vector2To3(wall[0]); drawLine.points3[i*2+1] = DrawHelper.Vector2To3(wall[1]); //drawLine.SetWidths(widths); drawLine.color = Color.black; drawLine.Draw3D(); } else{ if(Input.GetMouseButtonUp(0)){ //Debug.Log("pause here!"); } drawLine.Resize(walls.Count + 2); widths.Clear(); int i = 0; for(; i < walls.Count; i++){ Wall2D line = walls[i]; drawLine.points3[i] = DrawHelper.Vector2To3(line.StartPos); widths.Add(wallThick * DrawHelper.unitPixels); } if(i == 0){ drawLine.points3[i] = DrawHelper.Vector2To3(wall[0]); } else{ drawLine.points3[i] = DrawHelper.Vector2To3(walls[i-1].EndPos); } drawLine.points3[i+1] = DrawHelper.Vector2To3(wall[1]); //drawLine.SetWidths(widths); drawLine.color = Color.black; drawLine.Draw3D(); } //draw ruler; List<Vector2> ruler = mParallel.GetRuler(wall, 0.1f, false); rulerLine.Resize(2); rulerLine.points3[0] = DrawHelper.Vector2To3(ruler[0]); rulerLine.points3[1] = DrawHelper.Vector2To3(ruler[1]); rulerLine.SetColor(Color.blue); rulerLine.Draw3D(); }
public void AddWall(Wall2D wall) { this.walls.Add(wall); }
void Update() { segmentFill.Clear(); segmentOutline.Resize(0); var savedWall = Home.Get().Walls; found.Clear(); for (int i = 0; i < savedWall.Count; i++) { Wall2D start = savedWall[i]; if (found.Contains(start)) { continue; } toDraw.Clear(); toDraw.Add(start); AddWallsJoined(toDraw, start); found.AddRange(toDraw); // if(wall.WallAtEnd != null && wall.WallAtEnd == start){ // } // else{ // } segmentFill.SetupSegment(toDraw, wallThick, segmentOutline, mParallel); } segmentFill.Draw(); segmentOutline.SetColor(Color.black); segmentOutline.Draw3D(); if (mState != State2D.Draw) { return; } Rect rect = camera2d.pixelRect; Vector2 current = Input.mousePosition; if (!rect.Contains(current)) { return; } if (Input.GetMouseButtonDown(0)) { if (drawBreak) { drawBreak = false; wall[0] = wall[1] = DrawHelper.PixelToReal(camera2d, current); walls.Clear(); drawStartIndex = walls.Count; } else { if (isContinue) { Wall2D newWall = new Wall2D(wall[0], wall[1]); //////////////// if (walls.Count >= 1) { newWall.WallAtStart = walls[walls.Count - 1]; Vector2 target = walls[0].StartPos; Vector2 delta = SnapPoint(newWall.EndPos, target); newWall.EndPos += delta; if (delta != Vector2.zero) { newWall.WallAtEnd = walls[0]; drawBreak = true; } } Home.Get().AddWall(newWall); /////////////// walls.Add(newWall); wall[0] = wall[1]; } else { wall[1] = DrawHelper.PixelToReal(camera2d, current); Wall2D newWall = new Wall2D(wall[0], wall[1]); if (walls.Count >= 1) { newWall.WallAtStart = walls[walls.Count - 1]; } Home.Get().AddWall(newWall); walls.Add(newWall); wall[0] = wall[1]; } } } if (Input.GetMouseButtonDown(1)) { drawBreak = true; // Vector3[] room = DiscreteToContinue(walls, drawStartIndex); // if(room != null){ // DrawWall(room); // } } if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) { Vector2 delta = current - lastMousePos; // if(mState == State2D.Idle){ // OnDrag(delta); // } // if(mState == State2D.Draw){ // wall[1] = PixelToReal(camera2d, current); // walls.Add(wall); // } lastMousePos = current; } //stop draw line; if (Input.GetMouseButtonDown(2)) { drawBreak = true; } if (!drawBreak) { wall[1] = DrawHelper.PixelToReal(camera2d, current); } if (!isContinue) { var wallSaved = Home.Get().Walls; drawLine.Resize(wallSaved.Count * 2 + 2); widths.Clear(); int i = 0; for (; i < wallSaved.Count; i++) { Wall2D line = wallSaved[i]; drawLine.points3[i * 2] = DrawHelper.Vector2To3(line.StartPos); drawLine.points3[i * 2 + 1] = DrawHelper.Vector2To3(line.EndPos); widths.Add(wallThick * DrawHelper.unitPixels); } //walls[walls.Count-1][1] = wall[1]; drawLine.points3[i * 2] = DrawHelper.Vector2To3(wall[0]); drawLine.points3[i * 2 + 1] = DrawHelper.Vector2To3(wall[1]); //drawLine.SetWidths(widths); drawLine.color = Color.black; drawLine.Draw3D(); } else { drawLine.Resize(walls.Count + 2); widths.Clear(); int i = 0; for (; i < walls.Count; i++) { Wall2D line = walls[i]; drawLine.points3[i] = DrawHelper.Vector2To3(line.StartPos); widths.Add(wallThick * DrawHelper.unitPixels); } if (i == 0) { drawLine.points3[i] = DrawHelper.Vector2To3(wall[0]); } else { drawLine.points3[i] = DrawHelper.Vector2To3(walls[i - 1].EndPos); } drawLine.points3[i + 1] = DrawHelper.Vector2To3(wall[1]); //drawLine.SetWidths(widths); drawLine.color = Color.black; drawLine.Draw3D(); } //draw ruler; List <Vector2> ruler = mParallel.GetRuler(wall, 0.1f, false); rulerLine.Resize(2); rulerLine.points3[0] = DrawHelper.Vector2To3(ruler[0]); rulerLine.points3[1] = DrawHelper.Vector2To3(ruler[1]); rulerLine.SetColor(Color.blue); rulerLine.Draw3D(); }
void Awake() { Canvas canvas2d = VectorLine.canvas; GraphicRaycaster graphicRaycaster = canvas2d.gameObject.AddComponent <GraphicRaycaster>(); graphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.ThreeD; Button drawBtn = GameObject.Find("UIRoot/DrawBtn").GetComponent <Button>(); bool clicked = false; drawBtn.onClick.AddListener(() => { clicked = !clicked; Debug.Log("On DrawBtn clicked.." + clicked); if (clicked) { if (this.mState != State2D.Draw) { this.mState = State2D.Draw; } } else { if (this.mState == State2D.Draw) { this.mState = State2D.Idle; } } }); GameObject background = new GameObject("background", typeof(RectTransform)); RectTransform rectTrans = background.GetComponent <RectTransform>(); rectTrans.parent = canvas2d.transform; rectTrans.anchoredPosition = new Vector2(0.5f, 0.5f); rectTrans.anchorMin = new Vector2(0f, 0f); rectTrans.anchorMax = new Vector2(1f, 1f); GraphicTest test = background.AddComponent <GraphicTest>(); // test.onDrag = (eventData) => { // Debug.Log("onDrag.."); // if(mState == State2D.Idle){ // OnDrag(eventData.delta); // } // }; test.color = new Color(0.95f, 0.95f, 0.95f, 1.0f); EventTrigger trigger = background.AddComponent <EventTrigger>(); //OnClick; EventTrigger.Entry entry = new EventTrigger.Entry(); trigger.triggers.Add(entry); entry.eventID = EventTriggerType.PointerClick; entry.callback.AddListener((eventData) => { // PointerEventData data = eventData as PointerEventData; // if(mState == State2D.Idle){ // //Debug.Log("onClick.."); // Vector2 wordPos = DrawHelper.PixelToReal(camera2d, data.position); // Wall2D wall; // if(segmentFill.Select(wordPos, out wall)){ // wall.Color = Color.green; // } // } }); Wall2D selected = null; //OnPress Down; entry = new EventTrigger.Entry(); trigger.triggers.Add(entry); entry.eventID = EventTriggerType.PointerDown; entry.callback.AddListener((eventData) => { PointerEventData data = eventData as PointerEventData; if (mState == State2D.Idle) { Vector2 wordPos = DrawHelper.PixelToReal(camera2d, data.position); if (segmentFill.Select(wordPos, out selected)) { //Debug.Log(string.Format("change green..{0}, {1}", selected.StartPos, selected.EndPos )); selected.Color = Color.green; Wall2D wall = selected; } } }); //OnPress Up; entry = new EventTrigger.Entry(); trigger.triggers.Add(entry); entry.eventID = EventTriggerType.PointerUp; entry.callback.AddListener((eventData) => { PointerEventData data = eventData as PointerEventData; if (mState == State2D.Idle) { if (selected != null) { selected.Color = Color.red; selected = null; } } }); //OnDrag; entry = new EventTrigger.Entry(); trigger.triggers.Add(entry); entry.eventID = EventTriggerType.Drag; entry.callback.AddListener((eventData) => { //Debug.Log("onDrag.."); PointerEventData data = eventData as PointerEventData; Vector2 realDelta = data.delta * DrawHelper.Scale; if (mState == State2D.Idle) { if (selected == null) { //camera2d.transform.position = camera2d.transform.position + new Vector3(-realDelta.x, -realDelta.y, 0f); DrawHelper.MoveRealRect(camera2d, realDelta); MakeGrid2(); } else { Wall2D wall = selected; Vector2 startT = wall.StartPos + realDelta; Vector2 endT = wall.EndPos + realDelta; List <Wall2D> joined = new List <Wall2D>(); if (wall.WallAtStart == null) { joined.Clear(); AddWallsJoined(joined, wall, true); if (joined.Count > 0) { Vector3 target = joined[joined.Count - 1].EndPos; Vector2 delta = SnapPoint(startT, target); startT = startT + delta; endT = endT + delta; } } if (wall.WallAtEnd == null) { joined.Clear(); AddWallsJoined(joined, wall, false); if (joined.Count > 0) { Vector3 target = joined[joined.Count - 1].StartPos; //Debug.Log(string.Format("endT: ({0}, {1}), target: ({2}, {3})", endT.x, endT.y, target.x - endT.x, target.y - endT.y)); Vector2 delta = SnapPoint(endT, target); startT = startT + delta; endT = endT + delta; } } if (wall.WallAtStart != null && wall.WallAtEnd != null) { Parallel.Line line = mParallel.GetOffsetLine(wall, realDelta); Parallel.Line line1 = mParallel.GetLine(wall.WallAtStart.StartPos, wall.WallAtStart.EndPos); Parallel.Line line2 = mParallel.GetLine(wall.WallAtEnd.StartPos, wall.WallAtEnd.EndPos); Vector2 p = Vector2.zero; if (mParallel.GetCrossPoint(line, line1, out p)) { if (wall.WallAtStart.StartPos != p) { wall.StartPos = p; wall.WallAtStart.EndPos = p; } else { Debug.Log("wallAtStart duplicate.."); } } if (mParallel.GetCrossPoint(line, line2, out p)) { if (wall.WallAtEnd.EndPos != p) { wall.EndPos = p; wall.WallAtEnd.StartPos = p; } else { Debug.Log("wallAtEnd duplicate.."); } } return; } wall.StartPos = startT; if (wall.WallAtStart != null) { // wall.WallAtStart.WallAtEnd = null; // wall.WallAtStart = null; wall.WallAtStart.EndPos = startT; } wall.EndPos = endT; if (wall.WallAtEnd != null) { // wall.WallAtEnd.WallAtStart = null; // wall.WallAtEnd = null; wall.WallAtEnd.StartPos = endT; } } } }); }
public Line GetOffsetLine(Wall2D wall, Vector2 realDelta) { Vector2 rightVector = GetOffset(wall.StartPos, wall.EndPos, true, 1.0f); float dist = Vector2.Dot(realDelta, rightVector); Vector2 offset = rightVector * dist; return GetParallelLine(wall.StartPos, wall.EndPos, offset); }