Add UI click
This commit is contained in:
parent
efdffb34b9
commit
96eb91ff78
@ -45,7 +45,7 @@ static void defaultInput(GLFWwindow *window, void *data) {
|
||||
}
|
||||
}
|
||||
|
||||
static void defaultKey(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
|
||||
static void defaultKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
|
||||
if(key == Input::getKeyBinding(keyOptions).key && action == GLFW_PRESS) {
|
||||
options->visible = !options->visible;
|
||||
}
|
||||
@ -93,6 +93,32 @@ static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *d
|
||||
}
|
||||
}
|
||||
|
||||
static void defaultMouseButtonCallback(GLFWwindow* window, int button, int action, int mods, void *data) {
|
||||
switch(Input::getCursorMode()) {
|
||||
case GLFWCursorMode::CAPTURE:
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
case GLFWCursorMode::FREE:
|
||||
case GLFWCursorMode::HIDDEN:
|
||||
{
|
||||
switch(action) {
|
||||
case GLFW_PRESS:
|
||||
break;
|
||||
case GLFW_RELEASE:
|
||||
{
|
||||
double x, y;
|
||||
glfwGetCursorPos(window, &x, &y);
|
||||
for(UIElement *element : kekData.uiElements) {
|
||||
UIPoint childPos = element->getPosition();
|
||||
if(element->clickAll(UIPoint((int) x - childPos.x, (int) y - childPos.y), UIPoint((int) x, (int) y), (GLFWMouseButton) button)) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W);
|
||||
keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S);
|
||||
@ -104,8 +130,9 @@ void init() {
|
||||
keyToggleCursorMode = Input::createKeyBinding("Toggle Cursor Mode", GLFW_KEY_TAB);
|
||||
|
||||
Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr));
|
||||
Input::addKeyListener(KeyCallback(defaultKey, nullptr));
|
||||
Input::addKeyListener(KeyCallback(defaultKeyCallback, nullptr));
|
||||
Input::addMouseListener(MouseCallback(defaultMouseCallback, nullptr));
|
||||
Input::addMouseButtonListener(MouseButtonCallback(defaultMouseButtonCallback, nullptr));
|
||||
|
||||
options = new ButtonElement(px(0), px(100), px(100), px(50));
|
||||
//UI::addElement(options);
|
||||
|
@ -177,7 +177,7 @@ int init() {
|
||||
Defaults::init();
|
||||
|
||||
fpsText = new TextElement(px(0), px(0));
|
||||
//UI::addElement(fpsText);
|
||||
UI::addElement(fpsText);
|
||||
|
||||
return KEK_SUCCESS;
|
||||
}
|
||||
|
@ -66,6 +66,7 @@ void UIElement::addChild(UIElement *child) {
|
||||
}
|
||||
|
||||
void UIElement::removeChild(UIElement *child) {
|
||||
child->parent = nullptr;
|
||||
std::remove(children.begin(), children.end(), child);
|
||||
}
|
||||
|
||||
|
@ -153,7 +153,7 @@ static float rectangleVerts[] = {
|
||||
RectangleElement::RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h): UIElement(x, y) {
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
this->color = Colors::BLACK;
|
||||
color = Colors::BLACK;
|
||||
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
@ -195,7 +195,9 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) {
|
||||
}
|
||||
|
||||
ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) {
|
||||
this->text = new TextElement(pw(0.5), ph(0.5));
|
||||
clickable = true;
|
||||
|
||||
text = new TextElement(pw(0.5), ph(0.5));
|
||||
text->origin = Origin::CENTER;
|
||||
addChild(text);
|
||||
}
|
||||
@ -208,20 +210,13 @@ UIElementType ButtonElement::getType() {
|
||||
return UIElementType::BUTTON;
|
||||
}
|
||||
|
||||
void ButtonElement::hover(UIPoint pos, UIPoint screenPos) {
|
||||
|
||||
}
|
||||
|
||||
void ButtonElement::hoverEnter(UIPoint pos, UIPoint screenPos) {
|
||||
color = hoverColor;
|
||||
}
|
||||
|
||||
void ButtonElement::hoverExit() {
|
||||
color = defaultColor;
|
||||
}
|
||||
|
||||
void ButtonElement::click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) {
|
||||
onClick();
|
||||
}
|
||||
|
||||
void ButtonElement::draw(UIPoint screenPos, glm::mat4 projection) {
|
||||
RectangleElement::color = hovering ? hoverColor : color;
|
||||
RectangleElement::draw(screenPos, projection);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ class ButtonElement: public RectangleElement {
|
||||
|
||||
public:
|
||||
TextElement *text;
|
||||
Color defaultColor;
|
||||
Color color;
|
||||
Color hoverColor;
|
||||
Callable onClick;
|
||||
|
||||
@ -80,14 +80,10 @@ public:
|
||||
|
||||
virtual UIElementType getType();
|
||||
|
||||
virtual void hover(UIPoint pos, UIPoint screenPos);
|
||||
|
||||
virtual void hoverEnter(UIPoint pos, UIPoint screenPos);
|
||||
|
||||
virtual void hoverExit();
|
||||
|
||||
virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button);
|
||||
|
||||
virtual void draw(UIPoint screenPos, glm::mat4 projection);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,10 @@ void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, v
|
||||
|
||||
}
|
||||
|
||||
void onButtonClick(void *data) {
|
||||
std::cout << "Clicked!" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Engine::init();
|
||||
|
||||
@ -54,11 +58,12 @@ int main(int argc, char **argv) {
|
||||
Input::addKeyListener(KeyCallback(keyCallback, nullptr));
|
||||
Input::addMouseButtonListener(MouseButtonCallback(mouseButtonCallback, nullptr));
|
||||
|
||||
ButtonElement *btn = new ButtonElement(px(0), px(100), px(200), px(50));
|
||||
ButtonElement *btn = new ButtonElement(px(10), px(100), px(200), px(50));
|
||||
btn->text->color = Colors::BLACK;
|
||||
btn->defaultColor = btn->color = Colors::WHITE;
|
||||
btn->color = Colors::WHITE;
|
||||
btn->hoverColor = Colors::GRAY;
|
||||
btn->text->setText("Hello There!");
|
||||
btn->onClick = Callable(onButtonClick, nullptr);
|
||||
UI::addElement(btn);
|
||||
|
||||
Engine::start();
|
||||
|
Loading…
Reference in New Issue
Block a user