Compare commits

..

4 commits

Author SHA1 Message Date
20859dbcd8 Enable picking color alpha for triangles 2024-01-23 15:04:09 +09:00
c6dd68ae14 Refactor usage of colors and color picking
Member variables of state changed from an array of floats to a Color
struct.
2024-01-23 14:53:47 +09:00
2e3a2385f2 Update submodule glm 2024-01-23 14:35:57 +09:00
18bb4e5f59 Update raygui submodule to 4.0
TODO: Refactor color picking in src/helpers.hpp
2024-01-23 14:31:39 +09:00
6 changed files with 57 additions and 86 deletions

View file

@ -30,7 +30,7 @@ namespace Tri {
// so this should be called during update, not draw // so this should be called during update, not draw
inline void draw_help(Tri::State *state) { inline void draw_help(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_HELP)) { if(state->get_flags().test(Tri::State::F_DISPLAY_HELP)) {
GuiFade(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({10.0f, if(!GuiWindowBox({10.0f,
10.0f, 10.0f,
800.0f - 20.0f, 800.0f - 20.0f,
@ -81,7 +81,7 @@ namespace Tri {
inline void draw_notification(Tri::State *state) { inline void draw_notification(Tri::State *state) {
float alpha = state->get_notification_alpha(); float alpha = state->get_notification_alpha();
if(alpha > 0.0f) { if(alpha > 0.0f) {
GuiFade(alpha); GuiSetAlpha(alpha);
GuiPanel({(800 - SHOW_HELP_WIDTH) / 2.0f, GuiPanel({(800 - SHOW_HELP_WIDTH) / 2.0f,
(600 - SHOW_HELP_HEIGHT) / 2.0f, (600 - SHOW_HELP_HEIGHT) / 2.0f,
SHOW_HELP_WIDTH, SHOW_HELP_WIDTH,
@ -96,23 +96,21 @@ namespace Tri {
inline void draw_color_picker(Tri::State *state) { inline void draw_color_picker(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_COLOR_P)) { if(state->get_flags().test(Tri::State::F_DISPLAY_COLOR_P)) {
GuiFade(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({4.0f, 4.0f, 242.0f, 292.0f}, "Tri Color Picker")) { if(!GuiWindowBox({4.0f, 4.0f, 242.0f, 328.0f}, "Tri Color Picker")) {
auto &colorArray = state->get_color(); auto &color = state->get_color();
Color color = GuiColorPicker( GuiColorPicker(
{8.0f, 32.0f, 206.0f, 240.0f}, {8.0f, 32.0f, 206.0f, 240.0f},
nullptr, nullptr,
{(unsigned char)(colorArray[0] * 255.0f), &color);
(unsigned char)(colorArray[1] * 255.0f), float alpha = ((float)color.a) / 255.0F;
(unsigned char)(colorArray[2] * 255.0f), GuiColorBarAlpha(
(unsigned char)(colorArray[3] * 255.0f)}); {8.0F, 280.0F, 206.0F, 20.0F},
colorArray = { nullptr,
color.r / 255.0f, &alpha
color.g / 255.0f, );
color.b / 255.0f, color.a = alpha * 255.0F;
color.a / 255.0f if(GuiButton({8.0f, 308.0f, 234.0f, 16.0f}, "Close")) {
};
if(GuiButton({8.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_color_picker(); state->close_color_picker();
} }
} else { } else {
@ -123,22 +121,14 @@ namespace Tri {
inline void draw_bg_color_picker(Tri::State *state) { inline void draw_bg_color_picker(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_BG_COLOR_P)) { if(state->get_flags().test(Tri::State::F_DISPLAY_BG_COLOR_P)) {
GuiFade(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({250.0f, 4.0f, 242.0f, 292.0f}, "BG Color Picker")) { if(!GuiWindowBox({250.0f, 4.0f, 242.0f, 292.0f}, "BG Color Picker")) {
auto &colorArray = state->get_bg_color(); auto &colorArray = state->get_bg_color();
Color color = GuiColorPicker( GuiColorPicker(
{254.0f, 32.0f, 206.0f, 240.0f}, {254.0f, 32.0f, 206.0f, 240.0f},
nullptr, nullptr,
{(unsigned char)(colorArray[0] * 255.0f), &colorArray);
(unsigned char)(colorArray[1] * 255.0f), if(GuiButton({254.0f, 274.0f, 234.0f, 16.0f}, "Close")) {
(unsigned char)(colorArray[2] * 255.0f),
255});
colorArray = {
color.r / 255.0f,
color.g / 255.0f,
color.b / 255.0f
};
if(GuiButton({254.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_bg_color_picker(); state->close_bg_color_picker();
} }
} else { } else {
@ -150,7 +140,7 @@ namespace Tri {
inline void draw_save(Tri::State *state) { inline void draw_save(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_SAVE)) { if(state->get_flags().test(Tri::State::F_DISPLAY_SAVE)) {
auto *filenameBuffer = state->get_save_filename_buffer(); auto *filenameBuffer = state->get_save_filename_buffer();
GuiFade(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({4.0f, 300.0f, 292.0f, 292.0f}, "Save")) { if(!GuiWindowBox({4.0f, 300.0f, 292.0f, 292.0f}, "Save")) {
GuiTextBox( GuiTextBox(
{8.0f, 328.0f, 284.0f, 20.0f}, {8.0f, 328.0f, 284.0f, 20.0f},
@ -174,7 +164,7 @@ namespace Tri {
inline void draw_change_size(Tri::State *state) { inline void draw_change_size(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_DISPLAY_CHANGE_SIZE)) { if(state->get_flags().test(Tri::State::F_DISPLAY_CHANGE_SIZE)) {
GuiFade(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({300.0f, 300.0f, 292.0f, 292.0f}, "Change Size")) { if(!GuiWindowBox({300.0f, 300.0f, 292.0f, 292.0f}, "Change Size")) {
GuiValueBox( GuiValueBox(
{384.0f, 328.0f, 80.0f, 16.0f}, {384.0f, 328.0f, 80.0f, 16.0f},
@ -264,23 +254,21 @@ namespace Tri {
inline void draw_edit_tri(Tri::State *state) { inline void draw_edit_tri(Tri::State *state) {
if(state->get_flags().test(Tri::State::F_TRI_EDIT_MODE)) { if(state->get_flags().test(Tri::State::F_TRI_EDIT_MODE)) {
GuiFade(1.0f); GuiSetAlpha(1.0f);
if(!GuiWindowBox({500.0f, 4.0f, 242.0f, 292.0f}, "Edit Tri Color Picker")) { if(!GuiWindowBox({500.0f, 4.0f, 242.0f, 328.0f}, "Edit Tri Color Picker")) {
auto &colorArray = state->get_selected_tri_color(); auto &color = state->get_selected_tri_color();
Color color = GuiColorPicker( GuiColorPicker(
{504.0f, 32.0f, 206.0f, 240.0f}, {504.0f, 32.0f, 206.0f, 240.0f},
nullptr, nullptr,
{(unsigned char)(colorArray[0] * 255.0f), &color);
(unsigned char)(colorArray[1] * 255.0f), float alpha = ((float)color.a) / 255.0F;
(unsigned char)(colorArray[2] * 255.0f), GuiColorBarAlpha(
(unsigned char)(colorArray[3] * 255.0f)}); {504.0F, 280.0F, 206.0F, 20.0F},
colorArray = { nullptr,
color.r / 255.0f, &alpha
color.g / 255.0f, );
color.b / 255.0f, color.a = alpha * 255.0F;
color.a / 255.0f if(GuiButton({504.0f, 308.0f, 234.0f, 16.0f}, "Close")) {
};
if(GuiButton({504.0f, 272.0f, 234.0f, 16.0f}, "Close")) {
state->close_selected_tri_mode(); state->close_selected_tri_mode();
} }
} else { } else {

View file

@ -3,6 +3,7 @@
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Weffc++"
#include <glm/ext/matrix_transform.hpp> #include <glm/ext/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/matrix_transform_2d.hpp> #include <glm/gtx/matrix_transform_2d.hpp>
#pragma GCC diagnostic pop #pragma GCC diagnostic pop

View file

@ -27,15 +27,15 @@ currentTri(),
currentTri_state(CurrentState::NONE), currentTri_state(CurrentState::NONE),
currentTri_maxState(CurrentState::NONE), currentTri_maxState(CurrentState::NONE),
pointCircle(), pointCircle(),
colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f}, colorPickerColor{255, 255, 255, 255},
bgColorPickerColor{0.0f, 0.0f, 0.0f}, bgColorPickerColor{0, 0, 0, 255},
bgColor(BLACK), bgColor(BLACK),
saveFilenameBuffer(), saveFilenameBuffer(),
failedMessage(), failedMessage(),
drawCache(), drawCache(),
pi(std::acos(-1.0f)), pi(std::acos(-1.0f)),
selectedTri(), selectedTri(),
selectedTriColor(), selectedTriColor{255, 255, 255, 255},
selectedTriBlinkTimer(), selectedTriBlinkTimer(),
inputWidth(800), inputWidth(800),
inputHeight(600) inputHeight(600)
@ -229,10 +229,8 @@ void Tri::State::handle_events() {
my = drawImage.height - my; my = drawImage.height - my;
colorPickerColor[0] = (float)(colors[mx + my * drawImage.width].r) / 255.0F; colorPickerColor = colors[mx + my * drawImage.width];
colorPickerColor[1] = (float)(colors[mx + my * drawImage.width].g) / 255.0F;
colorPickerColor[2] = (float)(colors[mx + my * drawImage.width].b) / 255.0F;
colorPickerColor[3] = 1.0f;
pointCircle.fillColor = colors[mx + my * drawImage.width]; pointCircle.fillColor = colors[mx + my * drawImage.width];
flags.reset(F_COPY_COLOR_MODE); flags.reset(F_COPY_COLOR_MODE);
set_notification_text("Color set"); set_notification_text("Color set");
@ -255,10 +253,7 @@ void Tri::State::handle_events() {
flags.set(F_TRI_EDIT_MODE); flags.set(F_TRI_EDIT_MODE);
flags.set(F_TRI_EDIT_DRAW_TRI); flags.set(F_TRI_EDIT_DRAW_TRI);
selectedTriBlinkTimer = 1.0f; selectedTriBlinkTimer = 1.0f;
selectedTriColor[0] = tris[i].fillColor.r / 255.0f; selectedTriColor = tris[i].fillColor;
selectedTriColor[1] = tris[i].fillColor.g / 255.0f;
selectedTriColor[2] = tris[i].fillColor.b / 255.0f;
selectedTriColor[3] = tris[i].fillColor.a / 255.0f;
break; break;
} }
} }
@ -281,18 +276,13 @@ void Tri::State::update() {
if(flags.test(F_COLOR_P_COLOR_DIRTY)) { if(flags.test(F_COLOR_P_COLOR_DIRTY)) {
flags.reset(F_COLOR_P_COLOR_DIRTY); flags.reset(F_COLOR_P_COLOR_DIRTY);
pointCircle.fillColor = Color{ pointCircle.fillColor = colorPickerColor;
(unsigned char)(255 * colorPickerColor[0]),
(unsigned char)(255 * colorPickerColor[1]),
(unsigned char)(255 * colorPickerColor[2]),
(unsigned char)(255 * colorPickerColor[3])};
} }
if(flags.test(F_BG_COLOR_P_COLOR_DIRTY)) { if(flags.test(F_BG_COLOR_P_COLOR_DIRTY)) {
flags.reset(F_BG_COLOR_P_COLOR_DIRTY); flags.reset(F_BG_COLOR_P_COLOR_DIRTY);
bgColor.r = (unsigned char)(255 * bgColorPickerColor[0]); bgColor = bgColorPickerColor;
bgColor.g = (unsigned char)(255 * bgColorPickerColor[1]); bgColor.a = 255;
bgColor.b = (unsigned char)(255 * bgColorPickerColor[2]);
} }
if(flags.test(F_TRI_EDIT_MODE)) { if(flags.test(F_TRI_EDIT_MODE)) {
@ -413,12 +403,12 @@ void Tri::State::append_notification_text(const char *text) {
notificationAlpha = 1.0f; notificationAlpha = 1.0f;
} }
std::array<float, 4>& Tri::State::get_color() { Color& Tri::State::get_color() {
flags.set(F_COLOR_P_COLOR_DIRTY); flags.set(F_COLOR_P_COLOR_DIRTY);
return colorPickerColor; return colorPickerColor;
} }
std::array<float, 3>& Tri::State::get_bg_color() { Color& Tri::State::get_bg_color() {
flags.set(F_BG_COLOR_P_COLOR_DIRTY); flags.set(F_BG_COLOR_P_COLOR_DIRTY);
return bgColorPickerColor; return bgColorPickerColor;
} }
@ -543,21 +533,13 @@ float Tri::State::get_pi() const {
return pi; return pi;
} }
std::array<float, 4>& Tri::State::get_selected_tri_color() { Color& Tri::State::get_selected_tri_color() {
tris.at(selectedTri).fillColor = Color{ tris.at(selectedTri).fillColor = selectedTriColor;
(unsigned char)(255.0f * selectedTriColor[0]),
(unsigned char)(255.0f * selectedTriColor[1]),
(unsigned char)(255.0f * selectedTriColor[2]),
(unsigned char)(255.0f * selectedTriColor[3])};
return selectedTriColor; return selectedTriColor;
} }
void Tri::State::close_selected_tri_mode() { void Tri::State::close_selected_tri_mode() {
tris.at(selectedTri).fillColor = Color{ tris.at(selectedTri).fillColor = selectedTriColor;
(unsigned char)(255.0f * selectedTriColor[0]),
(unsigned char)(255.0f * selectedTriColor[1]),
(unsigned char)(255.0f * selectedTriColor[2]),
(unsigned char)(255.0f * selectedTriColor[3])};
flags.set(F_DRAW_CACHE_DIRTY); flags.set(F_DRAW_CACHE_DIRTY);
reset_modes(); reset_modes();
} }

View file

@ -57,8 +57,8 @@ namespace Tri {
CurrentState currentTri_maxState; CurrentState currentTri_maxState;
Circle pointCircle; Circle pointCircle;
std::array<float, 4> colorPickerColor; Color colorPickerColor;
std::array<float, 3> bgColorPickerColor; Color bgColorPickerColor;
Color bgColor; Color bgColor;
std::array<char, 256> saveFilenameBuffer; std::array<char, 256> saveFilenameBuffer;
@ -69,7 +69,7 @@ namespace Tri {
const float pi; const float pi;
unsigned int selectedTri; unsigned int selectedTri;
std::array<float, 4> selectedTriColor; Color selectedTriColor;
float selectedTriBlinkTimer; float selectedTriBlinkTimer;
int inputWidth; int inputWidth;
@ -97,8 +97,8 @@ namespace Tri {
void append_notification_text(const char *text); void append_notification_text(const char *text);
public: public:
std::array<float, 4>& get_color(); Color& get_color();
std::array<float, 3>& get_bg_color(); Color& get_bg_color();
std::array<char, 256>* get_save_filename_buffer(); std::array<char, 256>* get_save_filename_buffer();
bool do_save(); bool do_save();
@ -119,7 +119,7 @@ namespace Tri {
float get_pi() const; float get_pi() const;
std::array<float, 4>& get_selected_tri_color(); Color& get_selected_tri_color();
void close_selected_tri_mode(); void close_selected_tri_mode();
private: private:

2
third_party/glm vendored

@ -1 +1 @@
Subproject commit efec5db081e3aad807d0731e172ac597f6a39447 Subproject commit 673a963a0f1eb82f5fcef00b7b873371555e5814

2
third_party/raygui vendored

@ -1 +1 @@
Subproject commit 4e2a878e715c4aafa6ad7bd58d851221503c6e60 Subproject commit 25c8c65a6e5f0f4d4b564a0343861898c6f2778b