Compare commits

...

5 commits

Author SHA1 Message Date
25dafb26b5 Impl. storing color changes in history
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 28s
2024-02-29 17:10:57 +09:00
b5efecd43d Quality-of-Life fixes
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 23s
Change RayGui color settings.

Change how notification panel fades.

Prevent accidentally closing edit-tri-color window immediately after
selecting a tri.
2024-02-29 16:27:24 +09:00
7480c11d95 Fix edit-tri mode 2024-02-29 15:55:34 +09:00
b8d4521223 Refactor State::restore_points_on_tri_del(...)
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 31s
2024-01-25 11:21:28 +09:00
852a099930 Impl "Action" object for undo/redo history
All checks were successful
Run UnitTests / build-and-run-tests (push) Successful in 50s
TODO: Store undo/redo history for color editing, allow deleting
individual triangles and undo/redo of them.
2024-01-24 17:24:56 +09:00
5 changed files with 339 additions and 78 deletions

View file

@ -26,6 +26,7 @@ set(Triangles_LIB_SOURCES
src/triangle.cpp
src/circle.cpp
src/raygui.cpp
src/helpers.cpp
)
add_library(TrianglesLib STATIC ${Triangles_LIB_SOURCES})

12
src/helpers.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "helpers.hpp"
bool operator==(const Color& a, const Color& b) {
return a.r == b.r
&& a.g == b.g
&& a.b == b.b
&& a.a == b.a;
}
bool operator!=(const Color& a, const Color& b) {
return !(a == b);
}

View file

@ -14,6 +14,8 @@
#define SHOW_HELP_WIDTH (state->get_width() / 2.0f)
#define SHOW_HELP_HEIGHT (state->get_height() / 2.0f)
#define NOTIFICATION_FADE_RATE 0.2F
#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600
#define CHANGE_SIZE_MIN_X 800
@ -21,6 +23,12 @@
#define CHANGE_SIZE_MIN_Y 600
#define CHANGE_SIZE_MAX_Y 1080
#define TRI_GUI_BG_COLOR 0x505050FF
#define TRI_GUI_BASE_COLOR 0x404040FF
#define TRI_GUI_TEXT_COLOR 0xFFFFFFFF
#define CLICK_TIMEOUT_TIME 0.4F
#ifndef NDEBUG
# include <cstdio>
#endif
@ -268,7 +276,8 @@ namespace Tri {
&alpha
);
color.a = alpha * 255.0F;
if(GuiButton({504.0f, 308.0f, 234.0f, 16.0f}, "Close")) {
if(GuiButton({504.0f, 308.0f, 234.0f, 16.0f}, "Close")
&& state->get_click_timeout() == 0.0F) {
state->close_selected_tri_mode();
}
} else {
@ -291,6 +300,15 @@ namespace Tri {
tri[2] = t_a;
}
}
inline float sq_lerp(float start, float end, float amt) {
amt = amt * amt;
return start * (1.0F - amt) + end * amt;
}
}
// Color operator helpers
bool operator==(const Color& a, const Color& b);
bool operator!=(const Color& a, const Color& b);
#endif

View file

