struct Button -> class Button

master
Vectozavr 2021-10-03 11:47:05 +07:00
parent 58672c0104
commit 122d30a980
14 changed files with 121 additions and 99 deletions

View File

@ -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();
}
}

View File

@ -101,7 +101,7 @@ void Camera::init(int width, int height, double fov, double ZNear, double ZFar)
std::vector<Triangle> &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)
{

View File

@ -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");

View File

@ -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)
};

View File

@ -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;

View File

@ -18,8 +18,8 @@ public:
void setWindow(std::shared_ptr<sf::RenderWindow> 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;

View File

@ -3,56 +3,66 @@
//
#include "Button.h"
#include <utility>
#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<void()> 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);
}

View File

@ -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<void()> click;
std::function<void()> _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<void()> 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; }
};

View File

@ -10,23 +10,23 @@
void Window::addButton(int x, int y, int w, int h, std::function<void()> 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<sf::Texture> t = ResourceManager::loadTexture(s_backTexture);
_backTexture = texture;
std::shared_ptr<sf::Texture> 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));
}

View File

@ -14,27 +14,27 @@
class Window {
private:
std::string s_name;
std::string s_backTexture;
std::vector<Button> buttons;
std::string _name;
std::string _backTexture;
std::vector<Button> _buttons;
sf::Sprite back;
sf::Sprite _back;
Point4D prevMousePosition;
Point4D _prevMousePosition;
std::shared_ptr<Screen> _screen;
std::shared_ptr<Mouse> _mouse;
public:
explicit Window(std::shared_ptr<Screen> screen, std::shared_ptr<Mouse> mouse, std::string name = "Menu", std::string backTexture = "") : _screen(screen), _mouse(mouse), s_name(std::move(name)), s_backTexture(std::move(backTexture)){}
explicit Window(std::shared_ptr<Screen> screen, std::shared_ptr<Mouse> mouse, std::string name = "Menu", std::string backTexture = "") : _screen(screen), _mouse(mouse), _name(std::move(name)), _backTexture(std::move(backTexture)){}
void addButton(int x, int y, int w, int h,
std::function<void()> click,
const std::string& text = "button", double sx = 1, double sy = 1,
const std::string& text = "_button", double sx = 1, double sy = 1,
const std::string& texture = "", tPos usualState = {}, tPos selectedState = {}, tPos pressedState = {},
const std::string& font = "../engine/fonts/Roboto-Medium.ttf", sf::Color textColor = {255, 255, 255}, const std::string& clickSound = "");
[[nodiscard]] std::string title() const { return s_name; }
void title(const std::string& title) { s_name = title; }
[[nodiscard]] std::string title() const { return _name; }
void title(const std::string& title) { _name = title; }
void setBackgroundTexture(const std::string& texture, double sx = 1, double sy = 1, int w = 1920, int h = 1080);

View File

@ -36,7 +36,7 @@ public:
// virtual functions
virtual void broadcast(){};
// here you have to send Init message back to 'targetId' and send NewClient message to all '_clients'
// here you have to send Init message _back to 'targetId' and send NewClient message to all '_clients'
virtual void processConnect(sf::Uint16 senderId){};
virtual void processClientUpdate(sf::Uint16 senderId, sf::Packet& packet){};
virtual void processDisconnect(sf::Uint16 senderId){};

View File

@ -178,7 +178,7 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr<RigidBody>
};
// list: vector4(normal, distance), index: min distance
auto [normals, minFace] = GetFaceNormals(polytope, faces);
auto [normals, minFace] = _getFaceNormals(polytope, faces);
Point4D minNormal;
double minDistance = INFINITY;
@ -200,9 +200,9 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr<RigidBody>
if (normals[i].dot(support) > 0) {
size_t f = i * 3;
AddIfUniqueEdge(uniqueEdges, faces, f, f + 1);
AddIfUniqueEdge(uniqueEdges, faces, f + 1, f + 2);
AddIfUniqueEdge(uniqueEdges, faces, f + 2, f );
_addIfUniqueEdge(uniqueEdges, faces, f, f + 1);
_addIfUniqueEdge(uniqueEdges, faces, f + 1, f + 2);
_addIfUniqueEdge(uniqueEdges, faces, f + 2, f);
faces[f + 2] = faces.back(); faces.pop_back();
faces[f + 1] = faces.back(); faces.pop_back();
@ -222,7 +222,7 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr<RigidBody>
polytope.push_back(support);
auto [newNormals, newMinFace] = GetFaceNormals(polytope, newFaces);
auto [newNormals, newMinFace] = _getFaceNormals(polytope, newFaces);
if(newNormals.empty())
break;
@ -255,7 +255,7 @@ CollisionPoint RigidBody::EPA(const Simplex& simplex, std::shared_ptr<RigidBody>
return point;
}
std::pair<std::vector<Point4D>, size_t> RigidBody::GetFaceNormals(const std::vector<Point4D>& polytope, const std::vector<size_t>& faces) {
std::pair<std::vector<Point4D>, size_t> RigidBody::_getFaceNormals(const std::vector<Point4D>& polytope, const std::vector<size_t>& faces) {
std::vector<Point4D> normals;
size_t minTriangle = 0;
double minDistance = INFINITY;
@ -285,7 +285,7 @@ std::pair<std::vector<Point4D>, size_t> RigidBody::GetFaceNormals(const std::vec
return { normals, minTriangle };
}
void RigidBody::AddIfUniqueEdge(std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b) {
void RigidBody::_addIfUniqueEdge(std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b) {
auto reverse = std::find( // 0--<--3
edges.begin(), // / \ B / A: 2-0

View File

@ -32,8 +32,8 @@ private:
static bool _triangle(Simplex& points, Point4D& direction);
static bool _tetrahedron(Simplex& points, Point4D& direction);
static std::pair<std::vector<Point4D>, size_t> GetFaceNormals(const std::vector<Point4D>& polytope, const std::vector<size_t>& faces);
static void AddIfUniqueEdge(std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b);
static std::pair<std::vector<Point4D>, size_t> _getFaceNormals(const std::vector<Point4D>& polytope, const std::vector<size_t>& faces);
static void _addIfUniqueEdge(std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b);
protected:
Point4D _velocity;
@ -58,7 +58,6 @@ public:
[[nodiscard]] bool inCollision() const {return _inCollision; }
[[nodiscard]] bool isCollider() const {return _isCollider; }
void setInCollision(bool c) { _inCollision = c; }
void setCollisionNormal(const Point4D& c) { _collisionNormal = c; }
void setCollision(bool c) { _collision= c; }
void setCollider(bool c) { _isCollider = c; }

View File

@ -65,8 +65,8 @@ Matrix4x4 Matrix4x4::Scale(const Point4D& factor) {
Matrix4x4 Matrix4x4::Translation(const Point4D& v) {
Matrix4x4 t{};
/*
* ( 1 0 0 dx )(x) (x + dx)
* ( 0 1 0 dy )(y) = (y + dy)
* ( 1 0 0 dx )(_x) (_x + dx)
* ( 0 1 0 dy )(_y) = (_y + dy)
* ( 0 0 1 dz )(z) (z + dz)
* ( 0 0 0 1 )(1) ( 1 )
*/