Coder Social home page Coder Social logo

cezuriku / better-drag-and-drop-sprite Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jcaguilera/better-drag-and-drop-sprite

0.0 1.0 0.0 14 KB

Easy way to drag and drop in Godot without using Collision Shapes

License: MIT License

GDScript 100.00%

better-drag-and-drop-sprite's Introduction

Better Drag-and-drop Sprite

Easy way to drag and drop in Godot without using Collision Shapes. This is based on the Drag-and-drop Sprite example by Godot Garden.

Improvements:

Issue 1: Sprite's center moves to the mouse position when we click

First issue I found was that when I cliked a sprite to drag it, the center of the sprite moved to the mouse position. That causes some problems when trying to drag multiple sprites, and also, it didn't look good. I fix it this way (using vectors): mouse_pos changes, but mouse to center is static, mouse_pos+mouse_to_center = sprite_posmouse_pos, is where we "click" the sprite to move it and mouse_to_center is a static vector that represents the distance from where we are grabbing the sprite to the center of it. To calculate this, I did this: First, when we click the sprite, we calculate the mouse_to_center using the sprite_pos (at the moment when we clicked). So, mouse_to_center = sprite_pos - mouse_pos. Then, when dragging the sprite, we calculate the "new" sprite position as the mouse_position plus the mouse_to_center vector. That's it!

Issue 2: Using multiple sprites (Overlapping sprites)

When we have multiple sprites in one place, we always need to move the sprite on the top. To calculate this, a lot of drag and drop examples use a picker object that is always following the mouse. Now, that method requires the use of Collision Shapes and Area2D nodes, and I didn't want to use them. So I figured out another way to do it. When the mouse enters a Sprites area, is added to a sprites array, so then, when we click, we order the list by z_index and get the top sprite that we want to move. Really easy!

Help me improve this example

My brain was not working at a 100% when I made this example, so I think a lot of code can be improved. Please feel free to help me improve this example, so we can help more people.

License

MIT License - see LICENSE for more details.

better-drag-and-drop-sprite's People

Contributors

cezuriku avatar jcaguilera avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.