Skip to content

Commit

Permalink
edit palette
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Sep 13, 2024
1 parent 45099b4 commit cf7e3ad
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
17 changes: 17 additions & 0 deletions freesprite/PalettizedEditorColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "FontRenderer.h"
#include "UIDropdown.h"
#include "Notification.h"
#include "PopupPickColor.h"
#include "UIColorInputField.h"

PalettizedEditorColorPicker::PalettizedEditorColorPicker(MainEditorPalettized* c)
{
Expand Down Expand Up @@ -113,6 +115,12 @@ void PalettizedEditorColorPicker::eventButtonRightClicked(int evt_id)
{
if (evt_id >= 200) {
//todo: open popup to edit the color
PopupPickColor* ppc = new PopupPickColor("Pick color", std::format("Select color for palette index {}", evt_id-200), true);
ppc->setCallbackListener(evt_id, this);
uint32_t palCol = upcastCaller->palette[evt_id - 200];
ppc->colorInput->setPickedColor(palCol);
ppc->setAlpha(palCol >> 24);
g_addPopup(ppc);
}
}

Expand Down Expand Up @@ -184,6 +192,15 @@ void PalettizedEditorColorPicker::eventFileOpen(int evt_id, PlatformNativePathSt

}

void PalettizedEditorColorPicker::eventColorSet(int evt_id, uint32_t color)
{
if (evt_id >= 200) {
upcastCaller->palette[evt_id - 200] = color;
upcastCaller->setPalette(upcastCaller->palette);
updateForcedColorPaletteButtons();
}
}

void PalettizedEditorColorPicker::updateForcedColorPaletteButtons()
{
colorPaletteTabs->tabs[0].wxs.freeAllDrawables();
Expand Down
1 change: 1 addition & 0 deletions freesprite/PalettizedEditorColorPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PalettizedEditorColorPicker :
void eventDropdownItemSelected(int evt_id, int index, std::string name) override;
void eventFileSaved(int evt_id, PlatformNativePathString name, int exporterIndex = -1) override;
void eventFileOpen(int evt_id, PlatformNativePathString name, int exporterIndex = -1) override;
void eventColorSet(int evt_id, uint32_t color) override;

void setMainEditorColorRGB(SDL_Color col, bool updateHSVSliders = true, bool updateRGBSliders = true, bool updateHSVTextBoxes = true) override {}
void updateLastColorButtons() override {}
Expand Down
57 changes: 53 additions & 4 deletions freesprite/PopupPickColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
#include "UIButton.h"
#include "FontRenderer.h"

PopupPickColor::PopupPickColor(std::string tt, std::string tx) {
PopupPickColor::PopupPickColor(std::string tt, std::string tx, bool acceptAlpha) {
this->title = tt;
this->text = tx;
this->acceptAlpha = acceptAlpha;
wxHeight = 200;

colorInput = new UIColorInputField();
colorInput->position = XY{ 30, wxHeight / 2 };
colorInput->wxWidth = 200;
colorInput->wxHeight = 30;
wxsManager.addDrawable(colorInput);

if (acceptAlpha) {
alphaInput = new UITextField();
alphaInput->position = XY{ 160, wxHeight / 2 };
alphaInput->wxWidth = 50;
alphaInput->wxHeight = 30;
alphaInput->setCallbackListener(2, this);
wxsManager.addDrawable(alphaInput);
setAlpha(255);
}

UIButton* nbutton = new UIButton();
nbutton->text = "Set";
nbutton->position = XY{ wxWidth - 260, wxHeight - 40 };
Expand Down Expand Up @@ -46,9 +55,49 @@ void PopupPickColor::eventButtonPressed(int evt_id)
{
if (evt_id == 0) {
if (callback != NULL) {
callback->eventColorSet(callback_id, colorInput->pickedColor);
callback->eventColorSet(callback_id, getColor());
}

}
g_closePopup(this);
}

void PopupPickColor::updateRGBTextBoxOnInputEvent(std::string data, uint8_t* value)
{
try {
int val;
if (data.size() == 3 && data[0] == 'x') {
val = std::stoi(data.substr(1), 0, 16);
}
else {
val = std::stoi(data);
}
if (val >= 0 && val <= 255) {
*value = val;
alphaInput->text = std::to_string(alpha);
}
}
catch (std::exception) {

}
}

void PopupPickColor::setAlpha(uint8_t a)
{
if (acceptAlpha) {
alpha = a;
alphaInput->text = std::to_string(alpha);
}
}

uint32_t PopupPickColor::getColor()
{
return (colorInput->pickedColor & 0xFFFFFF) | (acceptAlpha ? (alpha << 24) : 0xFF000000);
}

void PopupPickColor::eventTextInput(int evt_id, std::string data)
{
if (evt_id == 2) {
updateRGBTextBoxOnInputEvent(data, &alpha);
}
}
14 changes: 12 additions & 2 deletions freesprite/PopupPickColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ class PopupPickColor :
public:
std::string title = "";
std::string text = "";

UIColorInputField* colorInput;
UITextField* alphaInput = NULL;

PopupPickColor(std::string tt, std::string tx);
PopupPickColor(std::string tt, std::string tx, bool acceptAlpha = false);

void render() override;

void eventButtonPressed(int evt_id) override;
void eventTextInput(int evt_id, std::string data) override;

void updateRGBTextBoxOnInputEvent(std::string data, uint8_t* value);
void setAlpha(uint8_t a);

uint32_t getColor();
protected:
bool acceptAlpha = false;
uint8_t alpha = 255;
};

2 changes: 1 addition & 1 deletion freesprite/TabbedView.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TabbedView :
tabs[openTab].wxs.renderAll(xyAdd(position, XY{(int)(200 * (nextTabSlideFromTheLeft ? -1 : 1) * (1.0-XM1PW3P1(tabSwitchTimer.percentElapsedTime(250)))), buttonsHeight}));
}
void handleInput(SDL_Event evt, XY gPosOffset) override {
if (evt.type == SDL_MOUSEBUTTONDOWN && evt.button.button == 1 && evt.button.state) {
if (evt.type == SDL_MOUSEBUTTONDOWN && (evt.button.button == 1 || evt.button.button == 3) && evt.button.state) {
if (!tabButtons.tryFocusOnPoint(XY{ evt.button.x, evt.button.y }, gPosOffset)) {
tabs[openTab].wxs.tryFocusOnPoint(XY{ evt.button.x, evt.button.y }, xyAdd(XY{ 0,buttonsHeight }, gPosOffset));
}
Expand Down

0 comments on commit cf7e3ad

Please sign in to comment.