Skip to content

Commit

Permalink
Merge pull request #6 from JekiTheMonkey/v0.8-alpha-hotfix
Browse files Browse the repository at this point in the history
v0.8-alpha-hotfix
  • Loading branch information
Tonetfal authored Aug 1, 2021
2 parents 4a2d583 + c909f85 commit 3e2b2c2
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 48 deletions.
8 changes: 6 additions & 2 deletions Headers/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Button : public sf::Drawable, public sf::Transformable
void resize();
void clear();

sf::Vector2f getButtonPosition(unsigned idx) const;

static bool parameterIdMatches(LogicalParameter::ID id);


Expand Down Expand Up @@ -73,8 +75,10 @@ class Button : public sf::Drawable, public sf::Transformable
void raiseKey(unsigned idx);
void lowerKey(unsigned idx);

unsigned getKeyCountersWidth(const sf::Text &text, unsigned idx) const;
unsigned getKeyCountersHeight(const sf::Text &text) const;
unsigned getButtonWidth(unsigned idx) const;
unsigned getButtonHeight(unsigned idx) const;
unsigned getKeyCountersWidth(unsigned idx) const;
unsigned getKeyCountersHeight(unsigned idx) const;


private:
Expand Down
1 change: 1 addition & 0 deletions Headers/KPSWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class KPSWindow
private:
void moveWindow();
void calculateWidthCenter(unsigned idx) const;
void openWindow();


private:
Expand Down
6 changes: 5 additions & 1 deletion Headers/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <vector>
#include <string>

class Button;

namespace sf
{
class Event;
Expand All @@ -23,6 +25,7 @@ class Settings
void update();

void setWindowReference(sf::RenderWindow& window);
void setButtonsReference(Button &button);
void buildKeySelector();

void setChangeabilityPosition();
Expand Down Expand Up @@ -95,7 +98,7 @@ class Settings
static sf::Color ButtonTextureColor;

// [Animation graphics]
static int AnimationStyle;
static unsigned AnimationStyle;
static std::string AnimationTexturePath;
static std::size_t AnimationVelocity;
static sf::Vector2f AnimationScale;
Expand Down Expand Up @@ -163,6 +166,7 @@ class Settings

private:
sf::RenderWindow* mWindow;
Button* mButton;
std::unique_ptr<KeySelector> mKeySelector;

static bool mIsButtonSelected;
Expand Down
5 changes: 4 additions & 1 deletion Source/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Application::Application(Menu &menu)
mKPSWindow = std::move(kpsWindow);

mSettings.setWindowReference(mWindow);
mSettings.setButtonsReference(*mButtons);
}

void Application::run()
Expand Down Expand Up @@ -235,7 +236,9 @@ void Application::openWindow()

mWindow.create(sf::VideoMode(getWindowWidth(), getWindowHeight()),
"JKPS", Settings::WindowTitleBar ? sf::Style::Default : sf::Style::None);
mWindow.setPosition(sf::Vector2i(desktop.width / 2 - mWindow.getSize().x / 2, desktop.height / 2 - mWindow.getSize().y / 2));
mWindow.setPosition(sf::Vector2i(
desktop.width / 2 - mWindow.getSize().x / 2,
desktop.height / 2 - mWindow.getSize().y / 2));
mWindow.setKeyRepeatEnabled(false);
mWindow.setFramerateLimit(60);
}
Expand Down
61 changes: 35 additions & 26 deletions Source/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ void Button::highlightKey(int buttonIndex)

void Button::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
states.transform.translate(Settings::WindowBonusSizeLeft, Settings::WindowBonusSizeTop);

for (auto& elem : mButtonsSprite)
target.draw(*elem, states);

Expand Down Expand Up @@ -187,7 +185,11 @@ bool Button::parameterIdMatches(LogicalParameter::ID id)
id == LogicalParameter::ID::AnimGfxScl ||
id == LogicalParameter::ID::AnimGfxClr ||
id == LogicalParameter::ID::AnimGfxOffset ||
id == LogicalParameter::ID::OtherHighText;
id == LogicalParameter::ID::OtherHighText ||
id == LogicalParameter::ID::MainWndwTop ||
id == LogicalParameter::ID::MainWndwBot ||
id == LogicalParameter::ID::MainWndwLft ||
id == LogicalParameter::ID::MainWndwRght;
}

