diff --git a/rsc/button.png b/rsc/button.png new file mode 100644 index 0000000..867cd63 Binary files /dev/null and b/rsc/button.png differ diff --git a/src/common/button.cpp b/src/common/button.cpp index 22421f5..454feeb 100644 --- a/src/common/button.cpp +++ b/src/common/button.cpp @@ -24,6 +24,7 @@ #include void Button::DrawTo(SDL_Renderer* renderer) { + image.SetClipY(image.GetClipH() * state); image.DrawTo(renderer, 0, 0); } @@ -31,7 +32,7 @@ void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture) //copy the given texture image.Free(); - //a blank texture can simply free the image + //a null texture can simply free the image if (!texture) { return; } @@ -45,6 +46,9 @@ void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture) SDL_SetRenderTarget(renderer, image.GetTexture()); SDL_RenderCopy(renderer, texture, nullptr, nullptr); SDL_SetRenderTarget(renderer, nullptr); + + //prune + image.SetClipH(image.GetClipH() / 3); } void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_Color color) { @@ -60,21 +64,30 @@ void Button::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string s, SDL_ if (!text) { throw(std::runtime_error("Failed to create a TTF texture")); } - int w, h; + + //get the dimensions & rects + int x, y, w, h; SDL_QueryTexture(text, nullptr, nullptr, &w, &h); + x = (image.GetClipW() - w) / 2; + y = (image.GetClipH() - h) / 2; + SDL_Rect src = {0, 0, w, h}; + SDL_Rect dst; //draw the text to the background - SDL_Rect src = {0, 0, w, h}; - SDL_Rect dst = { - (image.GetClipW() - w) / 2, - (image.GetClipH() - h) / 2, - w, h}; SDL_SetRenderTarget(renderer, image.GetTexture()); - SDL_RenderCopy(renderer, text, &src, &dst); + + for (int i = 0; i < 3; i++) { + dst = {x, y + image.GetClipH() * i, w, h}; + SDL_RenderCopy(renderer, text, &src, &dst); + } + SDL_SetRenderTarget(renderer, nullptr); //free the texture SDL_DestroyTexture(text); + + //DEBUG: testing +// image.SetClipH(image.GetClipH() * 3); } void Button::SetX(int x) { @@ -85,22 +98,55 @@ void Button::SetY(int y) { posY = y; } -void Button::MouseMotion(SDL_MouseMotionEvent const&) { - //TODO: empty +Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) { + //if out of bounds, exit + if (CheckBounds(event.x, event.y)) { + return state = State::IDLE; + } + + //if in bounds, check button + if (event.state & SDL_BUTTON_LMASK) { + state = State::PRESSED; + } + else { + state = State::HOVER; + } + + return state; } -void Button::MouseButtonDown(SDL_MouseButtonEvent const&) { - //TODO: empty +Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& event) { + //if out of bounds, exit + if (CheckBounds(event.x, event.y)) { + return state = State::IDLE; + } + + //if in bounds, set button + return state = State::PRESSED; } -void Button::MouseButtonUp(SDL_MouseButtonEvent const&) { - //TODO: empty +Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& event) { + //if out of bounds, exit + if (CheckBounds(event.x, event.y)) { + return state = State::IDLE; + } + + //if in bounds, set button + return state = State::HOVER; } -void Button::SetOnPress(fptr arg) { - onPress = arg; +void Button::SetState(State s) { + state = s; } -void Button::SetOnRelease(fptr arg) { - onRelease = arg; +Button::State Button::GetState() { + return state; } + +bool Button::CheckBounds(int x, int y) { + return + x < posX || + y < posY || + x > posX + image.GetClipW() || + y > posY + image.GetClipH(); +} \ No newline at end of file diff --git a/src/common/button.hpp b/src/common/button.hpp index 5cae2b1..c6fd91f 100644 --- a/src/common/button.hpp +++ b/src/common/button.hpp @@ -29,8 +29,9 @@ class Button { public: - //types - typedef int (*fptr)(); + enum State { + IDLE = 0, HOVER = 1, PRESSED = 2 + }; //methods Button() = default; @@ -45,17 +46,18 @@ public: void SetY(int y); //capture input - void MouseMotion(SDL_MouseMotionEvent const&); - void MouseButtonDown(SDL_MouseButtonEvent const&); - void MouseButtonUp(SDL_MouseButtonEvent const&); + State MouseMotion(SDL_MouseMotionEvent const&); + State MouseButtonDown(SDL_MouseButtonEvent const&); + State MouseButtonUp(SDL_MouseButtonEvent const&); - //responses - void SetOnPress(fptr); - void SetOnRelease(fptr); + //states + void SetState(State); //idle, busy or disabled + State GetState(); protected: + bool CheckBounds(int x, int y); + Image image; int posX = 0, posY = 0; - fptr onPress = nullptr; - fptr onRelease = nullptr; + State state; }; diff --git a/src/example_scene.cpp b/src/example_scene.cpp index 9b67520..cd62893 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -49,14 +49,16 @@ ExampleScene::ExampleScene(lua_State* L) { camera.scale = 0.25; //DEBUG: testing buttons + buttonBG.Load(GetRenderer(), "./rsc/button.png"); font = TTF_OpenFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12); if (!font) { std::ostringstream msg; msg << "Failed to load a font: " << SDL_GetError(); throw(std::runtime_error(msg.str())); } - button.SetBackgroundTexture(GetRenderer(), tileSheet.GetTexture()); - button.SetText(GetRenderer(), font, "BUTTON TEXT", {255, 255, 255, 255}); + button.SetBackgroundTexture(GetRenderer(), buttonBG.GetTexture()); + button.SetText(GetRenderer(), font, "BUTTON TEXT", {0, 0, 0, 255}); +// {140, 62, 54, 255} } ExampleScene::~ExampleScene() { @@ -94,6 +96,8 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) { //------------------------- void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) { + button.MouseMotion(event); + //right mouse button moves the camera if (event.state & SDL_BUTTON_RMASK) { camera.x -= event.xrel / camera.scale; @@ -102,6 +106,8 @@ void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) { } void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) { + button.MouseButtonDown(event); + switch(event.button) { case SDL_BUTTON_LEFT: { //DOCS: broke this down into several lines for clarity @@ -123,7 +129,7 @@ void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) { } void ExampleScene::MouseButtonUp(SDL_MouseButtonEvent const& event) { - // + button.MouseButtonUp(event); } void ExampleScene::MouseWheel(SDL_MouseWheelEvent const& event) { diff --git a/src/example_scene.hpp b/src/example_scene.hpp index afffeaf..757ae95 100644 --- a/src/example_scene.hpp +++ b/src/example_scene.hpp @@ -64,5 +64,6 @@ private: int layer = 0; TTF_Font* font = nullptr; + Image buttonBG; Button button; };