From 130a01feda52408977e269732c88af19f4b75775 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 24 Jul 2021 20:48:37 +0200 Subject: [PATCH] Forgot to add coineditorblock --- mario/blocks/coineditorblock.cpp | 75 ++++++++++++++++++++++++++++++++ mario/blocks/coineditorblock.hpp | 29 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 mario/blocks/coineditorblock.cpp create mode 100644 mario/blocks/coineditorblock.hpp diff --git a/mario/blocks/coineditorblock.cpp b/mario/blocks/coineditorblock.cpp new file mode 100644 index 0000000..a74d235 --- /dev/null +++ b/mario/blocks/coineditorblock.cpp @@ -0,0 +1,75 @@ +#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() / 1.5; + _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 != NULL && !hidden ) { + _amount_text->render(); + } +} + +void CoinEditorBlock::updateSizeAndPosition() { + MarioBlock::updateSizeAndPosition(); + auto block_size = getDoubleRect().second; + _amount_text->setPos( getPos() + block_size - block_size / 1.5 ); + _amount_text->updateSizeAndPosition(); +} + +void CoinEditorBlock::addOne() { + if ( _amount < 15 ) { + _amount++; + updateText(); + } +} + +void CoinEditorBlock::subtractOne() { + if ( _amount > 1 ) { + _amount--; + updateText(); + } +} + +void CoinEditorBlock::setAmount( int amount ) { + if ( amount < 1 ) { + amount = 1; + } else if ( amount > 15 ) { + amount = 15; + } + _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() { + return _amount; +} + +void CoinEditorBlock::setData( uint8_t data ) { + setAmount( data ); +} diff --git a/mario/blocks/coineditorblock.hpp b/mario/blocks/coineditorblock.hpp new file mode 100644 index 0000000..e76a85b --- /dev/null +++ b/mario/blocks/coineditorblock.hpp @@ -0,0 +1,29 @@ +#ifndef COIN_BLOCK_H +#define COIN_BLOCK_H + +#include "../blocks.hpp" +#include "../global_vars.hpp" +#include "../sprites.hpp" +#include "../../sdlpp/sdlpp_textrenderer.hpp" + +class CoinEditorBlock : public MarioBlock { +public: + CoinEditorBlock( int x, int y, + std::shared_ptr< SDLPP::Renderer > renderer ); + virtual void render() override; + virtual void updateSizeAndPosition() override; + void addOne(); + void subtractOne(); + void setAmount( int amount ); + virtual void onScrollUp() override; + virtual void onScrollDown() override; + virtual uint8_t getData() override; + virtual void setData( uint8_t data ) override; + +private: + void updateText(); + int _amount = 1; + std::shared_ptr< SDLPP::TextRenderer > _amount_text; +}; + +#endif