Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed variables to be more fitting. fixed bugs and logic in the tex… #114

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions source/Interfaces/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace i_2D {
* @param font The font used by the inventory list
*/
void Inventory::ConstructInventory(sf::Font &font, const std::vector<std::string> &interfaceAgentInventory) {
mInventoryWindow = std::make_unique<sf::RectangleShape>();
mInventoryWindow->setSize({mWorldSize.x, mWorldSize.y / 2});
mInventoryWindow->setFillColor(sf::Color::Black);
mInventoryWindow->setPosition(sf::Vector2f{0.f, 50.f});
mCanvas = std::make_unique<sf::RectangleShape>();
mCanvas->setSize({mWorldSize.x, mWorldSize.y / 2});
mCanvas->setFillColor(sf::Color::Black);
mCanvas->setPosition(sf::Vector2f{0.f, 50.f});

mInventoryItems.clear();
mInventoryItems.resize(interfaceAgentInventory.size());
std::copy(interfaceAgentInventory.begin(), interfaceAgentInventory.end(), mInventoryItems.begin());
mItems.clear();
mItems.resize(interfaceAgentInventory.size());
std::ranges::copy(interfaceAgentInventory,mItems.begin());

// Set row & col sizes
if (mWorldSize.x > 1800) {
Expand All @@ -38,16 +38,16 @@ namespace i_2D {
v1.push_back(std::make_unique<Button>(
"", sf::Vector2f{(mWorldSize.x) / mCol, (mWorldSize.y / 2 - 50) / mRow},
sf::Color::Black, sf::Color::White, font));
if (Index < mInventoryItems.size()) {
v1[j]->SetString(mInventoryItems[Index]);
if (Index < mItems.size()) {
v1[j]->SetString(mItems[Index]);
++Index;
} else {
v1[j]->SetString("empty");
}
v1[j]->SetPosition(sf::Vector2f{j * (mWorldSize.x / mCol),
mWorldSize.y / 2 + 50 + i * (mWorldSize.y / 2 - 50) / mRow});
}
mInventoryList.push_back(std::move(v1));
mListOfButtons.push_back(std::move(v1));
}
}

Expand All @@ -56,8 +56,8 @@ namespace i_2D {
* @param window The render window of the world
*/
void Inventory::DrawTo(sf::RenderWindow &window) {
window.draw(*mInventoryWindow);
for (const auto &x: mInventoryList) {
window.draw(*mCanvas);
for (const auto &x: mListOfButtons) {
for (const auto &y: x) {
y->DrawTo(window);
}
Expand All @@ -77,20 +77,20 @@ namespace i_2D {
*/
std::string Inventory::HandleMouseMove(sf::RenderWindow &window) {
std::string s1 = "null";
for(size_t i = 0; i < mInventoryList.size(); ++i){
for(size_t j = 0; j < mInventoryList.size(); ++j){
if(mInventoryList[i][j]->IsMouseOver(window)){
mInventoryList[i][j]->SetBackColor(sf::Color::Magenta);
if(i*mCol+j < mInventoryItems.size()){
s1 = (mInventoryItems[i*mCol+j]);
for(int i = 0; i < mListOfButtons.size(); ++i){
for(int j = 0; j < mListOfButtons.size(); ++j){
if(mListOfButtons[i][j]->IsMouseOver(window)){
mListOfButtons[i][j]->SetBackColor(sf::Color::Magenta);
if(i*mCol+j < mItems.size()){
s1 = (mItems[i*mCol+j]);
if(s1 == "Boots"){
s1 = s1.substr(0,s1.size()-1);
}
s1[0] = tolower(s1[0]);
s1 += "Texture";
}
}else{
mInventoryList[i][j]->SetBackColor(sf::Color::Black);
mListOfButtons[i][j]->SetBackColor(sf::Color::Black);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions source/Interfaces/Inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ namespace i_2D {
class Inventory {
private:
/// Projected inventory screen
std::unique_ptr<sf::RectangleShape> mInventoryWindow;
std::unique_ptr<sf::RectangleShape> mCanvas;
/// 2D array of buttons representing items
std::vector<std::vector<std::unique_ptr<Button>>> mInventoryList;
std::vector<std::vector<std::unique_ptr<Button>>> mListOfButtons;
sf::Vector2f mWorldSize; ///< Size of the world
int mCol=0; ///< Number of columns enumerating the inventory list
int mRow=0; ///< Number of rows enumerating the inventory list

std::vector<std::string> mInventoryItems; ///< List of items in an inventory
std::vector<std::string> mItems; ///< List of items in an inventory
std::unique_ptr<sf::Texture> mItemDisplay; ///< The image of the items currently on display

public:
Expand Down
10 changes: 5 additions & 5 deletions source/Interfaces/MainInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace i_2D {
mTextBox = std::make_unique<TextBox>(mFont);
auto a = mWindow.getSize().x;
auto b = mWindow.getSize().y;
mMenu.initialize(mFont, sf::Vector2f{static_cast<float>(a), static_cast<float>(b)});
mMenu.Initialize(mFont, sf::Vector2f{static_cast<float>(a), static_cast<float>(b)});
ChooseTexture();
}

Expand Down Expand Up @@ -180,7 +180,7 @@ namespace i_2D {
// Display everything
mTextBox->DrawTo(mWindow);
mMessageBoard->DrawTo(mWindow);
mMenu.drawto(mWindow);
mMenu.DrawTo(mWindow);
mWindow.display();
}

Expand Down Expand Up @@ -314,9 +314,9 @@ namespace i_2D {
HandleResize(event, grid);

} else if (event.type == sf::Event::MouseMoved) {
auto c = mMenu.HandleMouseMove(mWindow);
if(c!="null"){
auto texture = mTextureHolder.GetTexture(c);
auto textureName = mMenu.HandleMouseMove(mWindow);
if(textureName!="null"){
auto texture = mTextureHolder.GetTexture(textureName);
mMenu.SetInventoryItemDisplay(texture);
}
} else if (event.type == sf::Event::MouseButtonPressed) {
Expand Down
4 changes: 2 additions & 2 deletions source/Interfaces/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace i_2D {
/**
* @brief initialize the buttons at the top of the window
*/
void Menu::initialize(sf::Font &font, sf::Vector2f size) {
void Menu::Initialize(sf::Font &font, sf::Vector2f size) {
sf::Color backgroundcolor = sf::Color::Black;
sf::Color textcolor = sf::Color::White;
mFont = &font;
Expand Down Expand Up @@ -43,7 +43,7 @@ namespace i_2D {
*
* @param window the main window of the graphic interface
*/
void Menu::drawto(sf::RenderWindow &window) {
void Menu::DrawTo(sf::RenderWindow &window) {
for( const auto &button : mMenuBar){
button->DrawTo(window);
}
Expand Down
4 changes: 2 additions & 2 deletions source/Interfaces/Menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace i_2D {
void SetInventoryItemDisplay(sf::Texture &texture){
if(mInventory) mInventory->SetItemDisplay(texture);
}
void initialize(sf::Font &font,sf::Vector2f size);
void drawto(sf::RenderWindow &window);
void Initialize(sf::Font &font,sf::Vector2f size);
void DrawTo(sf::RenderWindow &window);
std::string HandleMouseMove(sf::RenderWindow &window);
void HandleMouseButtonPressed(sf::RenderWindow &window,
const std::vector<std::string> &interfaceAgentInventory);
Expand Down
56 changes: 21 additions & 35 deletions source/Interfaces/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace i_2D {
*/
TextBox::TextBox(const sf::Font &font, int size, sf::Color color, bool sel) {

mTextBox = std::make_unique<sf::Text>(font);
mTextBox->setCharacterSize(size);
mTextBox->setFillColor(color);
mTextBox->setPosition({10, 650});
mText = std::make_unique<sf::Text>(font);
mText->setCharacterSize(size);
mText->setFillColor(color);
mText->setPosition({10, 650});
isSelected = sel;
if (!isSelected)
mTextBox->setString("Press Click!");
mText->setString("Press Click!");

}

Expand All @@ -35,9 +35,9 @@ namespace i_2D {
* @param s The string to be set to
*/
void TextBox::SetString(const std::string &s) {
mTextBox->setString(s);
mText.str("");
mText << s;
mText->setString(s);
mStreamText.str("");
mStreamText << s;
}

/**
Expand All @@ -47,12 +47,12 @@ namespace i_2D {
void TextBox::SetSelected(bool sel) {
isSelected = sel;
if (!sel) {
std::string t = mText.str();
std::string t = mStreamText.str();
std::string newT;
for (char i: t) {
newT += i;
}
mTextBox->setString((newT));
mText->setString((newT));
}
}

Expand All @@ -65,9 +65,9 @@ namespace i_2D {
int charTyped = static_cast<int>(input.text.unicode);
if (charTyped < 128) {
if (hasLimit) {
if (mText.str().length() <= MAX_CHAR) {
if (static_cast<int>(mStreamText.str().length()) < limit) {
InputLogic(charTyped);
} else if (mText.str().length() > MAX_CHAR && charTyped == DELETE_KEY) {
} else if (static_cast<int>(mStreamText.str().length()) >= limit && charTyped == DELETE_KEY) {
DeleteLastChar();
}
} else {
Expand All @@ -84,39 +84,25 @@ namespace i_2D {
void TextBox::InputLogic(int charTyped) {
if (isSelected) {
if (charTyped != DELETE_KEY && charTyped != ENTER_KEY && charTyped != ESCAPE_KEY) {
if (mText.str().length() < MAX_CHAR) {
mText << static_cast<char>(charTyped);
} else if (mText.str().length() >= MAX_CHAR) {
hasLimit = true;
}
} else if (charTyped == ENTER_KEY) {
// Handle line breaks
if (mText.str().length() < MAX_CHAR) {
mText << '\n';
hasLimit = false;
}
mStreamText << static_cast<char>(charTyped);
} else if (charTyped == DELETE_KEY) {
if (mText.str().length() > 0) {
if (mStreamText.str().length() > 0) {
DeleteLastChar();
}
}
}

mTextBox->setString(mText.str() + "_");
mText->setString(mStreamText.str() + "_");
}

/**
* @brief removes the last character from the TextBox's current string
*/
void TextBox::DeleteLastChar() {
std::string t = mText.str();
std::string newT;
for (unsigned int i = 0; i < t.length() - 1; i++) {
newT += t[i];
}
mText.str("");
mText << newT;
mTextBox->setString(mText.str());
std::string t = mStreamText.str();
mStreamText.str("");
mStreamText << t.substr(0,t.size()-1);
mText->setString(mStreamText.str());
}

/**
Expand All @@ -130,13 +116,13 @@ namespace i_2D {
//Subtracts the vector (5, 5) from the position of mTextBox. This creates a new position
// slightly to the left and up from the original position, effectively creating a margin.

mBorderRect.setPosition(mTextBox->getPosition() - sf::Vector2f(5, 5));
mBorderRect.setPosition(mText->getPosition() - sf::Vector2f(5, 5));
mBorderRect.setFillColor(sf::Color(200, 200, 200));
mBorderRect.setOutlineThickness(2.0f); // thickness of the border
mBorderRect.setOutlineColor(sf::Color::Black);

window.draw(mBorderRect);
window.draw(*mTextBox);
window.draw(*mText);

}

Expand Down
18 changes: 8 additions & 10 deletions source/Interfaces/TextBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ namespace i_2D {

private:
/// The string of the text
std::unique_ptr<sf::Text> mTextBox;
std::ostringstream mText; ///< Use to take in string
std::unique_ptr<sf::Text> mText;
std::ostringstream mStreamText; ///< Use to take in string
bool isSelected = false; /// Flag for checking text mode
bool hasLimit = false; /// Flag for checking limit mode
int limit = 10000; /// The limit of characters allowed
const size_t MAX_CHAR = 60; ///< max character per line in the textbox
bool hasLimit = true; /// Flag for checking limit mode
int limit = 60; /// The limit of characters allowed
// Draw the border around the TextBox
sf::RectangleShape mBorderRect;

Expand All @@ -50,7 +49,7 @@ namespace i_2D {
* @param font The font to be set to
*/
void SetFont(const sf::Font &font) {
mTextBox->setFont(font);
mText->setFont(font);
}

/**
Expand All @@ -59,7 +58,7 @@ namespace i_2D {
* @param pos The position to be set to
*/
void SetPosition(sf::Vector2f pos) {
mTextBox->setPosition(pos);
mText->setPosition(pos);
}

/**
Expand All @@ -79,7 +78,7 @@ namespace i_2D {
*/
void SetLimit(bool ToF, int lim) {
hasLimit = ToF;
limit = lim - 1;
limit = lim;
}

void SetSelected(bool sel);
Expand All @@ -90,7 +89,7 @@ namespace i_2D {
* @return Return the string
*/
std::string GetText() {
return mText.str();
return mStreamText.str();
}

void DrawTo(sf::RenderWindow &window);
Expand All @@ -115,7 +114,6 @@ namespace i_2D {
return mBorderRect.getGlobalBounds().contains(point);
}


};
}