Drag and Drop not works as expected - drag-and-drop

I have 2 identical Items in the scene. Each one can be dragged and also accepts interaction with other dragged objects. To avoid interactions with itself I set drop.accept = false in appropriate drag functions. It works fine when I drag for example rect1 over rect2. rect2 responds to entering and exiting events. But if after that I try to move rect2 over rect1 the drag mechanism stops working and dragging events are not called at all, even if I again move rect1 over rect2.
My target is to know, at each moment of dragging, what object is dragged and on which objects it is moved over.
import QtQuick 2.4
import QtQuick.Window 2.0
Window {
id: win
width: 800
height: 600
Rectangle {
id: rect1
objectName: "rect1"
width: 100
height: 100
x: 200
y: 200
color: "orange"
property bool hold: false
z: hold ? 2 : 1
Text {
anchors.centerIn: parent
text: "rect1"
}
MouseArea {
id: area1
anchors.fill: parent
drag.target: rect1.hold ? rect1 : undefined
onPressed: rect1.hold = true;
onReleased: rect1.hold = false;
}
DropArea {
anchors.fill: parent
onEntered: {
if(drag.source == rect1) {
drag.accepted = false;
}
else{
drag.accepted = true;
console.log("rect1: entered");
}
}
onExited: {
if(drag.source == rect1) {
drag.accepted = false;
}
else {
drag.accepted = true;
console.log("rect1: exited");
}
}
}
Drag.active: rect1.hold
Drag.source: rect1.hold ? rect1 : undefined
Drag.hotSpot: Qt.point(width/2,height/2)
}
Rectangle {
id: rect2
objectName: "rect2"
width: 100
height: 100
x: 500
y: 300
color: "lightblue"
property bool hold: false
z: hold ? 2 : 1
Text {
anchors.centerIn: parent
text: "rect2"
}
MouseArea {
id: area2
anchors.fill: parent
drag.target: rect2.hold ? rect2 : undefined
onPressed: rect2.hold = true;
onReleased: rect2.hold = false;
}
DropArea {
anchors.fill: parent
onEntered: {
if(drag.source == rect2) {
drag.accepted = false;
}
else {
drag.accepted = true;
console.log("rect2: entered");
}
}
onExited: {
if(drag.source == rect2) {
drag.accepted = false;
}
else {
drag.accepted = true;
console.log("rect2: exited");
}
}
}
Drag.active: rect2.hold
Drag.source: rect2.hold ? rect2 : undefined
Drag.hotSpot: Qt.point(width/2,height/2)
}
}

Related

Scaling all nodes of an object together in sceneKit

I'm using apple' approach to handle AR in sceneKit, and here I use this method to scale the object, it works fine when all the nodes of the object merged into one node, but because of the object's animations, I can't make all into one node. So Need to change this method differently that I can scale all the nodes together.
Can anyone help me on that? Thanks
var trackedObject: VirtualObject? {
didSet {
guard trackedObject != nil else { return }
selectedObject = trackedObject
}
}
#objc func pinched(_ gesture :UIPinchGestureRecognizer) {
switch gesture.state {
case .began:
gesture.scale = isScaledChanged ? CGFloat(trackedObject?.objectScale.x ?? 1) : CGFloat(1)
case .changed:
var newScale: SCNVector3
if gesture.scale < 0.5 {
newScale = SCNVector3(x: 0.5, y: 0.5, z: 0.5)
} else if gesture.scale > 3 {
newScale = SCNVector3(3, 3, 3)
} else {
newScale = SCNVector3(gesture.scale, gesture.scale, gesture.scale)
}
isScaledChanged = true
trackedObject?.objectScale = newScale
default:
break
}
}
and in the VirtualObject class
var objectScale: SCNVector3 {
get {
return childNodes.first!.scale
}
set (newValue) {
childNodes.first!.scale = newValue
}
}
I did something like that, but didn't work well
var objectScale: SCNVector3 {
get {
for node in childNodes {
return node.scale
}
return childNodes.first!.scale
}
set (newValue) {
for node in childNodes {
node.scale = newValue
}
}
}

Flutter UI diagonal scrolling with snap

I'm trying to create a widget like in the video for scrolling in all directions with a snapping option
https://cdn.dribbble.com/users/5576/screenshots/7406355/media/30bf6f1d359b8b409cc25c612de9432c.mp4
what I'm tried till now
Widget build(BuildContext context) {
return InteractiveViewer(
constrained: false,
transformationController: _transformController,
onInteractionStart: _onPanStarted,
onInteractionUpdate: _onPanUpdated,
onInteractionEnd: _onPanEnd,
scaleEnabled: false,
panEnabled: true,
child: _buildGrid(),
);
}
and build function is
Widget _buildGrid() {
Table _table = Table(
defaultColumnWidth: FixedColumnWidth(_cardWidth),
children: List.generate(
_rows.length,
(x) => _rows[x],
),
);
return _table;
}
snapping function when dragging is ended
_snapTo({bool animate = true}) {
if (_animationController.isAnimating) {
_animationController.stop();
}
// moving to
var xDistance =
_currentOffset.getTranslation().x - _oldOffset.getTranslation().x;
var yDistance =
_currentOffset.getTranslation().y - _oldOffset.getTranslation().y;
if (xDistance > 20) {
_currentCol--;
_direction = Direction.LEFT;
} else if (xDistance < -20) {
_currentCol++;
_direction = Direction.RIGHT;
}
if (yDistance > 30) {
_currentRow--;
if (_direction == Direction.RIGHT) {
_direction = Direction.TOP_RIGHT;
} else if (_direction == Direction.LEFT) {
_direction = Direction.TOP_LEFT;
} else {
_direction = Direction.TOP;
}
} else if (yDistance < -30) {
_currentRow++;
if (_direction == Direction.RIGHT) {
_direction = Direction.DOWN_RIGHT;
} else if (_direction == Direction.LEFT) {
_direction = Direction.DOWN_LEFT;
} else {
_direction = Direction.DOWN;
}
}
print("$_currentCol,$_currentRow");
log("${xDistance.toInt()},${yDistance.toInt()}");
// Getting x,y to move to
var dx = (_currentCol * _cardWidth) - ((Get.width - _cardWidth) / 2);
var dy = (_currentRow * _cardHeight) - ((Get.height - _cardHeight) / 2);
var matrix = Matrix4.translationValues(-dx, -dy, 0);
_animation = Matrix4Tween(begin: _currentOffset, end: matrix).animate(
CurvedAnimation(
parent: _animationController,
curve: Curves.linear,
),
);
if (_currentOffset == matrix) return;
if (animate) {
_animationController.forward(from: 0);
} else {
setState(() {
_transformController.value = matrix;
});
}
// ..reset();
// ..animateTo(1);
_oldOffset = matrix;
// setState(() {});
// _animationController.reset();
}
any suggestion will be appreciated

