game/mario/blocks/coineditorblock.cpp
zv0n 0d855ed218
Some checks reported errors
continuous-integration/drone/push Build is passing
continuous-integration/drone Build was killed
Mario: formatting
2021-10-18 10:10:43 +02:00

76 lines
1.9 KiB
C++

#include "coineditorblock.hpp"
#include "../objectids.hpp"
CoinEditorBlock::CoinEditorBlock(int x, int y,
std::shared_ptr<SDLPP::Renderer> renderer)
: MarioBlock(x, y, renderer, g_translucent_mod_texture, MOD_COIN_SRC,
false, false) {
setId(COIN_MODIFIER_ID);
auto mypos = getDoubleRect();
auto size = mypos.second.getX() / size_divisor;
_amount_text = std::make_shared<SDLPP::TextRenderer>(
mypos.first.getX() + mypos.second.getX() - size,
mypos.first.getY() + mypos.second.getX() - size, size, size, renderer,
"1", g_text_config);
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
_amount_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
}
void CoinEditorBlock::render() {
MarioBlock::render();
if (_amount_text != nullptr && !hidden) {
_amount_text->render();
}
}
void CoinEditorBlock::updateSizeAndPosition() {
MarioBlock::updateSizeAndPosition();
auto block_size = getDoubleRect().second;
_amount_text->setPos(getPos() + block_size - block_size / size_divisor);
_amount_text->updateSizeAndPosition();
}
void CoinEditorBlock::addOne() {
if (_amount < max_amount) {
_amount++;
updateText();
}
}
void CoinEditorBlock::subtractOne() {
if (_amount > 1) {
_amount--;
updateText();
}
}
void CoinEditorBlock::setAmount(int amount) {
if (amount < 1) {
amount = 1;
} else if (amount > max_amount) {
amount = max_amount;
}
_amount = amount;
updateText();
}
void CoinEditorBlock::updateText() {
_amount_text->changeText(std::to_string(_amount));
}
void CoinEditorBlock::onScrollUp() {
addOne();
}
void CoinEditorBlock::onScrollDown() {
subtractOne();
}
uint8_t CoinEditorBlock::getData() const {
return _amount;
}
void CoinEditorBlock::setData(uint8_t data) {
setAmount(data);
}