Created
August 20, 2016 08:54
-
-
Save voidproc/85af87131bfa42b2e369e61284b3da6c to your computer and use it in GitHub Desktop.
Combine images with Siv3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Siv3D.hpp> | |
#include <algorithm> | |
void Main() | |
{ | |
Window::SetTitle(L"imgcomb"); | |
Window::Resize(30 + 240 + 30 + 240 + 30, 500); | |
Graphics::SetBackground(Color(240)); | |
Font font(10, Typeface::Bold); | |
GUIStyle guiStyle = GUIStyle::Default; | |
guiStyle.font = font; | |
GUI gui(guiStyle); | |
gui.setTitle(L"Save a combined image"); | |
gui.setPos(30, 50 + 240 + 30); | |
WidgetStyle widgetStyle; | |
widgetStyle.font = font; | |
gui.add(GUIText::Create(L"image1", widgetStyle)); | |
gui.add(L"tf-width1", GUITextField::Create(none, widgetStyle)); | |
gui.add(GUIText::Create(L"x", widgetStyle)); | |
gui.addln(L"tf-height1", GUITextField::Create(none, widgetStyle)); | |
gui.add(GUIText::Create(L"image2", widgetStyle)); | |
gui.add(L"tf-width2", GUITextField::Create(none, widgetStyle)); | |
gui.add(GUIText::Create(L"x", widgetStyle)); | |
gui.addln(L"tf-height2", GUITextField::Create(none, widgetStyle)); | |
gui.add(L"bt-comb-h", GUIButton::Create(L"Holizontally", widgetStyle)); | |
gui.add(L"bt-comb-v", GUIButton::Create(L"Vertically", widgetStyle)); | |
Array<Image> image{ Image(240, 240, Palette::White), Image(240, 240, Palette::White) }; | |
Array<Texture> texture{ Texture(image[0]), Texture(image[1]) }; | |
const auto setGuiImageSizeText = [&](const int w, const int h, const int imageSide /* 0 or 1 */) | |
{ | |
const Array<String> guiid_w{ L"tf-width1", L"tf-width2" }; | |
const Array<String> guiid_h{ L"tf-height1", L"tf-height2" }; | |
gui.textField(guiid_w[imageSide]).setText(Format(w)); | |
gui.textField(guiid_h[imageSide]).setText(Format(h)); | |
}; | |
const auto loadImage = [&](const FilePath& filePath, const int imageSide /* 0 or 1 */) | |
{ | |
image[imageSide] = Image(filePath); | |
texture[imageSide] = Texture(image[imageSide]); | |
setGuiImageSizeText(image[imageSide].width, image[imageSide].height, imageSide); | |
}; | |
for (int i : step(image.size())) | |
{ | |
setGuiImageSizeText(image[i].width, image[i].height, i); | |
} | |
while (System::Update()) | |
{ | |
font(L"Drag & drop image files").drawCenter(Window::Size().x / 2, 40 / 2, Palette::Black); | |
Array<Rect> rect{ Rect(30, 50, 240), Rect(30 + 240 + 30, 50, 240) }; | |
for (int i : step(rect.size())) | |
{ | |
rect[i].drawShadow({ 5, 5 }, 14.0, 2.0, ColorF(0.0, 0.5)); | |
rect[i](texture[i]).draw(); | |
rect[i].drawFrame(0.0, 3.0, Palette::Royalblue); | |
Rect label(rect[i].pos, 16); | |
label.draw(Palette::White).drawFrame(0.0, 3.0, Palette::Black); | |
font(Format(i + 1)).drawCenter(label.center, Palette::Royalblue); | |
} | |
if (Dragdrop::HasItems()) | |
{ | |
const auto items = Dragdrop::GetFilePaths(); | |
const Point dropPos = Dragdrop::GetPos(); | |
Array<int> imageSideNum{ 0, 1 }; | |
if (rect[1].contains(dropPos)) | |
{ | |
std::reverse(imageSideNum.begin(), imageSideNum.end()); | |
} | |
for (int i = 0; i < Min((int)items.size(), 2); i++) | |
{ | |
loadImage(items[i], imageSideNum[i]); | |
} | |
} | |
Optional<bool> holizontal = none; | |
if (gui.button(L"bt-comb-h").pushed) | |
{ | |
holizontal = true; | |
} | |
else if (gui.button(L"bt-comb-v").pushed) | |
{ | |
holizontal = false; | |
} | |
if (holizontal.has_value()) | |
{ | |
const auto save = Dialog::GetSaveImage(); | |
if (save) | |
{ | |
int w1 = Parse<int>(gui.textField(L"tf-width1").text); | |
int h1 = Parse<int>(gui.textField(L"tf-height1").text); | |
int w2 = Parse<int>(gui.textField(L"tf-width2").text); | |
int h2 = Parse<int>(gui.textField(L"tf-height2").text); | |
if (holizontal.value()) | |
{ | |
// Horizontally | |
if (h1 == h2) | |
{ | |
Image newImage(w1 + w2, h1); | |
image[0].scaled(w1, h1).overwrite(newImage, { 0, 0 }); | |
image[1].scaled(w2, h2).overwrite(newImage, { w1, 0 }); | |
newImage.save(save.value()); | |
} | |
} | |
else | |
{ | |
// Vertically | |
if (w1 == w2) | |
{ | |
Image newImage(w1, h1 + h2); | |
image[0].scaled(w1, h1).overwrite(newImage, { 0, 0 }); | |
image[1].scaled(w2, h2).overwrite(newImage, { 0, h1 }); | |
newImage.save(save.value()); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment