Compare commits
2 commits
b8d4521223
...
b5efecd43d
Author | SHA1 | Date | |
---|---|---|---|
b5efecd43d | |||
7480c11d95 |
3 changed files with 85 additions and 17 deletions
|
@ -14,6 +14,8 @@
|
||||||
#define SHOW_HELP_WIDTH (state->get_width() / 2.0f)
|
#define SHOW_HELP_WIDTH (state->get_width() / 2.0f)
|
||||||
#define SHOW_HELP_HEIGHT (state->get_height() / 2.0f)
|
#define SHOW_HELP_HEIGHT (state->get_height() / 2.0f)
|
||||||
|
|
||||||
|
#define NOTIFICATION_FADE_RATE 0.2F
|
||||||
|
|
||||||
#define DEFAULT_WIDTH 800
|
#define DEFAULT_WIDTH 800
|
||||||
#define DEFAULT_HEIGHT 600
|
#define DEFAULT_HEIGHT 600
|
||||||
#define CHANGE_SIZE_MIN_X 800
|
#define CHANGE_SIZE_MIN_X 800
|
||||||
|
@ -21,6 +23,12 @@
|
||||||
#define CHANGE_SIZE_MIN_Y 600
|
#define CHANGE_SIZE_MIN_Y 600
|
||||||
#define CHANGE_SIZE_MAX_Y 1080
|
#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
|
#ifndef NDEBUG
|
||||||
# include <cstdio>
|
# include <cstdio>
|
||||||
#endif
|
#endif
|
||||||
|
@ -268,7 +276,8 @@ namespace Tri {
|
||||||
&alpha
|
&alpha
|
||||||
);
|
);
|
||||||
color.a = alpha * 255.0F;
|
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();
|
state->close_selected_tri_mode();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -291,6 +300,11 @@ namespace Tri {
|
||||||
tri[2] = t_a;
|
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
|
#endif
|
||||||
|
|
|
@ -10,8 +10,6 @@
|
||||||
|
|
||||||
#include "helpers.hpp"
|
#include "helpers.hpp"
|
||||||
|
|
||||||
#define STARTING_HELP_FADE_RATE 0.2f
|
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||||
Tri::State::State(int argc, char **argv) :
|
Tri::State::State(int argc, char **argv) :
|
||||||
|
@ -20,6 +18,7 @@ width(DEFAULT_WIDTH),
|
||||||
height(DEFAULT_HEIGHT),
|
height(DEFAULT_HEIGHT),
|
||||||
dt(1.0f/60.0f),
|
dt(1.0f/60.0f),
|
||||||
notificationAlpha(1.0f),
|
notificationAlpha(1.0f),
|
||||||
|
notificationAmt(0.0F),
|
||||||
notificationText(),
|
notificationText(),
|
||||||
tris(),
|
tris(),
|
||||||
currentTri(),
|
currentTri(),
|
||||||
|
@ -38,7 +37,8 @@ selectedTriBlinkTimer(),
|
||||||
inputWidth(800),
|
inputWidth(800),
|
||||||
inputHeight(600),
|
inputHeight(600),
|
||||||
history(),
|
history(),
|
||||||
history_idx(0)
|
history_idx(0),
|
||||||
|
clickTimeout(0.0F)
|
||||||
{
|
{
|
||||||
InitWindow(width, height, "Triangles");
|
InitWindow(width, height, "Triangles");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
@ -53,7 +53,9 @@ history_idx(0)
|
||||||
flags.set(F_DRAW_CACHE_INITIALIZED);
|
flags.set(F_DRAW_CACHE_INITIALIZED);
|
||||||
flags.set(F_DRAW_CACHE_DIRTY);
|
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
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
|
@ -121,6 +123,10 @@ void Tri::State::handle_events() {
|
||||||
flags.flip(F_DISPLAY_HELP);
|
flags.flip(F_DISPLAY_HELP);
|
||||||
break;
|
break;
|
||||||
case KEY_U:
|
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);
|
flags.set(F_DRAW_CACHE_DIRTY);
|
||||||
if (history_idx > 0) {
|
if (history_idx > 0) {
|
||||||
switch(history[history_idx-1].type) {
|
switch(history[history_idx-1].type) {
|
||||||
|
@ -236,7 +242,7 @@ void Tri::State::handle_events() {
|
||||||
"to what was\n"
|
"to what was\n"
|
||||||
"clicked on");
|
"clicked on");
|
||||||
} else {
|
} else {
|
||||||
notificationAlpha = 0.0f;
|
clear_notification_alpha();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case KEY_I:
|
case KEY_I:
|
||||||
|
@ -265,7 +271,7 @@ void Tri::State::handle_events() {
|
||||||
keyPressed = GetKeyPressed();
|
keyPressed = GetKeyPressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && clickTimeout == 0.0F) {
|
||||||
if(can_draw()) {
|
if(can_draw()) {
|
||||||
switch(currentTri_state) {
|
switch(currentTri_state) {
|
||||||
case CurrentState::NONE: {
|
case CurrentState::NONE: {
|
||||||
|
@ -345,14 +351,16 @@ void Tri::State::handle_events() {
|
||||||
if(my < 0) { my = 0; }
|
if(my < 0) { my = 0; }
|
||||||
else if(my >= (int)height) { my = height - 1; }
|
else if(my >= (int)height) { my = height - 1; }
|
||||||
|
|
||||||
for (auto &tri : tris) {
|
for (unsigned int i = 0; i < tris.size(); ++i) {
|
||||||
if(is_within_shape(tri, {mx, my})) {
|
if(is_within_shape(tris[i], {mx, my})) {
|
||||||
tri.outlineColor = invert_color(tri.fillColor);
|
tris[i].outlineColor = invert_color(tris[i].fillColor);
|
||||||
flags.reset(F_SELECT_TRI_MODE);
|
flags.reset(F_SELECT_TRI_MODE);
|
||||||
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 = tri.fillColor;
|
selectedTriColor = tris[i].fillColor;
|
||||||
|
selectedTri = i;
|
||||||
|
clickTimeout = CLICK_TIMEOUT_TIME;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -367,9 +375,11 @@ void Tri::State::update() {
|
||||||
dt = GetFrameTime();
|
dt = GetFrameTime();
|
||||||
|
|
||||||
if(notificationAlpha > 0.0f) {
|
if(notificationAlpha > 0.0f) {
|
||||||
notificationAlpha -= dt * STARTING_HELP_FADE_RATE;
|
notificationAmt += dt * NOTIFICATION_FADE_RATE;
|
||||||
if(notificationAlpha < 0.0f) {
|
if (notificationAmt > 1.0F) {
|
||||||
notificationAlpha = 0.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);
|
flags.flip(F_TRI_EDIT_DRAW_TRI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (clickTimeout > 0.0F) {
|
||||||
|
clickTimeout -= dt;
|
||||||
|
if (clickTimeout < 0.0F) {
|
||||||
|
clickTimeout = 0.0F;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tri::State::draw() {
|
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)) {
|
if(flags.test(F_TRI_EDIT_MODE) && flags.test(F_TRI_EDIT_DRAW_TRI)) {
|
||||||
// tris.at(selectedTri).setOutlineThickness(4.0f);
|
// tris.at(selectedTri).setOutlineThickness(4.0f);
|
||||||
tris[selectedTri].draw();
|
tris.at(selectedTri).draw();
|
||||||
// tris.at(selectedTri).setOutlineThickness(0.0f);
|
// tris.at(selectedTri).setOutlineThickness(0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,6 +493,16 @@ float Tri::State::get_notification_alpha() const {
|
||||||
return notificationAlpha;
|
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 {
|
const char* Tri::State::get_notification_text() const {
|
||||||
return notificationText.data();
|
return notificationText.data();
|
||||||
}
|
}
|
||||||
|
@ -485,7 +512,7 @@ void Tri::State::set_notification_text(const char *text) {
|
||||||
std::strncpy(notificationText.data(),
|
std::strncpy(notificationText.data(),
|
||||||
text,
|
text,
|
||||||
notificationText.max_size() - 1);
|
notificationText.max_size() - 1);
|
||||||
notificationAlpha = 1.0f;
|
reset_notification_alpha();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tri::State::append_notification_text(const char *text) {
|
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,
|
notificationText.data() + length,
|
||||||
text,
|
text,
|
||||||
notificationText.max_size() - length - 1);
|
notificationText.max_size() - length - 1);
|
||||||
notificationAlpha = 1.0f;
|
reset_notification_alpha();
|
||||||
}
|
}
|
||||||
|
|
||||||
Color& Tri::State::get_color() {
|
Color& Tri::State::get_color() {
|
||||||
|
@ -639,6 +666,20 @@ Color& Tri::State::get_selected_tri_color() {
|
||||||
void Tri::State::close_selected_tri_mode() {
|
void Tri::State::close_selected_tri_mode() {
|
||||||
tris.at(selectedTri).fillColor = selectedTriColor;
|
tris.at(selectedTri).fillColor = selectedTriColor;
|
||||||
flags.set(F_DRAW_CACHE_DIRTY);
|
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();
|
reset_modes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -686,3 +727,7 @@ void Tri::State::restore_points_on_tri_del(Action::IndexT end) {
|
||||||
assert(!"Unreachable code");
|
assert(!"Unreachable code");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float Tri::State::get_click_timeout() const {
|
||||||
|
return clickTimeout;
|
||||||
|
}
|
||||||
|
|
|
@ -81,6 +81,7 @@ namespace Tri {
|
||||||
unsigned int height;
|
unsigned int height;
|
||||||
float dt;
|
float dt;
|
||||||
float notificationAlpha;
|
float notificationAlpha;
|
||||||
|
float notificationAmt;
|
||||||
std::array<char, 256> notificationText;
|
std::array<char, 256> notificationText;
|
||||||
|
|
||||||
std::vector<Triangle> tris;
|
std::vector<Triangle> tris;
|
||||||
|
@ -109,6 +110,8 @@ namespace Tri {
|
||||||
std::vector<Action> history;
|
std::vector<Action> history;
|
||||||
Action::IndexT history_idx;
|
Action::IndexT history_idx;
|
||||||
|
|
||||||
|
float clickTimeout;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void handle_events();
|
void handle_events();
|
||||||
void update();
|
void update();
|
||||||
|
@ -124,6 +127,9 @@ namespace Tri {
|
||||||
const BitsetType get_flags() const;
|
const BitsetType get_flags() const;
|
||||||
|
|
||||||
float get_notification_alpha() const;
|
float get_notification_alpha() const;
|
||||||
|
void reset_notification_alpha();
|
||||||
|
void clear_notification_alpha();
|
||||||
|
|
||||||
const char* get_notification_text() const;
|
const char* get_notification_text() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -165,6 +171,9 @@ namespace Tri {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void restore_points_on_tri_del(Action::IndexT end);
|
void restore_points_on_tri_del(Action::IndexT end);
|
||||||
|
|
||||||
|
public:
|
||||||
|
float get_click_timeout() const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue