2020-07-21 11:34:39 +00:00
|
|
|
#include "state.hpp"
|
|
|
|
|
2020-07-22 06:19:37 +00:00
|
|
|
#include <cstring>
|
2020-07-22 07:12:21 +00:00
|
|
|
#include <cassert>
|
2020-07-22 09:19:34 +00:00
|
|
|
#include <string>
|
2020-08-04 12:13:17 +00:00
|
|
|
#include <cmath>
|
2020-07-22 06:19:37 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
#include <raylib.h>
|
2020-07-22 06:11:10 +00:00
|
|
|
|
2020-08-04 12:13:17 +00:00
|
|
|
#include "helpers.hpp"
|
2020-07-21 11:34:39 +00:00
|
|
|
|
2020-07-22 07:12:21 +00:00
|
|
|
#define STARTING_HELP_FADE_RATE 0.2f
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
# include <cstdio>
|
|
|
|
#endif
|
|
|
|
|
2020-07-22 06:19:37 +00:00
|
|
|
Tri::State::State(int argc, char **argv) :
|
2020-07-21 11:34:39 +00:00
|
|
|
width(800),
|
|
|
|
height(600),
|
2021-03-29 08:03:39 +00:00
|
|
|
dt(1.0f/60.0f),
|
2020-07-29 08:53:39 +00:00
|
|
|
notification_alpha(1.0f),
|
2020-07-22 06:33:09 +00:00
|
|
|
trisIndex(0),
|
2020-07-22 07:12:21 +00:00
|
|
|
currentTri_state(CurrentState::NONE),
|
2020-07-22 07:26:57 +00:00
|
|
|
currentTri_maxState(CurrentState::NONE),
|
2020-07-22 08:46:20 +00:00
|
|
|
colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f},
|
|
|
|
bgColorPickerColor{0.0f, 0.0f, 0.0f},
|
2021-03-29 08:03:39 +00:00
|
|
|
bgColor(BLACK),
|
|
|
|
pi(std::acos(-1.0f)),
|
|
|
|
inputWidth(800),
|
|
|
|
inputHeight(600)
|
2020-07-21 11:44:10 +00:00
|
|
|
{
|
2021-03-29 08:03:39 +00:00
|
|
|
InitWindow(width, height, "Triangles");
|
|
|
|
SetTargetFPS(60);
|
|
|
|
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.set(F_IS_RUNNING); // is running
|
2020-07-22 06:11:10 +00:00
|
|
|
|
2020-08-04 12:13:17 +00:00
|
|
|
set_notification_text("Press \"H\" for help");
|
2020-07-29 08:53:39 +00:00
|
|
|
|
2020-07-22 06:28:36 +00:00
|
|
|
pointCircle.setRadius(7.0f);
|
2021-03-29 08:03:39 +00:00
|
|
|
pointCircle.translate({7.0f, 7.0f});
|
|
|
|
pointCircle.fillColor = WHITE;
|
|
|
|
pointCircle.outlineColor = BLACK;
|
2020-07-22 09:19:34 +00:00
|
|
|
|
|
|
|
saveFilenameBuffer.fill(0);
|
2020-07-22 10:03:33 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
drawCache = LoadRenderTexture(width, height);
|
|
|
|
flags.set(F_DRAW_CACHE_INITIALIZED);
|
|
|
|
flags.set(F_DRAW_CACHE_DIRTY);
|
|
|
|
|
|
|
|
GuiSetStyle(DEFAULT, BACKGROUND_COLOR, 0x303030);
|
2020-07-21 11:44:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 06:11:10 +00:00
|
|
|
Tri::State::~State() {
|
2021-03-29 08:03:39 +00:00
|
|
|
UnloadRenderTexture(drawCache);
|
|
|
|
CloseWindow();
|
2020-07-22 06:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::handle_events() {
|
2021-03-29 08:03:39 +00:00
|
|
|
if(WindowShouldClose()) {
|
|
|
|
flags.reset(F_IS_RUNNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
int keyPressed = GetKeyPressed();
|
|
|
|
while(keyPressed > 0) {
|
|
|
|
if(!flags.test(F_DISPLAY_SAVE)) {
|
|
|
|
switch(keyPressed) {
|
|
|
|
case KEY_H:
|
|
|
|
flags.flip(F_DISPLAY_HELP);
|
|
|
|
break;
|
|
|
|
case KEY_U:
|
|
|
|
flags.set(F_DRAW_CACHE_DIRTY);
|
|
|
|
if(currentTri_state > 0) {
|
|
|
|
switch(currentTri_state) {
|
|
|
|
case FIRST:
|
|
|
|
currentTri_state = CurrentState::NONE;
|
|
|
|
break;
|
|
|
|
case SECOND:
|
|
|
|
currentTri_state = CurrentState::FIRST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(!"Unreachable code");
|
|
|
|
break;
|
2020-07-22 10:09:05 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
} else if(trisIndex > 0) {
|
|
|
|
--trisIndex;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
case FIRST:
|
|
|
|
currentTri_state = CurrentState::SECOND;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(!"Unreachable code");
|
|
|
|
break;
|
2020-07-29 08:53:39 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
} 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;
|
2020-07-30 11:11:57 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_C:
|
|
|
|
if(flags.test(F_DISPLAY_COLOR_P)) {
|
|
|
|
close_color_picker();
|
|
|
|
} else {
|
|
|
|
flags.set(F_DISPLAY_COLOR_P);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_B:
|
|
|
|
if(flags.test(F_DISPLAY_BG_COLOR_P)) {
|
|
|
|
close_bg_color_picker();
|
|
|
|
} else {
|
|
|
|
flags.set(F_DISPLAY_BG_COLOR_P);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_S:
|
|
|
|
flags.flip(F_DISPLAY_SAVE);
|
|
|
|
break;
|
|
|
|
case KEY_P:
|
|
|
|
flags.flip(F_COPY_COLOR_MODE);
|
|
|
|
if(flags.test(F_COPY_COLOR_MODE)) {
|
|
|
|
set_notification_text(
|
|
|
|
"Copy color mode\n"
|
|
|
|
"Click to change\n"
|
|
|
|
"current draw color\n"
|
|
|
|
"to what was\n"
|
|
|
|
"clicked on");
|
|
|
|
} else {
|
|
|
|
notification_alpha = 0.0f;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_I:
|
|
|
|
flags.flip(F_DISPLAY_CHANGE_SIZE);
|
|
|
|
if(!flags.test(F_DISPLAY_CHANGE_SIZE)) {
|
|
|
|
inputWidth = width;
|
|
|
|
inputHeight = height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_E:
|
|
|
|
if(flags.test(F_TRI_EDIT_MODE)) {
|
|
|
|
close_selected_tri_mode();
|
|
|
|
} else {
|
|
|
|
flags.flip(F_SELECT_TRI_MODE);
|
|
|
|
if(flags.test(F_SELECT_TRI_MODE)) {
|
|
|
|
set_notification_text("Click on a tri\nto edit it");
|
2020-08-04 12:13:17 +00:00
|
|
|
}
|
2020-07-22 06:33:09 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
break;
|
2020-07-22 06:11:10 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
}
|
|
|
|
keyPressed = GetKeyPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
|
|
|
if(can_draw()) {
|
|
|
|
switch(currentTri_state) {
|
|
|
|
case CurrentState::NONE:
|
|
|
|
currentTri[0] = {GetMouseX(), GetMouseY()};
|
|
|
|
if(trisIndex < tris.size()) {
|
|
|
|
tris.resize(trisIndex);
|
2020-07-22 06:33:09 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
currentTri_state = CurrentState::FIRST;
|
|
|
|
currentTri_maxState = CurrentState::FIRST;
|
|
|
|
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);
|
2020-08-04 12:13:17 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
++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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if(flags.test(F_COPY_COLOR_MODE)) {
|
|
|
|
check_draw_cache();
|
|
|
|
Image drawImage = GetTextureData(drawCache.texture);
|
|
|
|
Color *colors = LoadImageColors(drawImage);
|
|
|
|
int mx = GetMouseX();
|
|
|
|
int my = GetMouseY();
|
|
|
|
if(mx < 0) { mx = 0; }
|
|
|
|
else if(mx >= (int)width) { mx = width - 1; }
|
|
|
|
if(my < 0) { my = 0; }
|
|
|
|
else if(my >= (int)height) { my = height - 1; }
|
|
|
|
|
|
|
|
colorPickerColor[0] = colors[mx + my * width].r;
|
|
|
|
colorPickerColor[1] = colors[mx + my * width].g;
|
|
|
|
colorPickerColor[2] = colors[mx + my * width].b;
|
|
|
|
colorPickerColor[3] = 1.0f;
|
|
|
|
pointCircle.fillColor = colors[mx + my * width];
|
|
|
|
flags.reset(F_COPY_COLOR_MODE);
|
|
|
|
set_notification_text("Color set");
|
|
|
|
|
|
|
|
UnloadImageColors(colors);
|
|
|
|
UnloadImage(drawImage);
|
|
|
|
} else if(flags.test(F_SELECT_TRI_MODE)) {
|
|
|
|
int mx = GetMouseX();
|
|
|
|
int my = GetMouseY();
|
|
|
|
if(mx < 0) { mx = 0; }
|
|
|
|
else if(mx >= (int)width) { mx = width - 1; }
|
|
|
|
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;
|
|
|
|
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[0] = tris[i].fillColor.r / 255.0f;
|
|
|
|
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;
|
2020-08-04 12:13:17 +00:00
|
|
|
}
|
2020-07-22 06:11:10 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
if(!flags.test(F_TRI_EDIT_MODE)) {
|
|
|
|
set_notification_text("Did not select\nanything");
|
|
|
|
}
|
2020-07-21 11:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 11:34:39 +00:00
|
|
|
|
|
|
|
void Tri::State::update() {
|
2021-03-29 08:03:39 +00:00
|
|
|
dt = GetFrameTime();
|
2020-07-22 06:11:10 +00:00
|
|
|
|
2020-07-29 08:53:39 +00:00
|
|
|
if(notification_alpha > 0.0f) {
|
2021-03-29 08:03:39 +00:00
|
|
|
notification_alpha -= dt * STARTING_HELP_FADE_RATE;
|
2020-07-29 08:53:39 +00:00
|
|
|
if(notification_alpha < 0.0f) {
|
|
|
|
notification_alpha = 0.0f;
|
2020-07-22 07:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 10:59:18 +00:00
|
|
|
if(flags.test(F_COLOR_P_COLOR_DIRTY)) {
|
|
|
|
flags.reset(F_COLOR_P_COLOR_DIRTY);
|
2021-03-29 08:03:39 +00:00
|
|
|
pointCircle.fillColor = Color{
|
2020-07-22 07:26:57 +00:00
|
|
|
(unsigned char)(255 * colorPickerColor[0]),
|
|
|
|
(unsigned char)(255 * colorPickerColor[1]),
|
|
|
|
(unsigned char)(255 * colorPickerColor[2]),
|
2021-03-29 08:03:39 +00:00
|
|
|
(unsigned char)(255 * colorPickerColor[3])};
|
2020-07-22 07:26:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 10:59:18 +00:00
|
|
|
if(flags.test(F_BG_COLOR_P_COLOR_DIRTY)) {
|
|
|
|
flags.reset(F_BG_COLOR_P_COLOR_DIRTY);
|
2020-07-22 08:46:20 +00:00
|
|
|
bgColor.r = (unsigned char)(255 * bgColorPickerColor[0]);
|
|
|
|
bgColor.g = (unsigned char)(255 * bgColorPickerColor[1]);
|
|
|
|
bgColor.b = (unsigned char)(255 * bgColorPickerColor[2]);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:13:17 +00:00
|
|
|
if(flags.test(F_TRI_EDIT_MODE)) {
|
2021-03-29 08:03:39 +00:00
|
|
|
selectedTriBlinkTimer -= dt * TRIANGLES_EDIT_TRI_BLINK_RATE;
|
2020-08-04 12:13:17 +00:00
|
|
|
if(selectedTriBlinkTimer <= 0.0f) {
|
|
|
|
selectedTriBlinkTimer = 1.0f;
|
|
|
|
flags.flip(F_TRI_EDIT_DRAW_TRI);
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 11:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::draw() {
|
2021-03-29 08:03:39 +00:00
|
|
|
// Should be able to directly draw a texture held by the RenderTexture2D
|
2020-08-02 10:59:18 +00:00
|
|
|
if(flags.test(F_DRAW_CACHE_INITIALIZED)) {
|
2020-07-22 10:03:33 +00:00
|
|
|
// draw cache initialized
|
2021-03-29 08:03:39 +00:00
|
|
|
check_draw_cache();
|
|
|
|
BeginDrawing();
|
|
|
|
DrawTexture(drawCache.texture, 0, 0, WHITE);
|
2020-07-22 10:03:33 +00:00
|
|
|
} else {
|
2021-03-29 08:03:39 +00:00
|
|
|
BeginDrawing();
|
|
|
|
draw_to_target(nullptr);
|
2020-07-22 10:03:33 +00:00
|
|
|
}
|
2020-07-22 09:19:34 +00:00
|
|
|
|
2020-08-04 12:13:17 +00:00
|
|
|
if(flags.test(F_TRI_EDIT_MODE) && flags.test(F_TRI_EDIT_DRAW_TRI)) {
|
2021-03-29 08:03:39 +00:00
|
|
|
// tris.at(selectedTri).setOutlineThickness(4.0f);
|
|
|
|
tris[selectedTri].draw();
|
|
|
|
// tris.at(selectedTri).setOutlineThickness(0.0f);
|
2020-08-04 12:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(can_draw()) {
|
|
|
|
for(unsigned int i = 0; i < currentTri_state; ++i) {
|
2021-03-29 08:03:39 +00:00
|
|
|
pointCircle.resetTransform();
|
|
|
|
pointCircle.translate(currentTri[i]);
|
|
|
|
pointCircle.draw();
|
2020-08-04 12:13:17 +00:00
|
|
|
}
|
2020-07-22 12:17:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
Tri::draw_notification(this);
|
|
|
|
Tri::draw_color_picker(this);
|
|
|
|
Tri::draw_bg_color_picker(this);
|
|
|
|
Tri::draw_edit_tri(this);
|
|
|
|
Tri::draw_change_size(this);
|
|
|
|
Tri::draw_save(this);
|
|
|
|
Tri::draw_help(this);
|
2020-07-22 09:19:34 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
EndDrawing();
|
2020-07-22 09:19:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
void Tri::State::draw_to_target(RenderTexture2D *target) {
|
|
|
|
if(target) {
|
|
|
|
BeginTextureMode(*target);
|
|
|
|
ClearBackground(bgColor);
|
2020-07-22 06:11:10 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
// draw tris
|
|
|
|
for(unsigned int i = 0; i < trisIndex; ++i) {
|
|
|
|
tris[i].draw();
|
|
|
|
}
|
|
|
|
EndTextureMode();
|
|
|
|
} else {
|
|
|
|
// Expects BeginDrawing() already having been called prior to this fn
|
|
|
|
ClearBackground(bgColor);
|
|
|
|
|
|
|
|
// draw tris
|
|
|
|
for(unsigned int i = 0; i < trisIndex; ++i) {
|
|
|
|
tris[i].draw();
|
|
|
|
}
|
2020-07-22 06:28:36 +00:00
|
|
|
}
|
2020-07-21 11:34:39 +00:00
|
|
|
}
|
2020-07-22 06:19:37 +00:00
|
|
|
|
2020-07-22 07:12:21 +00:00
|
|
|
unsigned int Tri::State::get_width() const {
|
2020-07-22 06:19:37 +00:00
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:12:21 +00:00
|
|
|
unsigned int Tri::State::get_height() const {
|
2020-07-22 06:19:37 +00:00
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Tri::State::BitsetType Tri::State::get_flags() const {
|
|
|
|
return flags;
|
|
|
|
}
|
2020-07-22 07:12:21 +00:00
|
|
|
|
2020-07-29 08:53:39 +00:00
|
|
|
float Tri::State::get_notification_alpha() const {
|
|
|
|
return notification_alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Tri::State::get_notification_text() const {
|
|
|
|
return notification_text.data();
|
2020-07-22 07:12:21 +00:00
|
|
|
}
|
2020-07-22 07:26:57 +00:00
|
|
|
|
2020-08-04 12:13:17 +00:00
|
|
|
void Tri::State::set_notification_text(const char *text) {
|
|
|
|
notification_text.fill(0);
|
|
|
|
std::strncpy(notification_text.data(),
|
|
|
|
text,
|
|
|
|
notification_text.max_size() - 1);
|
|
|
|
notification_alpha = 1.0f;
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
std::array<float, 4>& Tri::State::get_color() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.set(F_COLOR_P_COLOR_DIRTY);
|
2020-07-22 07:26:57 +00:00
|
|
|
return colorPickerColor;
|
|
|
|
}
|
2020-07-22 08:46:20 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
std::array<float, 3>& Tri::State::get_bg_color() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.set(F_BG_COLOR_P_COLOR_DIRTY);
|
2020-07-22 08:46:20 +00:00
|
|
|
return bgColorPickerColor;
|
|
|
|
}
|
2020-07-22 09:19:34 +00:00
|
|
|
|
|
|
|
Tri::State::FilenameBufferType* Tri::State::get_save_filename_buffer() {
|
|
|
|
return &saveFilenameBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tri::State::do_save() {
|
2021-03-29 08:03:39 +00:00
|
|
|
RenderTexture2D saveTexture = LoadRenderTexture(width, height);
|
2020-07-22 09:19:34 +00:00
|
|
|
|
2020-07-22 12:17:49 +00:00
|
|
|
draw_to_target(&saveTexture);
|
2020-07-22 09:19:34 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
Image saveImage = GetTextureData(saveTexture.texture);
|
|
|
|
UnloadRenderTexture(saveTexture);
|
|
|
|
if(ExportImage(saveImage, saveFilenameBuffer.data())) {
|
2020-07-22 09:19:34 +00:00
|
|
|
#ifndef NDEBUG
|
2021-03-29 08:03:39 +00:00
|
|
|
printf("Saved to \"%s\"\n", saveFilenameBuffer.data());
|
2020-07-22 09:19:34 +00:00
|
|
|
#endif
|
2020-07-30 11:11:57 +00:00
|
|
|
failedMessage.clear();
|
2021-03-29 08:03:39 +00:00
|
|
|
UnloadImage(saveImage);
|
2020-07-22 09:19:34 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
#ifndef NDEBUG
|
2021-03-29 08:03:39 +00:00
|
|
|
printf("ERROR: Failed to save \"%s\"\n", saveFilenameBuffer.data());
|
2020-07-22 09:19:34 +00:00
|
|
|
#endif
|
2020-07-30 11:11:57 +00:00
|
|
|
failedMessage = std::string("Failed to save (does the name end in \".png\"?)");
|
2021-03-29 08:03:39 +00:00
|
|
|
UnloadImage(saveImage);
|
2020-07-22 09:19:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
const std::string& Tri::State::failed_message() const {
|
2020-07-30 11:11:57 +00:00
|
|
|
return failedMessage;
|
2020-07-22 09:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::close_save() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.reset(F_DISPLAY_SAVE);
|
2020-07-22 09:19:34 +00:00
|
|
|
}
|
2020-07-22 10:03:33 +00:00
|
|
|
|
2020-07-29 08:53:39 +00:00
|
|
|
bool Tri::State::can_draw() const {
|
2020-08-02 10:59:18 +00:00
|
|
|
return !flags.test(F_DISPLAY_HELP)
|
|
|
|
&& !flags.test(F_DISPLAY_COLOR_P)
|
|
|
|
&& !flags.test(F_DISPLAY_BG_COLOR_P)
|
|
|
|
&& !flags.test(F_DISPLAY_SAVE)
|
|
|
|
&& !flags.test(F_COPY_COLOR_MODE)
|
2020-08-04 12:13:17 +00:00
|
|
|
&& !flags.test(F_DISPLAY_CHANGE_SIZE)
|
|
|
|
&& !flags.test(F_SELECT_TRI_MODE)
|
|
|
|
&& !flags.test(F_TRI_EDIT_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::reset_modes() {
|
|
|
|
flags.reset(F_DISPLAY_HELP);
|
|
|
|
flags.reset(F_DISPLAY_COLOR_P);
|
|
|
|
flags.reset(F_DISPLAY_BG_COLOR_P);
|
|
|
|
flags.reset(F_DISPLAY_SAVE);
|
|
|
|
flags.reset(F_COPY_COLOR_MODE);
|
|
|
|
flags.reset(F_DISPLAY_CHANGE_SIZE);
|
|
|
|
flags.reset(F_SELECT_TRI_MODE);
|
|
|
|
flags.reset(F_TRI_EDIT_MODE);
|
2020-07-22 10:03:33 +00:00
|
|
|
}
|
2020-07-24 08:55:39 +00:00
|
|
|
|
|
|
|
void Tri::State::close_help() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.reset(F_DISPLAY_HELP);
|
2020-07-24 08:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::close_color_picker() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.reset(F_DISPLAY_COLOR_P);
|
|
|
|
flags.set(F_DRAW_CACHE_DIRTY);
|
2020-07-24 08:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::close_bg_color_picker() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.reset(F_DISPLAY_BG_COLOR_P);
|
|
|
|
flags.set(F_DRAW_CACHE_DIRTY);
|
2020-07-24 08:55:39 +00:00
|
|
|
}
|
2020-07-30 11:11:57 +00:00
|
|
|
|
|
|
|
bool Tri::State::change_width_height() {
|
|
|
|
std::bitset<2> warnings;
|
2021-03-29 08:03:39 +00:00
|
|
|
if(inputWidth < 0 || inputHeight < 0) {
|
2020-07-30 11:11:57 +00:00
|
|
|
failedMessage = "Width or Height cannot be less than 0";
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
if(inputWidth < 800) {
|
|
|
|
inputWidth = 800;
|
|
|
|
warnings.set(0);
|
2020-07-30 11:11:57 +00:00
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
if(inputHeight < 600) {
|
|
|
|
inputHeight = 600;
|
|
|
|
warnings.set(1);
|
2020-07-30 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(warnings.test(0) && warnings.test(1)) {
|
2021-03-29 08:03:39 +00:00
|
|
|
set_notification_text("Width set to 800\nHeight set to 600");
|
2020-07-30 11:11:57 +00:00
|
|
|
} else if(warnings.test(0)) {
|
2021-03-29 08:03:39 +00:00
|
|
|
set_notification_text("Width set to 800");
|
2020-07-30 11:11:57 +00:00
|
|
|
} else if(warnings.test(1)) {
|
2021-03-29 08:03:39 +00:00
|
|
|
set_notification_text("Height set to 600");
|
2020-07-30 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
this->width = inputWidth;
|
|
|
|
this->height = inputHeight;
|
|
|
|
|
|
|
|
SetWindowSize(this->width, this->height);
|
2020-07-30 11:11:57 +00:00
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
UnloadRenderTexture(drawCache);
|
|
|
|
drawCache = LoadRenderTexture(this->width, this->height);
|
2020-07-30 11:11:57 +00:00
|
|
|
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.set(F_DRAW_CACHE_DIRTY);
|
2020-07-30 11:11:57 +00:00
|
|
|
|
|
|
|
currentTri_state = CurrentState::NONE;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::close_input_width_height_window() {
|
2020-08-02 10:59:18 +00:00
|
|
|
flags.reset(F_DISPLAY_CHANGE_SIZE);
|
2020-07-30 11:11:57 +00:00
|
|
|
}
|
2020-08-04 12:13:17 +00:00
|
|
|
|
|
|
|
float Tri::State::get_pi() const {
|
|
|
|
return pi;
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:03:39 +00:00
|
|
|
std::array<float, 4>& Tri::State::get_selected_tri_color() {
|
|
|
|
tris.at(selectedTri).fillColor = Color{
|
2020-08-04 12:13:17 +00:00
|
|
|
(unsigned char)(255.0f * selectedTriColor[0]),
|
|
|
|
(unsigned char)(255.0f * selectedTriColor[1]),
|
|
|
|
(unsigned char)(255.0f * selectedTriColor[2]),
|
2021-03-29 08:03:39 +00:00
|
|
|
(unsigned char)(255.0f * selectedTriColor[3])};
|
2020-08-04 12:13:17 +00:00
|
|
|
return selectedTriColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tri::State::close_selected_tri_mode() {
|
2021-03-29 08:03:39 +00:00
|
|
|
tris.at(selectedTri).fillColor = Color{
|
2020-08-04 12:13:17 +00:00
|
|
|
(unsigned char)(255.0f * selectedTriColor[0]),
|
|
|
|
(unsigned char)(255.0f * selectedTriColor[1]),
|
|
|
|
(unsigned char)(255.0f * selectedTriColor[2]),
|
2021-03-29 08:03:39 +00:00
|
|
|
(unsigned char)(255.0f * selectedTriColor[3])};
|
2020-08-04 12:13:17 +00:00
|
|
|
flags.set(F_DRAW_CACHE_DIRTY);
|
|
|
|
reset_modes();
|
|
|
|
}
|
2021-03-29 08:03:39 +00:00
|
|
|
|
|
|
|
bool Tri::State::check_draw_cache() {
|
|
|
|
if(flags.test(F_DRAW_CACHE_INITIALIZED) && flags.test(F_DRAW_CACHE_DIRTY)) {
|
|
|
|
// draw cache initialized and dirty
|
|
|
|
flags.reset(F_DRAW_CACHE_DIRTY);
|
|
|
|
draw_to_target(&drawCache);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int* Tri::State::get_input_width() {
|
|
|
|
return &inputWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int* Tri::State::get_input_height() {
|
|
|
|
return &inputHeight;
|
|
|
|
}
|