Impl changing window size

This commit is contained in:
Stephen Seo 2020-07-30 20:11:57 +09:00
parent c386dcd8d9
commit 508ed0d7d4
3 changed files with 111 additions and 10 deletions

View file

@ -96,13 +96,33 @@ namespace Tri {
} else if(ImGui::Button("Cancel")) { } else if(ImGui::Button("Cancel")) {
state->close_save(); state->close_save();
} }
auto string_view = state->failed_save_message(); auto string_view = state->failed_message();
if(!string_view.empty()) { if(!string_view.empty()) {
ImGui::TextUnformatted(string_view.data(), string_view.data() + string_view.size()); ImGui::TextUnformatted(string_view.data(), string_view.data() + string_view.size());
} }
ImGui::End(); ImGui::End();
} }
} }
inline void draw_change_size(Tri::State *state) {
if(state->get_flags().test(10)) {
ImGui::Begin("ChangeSize");
ImGui::InputInt2("Width and Height", state->get_input_width_height());
auto string_view = state->failed_message();
if(!string_view.empty()) {
ImGui::TextUnformatted(string_view.data(), string_view.data() + string_view.size());
}
if(ImGui::Button("Cancel")) {
state->close_input_width_height_window();
}
if(ImGui::Button("Set")) {
if(state->change_width_height()) {
state->close_input_width_height_window();
}
}
ImGui::End();
}
}
} }
#endif #endif

View file

