Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Viewport.force_drop() to go along with Viewport.force_drag() #10624

Closed
GTG3000 opened this issue Sep 1, 2024 · 4 comments
Closed
Milestone

Comments

@GTG3000
Copy link

GTG3000 commented Sep 1, 2024

Describe the project you are working on

A game featuring tiled inventories. If player clicks on an item, or if player splits a stack and then drops that onto an item, there will be a force_drag call to put that item into "their hand", that is dragging somewhere else.

If the inventory is closed/game is exited, it would be nice not to lose that data, so the player UI has some handling with code that goes kind of like this:

var data = get_viewport().gui_get_drag_data();
if data:
  var fakeInput = InputEventMouseButton.new();
  fakeInput.pressed = false;
  fakeInput.button_index = MOUSE_BUTTON_LEFT;
  Input.parse_input_event(fakeInput);
  ...

Describe the problem or limitation you are having in your project

Forcing the drag/drop data to empty that way works but it was quite unintuitive to figure out, and does look hacky. You shouldn't need to emulate events to work with a gui system like that.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I think there must be a counter-part to force_drag that terminates the current drag state and returns the drag data.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

func on_dragbutton_pressed():
  force_drag({item = item}, item.generate_preview());

func on_inventory_closed():
  var dragData = get_viewport().gui_force_drop();
  if dragData:
    # return item to inventory

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, you can emulate left mouse button being released after grabbing the data from gui_get_drag_data

Is there a reason why this should be core and not an add-on in the asset library?

The change is a little to small to be a stand-alone add-on in my opinion. And the built-in drag-and-drop system could use to be more friendly.

@KoBeWi
Copy link
Member

KoBeWi commented Sep 1, 2024

There is unexposed gui_cancel_drag() function in Viewport. If it was exposed, you could use it with gui_get_drag_data() to achieve the same effect. force_drop() sounds like something that's supposed to drop data onto specific Control; cancelling drag and returning data is rather odd IMO.

@Calinou Calinou changed the title Implement force_drop() to go along with force_drag() Implement Viewport.force_drop() to go along with Viewport.force_drag() Sep 5, 2024
@graydoubt
Copy link

I ran into the same issue. Not sure if I jumped the gun, but I submitted PR godotengine/godot#96614 to expose the Viewport.gui_cancel_drag() method to GDScript. It's a small change and avoids the need for a fake input mouse click that could have side effects.

@GTG3000
Copy link
Author

GTG3000 commented Sep 6, 2024

Yeah, this seems like the perfect solution. Currently dealing with those side effects myself. Thank you!

@KoBeWi
Copy link
Member

KoBeWi commented Sep 6, 2024

Solved by godotengine/godot#96614

@KoBeWi KoBeWi closed this as completed Sep 6, 2024
@KoBeWi KoBeWi added this to the 4.4 milestone Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants