Skip to content

Commit

Permalink
Cleanup of fill-colour interface. Now all dialogs where the fill colo…
Browse files Browse the repository at this point in the history
…ur may be specified (resize canvas anchor mode, resize canvas aspect mode, resize canvas remove borders mode, and the fill rotate dialog) use the same interface and allow the origin or cursor colour to be eye-dropped.
  • Loading branch information
bluescan committed Jan 3, 2021
1 parent 76cdc5c commit 665fec8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
82 changes: 44 additions & 38 deletions Src/Resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ namespace Viewer
void DoResizeWidthHeightInterface(int srcW, int srcH, int& dstW, int& dstH);
void DoResizeFilterInterface(int srcW, int srcH, int dstW, int dstH);
void DoResizeAnchorInterface();
void DoFillColourInterface(const char* tootTipText = nullptr);
void DoResizeCrop(int srcW, int srcH, int dstW, int dstH);

void DoResizeCanvasAnchorTab(bool justOpened);
void DoResizeCanvasBorderTab(bool justOpened);
void DoResizeCanvasRemoveBordersTab(bool justOpened);
void DoResizeCanvasAspectTab(bool justOpened);
}

Expand Down Expand Up @@ -128,11 +129,11 @@ void Viewer::DoResizeCrop(int srcW, int srcH, int dstW, int dstH)
{
int originX = (Viewer::CursorX * (srcW - dstW)) / srcW;
int originY = (Viewer::CursorY * (srcH - dstH)) / srcH;
CurrImage->Crop(dstW, dstH, originX, originY, Config.CropFillColour);
CurrImage->Crop(dstW, dstH, originX, originY, Config.FillColour);
}
else
{
CurrImage->Crop(dstW, dstH, tPicture::Anchor(Config.CropAnchor), Config.CropFillColour);
CurrImage->Crop(dstW, dstH, tPicture::Anchor(Config.CropAnchor), Config.FillColour);
}
CurrImage->Bind();
Viewer::SetWindowTitle();
Expand All @@ -141,6 +142,31 @@ void Viewer::DoResizeCrop(int srcW, int srcH, int dstW, int dstH)
}


void Viewer::DoFillColourInterface(const char* toolTipText)
{
tColourf floatCol(Config.FillColour);
ImGui::ColorEdit4
(
"Fill Colour", floatCol.E,
ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_AlphaPreviewHalf | ImGuiColorEditFlags_NoInputs
);
Config.FillColour.Set(floatCol);
if (toolTipText)
ShowToolTip(toolTipText);

ImGui::SameLine();
tPicture* picture = CurrImage ? CurrImage->GetCurrentPic() : nullptr;
if (ImGui::Button("Origin", tVector2(63, 0)) && picture)
Config.FillColour = picture->GetPixel(0, 0);
ShowToolTip("Pick the colour from pixel (0, 0) in the current image.");

ImGui::SameLine();
if (ImGui::Button("Cursor", tVector2(63, 0)))
Config.FillColour.Set(Viewer::PixelColour);
ShowToolTip("Pick the colour from the cursor pixel in the current image.");
}


//
// Below are the top-level modals.
//
Expand Down Expand Up @@ -213,11 +239,11 @@ void Viewer::DoResizeCanvasModal(bool justOpened)
ImGui::EndTabItem();
}

tab = ImGui::BeginTabItem("Border");
tab = ImGui::BeginTabItem("Remove Borders");
ShowToolTip("Remove same-coloured border from image.");
if (tab)
{
DoResizeCanvasBorderTab(justOpenedBorder);
DoResizeCanvasRemoveBordersTab(justOpenedBorder);
justOpenedBorder = false;
ImGui::EndTabItem();
}
Expand All @@ -239,7 +265,10 @@ void Viewer::DoResizeCanvasModal(bool justOpened)

void Viewer::DoResizeCanvasAnchorTab(bool justOpened)
{
tAssert(CurrImage); tPicture* picture = CurrImage->GetCurrentPic(); tAssert(picture);
tAssert(CurrImage);
tPicture* picture = CurrImage->GetCurrentPic();
tAssert(picture);

int srcW = picture->GetWidth();
int srcH = picture->GetHeight();
static int dstW = 512;
Expand All @@ -253,11 +282,8 @@ void Viewer::DoResizeCanvasAnchorTab(bool justOpened)
ImGui::NewLine();
DoResizeWidthHeightInterface(srcW, srcH, dstW, dstH);
if ((dstW > srcW) || (dstH > srcH))
{
tColourf floatCol(Config.CropFillColour);
ImGui::ColorEdit4("Fill Colour", floatCol.E, ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_AlphaPreviewHalf);
Config.CropFillColour.Set(floatCol);
}
DoFillColourInterface();

DoResizeAnchorInterface();

ImGui::NewLine();
Expand All @@ -267,7 +293,7 @@ void Viewer::DoResizeCanvasAnchorTab(bool justOpened)
if (ImGui::Button("Reset", tVector2(100, 0)))
{
Config.CropAnchor = 4;
Config.CropFillColour = tColouri::black;
Config.FillColour = tColouri::black;
dstW = srcW;
dstH = srcH;
}
Expand All @@ -285,18 +311,12 @@ void Viewer::DoResizeCanvasAnchorTab(bool justOpened)
}