@ -25,7 +25,8 @@ currentTri_state(CurrentState::NONE),
currentTri_maxState(CurrentState::NONE), currentTri_maxState(CurrentState::NONE),
colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f}, colorPickerColor{1.0f, 1.0f, 1.0f, 1.0f},
bgColorPickerColor{0.0f, 0.0f, 0.0f}, bgColorPickerColor{0.0f, 0.0f, 0.0f},
bgColor(sf::Color::Black) bgColor(sf::Color::Black),
inputWidthHeight{800, 600}
{ {
flags.set(1); // is running flags.set(1); // is running
ImGui::SFML::Init(window); ImGui::SFML::Init(window);
@ -67,6 +68,7 @@ void Tri::State::handle_events() {
flags.reset(1); flags.reset(1);
} else if(event.type == sf::Event::KeyPressed) { } else if(event.type == sf::Event::KeyPressed) {
if(!flags.test(6)) { if(!flags.test(6)) {
// TODO use a switch statement
if(event.key.code == sf::Keyboard::H) { if(event.key.code == sf::Keyboard::H) {
flags.flip(0); flags.flip(0);
} else if(event.key.code == sf::Keyboard::U) { } else if(event.key.code == sf::Keyboard::U) {
@ -144,6 +146,12 @@ void Tri::State::handle_events() {
} else { } else {
notification_alpha = 0.0f; notification_alpha = 0.0f;
} }
} else if(event.key.code == sf::Keyboard::I) {
flags.flip(10);
if(!flags.test(10)) {
inputWidthHeight[0] = width;
inputWidthHeight[1] = height;
}
} }
} }
} else if(event.type == sf::Event::MouseButtonPressed) { } else if(event.type == sf::Event::MouseButtonPressed) {
@ -228,6 +236,7 @@ void Tri::State::update() {
Tri::draw_notification(this); Tri::draw_notification(this);
Tri::draw_color_picker(this); Tri::draw_color_picker(this);
Tri::draw_bg_color_picker(this); Tri::draw_bg_color_picker(this);
Tri::draw_change_size(this);
Tri::draw_save(this); Tri::draw_save(this);
Tri::draw_help(this); Tri::draw_help(this);
@ -308,7 +317,7 @@ bool Tri::State::do_save() {
#ifndef NDEBUG #ifndef NDEBUG
puts("ERROR: Failed to create texture for saving"); puts("ERROR: Failed to create texture for saving");
#endif #endif
failedSaveMessage = std::string("Failed to create texture for saving"); failedMessage = std::string("Failed to create texture for saving");
return false; return false;
} }
@ -321,19 +330,19 @@ bool Tri::State::do_save() {
#ifndef NDEBUG #ifndef NDEBUG
printf("Saved to \"%s\"\n", filename.c_str()); printf("Saved to \"%s\"\n", filename.c_str());
#endif #endif
failedSaveMessage.clear(); failedMessage.clear();
return true; return true;
} else { } else {
#ifndef NDEBUG #ifndef NDEBUG
printf("ERROR: Failed to save \"%s\"\n", filename.c_str()); printf("ERROR: Failed to save \"%s\"\n", filename.c_str());
#endif #endif
failedSaveMessage = std::string("Failed to save (does the name end in \".png\"?)"); failedMessage = std::string("Failed to save (does the name end in \".png\"?)");
return false; return false;
} }
} }
std::string_view Tri::State::failed_save_message() const { std::string_view Tri::State::failed_message() const {
return failedSaveMessage; return failedMessage;
} }
void Tri::State::close_save() { void Tri::State::close_save() {
@ -345,7 +354,8 @@ bool Tri::State::can_draw() const {
&& !flags.test(2) && !flags.test(2)
&& !flags.test(5) && !flags.test(5)
&& !flags.test(6) && !flags.test(6)
&& !flags.test(9); && !flags.test(9)
&& !flags.test(10);
} }
void Tri::State::close_help() { void Tri::State::close_help() {
@ -361,3 +371,67 @@ void Tri::State::close_bg_color_picker() {
flags.reset(5); flags.reset(5);
flags.set(7); flags.set(7);
} }
bool Tri::State::change_width_height() {
std::bitset<2> warnings;
if(inputWidthHeight[0] < 0 || inputWidthHeight[1] < 0) {
failedMessage = "Width or Height cannot be less than 0";
return false;
}
if(inputWidthHeight[0] < 200) {
inputWidthHeight[0] = 200;
warnings.set(0);
}
if(inputWidthHeight[1] < 150) {
inputWidthHeight[1] = 150;
warnings.set(1);
}
if(warnings.test(0) && warnings.test(1)) {
notification_alpha = 1.0f;
notification_text.fill(0);
std::strcpy(
notification_text.data(),
"Width set to 200\nHeight set to 150"
);
} else if(warnings.test(0)) {
notification_alpha = 1.0f;
notification_text.fill(0);
std::strcpy(
notification_text.data(),
"Width set to 200"
);
} else if(warnings.test(1)) {
notification_alpha = 1.0f;
notification_text.fill(0);
std::strcpy(
notification_text.data(),
"Height set to 150"
);
}
this->width = inputWidthHeight[0];
this->height = inputWidthHeight[1];
window.setSize(sf::Vector2u(width, height));
sf::View newView(
sf::Vector2f(width / 2.0f, height / 2.0f),
sf::Vector2f(width, height));
window.setView(newView);
drawCache.create(width, height);
drawCacheSprite.setTexture(drawCache.getTexture(), true);
flags.set(7);
currentTri_state = CurrentState::NONE;
return true;
}
int* Tri::State::get_input_width_height() {
return inputWidthHeight;
}
void Tri::State::close_input_width_height_window() {
flags.reset(10);
}

View file

@ -27,6 +27,7 @@ namespace Tri {
* 7 - draw cache dirty * 7 - draw cache dirty
* 8 - draw cache initialized * 8 - draw cache initialized
* 9 - copy color mode * 9 - copy color mode
* 10 - display change size
*/ */
typedef std::bitset<64> BitsetType; typedef std::bitset<64> BitsetType;
BitsetType flags; BitsetType flags;
@ -53,11 +54,13 @@ namespace Tri {
typedef std::array<char, 256> FilenameBufferType; typedef std::array<char, 256> FilenameBufferType;
FilenameBufferType saveFilenameBuffer; FilenameBufferType saveFilenameBuffer;
std::string failedSaveMessage; std::string failedMessage;
sf::RenderTexture drawCache; sf::RenderTexture drawCache;
sf::Sprite drawCacheSprite; sf::Sprite drawCacheSprite;
int inputWidthHeight[2];
public: public:
void handle_events(); void handle_events();
void update(); void update();
@ -80,7 +83,7 @@ namespace Tri {
FilenameBufferType* get_save_filename_buffer(); FilenameBufferType* get_save_filename_buffer();
bool do_save(); bool do_save();
std::string_view failed_save_message() const; std::string_view failed_message() const;
void close_save(); void close_save();
private: private:
@ -91,6 +94,10 @@ namespace Tri {
void close_color_picker(); void close_color_picker();
void close_bg_color_picker(); void close_bg_color_picker();
bool change_width_height();
int* get_input_width_height();
void close_input_width_height_window();
}; };
} }