void Button::setTextures(std::vector<std::unique_ptr<sf::Sprite>>& vector, sf::Texture& texture)
Expand Down Expand Up @@ -217,13 +219,11 @@ void Button::centerOrigin(std::vector<std::unique_ptr<sf::Sprite>>& vector)

void Button::setButtonPositions(std::vector<std::unique_ptr<sf::Sprite>>& vector)
{
for (size_t i = 0; i < vector.size(); ++i)
unsigned i = 0;
for (auto &elem : vector)
{
sf::Vector2f size(Settings::ButtonTextureSize);
sf::Vector2f position(
Settings::ButtonDistance * i + size.x * i + size.x / 2,
size.y / 2);
vector[i]->setPosition(position);
elem->setPosition(getButtonWidth(i), getButtonHeight(i));
++i;
}
}

Expand Down Expand Up @@ -298,7 +298,7 @@ void Button::setupKeyCounterText(sf::Text &text, unsigned idx)
void Button::setupTextPosition(sf::Text &text, unsigned idx)
{
text.setOrigin(getCenterOriginText(text));
text.setPosition(getKeyCountersWidth(text, idx), getKeyCountersHeight(text) + mButtonsYOffset[idx]);
text.setPosition(getKeyCountersWidth(idx), getKeyCountersHeight(idx));
}

bool Button::isTextTooBig(const sf::Text &text) const
Expand Down Expand Up @@ -433,8 +433,7 @@ void Button::raiseKey(unsigned idx)
sf::Sprite &btnSprite = *mButtonsSprite[idx];
sf::Text &btnText = *mButtonsText[idx];

const float buttonHeight(mButtonsSprite[0]->getGlobalBounds().height / 2.f);
const float counterHeight(getKeyCountersHeight(*mButtonsText[0]));
const float buttonHeight(getButtonHeight(idx));

sf::Vector2f spritePos = btnSprite.getPosition();
float step = Settings::AnimationOffset / Settings::AnimationVelocity;
Expand All @@ -450,7 +449,7 @@ void Button::raiseKey(unsigned idx)
{
btnYOffset = 0;
btnSprite.setPosition(spritePos.x, buttonHeight);
btnText.setPosition(btnText.getPosition().x, counterHeight);
btnText.setPosition(btnText.getPosition().x, buttonHeight);
}
}

Expand All @@ -460,8 +459,7 @@ void Button::lowerKey(unsigned idx)
sf::Sprite &btnSprite = *mButtonsSprite[idx];
sf::Text &btnText = *mButtonsText[idx];

const float buttonLoweredHeight(mButtonsSprite[0]->getGlobalBounds().height / 2.f + Settings::AnimationOffset);
const float counterLoweredHeight(getKeyCountersHeight(*mButtonsText[0]) + Settings::AnimationOffset);
const float buttonLoweredHeight(getButtonHeight(idx) + Settings::AnimationOffset);

if (btnSprite.getPosition().y != buttonLoweredHeight)
{
Expand All @@ -471,22 +469,25 @@ void Button::lowerKey(unsigned idx)
}
}

unsigned int Button::getKeyCountersWidth(const sf::Text &text, unsigned idx) const
unsigned Button::getButtonWidth(unsigned idx) const
{
const sf::Text &elem(*mButtonsText[idx]);
unsigned int buttonCenterX =
Settings::ButtonDistance * idx +
Settings::ButtonTextureSize.x * (idx + 1) -
Settings::ButtonTextureSize.x / 2U;
return (Settings::ButtonDistance + Settings::ButtonTextureSize.x) * idx
+ Settings::WindowBonusSizeLeft + Settings::ButtonTextureSize.x / 2;
}

return buttonCenterX + Settings::KeyCounterPosition.x;
unsigned Button::getButtonHeight(unsigned idx) const
{
return Settings::WindowBonusSizeTop + Settings::ButtonTextureSize.y / 2;
}

