add file drop API

pull/20/head
darkerbit 2022-06-05 12:27:46 -07:00 committed by cosmonaut
parent a10f18b4eb
commit ff544140e0
1 changed files with 30 additions and 0 deletions

View File

@ -104,6 +104,17 @@ namespace MoonWorks
protected abstract void Update(TimeSpan delta); protected abstract void Update(TimeSpan delta);
protected abstract void Draw(double alpha); protected abstract void Draw(double alpha);
// Called when a file is dropped on the game window.
protected virtual void DropFile(string filePath) {}
/* Required to distinguish between multiple files dropped at once
* vs multiple files dropped one at a time.
*
* Called once for every multi-file drop.
*/
protected virtual void DropBegin() {}
protected virtual void DropComplete() {}
private void Tick() private void Tick()
{ {
AdvanceElapsedTime(); AdvanceElapsedTime();
@ -179,6 +190,18 @@ namespace MoonWorks
case SDL.SDL_EventType.SDL_MOUSEWHEEL: case SDL.SDL_EventType.SDL_MOUSEWHEEL:
Inputs.Mouse.Wheel += _event.wheel.y; Inputs.Mouse.Wheel += _event.wheel.y;
break; break;
case SDL.SDL_EventType.SDL_DROPBEGIN:
DropBegin();
break;
case SDL.SDL_EventType.SDL_DROPCOMPLETE:
DropComplete();
break;
case SDL.SDL_EventType.SDL_DROPFILE:
HandleFileDrop(_event);
break;
} }
} }
} }
@ -211,6 +234,13 @@ namespace MoonWorks
} }
} }
private unsafe void HandleFileDrop(SDL2.SDL.SDL_Event evt)
{
// Need to do it this way because SDL2 expects you to free the filename string.
string filePath = SDL.UTF8_ToManaged(evt.drop.file, true);
DropFile(filePath);
}
private TimeSpan AdvanceElapsedTime() private TimeSpan AdvanceElapsedTime()
{ {
long currentTicks = gameTimer.Elapsed.Ticks; long currentTicks = gameTimer.Elapsed.Ticks;