Button works, but the input needs to be interpreted
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 321 B |
+63
-17
@@ -24,6 +24,7 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
void Button::DrawTo(SDL_Renderer* renderer) {
|
void Button::DrawTo(SDL_Renderer* renderer) {
|
||||||
|
image.SetClipY(image.GetClipH() * state);
|
||||||
image.DrawTo(renderer, 0, 0);
|
image.DrawTo(renderer, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture)
|
|||||||
//copy the given texture
|
//copy the given texture
|
||||||
image.Free();
|
image.Free();
|
||||||
|
|
||||||
//a blank texture can simply free the image
|
//a null texture can simply free the image
|
||||||
if (!texture) {
|
if (!texture) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -45,6 +46,9 @@ void Button::SetBackgroundTexture(SDL_Renderer* renderer, SDL_Texture* texture)
|
|||||||
SDL_SetRenderTarget(renderer, image.GetTexture());
|
SDL_SetRenderTarget(renderer, image.GetTexture());
|
||||||
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
|
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
|
||||||
SDL_SetRenderTarget(renderer, 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) {
|
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) {
|
if (!text) {
|
||||||
throw(std::runtime_error("Failed to create a TTF texture"));
|
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);
|
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
|
//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_SetRenderTarget(renderer, image.GetTexture());
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
dst = {x, y + image.GetClipH() * i, w, h};
|
||||||
SDL_RenderCopy(renderer, text, &src, &dst);
|
SDL_RenderCopy(renderer, text, &src, &dst);
|
||||||
|
}
|
||||||
|
|
||||||
SDL_SetRenderTarget(renderer, nullptr);
|
SDL_SetRenderTarget(renderer, nullptr);
|
||||||
|
|
||||||
//free the texture
|
//free the texture
|
||||||
SDL_DestroyTexture(text);
|
SDL_DestroyTexture(text);
|
||||||
|
|
||||||
|
//DEBUG: testing
|
||||||
|
// image.SetClipH(image.GetClipH() * 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::SetX(int x) {
|
void Button::SetX(int x) {
|
||||||
@@ -85,22 +98,55 @@ void Button::SetY(int y) {
|
|||||||
posY = y;
|
posY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::MouseMotion(SDL_MouseMotionEvent const&) {
|
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
|
||||||
//TODO: empty
|
//if out of bounds, exit
|
||||||
|
if (CheckBounds(event.x, event.y)) {
|
||||||
|
return state = State::IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::MouseButtonDown(SDL_MouseButtonEvent const&) {
|
//if in bounds, check button
|
||||||
//TODO: empty
|
if (event.state & SDL_BUTTON_LMASK) {
|
||||||
|
state = State::PRESSED;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
state = State::HOVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::MouseButtonUp(SDL_MouseButtonEvent const&) {
|
return state;
|
||||||
//TODO: empty
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::SetOnPress(fptr arg) {
|
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
||||||
onPress = arg;
|
//if out of bounds, exit
|
||||||
|
if (CheckBounds(event.x, event.y)) {
|
||||||
|
return state = State::IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::SetOnRelease(fptr arg) {
|
//if in bounds, set button
|
||||||
onRelease = arg;
|
return state = State::PRESSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
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::SetState(State s) {
|
||||||
|
state = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
+12
-10
@@ -29,8 +29,9 @@
|
|||||||
|
|
||||||
class Button {
|
class Button {
|
||||||
public:
|
public:
|
||||||
//types
|
enum State {
|
||||||
typedef int (*fptr)();
|
IDLE = 0, HOVER = 1, PRESSED = 2
|
||||||
|
};
|
||||||
|
|
||||||
//methods
|
//methods
|
||||||
Button() = default;
|
Button() = default;
|
||||||
@@ -45,17 +46,18 @@ public:
|
|||||||
void SetY(int y);
|
void SetY(int y);
|
||||||
|
|
||||||
//capture input
|
//capture input
|
||||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
State MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
State MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
State MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
|
|
||||||
//responses
|
//states
|
||||||
void SetOnPress(fptr);
|
void SetState(State); //idle, busy or disabled
|
||||||
void SetOnRelease(fptr);
|
State GetState();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool CheckBounds(int x, int y);
|
||||||
|
|
||||||
Image image;
|
Image image;
|
||||||
int posX = 0, posY = 0;
|
int posX = 0, posY = 0;
|
||||||
fptr onPress = nullptr;
|
State state;
|
||||||
fptr onRelease = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -49,14 +49,16 @@ ExampleScene::ExampleScene(lua_State* L) {
|
|||||||
camera.scale = 0.25;
|
camera.scale = 0.25;
|
||||||
|
|
||||||
//DEBUG: testing buttons
|
//DEBUG: testing buttons
|
||||||
|
buttonBG.Load(GetRenderer(), "./rsc/button.png");
|
||||||
font = TTF_OpenFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12);
|
font = TTF_OpenFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12);
|
||||||
if (!font) {
|
if (!font) {
|
||||||
std::ostringstream msg;
|
std::ostringstream msg;
|
||||||
msg << "Failed to load a font: " << SDL_GetError();
|
msg << "Failed to load a font: " << SDL_GetError();
|
||||||
throw(std::runtime_error(msg.str()));
|
throw(std::runtime_error(msg.str()));
|
||||||
}
|
}
|
||||||
button.SetBackgroundTexture(GetRenderer(), tileSheet.GetTexture());
|
button.SetBackgroundTexture(GetRenderer(), buttonBG.GetTexture());
|
||||||
button.SetText(GetRenderer(), font, "BUTTON TEXT", {255, 255, 255, 255});
|
button.SetText(GetRenderer(), font, "BUTTON TEXT", {0, 0, 0, 255});
|
||||||
|
// {140, 62, 54, 255}
|
||||||
}
|
}
|
||||||
|
|
||||||
ExampleScene::~ExampleScene() {
|
ExampleScene::~ExampleScene() {
|
||||||
@@ -94,6 +96,8 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) {
|
void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) {
|
||||||
|
button.MouseMotion(event);
|
||||||
|
|
||||||
//right mouse button moves the camera
|
//right mouse button moves the camera
|
||||||
if (event.state & SDL_BUTTON_RMASK) {
|
if (event.state & SDL_BUTTON_RMASK) {
|
||||||
camera.x -= event.xrel / camera.scale;
|
camera.x -= event.xrel / camera.scale;
|
||||||
@@ -102,6 +106,8 @@ void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
||||||
|
button.MouseButtonDown(event);
|
||||||
|
|
||||||
switch(event.button) {
|
switch(event.button) {
|
||||||
case SDL_BUTTON_LEFT: {
|
case SDL_BUTTON_LEFT: {
|
||||||
//DOCS: broke this down into several lines for clarity
|
//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) {
|
void ExampleScene::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
||||||
//
|
button.MouseButtonUp(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExampleScene::MouseWheel(SDL_MouseWheelEvent const& event) {
|
void ExampleScene::MouseWheel(SDL_MouseWheelEvent const& event) {
|
||||||
|
|||||||
@@ -64,5 +64,6 @@ private:
|
|||||||
int layer = 0;
|
int layer = 0;
|
||||||
|
|
||||||
TTF_Font* font = nullptr;
|
TTF_Font* font = nullptr;
|
||||||
|
Image buttonBG;
|
||||||
Button button;
|
Button button;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user