Open file dialog?

Is there a cross platform way to show file dialog in cocos2d-x? If not, how to show the file open dialog in specific platform like Mac in cocos2d-x code? Thanks

I know how to do it for Mac version. Just change the cpp file to mm file and then call the Objective-c API directly. But is there a cross-platform way to do it? Thanks

Can you give me a example about which type of “file dialog”

you need to know.

in Ios you can cross the object-c and c**.
in android you should know what is the jni.
you can read how to communicate java and c** by jni with this topic

http://www.cocos2d-x.org/wiki/How_to_use_jni

bagus flyer wrote:

Is there a cross platform way to show file dialog in cocos2d-x? If not, how to show the file open dialog in specific platform like Mac in cocos2d-x code? Thanks

I mean dialog like file open, file save, etc.
That means there is no cross-platform way to do this, am I right?

Thanks for your reply.

you can reference this link it’s my blog
http://blog.csdn.net/liuyuyefz/article/details/8173550
bagus flyer wrote:

I mean dialog like file open, file save, etc.
That means there is no cross-platform way to do this, am I right?
>
Thanks for your reply.

Thank you anyway although it’s not what I want.
I currently use Mac’s SDK and it works.

By the way, here is the screenshot for File selection dialog for Mac:

yuye liu wrote:

Can you give me a example about which type of “file dialog”
>
you need to know.
>
in Ios you can cross the object-c and c**.
>
in android you should know what is the jni.
>
you can read how to communicate java and c** by jni with this topic
>
http://www.cocos2d-x.org/wiki/How_to_use_jni
>
bagus flyer wrote:
> Is there a cross platform way to show file dialog in cocos2d-x? If not, how to show the file open dialog in specific platform like Mac in cocos2d-x code? Thanks

are you using Cocos2D-X for something other than game development?

I’m using Cocos2d-x for a tool which is for an educational software (game like app).

Jason Slack-Moehrle wrote:

are you using Cocos2D-X for something other than game development?

I see. I was confused as to why a game needs a file system dialog. I think the best thing to do is just call the platform specific way of doing this. You said you have OS X working (prob Objective-C or did you use the C++ implementation?) There should be no reason to change from .cpp to .mm. .MM also isn’t portable.

For Windows look up Common Item Dialog.

For Android maybe this helps: http://developer.android.com/guide/topics/ui/dialogs.html

Searched out there are C++ style code using “FileOpenPicker/CFileDialog/QFileDialog”, but the cocos2d-x project without “clr” support, how to make this work out,anyone? any example code?

:;qst

Finally solution, works well without clr, code example as following:

scope_guard.h

#ifndef SCOPE_GUARD_H
#define SCOPE_GUARD_H

#include

// One shall use ON_SCOPE_EXIT() macro to create a anonymous ScopeGuard object.
// The anonymous function “callback” will be automatically called in the
// deconstractor when the object is out of the scope.

// Usage:
// ON_SCOPE_EXIT([&]{ CloseHandle(hFile);});
// ON_SCOPE_EXIT([&p]{ delete p;});

namespace utility
{

class ScopeGuard
{
public:
	ScopeGuard(std::function<void()> callback)
		:callback_(callback), dismissed_(false)
	{  }

	~ScopeGuard()
	{
		if (!dismissed_)
		{
			callback_();
		}
	}

	void Dismiss()
	{
		dismissed_ = true;
	}

	// Non-copyable. Disable copy constructor and assignment operator.
	ScopeGuard(const ScopeGuard&) = delete;
	ScopeGuard& operator=(const ScopeGuard&) = delete;

private:
	std::function<void()> callback_;
	bool dismissed_;
};

} // namespace utility

#define SCOPE_GUARD_NAME_CANT(line) scope_guard##line
#define MAKE_SCOPE_GUARD(callback, line) utility::ScopeGuard SCOPE_GUARD_NAME_CANT(line)(callback)

#define ON_SCOPE_EXIT(callback) MAKE_SCOPE_GUARD(callback, LINE)

#endif

OpenFile header and impl:

#include <Shlobj.h>
#include <Shobjidl.h>
#include
#include

#include “scope_guard.h”

std::vectorstd::wstring openFiles()
{
HRESULT hr = S_OK;
std::vectorstd::wstring filePaths;

IFileOpenDialog *fileDlg = NULL;
hr = CoCreateInstance(CLSID_FileOpenDialog,
	NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fileDlg));
if (FAILED(hr)) return filePaths;
ON_SCOPE_EXIT([&] { fileDlg->Release(); });

IKnownFolderManager *pkfm = NULL;
hr = CoCreateInstance(CLSID_KnownFolderManager,
	NULL,
	CLSCTX_INPROC_SERVER,
	IID_PPV_ARGS(&pkfm));
if (FAILED(hr)) return filePaths;
ON_SCOPE_EXIT([&] { pkfm->Release(); });

IKnownFolder *pKnownFolder = NULL;
hr = pkfm->GetFolder(FOLDERID_PublicMusic, &pKnownFolder);
//if (FAILED(hr)) return filePaths;
ON_SCOPE_EXIT([&] { pKnownFolder->Release(); });

IShellItem *psi = NULL;
hr = pKnownFolder->GetShellItem(0, IID_PPV_ARGS(&psi));
if (FAILED(hr)) return filePaths;
ON_SCOPE_EXIT([&] { psi->Release(); });

hr = fileDlg->AddPlace(psi, FDAP_BOTTOM);
COMDLG_FILTERSPEC rgSpec[] = {
	{ L"图片文件", L"*.png;*.jpg;*.jpeg;*.gif;" }
};
fileDlg->SetFileTypes(1, rgSpec);

DWORD dwOptions;
fileDlg->GetOptions(&dwOptions);
//
fileDlg->SetOptions(dwOptions | FOS_ALLOWMULTISELECT);
hr = fileDlg->Show(NULL);
if (SUCCEEDED(hr)) {
	IShellItemArray *pRets;
	hr = fileDlg->GetResults(&pRets);
	if (SUCCEEDED(hr)) {
		DWORD count;
		pRets->GetCount(&count);
		for (DWORD i = 0; i < count; i++) {
			IShellItem *pRet;
			LPWSTR nameBuffer;
			pRets->GetItemAt(i, &pRet);
			pRet->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &nameBuffer);
			filePaths.push_back(std::wstring(nameBuffer));
			pRet->Release();
			CoTaskMemFree(nameBuffer);
		}
		pRets->Release();
	}
}
CCLOG("Selected image files path: c \\n", filePaths);
return filePaths;

}

tiny file dialogs on sourceforge offers C C++ cross-platform file dialogs. It calls applescript on OSX.

1 Like