Skip to content

Commit

Permalink
move adjust hsv to filters + preview
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Feb 11, 2025
1 parent acc2652 commit ddaee01
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 164 deletions.
18 changes: 17 additions & 1 deletion freesprite/BaseFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@ Layer* FilterSwapRGBToBGR::run(Layer* src, std::map<std::string, std::string> op
Layer* c = copy(src);
SDL_ConvertPixels(c->w, c->h, SDL_PIXELFORMAT_ARGB8888, src->pixelData, c->w * 4, SDL_PIXELFORMAT_ABGR8888, c->pixelData, c->w * 4);
return c;
}
}

Layer* FilterAdjustHSV::run(Layer* src, std::map<std::string, std::string> options)
{
double h = std::stod(options["hue"]);
double s = std::stod(options["saturation"]) / 100.0;
double v = std::stod(options["value"]) / 100.0;
hsv hsvv = { h,s,v };
Layer* c = copy(src);
for (int y = 0; y < c->h; y++) {
for (int x = 0; x < c->w; x++) {
u32 px = src->getPixelAt({ x, y }, true);
c->setPixel({ x,y }, hsvShift(px, hsvv));
}
}
return c;
}
18 changes: 18 additions & 0 deletions freesprite/BaseFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class BaseFilter
virtual Layer* run(Layer* src, std::map<std::string, std::string> options) { return NULL; };
virtual std::vector<FilterParameter> getParameters() { return {}; }
protected:
/// <summary>
/// Use this instead of Layer::copy in your filter function. Calls Layer::copyWithNoTextureInit so that it's thread-safe.
/// </summary>
/// <param name="src">source layer to be copied</param>
/// <returns>a copy of the layer</returns>
Layer* copy(Layer* src);
};

Expand Down Expand Up @@ -77,3 +82,16 @@ class FilterSwapRGBToBGR : public BaseFilter {
std::string name() override { return "Swap channels RGB->BGR"; }
Layer* run(Layer* src, std::map<std::string, std::string> options) override;
};

class FilterAdjustHSV : public BaseFilter {
public:
std::string name() override { return "Adjust HSV"; }
Layer* run(Layer* src, std::map<std::string, std::string> options) override;
std::vector<FilterParameter> getParameters() override {
return {
FLOAT_PARAM("hue", -360, 360, 0),
FLOAT_PARAM("saturation", -100, 100, 0),
FLOAT_PARAM("value", -100, 100, 0),
};
}
};
1 change: 0 additions & 1 deletion freesprite/EventCallbackListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#define EVENT_TILEMAP_RENDERALLLTOIMAGES 51
#define EVENT_TILEMAP_RENDERCURRENTLTOIMAGE 52
#define EVENT_TILEMAP_RESIZE 53
#define EVENT_MAINEDITOR_ADJHSV 54

