Skip to content

Commit

Permalink
SelectAction timer fix. Select action waits until either a key is pre…
Browse files Browse the repository at this point in the history
…ssed (non-zero return) or the time is up (returns zero). General cleanup and comments.
  • Loading branch information
Felicivi committed Dec 5, 2023
1 parent ad99169 commit a2ceddc
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 66 deletions.
7 changes: 3 additions & 4 deletions source/Interfaces/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void i_2D::Button::setMFont(const sf::Font &font) {
*/
void i_2D::Button::setPosition(sf::Vector2f pos) {
button.setPosition(pos);

float xPos = (pos.x + button.getGlobalBounds().width / 2) - (text->getLocalBounds().width/2);
float yPos = (pos.y + button.getGlobalBounds().height / 2) - (text->getLocalBounds().height/2);
text->setPosition({xPos, yPos});
Expand All @@ -51,10 +50,10 @@ bool i_2D::Button::isMouseOver(sf::RenderWindow &window) {
float btnPosX = button.getPosition().x;
float btnPosY = button.getPosition().y;

float btnxPosWidth = button.getPosition().x + button.getLocalBounds().width;
float btnyPosHeight = button.getPosition().y + button.getLocalBounds().height;
float btnXPosWidth = button.getPosition().x + button.getLocalBounds().width;
float btnYPosHeight = button.getPosition().y + button.getLocalBounds().height;

if(mouseX < btnxPosWidth && mouseX > btnPosX && mouseY < btnyPosHeight && mouseY > btnPosY){
if(mouseX < btnXPosWidth && mouseX > btnPosX && mouseY < btnYPosHeight && mouseY > btnPosY){
return true;
}
return false;
Expand Down
3 changes: 1 addition & 2 deletions source/Interfaces/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

namespace i_2D
{
class Component : public sf::Drawable
, public sf::Transformable
class Component : public sf::Drawable, public sf::Transformable
{
public:
typedef std::shared_ptr<Component> Ptr;
Expand Down
4 changes: 4 additions & 0 deletions source/Interfaces/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ void i_2D::Inventory::ConstructInventory(sf::Font &font) {
mInventoryWindow->setSize({mWorldSize.x,mWorldSize.y/2});
mInventoryWindow->setFillColor(sf::Color::Black);
mInventoryWindow->setPosition(sf::Vector2f{0.f,50.f});

// Set row & col sizes
if(mWorldSize.x > 1800){
mCol = 5;
}else mCol = 3;
if(mWorldSize.y > 900){
mRow = 4;
}else mRow = 3;

// Create 2d vector of buttons
for(int i = 0; i < mRow; i++) {
std::vector<std::unique_ptr<Button>> v1;
for (int j = 0; j < mCol; j++) {
Expand Down
93 changes: 40 additions & 53 deletions source/Interfaces/MainInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace i_2D {

sf::Clock timer;
float elapsedTime = 0.0f;
bool showReminder = false;

/**
* @brief Constructs a `MainInterface` object.
Expand All @@ -30,11 +29,6 @@ namespace i_2D {
auto a = mWindow.getSize().x;
auto b = mWindow.getSize().y;
mMenu.initialize(mFont, sf::Vector2f{static_cast<float>(a), static_cast<float>(b)});
// mTestButton = std::make_unique<Button>("test",
// sf::Vector2f (200,50),sf::Color::Black,
// sf::Color::White,mFont);
// mTestButton->setPosition(sf::Vector2f (200,50));

ChooseTexture();
}

Expand Down Expand Up @@ -180,42 +174,35 @@ namespace i_2D {
void MainInterface::DrawTimer() {
// Get elapsed time in seconds
elapsedTime = timer.getElapsedTime().asSeconds();
if (mPlayerHasMoved) {
timer.restart();
showReminder = false; // Clear the reminder flag
mPlayerHasMoved = false;

}
// Display timer or reminder based on elapsed time
float testNum = 0.12345;
std::string testStr = std::to_string(testNum);


// Set up font and location
sf::Text timerText(mFont);
timerText.setCharacterSize(24);
timerText.setPosition({750.0f, 75.0f}); // Adjust position as needed


if (elapsedTime <= 5.0f) {
timerText.setString("Time: " + std::to_string(elapsedTime) + " S");
timerText.setFillColor(sf::Color::Blue);
mWindow.draw(timerText);
} else {
timerText.setCharacterSize(34);
timerText.setString("Make a Move!!!");
timerText.setFillColor(sf::Color::Red);
mWindow.draw(timerText);
}
// Create text for current value
timerText.setString("Time: " + std::to_string(elapsedTime) + " S");
timerText.setFillColor(sf::Color::Blue);
mWindow.draw(timerText);
}

void MainInterface::DrawHealthInfo()
{
// Reference health property
int health = GetProperty<int>("Health");

// Set text properties and draw
sf::Text healthText(mFont);
healthText.setCharacterSize(24);
healthText.setPosition({20.0f, 75.0f});
healthText.setFillColor(sf::Color::Green);
healthText.setString("Hp: " + std::to_string(health));
mWindow.draw(healthText);
//TODO fix this
// // Reference health property
// int health = GetProperty<int>("Health");
//
// // Set text properties and draw
// sf::Text healthText(mFont);
// healthText.setCharacterSize(24);
// healthText.setPosition({20.0f, 75.0f});
// healthText.setFillColor(sf::Color::Green);
// healthText.setString("Hp: " + std::to_string(health));
// mWindow.draw(healthText);
}

/**
Expand Down Expand Up @@ -284,22 +271,15 @@ namespace i_2D {
const type_options_t &type_options,
const item_map_t &item_map,
const agent_map_t &agent_map) {
// Initialize action_id and timer
size_t action_id = 0;
timer.restart();

while (mWindow.isOpen()) {
// While the timer is going
while (mWindow.isOpen() && timer.getElapsedTime().asSeconds() < mInputWaitTime) {
sf::Event event;

// if(sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)){
// if(!mTextBox->IsSelected()) {
// mTextBox->SetSelected(true);
// }else{
// mMessageBoard->Send(mTextBox->GetText());
// mTextBox->SetString("");
// mTextBox->SetSelected(false);
// }
// }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
// mTextBox->SetSelected(false);
// }

// Check through all events generated in this frame
while (mWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
mWindow.close();
Expand All @@ -311,8 +291,7 @@ namespace i_2D {
}

} else if (event.type == sf::Event::KeyPressed) {
mPlayerHasMoved = true;
return HandleKeyEvent(event);
action_id = HandleKeyEvent(event);

} else if (event.type == sf::Event::Resized) {
HandleResize(event, grid);
Expand All @@ -323,16 +302,20 @@ namespace i_2D {
} else if (event.type == sf::Event::MouseButtonPressed) {
MouseClickEvent(event);


} else if (event.type == sf::Event::MouseWheelScrolled) {
// HandleScroll(event);
}
}

// Check if a valid action was taken and return that if so
if(action_id != 0)
{
return action_id;
}

// Otherwise update the screen drawing again...
DrawGrid(grid, type_options, item_map, agent_map);

}

// The timer has ended or the window has been closed
return 0;
}

Expand Down Expand Up @@ -397,7 +380,7 @@ namespace i_2D {
break; // The user pressed an unknown key.
}
// If we waited for input, but don't understand it, notify the user.
if (action_id == 0) {
if (action_id == 0 && !mTextBox->IsSelected()) {
std::cout << "Unknown key." << std::endl;
}
// Do the action!
Expand Down Expand Up @@ -559,5 +542,9 @@ namespace i_2D {
renderTexture.draw(cellRect);
}

void MainInterface::setMInputWaitTime(double waitTime) {
MainInterface::mInputWaitTime = waitTime;
}


}
5 changes: 3 additions & 2 deletions source/Interfaces/MainInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ namespace i_2D {
sf::Font mFont; ///< one font for all objects using font
std::unique_ptr<TextBox> mTextBox; /// for chatting and possible event handling by text
std::unique_ptr<MessageBoard> mMessageBoard;
// std::unique_ptr<Button> mTestButton;

// Texture vars
TextureHolder mTextureHolder; ///< for the texture holder
Expand All @@ -67,7 +66,9 @@ namespace i_2D {
int mGridWidth = 0;
int mGridHeight = 0;

bool mPlayerHasMoved = false;
double mInputWaitTime = 0.5f;
public:
void setMInputWaitTime(double mInputWaitTime);

public:

Expand Down
15 changes: 15 additions & 0 deletions source/Interfaces/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ void i_2D::TextBox::SetString(const std::string &s) {
mText << s;
}

/***
* @brief Setter for isSelected, updates the text displayed in the box
* @param sel whether the TextBox is to be set selected or not
*/
void i_2D::TextBox::SetSelected(bool sel) {
isSelected = sel;
if(!sel){
Expand All @@ -48,6 +52,10 @@ void i_2D::TextBox::SetSelected(bool sel) {
}
}

/***
* @brief TypedOn event handler
* @param input the new text that has been typed into the TextBox
*/
void i_2D::TextBox::TypedOn(sf::Event input) {
if(isSelected){
int charTyped = static_cast<int>(input.text.unicode);
Expand All @@ -65,6 +73,10 @@ void i_2D::TextBox::TypedOn(sf::Event input) {
}
}

/***
* @brief Determines what actions to take with different key presses
* @param charTyped The character that has just been entered into the TextBox
*/
void i_2D::TextBox::InputLogic(int charTyped) {
if(isSelected)
{
Expand All @@ -91,6 +103,9 @@ void i_2D::TextBox::InputLogic(int charTyped) {
mTextBox->setString(mText.str() + "_");
}

/**
* @brief removes the last character from the TextBox's current string
*/
void i_2D::TextBox::DeleteLastChar() {
std::string t = mText.str();
std::string newT;
Expand Down
5 changes: 5 additions & 0 deletions source/Interfaces/TextBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ namespace i_2D {
return isSelected;
}

/**
* @brief Checks if a point is within the TextBox
* @param point an xy point to check the location of
* @return True if the point is in bounds, False otherwise
*/
bool Contains(sf::Vector2f point) const {
return mBorderRect.getGlobalBounds().contains(point);
}
Expand Down
10 changes: 5 additions & 5 deletions source/Interfaces/TextureHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ namespace i_2D
const sf::Texture &TextureHolder::GetTexture(std::string id) {
// Find iterator of matching id (avoids copy of unique_ptr)
auto got = textures_.find(id);
// Return dereferenced unique_ptr found at iterator pair
// Return dereference unique_ptr found at iterator pair
return *got->second;
}

/*
/**
* @brief This function loads texture for the maze world images - Default maze
* @return std::map< name, texture> returns the map, key is the name of the texture and values is the actual texture
*/
Expand All @@ -57,7 +57,7 @@ namespace i_2D
textures['@'] = GetTexture("agentTexture");
return textures;
}
/*
/**
* @brief This function loads texture for the second world images group 4
* @return std::map< name, texture> returns the map, key is the name of the texture and values is the actual texture
*/
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace i_2D

return textures;
}
/*
/**
* @brief This function loads texture for the manual world images group 8
* @return std::map< name, texture> returns the map, key is the name of the texture and values is the actual texture
*/
Expand Down Expand Up @@ -124,7 +124,7 @@ namespace i_2D

return textures;
}
/*
/**
* @brief This function loads texture for the generative world images group 6
* @return std::map< name, texture> returns the map, key is the name of the texture and values is the actual texture
*/
Expand Down

0 comments on commit a2ceddc

Please sign in to comment.