public bool Drop(IDraggable item) { ItemIcon ii = item as ItemIcon; if (ii == null || ii.Item==null) return false; UIElement element = item as UIElement; Vector2 relPos = element.TotalOffset - this.TotalOffset + 0.5f * new Vector2(item.Rect.Width, item.Rect.Height); if (relPos.X < sideOffset || relPos.X > _size.X - sideOffset || relPos.Y < 0 || relPos.Y > _size.Y) return false; int rowLength = (int)((_size.X - 2 * sideOffset) / (partSize * iconSpread)); int row = (int)((relPos.Y - 15) / (partSize * iconSpread)); int column = (int)((relPos.X - sideOffset) / (partSize * iconSpread)); column = column >= 0 ? column : 0; column = column < rowLength ? column : rowLength - 1; int index = column + row * rowLength; item.Drop(this); _inventory.Insert(ii.Item, index, true); UpdateContent(); return true; }
/// <summary> /// マウスイベントの開始 /// </summary> /// <param name="target">イベント発生元コントロール</param> /// <param name="button">ドラッグイベントに対応するマウスボタン</param> public void Start(Control target, MouseButtons button) { IDraggable active = this.DefaultItem; //ボタンが押されていない間のマウスムーブイベント this.Disposables.Add( target.MouseMoveAsObservable() .Where(e => e.Button == MouseButtons.None) .Subscribe( e => { //選択中のオブジェクトを先行してチェック active = SelectedItems.HitTest(e.Location); if (active != null) { target.Cursor = active.Cursor; return; } //カーソルの下にあるm_shapes内のオブジェクトをactiveに設定する。 //オブジェクトのない場所にカーソルがある場合はDefaultItemがActiveとなる。 active = DefaultItem; foreach (IDraggable item in Items.Reverse <IDraggable>()) { var result = item.HitTest(e.Location); if (result != null) { active = result; break; } } ; target.Cursor = active.Cursor; })); //マウスダウンイベント this.Disposables.Add( target.MouseDownAsObservable() .Subscribe( e => { var act = active as ISelectable; if (act != null && !act.IsSelected) { foreach (ISelectable item in this.Selectables) { item.IsSelected = item == act; } this.SelectedItems = new CompositeDraggable(Enumerable.Empty <IDraggable>()); target.Refresh(); } ; })); //ドラッグイベント this.Disposables.Add( target.MouseDragAsObservable(button) .Subscribe( e => { active.Drag(e); target.Refresh(); })); //マウスアップイベント this.Disposables.Add( target.MouseUpAsObservable() .Subscribe( e => { var item = active.Drop(); if (item != null) { this.AddItem(item); } target.Refresh(); })); }
void Update() { if (Input.GetMouseButton(0)) { touching = true; } Vector2 pos; Touch touch = new Touch(); if (Input.touchCount > 0) { touch = Input.GetTouch(0); pos = touch.position; touching = true; } pos = Input.mousePosition; TouchedScreenPosMoved = (touch.phase != TouchPhase.Began || !Input.GetMouseButtonDown(0)) ? (lastTouchedScreenPos - pos) : Vector2.zero; lastTouchedScreenPos = pos; Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, 100000)); if ((Input.touchCount > 0 && touch.phase == TouchPhase.Began) || Input.GetMouseButtonDown(0)) { if (startTouch != null) { startTouch.Invoke(pos); } Ray touchRay = new Ray(Vector3.forward, touchedPos); RaycastHit2D hit = Physics2D.Raycast(touchedPos, Vector2.zero, Mathf.Infinity, tappableMask); if (hit) { Transform checking = hit.transform; IClickable clickable = checking.GetComponent <IClickable>();; while (clickable == null && checking.parent != null) { checking = checking.parent; clickable = checking.GetComponent <IClickable>(); } if (clickable != null) { clickable.Click(); } } if (dragging && draggingObject == null) { hit = Physics2D.Raycast(touchedPos, Vector2.zero, 10, draggableMask); if (hit) { IDraggable draggable = hit.collider.GetComponent <IDraggable>(); if (draggable != null) { draggingObject = draggable; draggingObject.Pick(); } } } } if ((Input.touchCount > 0 && touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) || Input.GetMouseButtonUp(0)) { touching = false; if (dragging && draggingObject != null) { draggingObject.Drop(); draggingObject = null; } } if (dragging && draggingObject != null) { draggingObject.UpdatePos(touchedPos); } if (Input.touchCount > 0 && (int)Input.GetTouch(0).phase > 2 && (int)Input.GetTouch(1).phase > 2) { Debug.Log("bep"); if ((Input.GetTouch(0).position - Input.GetTouch(1).position).magnitude / Screen.width > MinInversePinchDist) { Debug.Log("bop"); inversePinch.Invoke(); } } }