unsigned int Button::getKeyCountersHeight(const sf::Text &text) const
unsigned int Button::getKeyCountersWidth(unsigned idx) const
{
unsigned int buttonCenterY = Settings::ButtonTextureSize.y / 2U;
return (Settings::ButtonDistance + Settings::ButtonTextureSize.x) * idx + Settings::WindowBonusSizeLeft + Settings::ButtonTextureSize.x / 2.f;
}

return buttonCenterY - Settings::KeyCounterPosition.y;
unsigned int Button::getKeyCountersHeight(unsigned idx) const
{
return Settings::WindowBonusSizeTop + mButtonsYOffset[idx] + Settings::ButtonTextureSize.y / 2.f;
}

sf::Vector2f Button::getCenterOriginText(const sf::Text &text) const
Expand All @@ -510,4 +511,12 @@ bool Button::isBeyondDefaultScale(const sf::Sprite &sprite) const
|| (scaleAmountPerFrame.y > 0 ?
defaultScale.y < spriteScale.y :
defaultScale.y > spriteScale.y);
}
}

sf::Vector2f Button::getButtonPosition(unsigned idx) const
{
assert(Settings::ButtonAmount >= idx);
sf::Vector2f position = mButtonsSprite[idx]->getPosition();

return position;
}
17 changes: 13 additions & 4 deletions Source/KPSWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ KPSWindow::KPSWindow(const FontHolder &fonts)
mKPSNumber.setString("0");
if (Settings::KPSWindowEnabledFromStart)
{
mWindow.create(sf::VideoMode(Settings::KPSWindowSize.x,
Settings::KPSWindowSize.y), "KPS Window", sf::Style::Close);
openWindow();
}