@ -10,8 +10,6 @@
#include "helpers.hpp"
#define STARTING_HELP_FADE_RATE 0.2f
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
Tri::State::State(int argc, char **argv) :
@ -20,13 +18,12 @@ width(DEFAULT_WIDTH),
height(DEFAULT_HEIGHT),
dt(1.0f/60.0f),
notificationAlpha(1.0f),
notificationAmt(0.0F),
notificationText(),
tris(),
trisIndex(0),
currentTri(),
currentTri_state(CurrentState::NONE),
currentTri_maxState(CurrentState::NONE),
pointCircle(),
pointCircle({7.0F, 7.0F}, 7.0F, WHITE),
colorPickerColor{255, 255, 255, 255},
bgColorPickerColor{0, 0, 0, 255},
bgColor(BLACK),
@ -36,9 +33,13 @@ drawCache(),
pi(std::acos(-1.0f)),
selectedTri(),
selectedTriColor{255, 255, 255, 255},
prevTriColor{255, 255, 255, 255},
selectedTriBlinkTimer(),
inputWidth(800),
inputHeight(600)
inputHeight(600),
history(),
history_idx(0),
clickTimeout(0.0F)
{
InitWindow(width, height, "Triangles");
SetTargetFPS(60);
@ -47,21 +48,71 @@ inputHeight(600)
set_notification_text("Press \"H\" for help");
pointCircle.setRadius(7.0f);
pointCircle.translate({7.0f, 7.0f});
pointCircle.fillColor = WHITE;
pointCircle.outlineColor = BLACK;
saveFilenameBuffer.fill(0);
drawCache = LoadRenderTexture(width, height);
flags.set(F_DRAW_CACHE_INITIALIZED);
flags.set(F_DRAW_CACHE_DIRTY);
GuiSetStyle(DEFAULT, BACKGROUND_COLOR, 0x303030);
GuiSetStyle(DEFAULT, BACKGROUND_COLOR, TRI_GUI_BG_COLOR);
GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, TRI_GUI_BASE_COLOR);
GuiSetStyle(DEFAULT, TEXT_COLOR_NORMAL, TRI_GUI_TEXT_COLOR);
}
#pragma GCC diagnostic pop
Tri::State::Action::Action() :
type(Tri::State::Action::AT_NONE),
idx(0),
color(BLACK),
data{{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}}
{}
Tri::State::Action::Action(const Type& type, IndexT idx, const Color& color, float *data) :
type(type),
idx(idx),
color(color),
data()
{
init(data);
}
Tri::State::Action::Action(Type&& type, IndexT idx, Color&& color, float *data) :
type(type),
idx(idx),
color(color),
data()
{
init(data);
}
Tri::State::Action &Tri::State::Action::setNewColor(Color color) {
this->data.newColor = color;
return *this;
}
void Tri::State::Action::init(float *data) {
switch(type) {
case AT_TRI:
case AT_TRI_DEL:
for(unsigned int i = 0; i < 6; ++i) {
this->data.tri[i] = data[i];
}
break;
case AT_POINT:
this->data.point[0] = data[0];
this->data.point[1] = data[1];
break;
case AT_COLOR:
break;
default:
type = AT_NONE;
idx = 0;
color = BLACK;
this->data = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}};
break;
}
}
Tri::State::~State() {
UnloadRenderTexture(drawCache);
CloseWindow();
@ -80,52 +131,102 @@ void Tri::State::handle_events() {
flags.flip(F_DISPLAY_HELP);
break;
case KEY_U:
if (flags.test(F_TRI_EDIT_MODE)) {
set_notification_text("Cannot undo during editing tri!");
break;
}
flags.set(F_DRAW_CACHE_DIRTY);
if(currentTri_state > 0) {
switch(currentTri_state) {
case FIRST:
if (history_idx > 0) {
switch(history[history_idx-1].type) {
case Action::AT_TRI:
tris.erase(
tris.cbegin() + history[history_idx-1].idx);
restore_points_on_tri_del(history_idx - 1);
break;
case Action::AT_TRI_DEL:
{
tris.emplace(
tris.cbegin() + history[history_idx].idx,
Triangle(
{{
{history[history_idx-1].data.tri[0],
history[history_idx-1].data.tri[1]},
{history[history_idx-1].data.tri[2],
history[history_idx-1].data.tri[3]},
{history[history_idx-1].data.tri[4],
history[history_idx-1].data.tri[5]},
}},
history[history_idx-1].color
)
);
currentTri_state = CurrentState::NONE;
break;
case SECOND:
currentTri_state = CurrentState::FIRST;
}
case Action::AT_POINT:
assert(history[history_idx-1].idx + 1 == currentTri_state
&& "Point in history must match point index");
assert(currentTri_state > 0
&& "There must be a point to undo a point");
currentTri_state = static_cast<CurrentState>(currentTri_state - 1);
break;
case Action::AT_COLOR:
tris.at(history[history_idx-1].idx).fillColor = history[history_idx-1].color;
break;
default:
assert(!"Unreachable code");
break;
}
} else if(trisIndex > 0) {
--trisIndex;
--history_idx;
}
break;
case KEY_R:
flags.set(F_DRAW_CACHE_DIRTY);
if(currentTri_state != CurrentState::NONE
&& currentTri_state < currentTri_maxState) {
switch(currentTri_state) {
case NONE:
currentTri_state = CurrentState::FIRST;
if(history_idx < history.size()) {
switch(history[history_idx].type) {
case Action::AT_TRI:
{
tris.emplace(
tris.cbegin() + history[history_idx].idx,
Triangle(
{{
{history[history_idx].data.tri[0],
history[history_idx].data.tri[1]},
{history[history_idx].data.tri[2],
history[history_idx].data.tri[3]},
{history[history_idx].data.tri[4],
history[history_idx].data.tri[5]},
}},
history[history_idx].color
)
);
currentTri_state = CurrentState::NONE;
break;
case FIRST:
currentTri_state = CurrentState::SECOND;
break;
default:
assert(!"Unreachable code");
break;
}
} else if(tris.size() > trisIndex) {
++trisIndex;
} else if(currentTri_state < currentTri_maxState) {
switch(currentTri_state) {
case NONE:
currentTri_state = CurrentState::FIRST;
break;
case FIRST:
currentTri_state = CurrentState::SECOND;
}
break;
case Action::AT_TRI_DEL:
tris.erase(
tris.cbegin() + history[history_idx].idx);
restore_points_on_tri_del(history_idx);
break;
case Action::AT_POINT:
assert(history[history_idx].idx == currentTri_state
&& "Point in history must match point index");
assert(currentTri_state < CurrentState::SECOND
&& "Current point state must be 0 or 1");
currentTri[currentTri_state].x = history[history_idx].data.point[0];
currentTri[currentTri_state].y = history[history_idx].data.point[1];
currentTri_state = static_cast<CurrentState>(
currentTri_state + 1);
pointCircle.fillColor = history[history_idx].color;
break;
case Action::AT_COLOR:
tris.at(history[history_idx].idx).fillColor = history[history_idx].data.newColor;
break;
default:
assert(!"Unreachable code");
break;
}
++history_idx;
}
break;
case KEY_C:
@ -155,7 +256,7 @@ void Tri::State::handle_events() {
"to what was\n"
"clicked on");
} else {
notificationAlpha = 0.0f;
clear_notification_alpha();
}
break;
case KEY_I:
@ -184,37 +285,56 @@ void Tri::State::handle_events() {
keyPressed = GetKeyPressed();
}
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && clickTimeout == 0.0F) {
if(can_draw()) {
switch(currentTri_state) {
case CurrentState::NONE:
case CurrentState::NONE: {
currentTri[0] = {GetMouseX(), GetMouseY()};
if(trisIndex < tris.size()) {
tris.resize(trisIndex);
}
currentTri_state = CurrentState::FIRST;
currentTri_maxState = CurrentState::FIRST;
break;
case CurrentState::FIRST:
if(history_idx < history.size()) {
history.resize(history_idx);
}
float points[2] = {currentTri[0].x, currentTri[0].y};
history.push_back(Action(Action::AT_POINT,
0,
pointCircle.fillColor,
points));
++history_idx;
break; }
case CurrentState::FIRST: {
currentTri[1] = {GetMouseX(), GetMouseY()};
if(trisIndex < tris.size()) {
tris.resize(trisIndex);
}
currentTri_state = CurrentState::SECOND;
currentTri_maxState = CurrentState::SECOND;
break;
case CurrentState::SECOND:
currentTri[2] = {GetMouseX(), GetMouseY()};
if(trisIndex < tris.size()) {
tris.resize(trisIndex);
if(history_idx < history.size()) {
history.resize(history_idx);
}
++trisIndex;
float points[2] = {currentTri[1].x, currentTri[1].y};
history.push_back(Action(Action::AT_POINT,
1,
pointCircle.fillColor,
points));
++history_idx;
break; }
case CurrentState::SECOND: {
currentTri[2] = {GetMouseX(), GetMouseY()};
make_counter_clockwise(currentTri);
tris.emplace_back(currentTri, pointCircle.fillColor);
currentTri_state = CurrentState::NONE;
currentTri_maxState = CurrentState::NONE;
flags.set(F_DRAW_CACHE_DIRTY);
break;
if(history_idx < history.size()) {
history.resize(history_idx);
}
float points[6] = {
currentTri[0].x, currentTri[0].y,
currentTri[1].x, currentTri[1].y,
currentTri[2].x, currentTri[2].y,
};
history.push_back(Action(Action::AT_TRI,
tris.size()-1,
pointCircle.fillColor,
points));
++history_idx;
break; }
}
} else if(flags.test(F_COPY_COLOR_MODE)) {
check_draw_cache();
@ -245,15 +365,17 @@ void Tri::State::handle_events() {
if(my < 0) { my = 0; }
else if(my >= (int)height) { my = height - 1; }
for(unsigned int i = trisIndex; i-- > 0; ) {
if(is_within_shape(tris.at(i), {mx, my})) {
selectedTri = i;
for (unsigned int i = 0; i < tris.size(); ++i) {
if(is_within_shape(tris[i], {mx, my})) {
tris[i].outlineColor = invert_color(tris[i].fillColor);
flags.reset(F_SELECT_TRI_MODE);
flags.set(F_TRI_EDIT_MODE);
flags.set(F_TRI_EDIT_DRAW_TRI);
selectedTriBlinkTimer = 1.0f;
selectedTriColor = tris[i].fillColor;
prevTriColor = tris[i].fillColor;
selectedTri = i;
clickTimeout = CLICK_TIMEOUT_TIME;
break;
}
}
@ -268,9 +390,11 @@ void Tri::State::update() {
dt = GetFrameTime();
if(notificationAlpha > 0.0f) {
notificationAlpha -= dt * STARTING_HELP_FADE_RATE;
if(notificationAlpha < 0.0f) {
notificationAlpha = 0.0f;
notificationAmt += dt * NOTIFICATION_FADE_RATE;
if (notificationAmt > 1.0F) {
clear_notification_alpha();
} else {
notificationAlpha = Tri::sq_lerp(1.0F, 0.0F, notificationAmt);
}
}
@ -292,6 +416,13 @@ void Tri::State::update() {
flags.flip(F_TRI_EDIT_DRAW_TRI);
}
}
if (clickTimeout > 0.0F) {
clickTimeout -= dt;
if (clickTimeout < 0.0F) {
clickTimeout = 0.0F;
}
}
}
void Tri::State::draw() {
@ -319,15 +450,13 @@ void Tri::State::draw() {
if(flags.test(F_TRI_EDIT_MODE) && flags.test(F_TRI_EDIT_DRAW_TRI)) {
// tris.at(selectedTri).setOutlineThickness(4.0f);
tris[selectedTri].draw();
tris.at(selectedTri).draw();
// tris.at(selectedTri).setOutlineThickness(0.0f);
}
if(can_draw()) {
for(unsigned int i = 0; i < currentTri_state; ++i) {
pointCircle.resetTransform();
pointCircle.translate(currentTri[i]);
pointCircle.draw();
DrawCircle(currentTri[i].x, currentTri[i].y, pointCircle.getRadius(), pointCircle.fillColor);
}
}
@ -348,7 +477,7 @@ void Tri::State::draw_to_target(RenderTexture2D *target) {
ClearBackground(bgColor);
// draw tris
for(unsigned int i = 0; i < trisIndex; ++i) {
for(unsigned int i = 0; i < tris.size(); ++i) {
tris[i].draw();
}
EndTextureMode();
@ -357,7 +486,7 @@ void Tri::State::draw_to_target(RenderTexture2D *target) {
ClearBackground(bgColor);
// draw tris
for(unsigned int i = 0; i < trisIndex; ++i) {
for(unsigned int i = 0; i < tris.size(); ++i) {
tris[i].draw();
}
}
@ -379,6 +508,16 @@ float Tri::State::get_notification_alpha() const {
return notificationAlpha;
}
void Tri::State::reset_notification_alpha() {
notificationAlpha = 1.0F;
notificationAmt = 0.0F;
}
void Tri::State::clear_notification_alpha() {
notificationAlpha = 0.0F;
notificationAmt = 1.0F;
}
const char* Tri::State::get_notification_text() const {
return notificationText.data();
}
@ -388,7 +527,7 @@ void Tri::State::set_notification_text(const char *text) {
std::strncpy(notificationText.data(),
text,
notificationText.max_size() - 1);
notificationAlpha = 1.0f;
reset_notification_alpha();
}
void Tri::State::append_notification_text(const char *text) {
@ -400,7 +539,7 @@ void Tri::State::append_notification_text(const char *text) {
notificationText.data() + length,
text,
notificationText.max_size() - length - 1);
notificationAlpha = 1.0f;
reset_notification_alpha();
}
Color& Tri::State::get_color() {
@ -540,8 +679,19 @@ Color& Tri::State::get_selected_tri_color() {
}
void Tri::State::close_selected_tri_mode() {
// Set tri's new color in history.
if (prevTriColor != selectedTriColor) {
if (history_idx < history.size()) {
history.resize(history_idx);
}
history.emplace_back(Action::AT_COLOR, selectedTri, prevTriColor, nullptr);
history.back().setNewColor(selectedTriColor);
++history_idx;
}
tris.at(selectedTri).fillColor = selectedTriColor;
flags.set(F_DRAW_CACHE_DIRTY);
reset_modes();
}
@ -563,3 +713,33 @@ int* Tri::State::get_input_width() {
int* Tri::State::get_input_height() {
return &inputHeight;
}
void Tri::State::restore_points_on_tri_del(Action::IndexT end) {
assert(end < history.size()
&& "Index on history must be in range");
currentTri[2].x = history[end].data.tri[4];
currentTri[2].y = history[end].data.tri[5];
pointCircle.fillColor = history[end].color;
unsigned int currentTriIdx = 1;
while(end-- > 0) {
assert(history[end].type == Action::AT_POINT
&& "Latest history must be AT_POINT type");
assert(history[end].idx == currentTriIdx
&& "Last point must be second point");
currentTri[currentTriIdx].x = history[end].data.point[0];
currentTri[currentTriIdx].y = history[end].data.point[1];
if(currentTriIdx > 0) {
--currentTriIdx;
} else {
currentTri_state = CurrentState::SECOND;
return;
}
}
assert(!"Unreachable code");
return;
}
float Tri::State::get_click_timeout() const {
return clickTimeout;
}

View file

@ -41,6 +41,43 @@ namespace Tri {
};
private:
struct Action {
public:
typedef std::vector<Action>::size_type IndexT;
enum Type {
AT_TRI,
AT_TRI_DEL,
AT_POINT,
AT_COLOR,
AT_NONE,
};
Action();
Action(const Type& type,
IndexT idx,
const Color& color,
float *data);
Action(Type&& type,
IndexT idx,
Color&& color,
float *data);
Type type;
IndexT idx;
Color color;
union Data {
float tri[6];
float point[2];
Color newColor;
} data;
Action &setNewColor(Color color);
private:
void init(float *data);
};
// use enum FlagName
typedef std::bitset<64> BitsetType;
BitsetType flags;
@ -48,13 +85,12 @@ namespace Tri {
unsigned int height;
float dt;
float notificationAlpha;
float notificationAmt;
std::array<char, 256> notificationText;
std::vector<Triangle> tris;
unsigned int trisIndex;
std::array<glm::vec2, 3> currentTri;
CurrentState currentTri_state;
CurrentState currentTri_maxState;
Circle pointCircle;
Color colorPickerColor;
@ -70,11 +106,17 @@ namespace Tri {
unsigned int selectedTri;
Color selectedTriColor;
Color prevTriColor;
float selectedTriBlinkTimer;
int inputWidth;
int inputHeight;
std::vector<Action> history;
Action::IndexT history_idx;
float clickTimeout;
public:
void handle_events();
void update();
@ -90,6 +132,9 @@ namespace Tri {
const BitsetType get_flags() const;
float get_notification_alpha() const;
void reset_notification_alpha();
void clear_notification_alpha();
const char* get_notification_text() const;
private:
@ -129,6 +174,11 @@ namespace Tri {
int* get_input_width();
int* get_input_height();
private:
void restore_points_on_tri_del(Action::IndexT end);
public:
float get_click_timeout() const;
};
}