Got some kind of jank button and mouse events coming in, now just need to connect them to the GUI to detect button presses and other interaction.

This commit is contained in:
Zed A. Shaw 2024-09-17 11:55:01 -04:00
parent 9741df30ab
commit 37d28094ec
5 changed files with 56 additions and 27 deletions

View file

@ -60,6 +60,12 @@ void SFMLBackend::handle_events() {
window_active_out = !window_active_out;
}
break;
case sf::Event::MouseButtonPressed: {
// rect::contains(x,y) for if mouse is in the button rect
sf::Event::MouseButtonEvent btn = event.mouseButton;
fmt::println("BUTTON: X={}, Y={}", btn.x, btn.y);
break;
}
default:
// do nothing
break;
@ -88,37 +94,34 @@ void SFMLBackend::write_text(int x, int y, string content, float size_mult) {
window.draw(text);
}
sf::RectangleShape SFMLBackend::box(int x, int y, int width, int height,
sf::Color fill, sf::Color outline, int thickness)
{
sf::RectangleShape box(translate(width, height));
box.setPosition(translate(x,y));
box.setOutlineColor(outline);
box.setOutlineThickness(thickness);
box.setFillColor(fill);
window.draw(box);
return box;
}
void SFMLBackend::update_entities() {
window.clear();
sf::RectangleShape face_box(translate(X_ROWS/4, Y_LINES/2));
face_box.setPosition(translate(2,2));
face_box.setOutlineColor(sf::Color(50, 200, 25));
face_box.setOutlineThickness(10);
face_box.setFillColor(sf::Color(200, 250, 200));
window.draw(face_box);
sf::RectangleShape face_box = box(2, 2,
X_ROWS/4, Y_LINES/2, sf::Color(100, 250, 200));
sf::RectangleShape stats_box(translate(X_ROWS - X_ROWS/4 - 5, Y_LINES/2));
stats_box.setPosition(translate(X_ROWS/4 + 4, 2));
stats_box.setOutlineColor(sf::Color(50, 200, 25));
stats_box.setOutlineThickness(10);
stats_box.setFillColor(sf::Color(0, 0, 0));
window.draw(stats_box);
sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2,
X_ROWS - X_ROWS/4 - 5, Y_LINES/2);
constexpr int hp_box_len = 45;
int current_hp = (float(game.hit_points) / float(game.starting_hp)) * float(hp_box_len);
sf::RectangleShape hp_bar(translate(current_hp,2));
hp_bar.setPosition(translate(2,21));
hp_bar.setFillColor(sf::Color(100, 250, 50));
window.draw(hp_bar);
sf::RectangleShape hp_bar = box(2, 21, current_hp, 2,
sf::Color(100, 250, 50));
sf::RectangleShape hp_box(translate(hp_box_len,2));
hp_box.setPosition(translate(2,21));
hp_box.setOutlineColor(sf::Color(100, 200, 50));
hp_box.setOutlineThickness(10);
hp_box.setFillColor(sf::Color::Transparent);
window.draw(hp_box);
sf::RectangleShape hp_box = box(2, 21, hp_box_len, 2);
string status = fmt::format("HP {}\nRounds {}\nStreaks {}\nDeaths {}",
game.hit_points, game.rounds,
@ -127,7 +130,13 @@ void SFMLBackend::update_entities() {
std::time_t t = std::time(nullptr);
string time = fmt::format("{:%r}", fmt::localtime(t));
write_text(X_ROWS/4+1, 14, time, 2.0f);
write_text(2, 14, time, 2.0f);
sf::RectangleShape start_btn = box(27, 16, 8, 3);
write_text(29, 16, "START", 1.0f);
sf::RectangleShape done_btn = box(37, 16, 8, 3);
write_text(39, 16, "DONE", 1.0f);
Window_update();