void Viewer::DoResizeCanvasBorderTab(bool justOpened)
void Viewer::DoResizeCanvasRemoveBordersTab(bool justOpened)
{
tAssert(CurrImage); tPicture* picture = CurrImage->GetCurrentPic(); tAssert(picture);
static tColouri borderCol = tColouri::black;
static bool channelR = true;
static bool channelG = true;
static bool channelB = true;
static bool channelA = true;
if (justOpened)
{
borderCol.Set(Viewer::PixelColour);
}
ImGui::NewLine();

// You cannot have all channels off.
Expand All @@ -315,25 +335,15 @@ void Viewer::DoResizeCanvasBorderTab(bool justOpened)
ImGui::Text("Channels");
ShowToolTip("These channels are checked for border colour match.\nAt least one must be selected.");

tColourf floatCol(borderCol);
ImGui::ColorEdit4("Colour", floatCol.E, ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_AlphaPreviewHalf);
borderCol.Set(floatCol);
ShowToolTip("If border matches this colour it will be cropped.");

if (ImGui::Button("Origin Colour", tVector2(100, 0)))
borderCol = picture->GetPixel(0, 0);
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - 100.0f);
if (ImGui::Button("Cursor Colour", tVector2(100, 0)))
borderCol.Set(Viewer::PixelColour);
DoFillColourInterface("If border matches this colour it will be cropped.");

ImGui::NewLine();
ImGui::Separator();
ImGui::NewLine();

if (ImGui::Button("Reset", tVector2(100, 0)))
{
borderCol.Set(Viewer::PixelColour);
Config.FillColour.Set(Viewer::PixelColour);
channelR = true;
channelG = true;
channelB = true;
Expand All @@ -354,7 +364,7 @@ void Viewer::DoResizeCanvasBorderTab(bool justOpened)
(channelA ? tMath::ColourChannel_A : 0);

CurrImage->Unbind();
CurrImage->Crop(borderCol, channels);
CurrImage->Crop(Config.FillColour, channels);
CurrImage->Bind();
Viewer::SetWindowTitle();
Viewer::ZoomDownscaleOnly();
Expand Down Expand Up @@ -412,11 +422,7 @@ void Viewer::DoResizeCanvasAspectTab(bool justOpened)
ShowHelpMark("Crop mode cuts off sides resulting in a filled image.\nLetterbox mode adds coloured borders resulting in whole image being visible.");

if (Config.ResizeAspectMode == 1)
{
tColourf floatCol(Config.CropFillColour);
ImGui::ColorEdit4("Fill Colour", floatCol.E, ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_AlphaPreviewHalf);
Config.CropFillColour.Set(floatCol);
}
DoFillColourInterface();

DoResizeAnchorInterface();

Expand All @@ -427,7 +433,7 @@ void Viewer::DoResizeCanvasAspectTab(bool justOpened)
if (ImGui::Button("Reset", tVector2(100, 0)))
{
Config.CropAnchor = 4;
Config.CropFillColour = tColouri::black;
Config.FillColour = tColouri::black;
Config.ResizeAspectNum = 16;
Config.ResizeAspectDen = 9;
Config.ResizeAspectMode = 0;
Expand Down
9 changes: 3 additions & 6 deletions Src/Rotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using namespace tStd;
using namespace tSystem;
using namespace tMath;
using namespace tImage;
namespace Viewer { extern void DoFillColourInterface(const char* = nullptr); }


void Viewer::DoRotateImageModal(bool justOpened)
Expand Down Expand Up @@ -66,11 +67,7 @@ void Viewer::DoRotateImageModal(bool justOpened)
);

if (Config.RotateMode == int(Settings::RotMode::Fill))
{
tColourf floatCol(Config.CropFillColour);
ImGui::ColorEdit4("Fill Colour", floatCol.E, ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_AlphaPreviewHalf);
Config.CropFillColour.Set(floatCol);
}
DoFillColourInterface();

ImGui::NewLine();
ImGui::Separator();
Expand All @@ -96,7 +93,7 @@ void Viewer::DoRotateImageModal(bool justOpened)
CurrImage->Unbind();
CurrImage->Rotate
(
tDegToRad(RotateAnglePreview), Config.CropFillColour,
tDegToRad(RotateAnglePreview), Config.FillColour,
tResampleFilter(Config.ResampleFilterRotateUp),
tResampleFilter(Config.ResampleFilterRotateDown)
);
Expand Down
6 changes: 3 additions & 3 deletions Src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void Viewer::Settings::ResetBehaviourSettings()
SaveFileJpegQuality = 95;
SaveAllSizeMode = 0;
CropAnchor = 4;
CropFillColour = tColouri::black;
FillColour = tColouri::black;
ResizeAspectNum = 16;
ResizeAspectDen = 9;
ResizeAspectMode = 0;
Expand Down Expand Up @@ -137,7 +137,7 @@ void Viewer::Settings::Load(const tString& filename, int screenW, int screenH)
ReadItem(SaveFileJpegQuality);
ReadItem(SaveAllSizeMode);
ReadItem(CropAnchor);
ReadItem(CropFillColour);
ReadItem(FillColour);
ReadItem(ResizeAspectNum);
ReadItem(ResizeAspectDen);
ReadItem(ResizeAspectMode);
Expand Down Expand Up @@ -216,7 +216,7 @@ bool Viewer::Settings::Save(const tString& filename)
WriteItem(SaveFileJpegQuality);
WriteItem(SaveAllSizeMode);
WriteItem(CropAnchor);
WriteItem(CropFillColour);
WriteItem(FillColour);
WriteItem(ResizeAspectNum);
WriteItem(ResizeAspectDen);
WriteItem(ResizeAspectMode);
Expand Down
2 changes: 1 addition & 1 deletion Src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace Viewer
};
int SaveAllSizeMode;
int CropAnchor; // E [-1, 9] with 4 being the default (middle), 0 being top-left, and -1 being 'cursor position'.
tColouri CropFillColour;
tColouri FillColour;
int ResizeAspectNum;
int ResizeAspectDen;
int ResizeAspectMode; // 0 = Crop Mode. 1 = Letterbox Mode.
Expand Down

0 comments on commit 665fec8

Please sign in to comment.