From f7561ebb2a95607578e7f1cdf6823d0fedf08fb3 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 19 Jan 2022 07:48:25 +1100 Subject: [PATCH] Initial 'Load file' map support for Windows 'Load file' opens a file dialog allowing you to load maps from anywhere on disc --- src/Menus.c | 10 ++++------ src/Window_Android.c | 4 ++++ src/Window_Carbon.c | 4 ++++ src/Window_SDL.c | 4 ++++ src/Window_Web.c | 2 +- src/Window_Win.c | 29 ++++++++++++++++++++++++++++- src/Window_X11.c | 4 ++++ src/interop_cocoa.m | 4 ++++ src/interop_ios.m | 4 ++++ 9 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/Menus.c b/src/Menus.c index a8292e658..94957f3bc 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -384,7 +384,7 @@ static void ListScreen_ContextRecreated(void* screen) { ListScreen_UpdatePage(s); if (!s->UploadClick) return; - ButtonWidget_SetConst(&s->upload, "Upload", &s->font); + ButtonWidget_SetConst(&s->upload, Input_TouchMode ? "Upload" : "Load file...", &s->font); } static void ListScreen_Reload(struct ListScreen* s) { @@ -1764,14 +1764,12 @@ static void LoadLevelScreen_LoadEntries(struct ListScreen* s) { StringsBuffer_Sort(&s->entries); } -#ifdef CC_BUILD_WEB static void LoadLevelScreen_UploadCallback(const cc_string* path) { Map_LoadFrom(path); } static void LoadLevelScreen_UploadFunc(void* s, void* w) { - Window_OpenFileDialog(".cw", LoadLevelScreen_UploadCallback); + Platform_LogConst("UPLOAD"); + cc_result res = Window_OpenFileDialog(".cw", LoadLevelScreen_UploadCallback); + if (res) Logger_SimpleWarn(res, "showing open file dialog"); } -#else -#define LoadLevelScreen_UploadFunc NULL -#endif void LoadLevelScreen_Show(void) { struct ListScreen* s = &ListScreen; diff --git a/src/Window_Android.c b/src/Window_Android.c index 04b21f3d9..39b381c59 100644 --- a/src/Window_Android.c +++ b/src/Window_Android.c @@ -363,6 +363,10 @@ static void ShowDialogCore(const char* title, const char* msg) { (*env)->DeleteLocalRef(env, args[1].l); } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + return ERR_NOT_SUPPORTED; +} + static struct Bitmap fb_bmp; void Window_AllocFramebuffer(struct Bitmap* bmp) { bmp->scan0 = (BitmapCol*)Mem_Alloc(bmp->width * bmp->height, 4, "window pixels"); diff --git a/src/Window_Carbon.c b/src/Window_Carbon.c index 2f1dbc439..3b766b919 100644 --- a/src/Window_Carbon.c +++ b/src/Window_Carbon.c @@ -607,6 +607,10 @@ static void ShowDialogCore(const char* title, const char* msg) { showingDialog = false; } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + return ERR_NOT_SUPPORTED; +} + static CGrafPtr fb_port; static struct Bitmap fb_bmp; static CGColorSpaceRef colorSpace; diff --git a/src/Window_SDL.c b/src/Window_SDL.c index 720415ee8..0adf80cca 100644 --- a/src/Window_SDL.c +++ b/src/Window_SDL.c @@ -272,6 +272,10 @@ static void ShowDialogCore(const char* title, const char* msg) { SDL_ShowSimpleMessageBox(0, title, msg, win_handle); } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + return ERR_NOT_SUPPORTED; +} + static SDL_Surface* surface; void Window_AllocFramebuffer(struct Bitmap* bmp) { surface = SDL_GetWindowSurface(win_handle); diff --git a/src/Window_Web.c b/src/Window_Web.c index 571268927..e2f8d160f 100644 --- a/src/Window_Web.c +++ b/src/Window_Web.c @@ -549,7 +549,7 @@ cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callb uploadCallback = callback; /* Calls Window_OnFileUploaded on success */ interop_OpenFileDialog(filter); - return ERR_NOT_SUPPORTED; + return 0; } void Window_AllocFramebuffer(struct Bitmap* bmp) { } diff --git a/src/Window_Win.c b/src/Window_Win.c index b4c53b4a8..303d90b8f 100644 --- a/src/Window_Win.c +++ b/src/Window_Win.c @@ -22,6 +22,7 @@ /* Hence the actual minimum supported OS is Windows 2000. This just avoids redeclaring structs. */ #endif #include +#include /* https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setpixelformat */ #define CC_WIN_STYLE WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN @@ -136,7 +137,8 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara case WM_MOUSEMOVE: /* Set before position change, in case mouse buttons changed when outside window */ - Input_SetNonRepeatable(KEY_LMOUSE, wParam & 0x01); + if (!(wParam & 0x01)) Input_SetReleased(KEY_LMOUSE); + //Input_SetNonRepeatable(KEY_LMOUSE, wParam & 0x01); Input_SetNonRepeatable(KEY_RMOUSE, wParam & 0x02); Input_SetNonRepeatable(KEY_MMOUSE, wParam & 0x10); /* TODO: do we need to set XBUTTON1/XBUTTON2 here */ @@ -535,6 +537,31 @@ static void ShowDialogCore(const char* title, const char* msg) { MessageBoxA(win_handle, msg, title, 0); } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + cc_string path; char pathBuffer[MAX_PATH]; + WCHAR str[MAX_PATH] = { 0 }; + OPENFILENAMEW ofn = { 0 }; + int i; + + ofn.lStructSize = sizeof(ofn); + ofn.hwndOwner = win_handle; + ofn.lpstrFile = str; + ofn.nMaxFile = MAX_PATH; + ofn.lpstrFilter = L"All\0*.*\0CW files\0*.cw\0"; // TODO rethink + ofn.nFilterIndex = 1; + ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; + + if (!GetOpenFileNameW(&ofn)) + return CommDlgExtendedError(); + String_InitArray(path, pathBuffer); + + for (i = 0; i < MAX_PATH && str[i]; i++) { + String_Append(&path, Convert_CodepointToCP437(str[i])); + } + callback(&path); + return 0; +} + static HDC draw_DC; static HBITMAP draw_DIB; void Window_AllocFramebuffer(struct Bitmap* bmp) { diff --git a/src/Window_X11.c b/src/Window_X11.c index 8e694d9e8..8c971f9b0 100644 --- a/src/Window_X11.c +++ b/src/Window_X11.c @@ -966,6 +966,10 @@ static void ShowDialogCore(const char* title, const char* msg) { XFlush(m.dpy); /* flush so window disappears immediately */ } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + return ERR_NOT_SUPPORTED; +} + static GC fb_gc; static XImage* fb_image; static struct Bitmap fb_bmp; diff --git a/src/interop_cocoa.m b/src/interop_cocoa.m index 1895c9594..767ea53ea 100644 --- a/src/interop_cocoa.m +++ b/src/interop_cocoa.m @@ -507,6 +507,10 @@ void ShowDialogCore(const char* title, const char* msg) { CFRelease(msgCF); } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + return ERR_NOT_SUPPORTED; +} + static struct Bitmap fb_bmp; void Window_AllocFramebuffer(struct Bitmap* bmp) { bmp->scan0 = (BitmapCol*)Mem_Alloc(bmp->width * bmp->height, 4, "window pixels"); diff --git a/src/interop_ios.m b/src/interop_ios.m index 4646c6351..279c571fb 100644 --- a/src/interop_ios.m +++ b/src/interop_ios.m @@ -260,6 +260,10 @@ void Window_LockLandscapeOrientation(cc_bool lock) { [UIViewController attemptRotationToDeviceOrientation]; } +cc_result Window_OpenFileDialog(const char* filter, OpenFileDialogCallback callback) { + return ERR_NOT_SUPPORTED; +} + /*#########################################################################################################################* *--------------------------------------------------------2D window--------------------------------------------------------*