game/mario/blocks/coineditorblock.cpp

76 lines
1.9 KiB
C++
Raw Permalink Normal View History

2021-07-24 18:48:37 +00:00
#include "coineditorblock.hpp"
#include "../objectids.hpp"
2021-10-18 08:10:43 +00:00
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);
2021-07-24 18:48:37 +00:00
auto mypos = getDoubleRect();
auto size = mypos.second.getX() / size_divisor;
2021-10-18 08:10:43 +00:00
_amount_text = std::make_shared<SDLPP::TextRenderer>(
2021-07-24 18:48:37 +00:00
mypos.first.getX() + mypos.second.getX() - size,
mypos.first.getY() + mypos.second.getX() - size, size, size, renderer,
2021-10-18 08:10:43 +00:00
"1", g_text_config);
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
_amount_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
2021-07-24 18:48:37 +00:00
}
void CoinEditorBlock::render() {
MarioBlock::render();
2021-10-18 08:10:43 +00:00
if (_amount_text != nullptr && !hidden) {
2021-07-24 18:48:37 +00:00
_amount_text->render();
}
}
void CoinEditorBlock::updateSizeAndPosition() {
MarioBlock::updateSizeAndPosition();
auto block_size = getDoubleRect().second;
2021-10-18 08:10:43 +00:00
_amount_text->setPos(getPos() + block_size - block_size / size_divisor);
2021-07-24 18:48:37 +00:00
_amount_text->updateSizeAndPosition();
}
void CoinEditorBlock::addOne() {
2021-10-18 08:10:43 +00:00
if (_amount < max_amount) {
2021-07-24 18:48:37 +00:00
_amount++;
updateText();
}
}
void CoinEditorBlock::subtractOne() {
2021-10-18 08:10:43 +00:00
if (_amount > 1) {
2021-07-24 18:48:37 +00:00
_amount--;
updateText();
}
}
2021-10-18 08:10:43 +00:00
void CoinEditorBlock::setAmount(int amount) {
if (amount < 1) {
2021-07-24 18:48:37 +00:00
amount = 1;
2021-10-18 08:10:43 +00:00
} else if (amount > max_amount) {
amount = max_amount;
2021-07-24 18:48:37 +00:00
}
_amount = amount;
updateText();
}
void CoinEditorBlock::updateText() {
2021-10-18 08:10:43 +00:00
_amount_text->changeText(std::to_string(_amount));
2021-07-24 18:48:37 +00:00
}
void CoinEditorBlock::onScrollUp() {
addOne();
}
void CoinEditorBlock::onScrollDown() {
subtractOne();
}
2021-08-07 10:13:23 +00:00
uint8_t CoinEditorBlock::getData() const {
2021-07-24 18:48:37 +00:00
return _amount;
}
2021-10-18 08:10:43 +00:00
void CoinEditorBlock::setData(uint8_t data) {
setAmount(data);
2021-07-24 18:48:37 +00:00
}