Accessing files dropped onto the application window
Cinder applications can respond to files dropped onto the application window through the callback, fileDrop
. This method takes a ci::app::FileDropEvent
object as a parameter with information about the event.
Getting ready
Your application must implement a fileDrop
method which takes a ci::app::FileDropEvent
object as a parameter.
Add the following method to the application's class declaration:
void fileDrop( FileDropEvent event );
How to do it…
We will learn how to work with the ci::app::FileDropEvent
object to work with file drop events. Perform the following steps to do so:
In the method implementation you can use the
ci::app::FileDropEvent
parameter to access the list of files dropped onto the application by calling thegetFiles
method. This method returns aconststd::vector
container withfs::path
objects:const vector<fs::path >& files = event.getFiles();
The position where the files were dropped onto the window can be accessed through the following callback methods:
To get a
ci::Vec2i
object with the position of the files dropped, type in the following line of code:Vec2i dropPosition = event.getPos();
To get the x and y coordinates separately, you can use the
getX
andgetY
methods, for example:int pOS X = event.getX(); int posY = event.getY();
You can find the number of dropped files by using the
getNumFiles
method:int numFiles = event.getNumFiles();
To access a specific file, if you already know its index, you can use the
getFile
method and pass the index as a parameter.For example, to access the file with an index of
2
, you can use the following line of code:const fs::path& file = event.getFile( 2 );
How it works…
A Cinder application will respond to the system's native event for file drops. It will then create a ci::app::FileDropEvent
object with information about the event and call the fileDrop
callback in our application. This way Cinder creates a uniform way of responding to file drop events across the Windows and OS X platforms.
There's more…
Cinder uses ci::fs::path
objects to define paths. These are typedef
instances of boost::filesystem::path
objects and allow for much greater flexibility when working with paths. To learn more about the fs::path
objects, please refer to the boost::filesystem
library reference, available at http://www.boost.org/doc/libs/1_50_0/libs/filesystem/doc/index.htm.