Compare commits

...

2 commits

Author SHA1 Message Date
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
3 changed files with 85 additions and 17 deletions

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,11 @@ 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;
}
}
#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,6 +18,7 @@ width(DEFAULT_WIDTH),
height(DEFAULT_HEIGHT),
dt(1.0f/60.0f),
notificationAlpha(1.0f),
notificationAmt(0.0F),
notificationText(),
tris(),
currentTri(),
@ -38,7 +37,8 @@ selectedTriBlinkTimer(),
inputWidth(800),
inputHeight(600),
history(),
history_idx(0)
history_idx(0),
clickTimeout(0.0F)
{
InitWindow(width, height, "Triangles");
SetTargetFPS(60);
@ -53,7 +53,9 @@ history_idx(0)
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
@ -121,6 +123,10 @@ 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 (history_idx > 0) {
switch(history[history_idx-1].type) {
@ -236,7 +242,7 @@ void Tri::State::handle_events() {
"to what was\n"
"clicked on");
} else {
notificationAlpha = 0.0f;
clear_notification_alpha();
}
break;
case KEY_I:
@ -265,7 +271,7 @@ 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: {
@ -345,14 +351,16 @@ void Tri::State::handle_events() {
if(my < 0) { my = 0; }
else if(my >= (int)height) { my = height - 1; }
for (auto &tri : tris) {
if(is_within_shape(tri, {mx, my})) {
tri.outlineColor = invert_color(tri.fillColor);
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 = tri.fillColor;
selectedTriColor = tris[i].fillColor;
selectedTri = i;
clickTimeout = CLICK_TIMEOUT_TIME;
break;
}
}
@ -367,9 +375,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);
}
}
@ -391,6 +401,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() {
@ -418,7 +435,7 @@ 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);
}
@ -476,6 +493,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();
}
@ -485,7 +512,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) {
@ -497,7 +524,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() {
@ -639,6 +666,20 @@ Color& Tri::State::get_selected_tri_color() {
void Tri::State::close_selected_tri_mode() {
tris.at(selectedTri).fillColor = selectedTriColor;
flags.set(F_DRAW_CACHE_DIRTY);
// Set tri's new color in history.
for (auto &action : history) {
if (action.type == Action::AT_TRI
&& tris.at(selectedTri).vertices.at(0).x == action.data.tri[0]
&& tris.at(selectedTri).vertices.at(0).y == action.data.tri[1]
&& tris.at(selectedTri).vertices.at(1).x == action.data.tri[2]
&& tris.at(selectedTri).vertices.at(1).y == action.data.tri[3]
&& tris.at(selectedTri).vertices.at(2).x == action.data.tri[4]
&& tris.at(selectedTri).vertices.at(2).y == action.data.tri[5]) {
action.color = selectedTriColor;
break;
}
}
reset_modes();
}
@ -686,3 +727,7 @@ void Tri::State::restore_points_on_tri_del(Action::IndexT end) {
assert(!"Unreachable code");
return;
}
float Tri::State::get_click_timeout() const {
return clickTimeout;
}

View file

@ -81,6 +81,7 @@ namespace Tri {
unsigned int height;
float dt;
float notificationAlpha;
float notificationAmt;
std::array<char, 256> notificationText;
std::vector<Triangle> tris;
@ -109,6 +110,8 @@ namespace Tri {
std::vector<Action> history;
Action::IndexT history_idx;
float clickTimeout;
public:
void handle_events();
void update();
@ -124,6 +127,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:
@ -165,6 +171,9 @@ namespace Tri {
private:
void restore_points_on_tri_del(Action::IndexT end);
public:
float get_click_timeout() const;
};
}