From 122d30a980c2d436800043721860940dcd298c5c Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sun, 3 Oct 2021 11:47:05 +0700 Subject: [PATCH] struct Button -> class Button --- Player.cpp | 4 +-- engine/Camera.cpp | 2 +- engine/Engine.cpp | 6 ++-- engine/Keyboard.h | 4 +-- engine/Mesh.h | 2 +- engine/Mouse.h | 4 +-- engine/gui/Button.cpp | 62 +++++++++++++++++++++--------------- engine/gui/Button.h | 59 +++++++++++++++++++++------------- engine/gui/Window.cpp | 34 ++++++++++---------- engine/gui/Window.h | 18 +++++------ engine/network/ServerUDP.h | 2 +- engine/physics/RigidBody.cpp | 14 ++++---- engine/physics/RigidBody.h | 5 ++- engine/utils/Matrix4x4.cpp | 4 +-- 14 files changed, 121 insertions(+), 99 deletions(-) diff --git a/Player.cpp b/Player.cpp index 0088517..82735c0 100755 --- a/Player.cpp +++ b/Player.cpp @@ -90,7 +90,7 @@ void Player::nextWeapon() { _removeWeaponCallBack(_weapons[_selectedWeapon]); _selectedWeapon = (_selectedWeapon + 1) % _weapons.size(); _addWeaponCallBack(_weapons[_selectedWeapon]); - Log::log("selected _selectedWeapon " + std::to_string(_selectedWeapon)); + Log::log("_selected _selectedWeapon " + std::to_string(_selectedWeapon)); _changeWeaponSound.play(); } } @@ -104,7 +104,7 @@ void Player::previousWeapon() { else _selectedWeapon = _weapons.size() - 1; _addWeaponCallBack(_weapons[_selectedWeapon]); - Log::log("selected _selectedWeapon " + std::to_string(_selectedWeapon)); + Log::log("_selected _selectedWeapon " + std::to_string(_selectedWeapon)); _changeWeaponSound.play(); } } diff --git a/engine/Camera.cpp b/engine/Camera.cpp index b26bdf1..d9265ab 100755 --- a/engine/Camera.cpp +++ b/engine/Camera.cpp @@ -101,7 +101,7 @@ void Camera::init(int width, int height, double fov, double ZNear, double ZFar) std::vector &Camera::sorted() { - // Sort _tris from back to front + // Sort _tris from _back to front // This is some replacement for Z-buffer std::sort(_triangles.begin(), _triangles.end(), [](Triangle &t1, Triangle &t2) { diff --git a/engine/Engine.cpp b/engine/Engine.cpp index 86f1064..a822cf9 100755 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -24,7 +24,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, screen->open(screenWidth, screenHeight, name, verticalSync, background, style); screen->attachMouse(mouse); - Log::log("Engine::create(): started engine (" + std::to_string(screenWidth) + " x " + std::to_string(screenHeight) + ") with title '" + name + "'."); + Log::log("Engine::create(): started engine (" + std::to_string(screenWidth) + " _x " + std::to_string(screenHeight) + ") with title '" + name + "'."); Time::update(); start(); @@ -74,7 +74,7 @@ void Engine::exit() { screen->close(); } ResourceManager::unloadAllResources(); - Log::log("Engine::exit(): exit engine (" + std::to_string(screen->width()) + " x " + std::to_string(screen->height()) + ") with title '" + + Log::log("Engine::exit(): exit engine (" + std::to_string(screen->width()) + " _x " + std::to_string(screen->height()) + ") with title '" + screen->title() + "'."); } @@ -84,7 +84,7 @@ void Engine::printDebugText() const { std::to_string((camera->position().x())) + "\n Y: " + std::to_string((camera->position().y())) + "\n Z: " + std::to_string((camera->position().z())) + "\n\n" + - std::to_string(screen->width()) + "x" + + std::to_string(screen->width()) + "_x" + std::to_string(screen->height()) + "\n" + std::to_string(Time::fps()) + " fps \n" + std::to_string((int) _triPerSec) + " _tris/s"); diff --git a/engine/Keyboard.h b/engine/Keyboard.h index 897b4ab..b5b96ef 100644 --- a/engine/Keyboard.h +++ b/engine/Keyboard.h @@ -13,8 +13,8 @@ private: public: Keyboard() = default; - static bool isKeyPressed(sf::Keyboard::Key key); // returns true if this key is pressed - bool isKeyTapped(sf::Keyboard::Key key); // returns true if this key is tapped and 1/5 sec passed (button bouncing problem solved) + static bool isKeyPressed(sf::Keyboard::Key key); // returns true if this key is _pressed + bool isKeyTapped(sf::Keyboard::Key key); // returns true if this key is tapped and 1/5 sec passed (_button bouncing problem solved) }; diff --git a/engine/Mesh.h b/engine/Mesh.h index 006f121..4d29e8c 100755 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -36,7 +36,7 @@ public: void rotate(const Point4D& r) override; // Rotate body around normalised vector 'v' by 'r' radians void rotate(const Point4D& v, double r) override; - // Rotate body around XYZ by (r.x, r.y, r.z) radians relative val 'point4D' + // Rotate body around XYZ by (r._x, r._y, r.z) radians relative val 'point4D' void rotateRelativePoint(const Point4D& point4D, const Point4D& r) override; // Rotate body around normalised vector 'v' by 'r' radians relative val 'point4D' void rotateRelativePoint(const Point4D& point4D, const Point4D& v, double r) override; diff --git a/engine/Mouse.h b/engine/Mouse.h index 2b43053..81d7cea 100644 --- a/engine/Mouse.h +++ b/engine/Mouse.h @@ -18,8 +18,8 @@ public: void setWindow(std::shared_ptr window); - static bool isButtonPressed(sf::Mouse::Button button); // returns true if this button is pressed - bool isButtonTapped(sf::Mouse::Button button); // returns true if this button is tapped and 1/5 sec passed (button bouncing problem solved) + static bool isButtonPressed(sf::Mouse::Button button); // returns true if this _button is _pressed + bool isButtonTapped(sf::Mouse::Button button); // returns true if this _button is tapped and 1/5 sec passed (_button bouncing problem solved) [[nodiscard]] Point4D getMousePosition() const; [[nodiscard]] Point4D getMouseDisplacement() const; diff --git a/engine/gui/Button.cpp b/engine/gui/Button.cpp index 3548669..b9de7b8 100755 --- a/engine/gui/Button.cpp +++ b/engine/gui/Button.cpp @@ -3,56 +3,66 @@ // #include "Button.h" + +#include #include "../ResourceManager.h" void Button::select() { - if (!selected && !pressed) + if (!_selected && !_pressed) { - button.setTextureRect(sf::IntRect(selectedState.tx, selectedState.ty, w, h)); - selected = true; + _button.setTextureRect(sf::IntRect(_selectedState.tx, _selectedState.ty, _w, _h)); + _selected = true; } } void Button::unSelect() { - if (selected && !pressed) + if (_selected && !_pressed) { - button.setTextureRect(sf::IntRect(usualState.tx, usualState.ty, w, h)); - selected = false; + _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); + _selected = false; } } void Button::press() { - if (!pressed) + if (!_pressed) { - button.setTextureRect(sf::IntRect(pressedState.tx, pressedState.ty, w, h)); - if(checkBox) - pressed = true; - clickSound.play(); - click(); + _button.setTextureRect(sf::IntRect(_pressedState.tx, _pressedState.ty, _w, _h)); + if(_checkBox) + _pressed = true; + _clickSound.play(); + _click(); } else { - button.setTextureRect(sf::IntRect(usualState.tx, usualState.ty, w, h)); - if(checkBox) - pressed = false; + _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); + if(_checkBox) + _pressed = false; } } void Button::init() { - button.setTexture(*ResourceManager::loadTexture(s_texture)); - button.setTextureRect(sf::IntRect(usualState.tx, usualState.ty, w, h)); - button.scale(sx, sy); - button.setPosition(x - w*sx/2, y - h*sy/2); + _button.setTexture(*ResourceManager::loadTexture(_texture)); + _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); + _button.scale(_sx, _sy); + _button.setPosition(_x - _w * _sx / 2, _y - _h * _sy / 2); - text.setFont(*ResourceManager::loadFont(s_font)); - text.setString(s_text); - text.setCharacterSize(h*sy/2); - text.setFillColor(textColor); - text.setPosition(x - text.getLocalBounds().width/2, y - h*sy/2 + text.getLocalBounds().height/4); + _text.setFont(*ResourceManager::loadFont(_font)); + _text.setString(_textString); + _text.setCharacterSize(_h * _sy / 2); + _text.setFillColor(_textColor); + _text.setPosition(_x - _text.getLocalBounds().width / 2, _y - _h * _sy / 2 + _text.getLocalBounds().height / 4); + + _clickSound.setBuffer(*ResourceManager::loadSoundBuffer(_clickSoundName)); + _clickSound.setVolume(15); +} + +Button::Button(int x, int y, int width, int height, std::function click, std::string text, double sx, + double sy, std::string texture, tPos usualState, tPos selectedState, tPos pressedState, + std::string font, sf::Color textColor, std::string clickSound) : _x(x), _y(y), _w(width), _h(height), _click(std::move(click)), + _textString(std::move(text)), _sx(sx), _sy(sy), _texture(std::move(texture)), _usualState(usualState), _selectedState(selectedState), _pressedState(pressedState), + _font(std::move(font)), _textColor(textColor), _clickSoundName(std::move(clickSound)){ - clickSound.setBuffer(*ResourceManager::loadSoundBuffer(s_clickSound)); - clickSound.setVolume(15); } diff --git a/engine/gui/Button.h b/engine/gui/Button.h index 1422ed1..bd04b51 100755 --- a/engine/gui/Button.h +++ b/engine/gui/Button.h @@ -14,44 +14,57 @@ struct tPos { int ty; }; -struct Button -{ - int x; - int y; +class Button { +private: + int _x{}; + int _y{}; - int w; - int h; + int _w{}; + int _h{}; - std::function click; + std::function _click; - std::string s_text; + std::string _textString; - double sx; - double sy; + double _sx{}; + double _sy{}; - std::string s_texture; - tPos usualState; - tPos selectedState; - tPos pressedState; + std::string _texture; + tPos _usualState{}; + tPos _selectedState{}; + tPos _pressedState{}; - std::string s_font; - sf::Color textColor; + std::string _font; + sf::Color _textColor; - std::string s_clickSound; + std::string _clickSoundName; - sf::Sprite button; - sf::Text text; - sf::Sound clickSound; + sf::Sprite _button; + sf::Text _text; + sf::Sound _clickSound; - bool selected = false; - bool pressed = false; - bool checkBox = false; + bool _selected = false; + bool _pressed = false; + bool _checkBox = false; + +public: + Button() = default; + Button(int x, int y, int width, int height, std::function click, std::string text, double sx, double sy, std::string texture, tPos usualState, tPos selectedState, tPos pressedState, std::string font, sf::Color textColor, std::string clickSound); void select(); void unSelect(); void press(); void init(); + + [[nodiscard]] int x() const { return _x; } + [[nodiscard]] int y() const { return _y; } + [[nodiscard]] int w() const { return _w; } + [[nodiscard]] int h() const { return _h; } + [[nodiscard]] double sx() const { return _sx; } + [[nodiscard]] double sy() const { return _sy; } + [[nodiscard]] sf::Sprite const& sprite() const { return _button; } + [[nodiscard]] sf::Text const& text() const { return _text; } }; diff --git a/engine/gui/Window.cpp b/engine/gui/Window.cpp index d20168b..1fcb978 100755 --- a/engine/gui/Window.cpp +++ b/engine/gui/Window.cpp @@ -10,23 +10,23 @@ void Window::addButton(int x, int y, int w, int h, std::function click, const std::string &text, double sx, double sy, const std::string &texture, tPos usualState, tPos selectedState, tPos pressedState, const std::string& font, sf::Color textColor, const std::string& clickSound) { - buttons.push_back(Button{x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState, font, textColor, clickSound}); - buttons.back().init(); + _buttons.push_back(Button{x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState, font, textColor, clickSound}); + _buttons.back().init(); } void Window::update() { - _screen->setTitle(s_name); - _screen->drawSprite(back); + _screen->setTitle(_name); + _screen->drawSprite(_back); Point4D mousePos = _mouse->getMousePosition(); - Point4D dMousePos = mousePos - prevMousePosition; - back.setPosition(back.getPosition() - sf::Vector2f(dMousePos.x()/30, dMousePos.y()/30)); + Point4D dMousePos = mousePos - _prevMousePosition; + _back.setPosition(_back.getPosition() - sf::Vector2f(dMousePos.x() / 30, dMousePos.y() / 30)); bool isPressed = _mouse->isButtonTapped(sf::Mouse::Left); - for(auto& button : buttons) { - if( mousePos.x() > button.x - button.w*button.sx/2 && mousePos.y() > button.y - button.h*button.sy/2 && - mousePos.x() < button.x + button.w*button.sx/2 && mousePos.y() < button.y + button.h*button.sy/2) { + for(auto& button : _buttons) { + if( mousePos.x() > button.x() - button.w() * button.sx() / 2 && mousePos.y() > button.y() - button.h() * button.sy() / 2 && + mousePos.x() < button.x() + button.w() * button.sx() / 2 && mousePos.y() < button.y() + button.h() * button.sy() / 2) { button.select(); if(isPressed) button.press(); @@ -35,19 +35,19 @@ void Window::update() { } if(_screen->isOpen()) { - _screen->drawSprite(button.button); - _screen->drawText(button.text); + _screen->drawSprite(button.sprite()); + _screen->drawText(button.text()); } } - prevMousePosition = mousePos; + _prevMousePosition = mousePos; } void Window::setBackgroundTexture(const std::string &texture, double sx, double sy, int w, int h) { - s_backTexture = texture; - std::shared_ptr t = ResourceManager::loadTexture(s_backTexture); + _backTexture = texture; + std::shared_ptr t = ResourceManager::loadTexture(_backTexture); t->setRepeated(true); - back = sf::Sprite(*t, sf::IntRect(0, 0, w + w/30.0, h + h/30.0)); - back.scale(sx, sy); - back.setPosition(sf::Vector2f(-w/30.0, -h/30.0)); + _back = sf::Sprite(*t, sf::IntRect(0, 0, w + w / 30.0, h + h / 30.0)); + _back.scale(sx, sy); + _back.setPosition(sf::Vector2f(-w / 30.0, -h / 30.0)); } diff --git a/engine/gui/Window.h b/engine/gui/Window.h index 316a149..7b55af4 100755 --- a/engine/gui/Window.h +++ b/engine/gui/Window.h @@ -14,27 +14,27 @@ class Window { private: - std::string s_name; - std::string s_backTexture; - std::vector