private IEnumerator Start() { yield return(new WaitForSeconds(0.5f)); List <SnapLocation> snapLocationList = new List <SnapLocation>(); TextAsset targetFile = Resources.Load <TextAsset>("snap_locations"); string snapLocationJson = targetFile.text; IDictionary result = (IDictionary)MiniJSON.Json.Deserialize(snapLocationJson); IList locationList = (IList)result["results"]; foreach (IDictionary location in locationList) { IDictionary attributes = location["attributes"] as IDictionary; SnapLocation snapLocation = new SnapLocation(Convert.ToInt32(attributes["OBJECTID"]), attributes["STORE_NAME"].ToString(), Convert.ToDouble(attributes["longitude"]), Convert.ToDouble(attributes["latitude"]), attributes["ADDRESS"].ToString(), attributes["ADDRESS2"].ToString(), attributes["CITY"].ToString(), attributes["STATE"].ToString(), attributes["ZIP5"].ToString(), attributes["County"].ToString()); snapLocationList.Add(snapLocation); } foreach (SnapLocation snap in snapLocationList) { SpawnPOIAtLocation(snap, snap.latitude, snap.longitude); } }
// Gets the closest SnapLocation within the range of the DraggableObject. // Returns null if no SnapLocations are in range. protected virtual SnapLocation GetClosestSnapLocation() { SnapLocation locationToGoTo = null; Vector2 myPos = transform.position; myPos += snapDetectionOffset; // Used to figure out which location is the closest before snapping. float smallestDistance = Mathf.Infinity; foreach (SnapLocation solution in snapToAreas) { Vector2 solutionPos = solution.transform.position; float solutionX = solutionPos.x; float solutionY = solutionPos.y; //Debug.Log("Me: (" + myPos.x + ", " + myPos.y + "), solution: (" + solutionX + ", " + solutionY + ")"); if ((myPos.x > solutionX - piecePlacementOffset.x) && (myPos.x < solutionX + piecePlacementOffset.x) && (myPos.y > solutionY - piecePlacementOffset.y) && (myPos.y < solutionY + piecePlacementOffset.y)) { float distance = Vector2.Distance(myPos, solutionPos); if (distance < smallestDistance) { locationToGoTo = solution; smallestDistance = distance; } } } return(locationToGoTo); }
public virtual void OnDrag(PointerEventData eventData) { if (isDraggable) { screenTapping.TappingEffect(eventData); //Play screenTapping animation Vector2 localPointerPosition; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasTransform, eventData.position, eventData.pressEventCamera, out localPointerPosition)) { float xPos = localPointerPosition.x - _pointerOffset.x; float yPos = localPointerPosition.y - _pointerOffset.y; transform.localPosition = new Vector2(xPos, yPos); } SnapLocation locationToGoTo = GetClosestSnapLocation(); if (locationToGoTo != null) { if (lastLocation != locationToGoTo) { lastLocation = locationToGoTo; locationToGoTo.Hover(gameObject, false); // Clear all highlights locationToGoTo.Hover(gameObject, true); // Set on highlight for current tile AudioController.Instance.SnapTile(); } } else { lastLocation = null; TurnOffHovering(); } } }
/// <summary> /// /// </summary> /// <param name="effectiveBounds"></param> /// <returns></returns> private SnapLocation FindSnap(ref Rectangle effectiveBounds) { Screen currentScreen = Screen.FromPoint(effectiveBounds.Location); Rectangle workingArea = currentScreen.WorkingArea; SnapLocation anchor = SnapLocation.None; if (InSnapRange(effectiveBounds.Left, workingArea.Left + AnchorDistance)) { effectiveBounds.X = workingArea.Left + AnchorDistance; anchor |= SnapLocation.Left; } else if (InSnapRange(effectiveBounds.Right, workingArea.Right - AnchorDistance)) { effectiveBounds.X = workingArea.Right - AnchorDistance - effectiveBounds.Width; anchor |= SnapLocation.Right; } if (InSnapRange(effectiveBounds.Top, workingArea.Top + AnchorDistance)) { effectiveBounds.Y = workingArea.Top + AnchorDistance; anchor |= SnapLocation.Top; } else if (InSnapRange(effectiveBounds.Bottom, workingArea.Bottom - AnchorDistance)) { effectiveBounds.Y = workingArea.Bottom - AnchorDistance - effectiveBounds.Height; anchor |= SnapLocation.Bottom; } return(anchor); }
public virtual void OnEndDrag(PointerEventData eventData) { if (isDraggable) { startTime = Time.time; isDragging = false; screenTapping.TappingEffect(eventData); //Play screenTapping animation SnapLocation locationToGoTo = GetClosestSnapLocation(); if (locationToGoTo != null) { transform.position = locationToGoTo.transform.position; locationToGoTo.Snap(gameObject); locationToGoTo.Hover(gameObject, false); } else { transform.localScale = nonDraggingScale; //Make the block samller //transform.localScale = new Vector3(1f, 1f, 1f); TurnOffHovering(); currentPosition = transform.localPosition; //transform.localPosition = defaultPosition; } if (EndDragEvent != null) { EndDragEvent(this); } } }
protected override void WndProc(ref Message m) { switch (m.Msg) { case WmEnterSizeMove: case WmSize: // Need to handle window size changed as well when // un-maximizing the form by dragging the title bar. _dragOffsetX = Cursor.Position.X - Left; _dragOffsetY = Cursor.Position.Y - Top; break; case WmMoving: LtrbRectangle boundsLtrb = Marshal.PtrToStructure <LtrbRectangle>(m.LParam); Rectangle bounds = boundsLtrb.ToRectangle(); // This is where the window _would_ be located if snapping // had not occurred. This prevents the cursor from sliding // off the title bar if the snap distance is too large. Rectangle effectiveBounds = new Rectangle( Cursor.Position.X - _dragOffsetX, Cursor.Position.Y - _dragOffsetY, bounds.Width, bounds.Height); _snapAnchor = FindSnap(ref effectiveBounds); LtrbRectangle newLtrb = LtrbRectangle.FromRectangle(effectiveBounds); Marshal.StructureToPtr(newLtrb, m.LParam, false); m.Result = new IntPtr(1); break; } base.WndProc(ref m); }
/// <summary> /// With an Gameobject tag, remove towers to replace with upgraded one. /// Check if upgrade possible first. /// </summary> /// <param name="tag"></param> void UpgradeWithTag(string tag) { GameObject[] list = GameObject.FindGameObjectsWithTag(tag); if (list.Length >= 3) { GameObject yield = GetYieldFor(tag); if (yield == null) { Debug.LogWarning("Could not get yield for tag: " + tag); return; } for (int i = 1; i <= list.Length; i++) { GameObject towerObjects = list[i - 1]; SnapLocation location = towerObjects.GetComponentInParent <SnapLocation>(); location.Clear(); if (i % 3 == 0) { GameObject upgradedInstance = Instantiate(yield, Vector3.zero, Quaternion.identity); _inventory.Add(upgradedInstance); onUpgrade.Invoke(); } } } }
/// <summary> /// Causes the object to position itself at the closest snap location /// Returns whether the item was actually dropped (could be not dropable) /// /// requires: - if dropable, snap_locations is not empty /// - snap_locations contains non null elements /// </summary> public virtual bool Drop() { if (!dropable) { return(false); } // find the closest snap location float min_dist = float.MaxValue; SnapLocation closest_snap_loc = current_snap_loc; foreach (SnapLocation loc in snap_locations) { if (loc.IsOccupied()) { continue; // not an option } float dist = Vector2.Distance(transform.position, loc.transform.position); if (dist < min_dist) { min_dist = dist; closest_snap_loc = loc; } } // return if no available snap locations if (closest_snap_loc == null || closest_snap_loc.IsOccupied()) { return(false); } // snap to the chosen snap location target_pos = closest_snap_loc.transform.position; current_snap_loc = closest_snap_loc; current_snap_loc.SetOccupied(true); OnSnapLocationChosen(closest_snap_loc); // hide snap objects ShowSnapLocations(false); // update state grabbed = false; dropping = true; // update graphics ChangeGraphicsByState(); // stop drawing on top of other items current_graphics_obj.GetComponent <SpriteRenderer>().sortingLayerName = NormalSortingLayer; return(true); }
/// <summary> /// Change state based on child event to manipulate /// </summary> /// <param name="location">The SnapLocation that triggered the event, and has changed</param> private void OnLocationContentChanged(SnapLocation location) { if (location.IsEmpty) { _size--; } else { _size++; } onSizeChanged?.Invoke(); }
public void SpawnPOIAtLocation(SnapLocation _snap, double lat, double lon) { var map = LocationProviderFactory.Instance.mapManager; if (_snap.city.ToLower().Equals("orlando")) { Debug.Log(_snap.storeName); Vector2d loc = new Vector2d(lat, lon); currentIndex++; GameObject store = Instantiate(storePrefab); store.transform.localPosition = _map.GeoToWorldPosition(loc, true); store.transform.localScale = new Vector3(1, 1, 1); ClickedStore cstore = store.AddComponent <ClickedStore>(); cstore.storeName = _snap.storeName; cstore.storeLocation = _snap.streetAddress; Debug.Log(currentIndex); } }
/// <summary> /// Forces the control to snap to the specified edges. /// </summary> /// <param name="anchor">The screen edges to snap to.</param> public void SnapTo(SnapLocation anchor) { Screen currentScreen = Screen.FromPoint(Location); Rectangle workingArea = currentScreen.WorkingArea; if ((anchor & SnapLocation.Left) != 0) { Left = workingArea.Left + AnchorDistance; } else if ((anchor & SnapLocation.Right) != 0) { Left = workingArea.Right - AnchorDistance - Width; } if ((anchor & SnapLocation.Top) != 0) { Top = workingArea.Top + AnchorDistance; } else if ((anchor & SnapLocation.Bottom) != 0) { Top = workingArea.Bottom - AnchorDistance - Height; } _snapAnchor = anchor; }
// Use this for initialization void Awake() { TextAsset targetFile = Resources.Load <TextAsset>("snap_locations"); string snapLocationJson = targetFile.text; IDictionary result = (IDictionary )MiniJSON.Json.Deserialize(snapLocationJson); IList locationList = (IList)result["results"]; foreach (IDictionary location in locationList) { IDictionary attributes = location["attributes"] as IDictionary; SnapLocation snapLocation = new SnapLocation(Convert.ToInt32(attributes["OBJECTID"]), attributes["STORE_NAME"].ToString(), Convert.ToDouble(attributes["longitude"]), Convert.ToDouble(attributes["latitude"]), attributes["ADDRESS"].ToString(), attributes["ADDRESS2"].ToString(), attributes["CITY"].ToString(), attributes["STATE"].ToString(), attributes["ZIP5"].ToString(), attributes["County"].ToString()); snapLocationList.Add(snapLocation); } }
protected override void OnSnapLocationChosen(SnapLocation loc) { base.OnSnapLocationChosen(loc); SetOpened(!loc.CompareTag(tag_envelope_snap_loc)); }
protected override void OnSnapLocationChosen(SnapLocation loc) { base.OnSnapLocationChosen(loc); SetOpen(loc.CompareTag(tag_openbook_snap_loc)); }
protected virtual void OnSnapLocationChosen(SnapLocation loc) { }