setupText();
Expand All @@ -34,8 +33,7 @@ void KPSWindow::handleEvent(sf::Event event)
{
if (!mWindow.isOpen())
{
mWindow.create(sf::VideoMode(Settings::KPSWindowSize.x,
Settings::KPSWindowSize.y), "KPS Window", sf::Style::Close);
openWindow();
}
else
{
Expand Down Expand Up @@ -117,3 +115,14 @@ bool KPSWindow::parameterIdMatches(LogicalParameter::ID id)
id == LogicalParameter::ID::KPSWndwTopPadding ||
id == LogicalParameter::ID::KPSWndwDistBtw;
}

void KPSWindow::openWindow()
{
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();

mWindow.create(sf::VideoMode(Settings::KPSWindowSize.x,
Settings::KPSWindowSize.y), "KPS Window", sf::Style::Close);
mWindow.setPosition(sf::Vector2i(
desktop.width / 1.5 - mWindow.getSize().x / 2,
desktop.height / 2 - mWindow.getSize().y / 2));
}
2 changes: 1 addition & 1 deletion Source/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <SFML/System/Clock.hpp>


std::string Menu::mProgramVersion("v0.8-alpha");
std::string Menu::mProgramVersion("v0.8-alpha-hotfix");

Menu::Menu()
: mScrollSpeed(40.f)
Expand Down
18 changes: 11 additions & 7 deletions Source/ParameterLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void ParameterLine::handleValueModEvent(sf::Event event)
return;

std::string str(mSelectedValue->mValText.getString());
sf::Keyboard::Key key = event.key.code;
bool isStrType = mType == LogicalParameter::Type::String || mType == LogicalParameter::Type::StringPath;
int btnIdx = 0;
if (mType == LogicalParameter::Type::Color
Expand All @@ -93,12 +94,16 @@ void ParameterLine::handleValueModEvent(sf::Event event)
assert(btnIdx != -1);
str = mParameter->getValPt(btnIdx);
}
sf::Keyboard::Key key = event.key.code;


if (!isStrType && key >= sf::Keyboard::Num0 && key <= sf::Keyboard::Num9)
if (!isStrType
&& ((key >= sf::Keyboard::Num0 && key <= sf::Keyboard::Num9)
|| (key >= sf::Keyboard::Numpad0 && key <= sf::Keyboard::Numpad9)))
{
int n = (key - sf::Keyboard::Num0);
int n = 0;
if (key >= sf::Keyboard::Num0 && key <= sf::Keyboard::Num9)
n = (key - sf::Keyboard::Num0);
if (key >= sf::Keyboard::Numpad0 && key <= sf::Keyboard::Numpad9)
n = (key - sf::Keyboard::Numpad0);

if (mSelectedValueIndex == 0 && (str[0] == '-' || n == 0))
return;
Expand Down Expand Up @@ -150,8 +155,7 @@ void ParameterLine::handleValueModEvent(sf::Event event)

if (!isStrType && key == sf::Keyboard::Hyphen)
{
if (mType != LogicalParameter::Type::Unsigned && mType != LogicalParameter::Type::Color
&& mParameter->mLowLimits >= 0)
if (mType == LogicalParameter::Type::Unsigned || mType == LogicalParameter::Type::Color)
return;

if (str[0] != '-')
Expand Down Expand Up @@ -309,7 +313,7 @@ void ParameterLine::handleButtonsInteractionEvent(sf::Event event)
}
}

if ((event.type == sf::Event::KeyPressed && key == sf::Keyboard::Escape)
if ((event.type == sf::Event::KeyPressed && (key == sf::Keyboard::Escape || key == sf::Keyboard::Enter))
|| (event.type == sf::Event::MouseButtonPressed && button == sf::Mouse::Right))
{
deselect();
Expand Down
19 changes: 13 additions & 6 deletions Source/Settings.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../Headers/Settings.hpp"
#include "../Headers/ConfigHelper.hpp"
#include "../Headers/DefaultFiles.hpp"
#include "../Headers/Button.hpp"

#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
Expand Down Expand Up @@ -54,7 +55,7 @@ sf::Vector2u Settings::ButtonTextureSize(0, 0);
sf::Color Settings::ButtonTextureColor(sf::Color::Transparent);

// [Animation graphics]
int Settings::AnimationStyle(-1);
unsigned Settings::AnimationStyle(0);
std::string Settings::AnimationTexturePath("none");
std::size_t Settings::AnimationVelocity(0);
sf::Vector2f Settings::AnimationScale(-1.f, -1.f);
Expand Down Expand Up @@ -132,6 +133,7 @@ bool Settings::mReloadAssetsRequest(false);

Settings::Settings()
: mWindow(nullptr)
, mButton(nullptr)
{
}

Expand Down Expand Up @@ -257,12 +259,12 @@ bool Settings::isPressPerformedOnButton(unsigned &buttonIndex)

bool Settings::isMouseInRange(unsigned index)
{
sf::Vector2i mousePosition(sf::Mouse::getPosition(*mWindow));
const sf::Vector2i mousePosition(sf::Mouse::getPosition(*mWindow));
const sf::Vector2f textureSize = static_cast<sf::Vector2f>(Settings::ButtonTextureSize);
const sf::Vector2f buttonPosition = mButton->getButtonPosition(index) - textureSize / 2.f;
const sf::FloatRect buttonRectangle(buttonPosition, textureSize);

return mousePosition.x > ButtonDistance * (index + 1) + ButtonTextureSize.x * index
&& mousePosition.x < ButtonDistance * (index + 1) + ButtonTextureSize.x * (index + 1)
&& mousePosition.y > ButtonDistance
&& mousePosition.y < ButtonDistance + ButtonTextureSize.y;
return buttonRectangle.contains(static_cast<sf::Vector2f>(mousePosition));
}

void Settings::setWindowReference(sf::RenderWindow& window)
Expand All @@ -271,6 +273,11 @@ void Settings::setWindowReference(sf::RenderWindow& window)
mKeySelector->setMainWindowPointer(mWindow);
}

void Settings::setButtonsReference(Button &button)
{
mButton = &button;
}

void Settings::buildKeySelector()
{
std::unique_ptr<KeySelector> keySelector(new KeySelector);
Expand Down

0 comments on commit 3e2b2c2

Please sign in to comment.