Compare commits

..

No commits in common. "25dafb26b54c0738cabb0be8c642c04f6ba79f99" and "1da2ddb1250ce045925788c3a00a9c30773c38ea" have entirely different histories.

5 changed files with 78 additions and 339 deletions

View file

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

View file

@ -1,12 +0,0 @@
#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,8 +14,6 @@
#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
@ -23,12 +21,6 @@
#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
@ -276,8 +268,7 @@ namespace Tri {
&alpha
);
color.a = alpha * 255.0F;
if(GuiButton({504.0f, 308.0f, 234.0f, 16.0f}, "Close")
&& state->get_click_timeout() == 0.0F) {
if(GuiButton({504.0f, 308.0f, 234.0f, 16.0f}, "Close")) {
state->close_selected_tri_mode();
}
} else {
@ -300,15 +291,6 @@ 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,6 +10,8 @@
#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) :
@ -18,12 +20,13 @@ 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),
pointCircle({7.0F, 7.0F}, 7.0F, WHITE),
currentTri_maxState(CurrentState::NONE),
pointCircle(),
colorPickerColor{255, 255, 255, 255},
bgColorPickerColor{0, 0, 0, 255},
bgColor(BLACK),
@ -33,13 +36,9 @@ drawCache(),
pi(std::acos(-1.0f)),
selectedTri(),
selectedTriColor{255, 255, 255, 255},
prevTriColor{255, 255, 255, 255},
selectedTriBlinkTimer(),
inputWidth(800),
inputHeight(600),
history(),
history_idx(0),
clickTimeout(0.0F)
inputHeight(600)
{
InitWindow(width, height, "Triangles");
SetTargetFPS(60);
@ -48,71 +47,21 @@ clickTimeout(0.0F)
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, TRI_GUI_BG_COLOR);
GuiSetStyle(DEFAULT, BASE_COLOR_NORMAL, TRI_GUI_BASE_COLOR);
GuiSetStyle(DEFAULT, TEXT_COLOR_NORMAL, TRI_GUI_TEXT_COLOR);
GuiSetStyle(DEFAULT, BACKGROUND_COLOR, 0x303030);
}
#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();
@ -131,102 +80,52 @@ 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) {
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
)
);
if(currentTri_state > 0) {
switch(currentTri_state) {
case FIRST:
currentTri_state = CurrentState::NONE;
break;
}
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;
case SECOND:
currentTri_state = CurrentState::FIRST;
break;
default:
assert(!"Unreachable code");
break;
}
--history_idx;
} else if(trisIndex > 0) {
--trisIndex;
}
break;
case KEY_R:
flags.set(F_DRAW_CACHE_DIRTY);
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;
if(currentTri_state != CurrentState::NONE
&& currentTri_state < currentTri_maxState) {
switch(currentTri_state) {
case NONE:
currentTri_state = CurrentState::FIRST;
break;
}
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;
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;
default:
assert(!"Unreachable code");
break;
}
++history_idx;
}
break;
case KEY_C:
@ -256,7 +155,7 @@ void Tri::State::handle_events() {
"to what was\n"
"clicked on");
} else {
clear_notification_alpha();
notificationAlpha = 0.0f;
}
break;
case KEY_I:
@ -285,56 +184,37 @@ void Tri::State::handle_events() {
keyPressed = GetKeyPressed();
}
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && clickTimeout == 0.0F) {
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
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;
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_maxState = CurrentState::FIRST;
break;
case CurrentState::FIRST:
currentTri[1] = {GetMouseX(), GetMouseY()};
currentTri_state = CurrentState::SECOND;
if(history_idx < history.size()) {
history.resize(history_idx);
if(trisIndex < tris.size()) {
tris.resize(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_state = CurrentState::SECOND;
currentTri_maxState = CurrentState::SECOND;
break;
case CurrentState::SECOND:
currentTri[2] = {GetMouseX(), GetMouseY()};
if(trisIndex < tris.size()) {
tris.resize(trisIndex);
}
++trisIndex;
make_counter_clockwise(currentTri);
tris.emplace_back(currentTri, pointCircle.fillColor);
currentTri_state = CurrentState::NONE;
currentTri_maxState = CurrentState::NONE;
flags.set(F_DRAW_CACHE_DIRTY);
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; }
break;
}
} else if(flags.test(F_COPY_COLOR_MODE)) {
check_draw_cache();
@ -365,17 +245,15 @@ void Tri::State::handle_events() {
if(my < 0) { my = 0; }
else if(my >= (int)height) { my = height - 1; }
for (unsigned int i = 0; i < tris.size(); ++i) {
if(is_within_shape(tris[i], {mx, my})) {
for(unsigned int i = trisIndex; i-- > 0; ) {
if(is_within_shape(tris.at(i), {mx, my})) {
selectedTri = i;
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;
}
}
@ -390,11 +268,9 @@ void Tri::State::update() {
dt = GetFrameTime();
if(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);
notificationAlpha -= dt * STARTING_HELP_FADE_RATE;
if(notificationAlpha < 0.0f) {
notificationAlpha = 0.0f;
}
}
@ -416,13 +292,6 @@ 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() {
@ -450,13 +319,15 @@ 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.at(selectedTri).draw();
tris[selectedTri].draw();
// tris.at(selectedTri).setOutlineThickness(0.0f);
}
if(can_draw()) {
for(unsigned int i = 0; i < currentTri_state; ++i) {
DrawCircle(currentTri[i].x, currentTri[i].y, pointCircle.getRadius(), pointCircle.fillColor);
pointCircle.resetTransform();
pointCircle.translate(currentTri[i]);
pointCircle.draw();
}
}
@ -477,7 +348,7 @@ void Tri::State::draw_to_target(RenderTexture2D *target) {
ClearBackground(bgColor);
// draw tris
for(unsigned int i = 0; i < tris.size(); ++i) {
for(unsigned int i = 0; i < trisIndex; ++i) {
tris[i].draw();
}
EndTextureMode();
@ -486,7 +357,7 @@ void Tri::State::draw_to_target(RenderTexture2D *target) {
ClearBackground(bgColor);
// draw tris
for(unsigned int i = 0; i < tris.size(); ++i) {
for(unsigned int i = 0; i < trisIndex; ++i) {
tris[i].draw();
}
}
@ -508,16 +379,6 @@ 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();
}
@ -527,7 +388,7 @@ void Tri::State::set_notification_text(const char *text) {
std::strncpy(notificationText.data(),
text,
notificationText.max_size() - 1);
reset_notification_alpha();
notificationAlpha = 1.0f;
}
void Tri::State::append_notification_text(const char *text) {
@ -539,7 +400,7 @@ void Tri::State::append_notification_text(const char *text) {
notificationText.data() + length,
text,
notificationText.max_size() - length - 1);
reset_notification_alpha();
notificationAlpha = 1.0f;
}
Color& Tri::State::get_color() {
@ -679,19 +540,8 @@ 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();
}
@ -713,33 +563,3 @@ 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,43 +41,6 @@ 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;
@ -85,12 +48,13 @@ 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;
@ -106,17 +70,11 @@ 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();
@ -132,9 +90,6 @@ 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:
@ -174,11 +129,6 @@ 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;
};
}