class EventCallbackListener {
public:
Expand Down
110 changes: 0 additions & 110 deletions freesprite/PopupAdjustHSV.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions freesprite/PopupAdjustHSV.h

This file was deleted.

7 changes: 4 additions & 3 deletions freesprite/PopupApplyFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ void PopupApplyFilter::setupWidgets()
{
float v = (p.defaultValue - p.minValue) / (p.maxValue - p.minValue);
UISlider* slider = new UISlider();
slider->position = XY{ 250, y };
slider->position = XY{ 285, y };
slider->sliderPos = v;
slider->wxHeight = 25;
slider->setCallbackListener(i, this);
wxsManager.addDrawable(slider);

UILabel* valueLabel = new UILabel();
valueLabel->position = xySubtract(slider->position, { 50, 0 });
valueLabel->position = xySubtract(slider->position, { 70, 0 });
paramLabels.push_back(valueLabel);
wxsManager.addDrawable(valueLabel);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ void PopupApplyFilter::updateLabels()
break;
case PT_FLOAT:
default:
label->text = std::to_string(p.defaultValue);
label->text = std::format("{:.1f}", p.defaultValue);
break;
}
}
Expand Down Expand Up @@ -249,5 +249,6 @@ void PopupApplyFilter::previewRenderThread()
pixelDataDirty = true;
delete l;
}
SDL_Delay(1);
}
}
2 changes: 0 additions & 2 deletions freesprite/freesprite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@
<ClCompile Include="PanelRPG2KTilemapPreview.cpp" />
<ClCompile Include="PanelSpritesheetPreview.cpp" />
<ClCompile Include="PanelTilemapPreview.cpp" />
<ClCompile Include="PopupAdjustHSV.cpp" />
<ClCompile Include="PopupApplyFilter.cpp" />
<ClCompile Include="PopupGlobalConfig.cpp" />
<ClCompile Include="PopupIntegerScale.cpp" />
Expand Down Expand Up @@ -337,7 +336,6 @@
<ClInclude Include="PanelTilemapPreview.h" />
<ClInclude Include="platform_linux.h" />
<ClInclude Include="platform_macos.h" />
<ClInclude Include="PopupAdjustHSV.h" />
<ClInclude Include="PopupApplyFilter.h" />
<ClInclude Include="PopupGlobalConfig.h" />
<ClInclude Include="PopupIntegerScale.h" />
Expand Down
6 changes: 0 additions & 6 deletions freesprite/freesprite.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,6 @@
<ClCompile Include="Canvas.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="PopupAdjustHSV.cpp">
<Filter>Source files\popup</Filter>
</ClCompile>
<ClCompile Include="MinecraftBlockPreviewScreen.cpp">
<Filter>Source files\screen</Filter>
</ClCompile>
Expand Down Expand Up @@ -893,9 +890,6 @@
<ClInclude Include="json\json.hpp">
<Filter>Header files\external\json</Filter>
</ClInclude>
<ClInclude Include="PopupAdjustHSV.h">
<Filter>Header files\popup</Filter>
</ClInclude>
<ClInclude Include="MinecraftBlockPreviewScreen.h">
<Filter>Header files\screen</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions freesprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ int main(int argc, char** argv)
//load filters
g_filters.push_back(new FilterBlur());
g_filters.push_back(new FilterSwapRGBToBGR());
g_filters.push_back(new FilterAdjustHSV());

TTF_Init();
g_fnt = new TextRenderer();
Expand Down
13 changes: 0 additions & 13 deletions freesprite/maineditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "PopupYesNo.h"
#include "PopupGlobalConfig.h"
#include "PopupPickColor.h"
#include "PopupAdjustHSV.h"
#include "PopupApplyFilter.h"

SDL_Rect MainEditor::getPaddedTilePosAndDimensions(XY tilePos)
Expand Down Expand Up @@ -772,14 +771,6 @@ void MainEditor::setUpWidgets()
}
}
},
{SDLK_h, { "Adjust HSV",
[](MainEditor* editor) {
PopupAdjustHSV* newPopup = new PopupAdjustHSV("Adjust layer HSV", "");
newPopup->setCallbackListener(EVENT_MAINEDITOR_ADJHSV, editor);
g_addPopup(newPopup);
}
}
},
{SDLK_o, { "Outline current layer",
[](MainEditor* editor) {
editor->layer_outline(false);
Expand Down Expand Up @@ -1299,10 +1290,6 @@ void MainEditor::eventPopupClosed(int evt_id, BasePopup* p)
else if (evt_id == EVENT_MAINEDITOR_INTEGERSCALE) {
integerScaleAllLayersFromCommand(((PopupIntegerScale*)p)->result, ((PopupIntegerScale*)p)->downscaleCheckbox->isChecked());
}
else if (evt_id == EVENT_MAINEDITOR_ADJHSV) {
PopupAdjustHSV* pp = (PopupAdjustHSV*)p;
layer_hsvShift({ pp->adjH, pp->adjS, pp->adjV });
}
}

void MainEditor::eventTextInputConfirm(int evt_id, std::string text)
Expand Down

0 comments on commit ddaee01

Please sign in to comment.