Skip to content

Commit

Permalink
open image from clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Sep 9, 2024
1 parent abb77b2 commit 43af914
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
14 changes: 14 additions & 0 deletions freesprite/StartScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ void StartScreen::takeInput(SDL_Event evt)
case SDL_MOUSEWHEEL:
break;
case SDL_KEYDOWN:
if (evt.key.keysym.sym == SDLK_v && g_ctrlModifier) {
tryOpenImageFromClipboard();
}
break;
case SDL_DROPFILE:
{
Expand Down Expand Up @@ -168,3 +171,14 @@ void StartScreen::tryLoadFile(std::string path)
}
}
}

void StartScreen::tryOpenImageFromClipboard()
{
Layer* l = platformGetImageFromClipboard();
if (l != NULL) {
g_addScreen(new MainEditor(l));
}
else {
g_addNotification(ErrorNotification("Error", "No image in clipboard"));
}
}
7 changes: 7 additions & 0 deletions freesprite/StartScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class StartScreen : public BaseScreen, public EventCallbackListener
}
}
},
{SDLK_v, { "Open from clipboard",
[](StartScreen* screen) {
screen->tryOpenImageFromClipboard();
}
}
},
},
g_iconNavbarTabFile
}
Expand Down Expand Up @@ -244,5 +250,6 @@ class StartScreen : public BaseScreen, public EventCallbackListener
void eventFileOpen(int evt_id, PlatformNativePathString name, int importerIndex = -1) override;

void tryLoadFile(std::string path);
void tryOpenImageFromClipboard();
};

18 changes: 9 additions & 9 deletions freesprite/freesprite.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,6 @@
<ClCompile Include="libpng\pngwutil.c">
<Filter>Source files\external\libpng</Filter>
</ClCompile>
<ClCompile Include="UIHueSlider.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="BrushReplaceColor.cpp">
<Filter>Source files\brushes</Filter>
</ClCompile>
Expand All @@ -351,15 +348,18 @@
<ClCompile Include="ToolText.cpp">
<Filter>Source files\brushes</Filter>
</ClCompile>
<ClCompile Include="PopupTextTool.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="PopupTileGeneric.cpp">
<Filter>Source files\popup</Filter>
</ClCompile>
<ClCompile Include="Gamepad.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="UIHueSlider.cpp">
<Filter>Source files\ui_widgets</Filter>
</ClCompile>
<ClCompile Include="PopupTextTool.cpp">
<Filter>Source files\popup</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="globals.h">
Expand Down Expand Up @@ -629,15 +629,15 @@
<ClInclude Include="ToolText.h">
<Filter>Header files\brushes</Filter>
</ClInclude>
<ClInclude Include="PopupTextTool.h">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="PopupTileGeneric.h">
<Filter>Header files\popup</Filter>
</ClInclude>
<ClInclude Include="Gamepad.h">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="PopupTextTool.h">
<Filter>Header files\popup</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="freesprite.rc">
Expand Down
2 changes: 2 additions & 0 deletions freesprite/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ void platformTrySaveOtherFile(EventCallbackListener* caller, std::string extensi
void platformTryLoadOtherFile(EventCallbackListener* caller, std::string extension, std::string windowTitle = "Open file");
void platformOpenFileLocation(PlatformNativePathString path);

Layer* platformGetImageFromClipboard();

FILE* platformOpenFile(PlatformNativePathString path, PlatformNativePathString mode);
27 changes: 27 additions & 0 deletions freesprite/platform_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,31 @@ FILE* platformOpenFile(PlatformNativePathString path, PlatformNativePathString m
FILE* ret;
_wfopen_s(&ret, path.c_str(), mode.c_str());
return ret;
}

Layer* platformGetImageFromClipboard() {
bool res = OpenClipboard(WINhWnd);
HANDLE dataHandle = GetClipboardData(CF_BITMAP);
if (dataHandle == NULL) {
CloseClipboard();
return NULL;
}
HBITMAP bmp = (HBITMAP)dataHandle;
BITMAP bitmap;
GetObject(bmp, sizeof(BITMAP), &bitmap);
HDC hdc = GetDC(WINhWnd);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, bmp);
Layer* layer = new Layer(bitmap.bmWidth, bitmap.bmHeight);
for (int y = 0; y < bitmap.bmHeight; y++) {
for (int x = 0; x < bitmap.bmWidth; x++) {
COLORREF color = GetPixel(memDC, x, y);
layer->setPixel({ x, y }, sdlcolorToUint32(SDL_Color{ GetRValue(color), GetGValue(color), GetBValue(color), 255 }));
}
}
SelectObject(memDC, oldBmp);
DeleteDC(memDC);
ReleaseDC(WINhWnd, hdc);
CloseClipboard();
return layer;
}

0 comments on commit 43af914

Please sign in to comment.