How to create a function in Swift that removes then adds a specific subview based on what the current view is?

currently my function looks like this:
func newTile () {
if mainTileTargetView == tileTargetView1 {
self.tileTargetView1.removeFromSuperview()
let tileView = UIView(frame: CGRectMake(0, 0, ScreenWidth, ScreenHeight))
self.view.addSubview(tileView)
self.tileTargetView2 = tileView
self.view.addSubview(tileTargetView2)
self.mainTileTargetView = self.tileTargetView2
if self.mad == true {
self.view.bringSubviewToFront(self.madVaultBoyImage)
} else {
self.view.bringSubviewToFront(self.thumbsUpVaultBoyImage)
}
} else if mainTileTargetView == tileTargetView2{
self.tileTargetView2.removeFromSuperview()
let tileView = UIView(frame: CGRectMake(0, 0, ScreenWidth, ScreenHeight))
self.view.addSubview(tileView)
self.tileTargetView3 = tileView
self.view.addSubview(tileTargetView3)
self.mainTileTargetView = self.tileTargetView3
if self.mad == true {
self.view.bringSubviewToFront(self.madVaultBoyImage)
} else {
self.view.bringSubviewToFront(self.thumbsUpVaultBoyImage)
}
}
It goes on for a few more views. As you can see it's pretty messy at the moment with a lot of repeated code. Is there a way to refactor it and make it look neater?
Here's my version:
func newTile () {
let tileView = UIView(frame: CGRectMake(0, 0, ScreenWidth, ScreenHeight))
self.view.addSubview(tileView)
var nextTarget: UIView!
switch mainTitleTargetView{
case titleTargetView1:
nextTarget = titleTargetView2
default:
nextTarget = titleTargetView3
}
replaceTitle(title: mainTitleTargetView, withTitle: nextTarget, forTileView: tileView)
if self.mad == true {
self.view.bringSubviewToFront(self.madVaultBoyImage)
} else {
self.view.bringSubviewToFront(self.thumbsUpVaultBoyImage)
}
}
func replaceTitle(title title1: UIView, withTitle title2: UIView, forTileView tile: UIView){
title1.removeFromSuperview()
title2 = tile
self.addSubview(title2)
self.mainTitleTargetView = title2
}

Unity Fade delay when game ends

I'm fairly new at Unity and i'm trying to making a game.
I want to have an subtitle fading in when you're at the end of the game. This start when you hit a button.
But when I code a image that I fadein, it plays it directly when you start the game.
Do you guys know a solution?
#pragma strict
private var guiShow : boolean = false;
var car : GameObject;
var rayLength = 10;
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
var fadesubtitles : boolean = false;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "car")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
guiShow = false;
}
else if(Input.GetKeyDown("e"))
{
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 150, 25), "Press F to escape");
if(Input.GetKeyDown("f")){
fadesubtitles = true;
}
}
}
if (fadesubtitles == true){
yield FadeGUITexture(guiObject, fadeTime, Fade.In);
yield WaitForSeconds(3.0);
yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
if (subtitles == true){
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
}
I'd start your game object in the 'disabled' state (uncheck it in the inspector). Then at then end of the game, have some code that enables it.
You can use iTween.
FadeFrom(GameObject target, Hashtable args)
Example:
iTween.FadeFrom(gameObject, iTween.Hash("alpha", 0f, "amount", 1f, "time", 2f));

select and deselect object in unity3D

i am making a game in which i am using is-clicked function when i click the object the letter written on it displayed now i want that when i clicked the same object again the word disappear... now how can i do that?
#pragma strict
static var nextPos = 200;
var word: String;
var sel: String;
var isClicked : boolean=false;
var xpos: float = 200;
function OnMouseDown()
{
if (!isClicked) {
isClicked = true;
xpos = nextPos;
nextPos += 8;
}
}
function OnGUI()
{
if (gameObject.name == "Sphere(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "A");
}
else if (gameObject.name == "Sphere 1(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "B");
}
else if (gameObject.name == "Sphere 2(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "C");
}
else if (gameObject.name == "Sphere 3(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "D");
}
}
write in OnMouseDown
else if(isClicked)
{
isClicked = false;
// do your xpos stuff here
}