summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2022-12-05 22:55:40 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2022-12-05 22:55:40 +0100
commit30f488cdcb88dad5dd1df425588cb6a7fd206bb5 (patch)
treeccd606c44bb2a6ab028548e21463b08c8c92577b
parentbf34d835e4ea49bd4ef2e5de2b1fea2fd3c95adc (diff)
Sketch ImGUI:
- Still needs font rendering
-rw-r--r--src/states/titleState.cpp35
-rw-r--r--src/states/titleState.h2
-rw-r--r--src/ui.cpp187
-rw-r--r--src/ui.h46
4 files changed, 263 insertions, 7 deletions
diff --git a/src/states/titleState.cpp b/src/states/titleState.cpp
index a0d17c8..d96c840 100644
--- a/src/states/titleState.cpp
+++ b/src/states/titleState.cpp
@@ -1,8 +1,12 @@
#include "titleState.h"
#include <SFML/Graphics.hpp>
+#include <string>
+
TitleState::TitleState(StateStack &stack, State::Context context)
: State(stack, context)
+ // TODO: Initialize UI properly to zeros
+ //, ui(0,0,false,false,0,0)
{
}
@@ -12,14 +16,31 @@ void TitleState::update(sf::Time dt){
}
void TitleState::render(){
- sf::RectangleShape rectangle;
- rectangle.setSize(sf::Vector2f(100, 50));
- rectangle.setOutlineColor(sf::Color::Red);
- rectangle.setOutlineThickness(5);
- rectangle.setPosition(10, 20);
- context_.window->draw(rectangle);
+ static bool radioState = false;
+
+ ui.initImUI();
+
+ if (ui.button(context_.window, 17, "Button 1", sf::Vector2i(0,0), sf::Vector2i(200,100)) ){
+ printf("Button 1 pressed!!\n");
+ }
+ if (ui.button(context_.window, 18, "Button 2", sf::Vector2i(0,200), sf::Vector2i(200,100)) ){
+ printf("Button 2 pressed!!\n");
+ }
+ if (ui.button(context_.window, 19, "Button 3", sf::Vector2i(0,400), sf::Vector2i(200,100)) ){
+ printf("Button 3 pressed!!\n");
+ }
+
+
+ radioState = ui.radioButton(context_.window, 21, "Radio", radioState,
+ sf::Vector2i(0,800), sf::Vector2i(200,100));
+
+ if (radioState){
+ ui.button(context_.window, 20, "Hidden button", sf::Vector2i(0,600), sf::Vector2i(200,100));
+ }
+
+ ui.finishImUI();
}
void TitleState::handleEvent(const sf::Event &event){
-
+ ui.handleEvent(event);
}
diff --git a/src/states/titleState.h b/src/states/titleState.h
index 8b914d8..83339f8 100644
--- a/src/states/titleState.h
+++ b/src/states/titleState.h
@@ -2,8 +2,10 @@
#define STATES_TITLESTATE_H
#include "../states.h"
+#include "../ui.h"
class TitleState : public State{
+ UI ui;
public:
TitleState(StateStack &stack, State::Context context);
virtual void update(sf::Time dt) override;
diff --git a/src/ui.cpp b/src/ui.cpp
new file mode 100644
index 0000000..7f4c1ea
--- /dev/null
+++ b/src/ui.cpp
@@ -0,0 +1,187 @@
+#include"ui.h"
+
+void UI::handleEvent(const sf::Event &event){
+ switch(event.type){
+ case sf::Event::MouseMoved:
+ mousePos.x = event.mouseMove.x;
+ mousePos.y = event.mouseMove.y;
+ break;
+ case sf::Event::MouseButtonPressed:
+ previousMouse = currentMouse;
+ switch(event.mouseButton.button) {
+ case sf::Mouse::Right:
+ currentMouse.rightDown = true;
+ break;
+ case sf::Mouse::Left:
+ currentMouse.leftDown = true;
+ break;
+ case sf::Mouse::Middle:
+ currentMouse.midDown = true;
+ break;
+ default:
+ break;
+ }
+ break;
+ case sf::Event::MouseButtonReleased:
+ previousMouse = currentMouse;
+ switch(event.mouseButton.button) {
+ case sf::Mouse::Right:
+ currentMouse.rightDown = false;
+ break;
+ case sf::Mouse::Left:
+ currentMouse.leftDown = false;
+ break;
+ case sf::Mouse::Middle:
+ currentMouse.midDown = false;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+bool UI::mouseButtonPressed(MouseButton m){
+ switch(m)
+ {
+ case MouseButton::Left:
+ return currentMouse.leftDown && !previousMouse.leftDown;
+ case MouseButton::Mid:
+ return currentMouse.midDown && !previousMouse.midDown;
+ case MouseButton::Right:
+ return currentMouse.rightDown && !previousMouse.rightDown;
+ default: return false;
+ }
+}
+bool UI::mouseButtonReleased(MouseButton m){
+ switch(m)
+ {
+ case MouseButton::Left:
+ return !currentMouse.leftDown && previousMouse.leftDown;
+ case MouseButton::Mid:
+ return !currentMouse.midDown && previousMouse.midDown;
+ case MouseButton::Right:
+ return !currentMouse.rightDown && previousMouse.rightDown;
+ default: return false;
+ }
+}
+bool UI::mouseButtonIsDown(MouseButton m){
+ switch(m)
+ {
+ case MouseButton::Left:
+ return currentMouse.leftDown;
+ case MouseButton::Mid:
+ return currentMouse.midDown;
+ case MouseButton::Right:
+ return currentMouse.rightDown;
+ default: return false;
+ }
+}
+bool UI::mouseButtonIsUp(MouseButton m){
+ switch(m)
+ {
+ case MouseButton::Left:
+ return !currentMouse.leftDown;
+ case MouseButton::Mid:
+ return !currentMouse.midDown;
+ case MouseButton::Right:
+ return !currentMouse.rightDown;
+ default: return false;
+ }
+}
+
+sf::Vector2i UI::mousePosition(){
+ return mousePos;
+}
+
+void UI::initImUI(){
+ hotItem = 0;
+}
+
+void UI::finishImUI(){
+ if (mouseButtonIsUp(MouseButton::Left)) {
+ activeItem = 0;
+ }
+ else {
+ if (activeItem == 0) activeItem = -1;
+ }
+}
+
+bool UI::button(sf::RenderWindow *win, int id, const std::string &text,
+ sf::Vector2i pos, sf::Vector2i size){
+
+ bool result = false;
+ sf::IntRect rect {pos, size};
+ if( rect.contains( mousePosition() ) ){
+ hotItem = id;
+ }
+ if (hotItem == id){
+ if(mouseButtonPressed(MouseButton::Left)){
+ activeItem = id;
+ }
+ }
+ if (activeItem == id){
+ if (mouseButtonReleased(MouseButton::Left)){
+ if ( hotItem == id ){
+ result = true;
+ }
+ activeItem = -1;
+ }
+ }
+
+ // Rendering:
+ sf::RectangleShape rectangle { sf::Vector2f(size) };
+ rectangle.setPosition( sf::Vector2f(pos) );
+
+ rectangle.setFillColor(sf::Color::White);
+ if (hotItem == id){
+ rectangle.setFillColor(sf::Color::Blue);
+ }
+ if (activeItem == id){
+ rectangle.setFillColor(sf::Color::Red);
+ }
+
+ win->draw(rectangle);
+
+ return result;
+}
+
+bool UI::radioButton(sf::RenderWindow *win, int id, const std::string &text,
+ bool state, sf::Vector2i pos, sf::Vector2i size){
+
+ sf::IntRect rect {pos, size};
+ if( rect.contains( mousePosition() ) ){
+ hotItem = id;
+ }
+ if (hotItem == id){
+ if(mouseButtonPressed(MouseButton::Left)){
+ activeItem = id;
+ }
+ }
+ if (activeItem == id){
+ if (mouseButtonReleased(MouseButton::Left)){
+ if ( hotItem == id ){
+ state = !state;
+ }
+ activeItem = -1;
+ }
+ }
+
+ // Rendering:
+ sf::CircleShape radio{ 50 };
+ radio.setPosition( sf::Vector2f(pos) );
+ radio.setOutlineColor( sf::Color::Blue );
+ radio.setOutlineThickness( 5 );
+
+ if(state){
+ radio.setFillColor( sf::Color::Blue );
+ } else {
+
+ }
+
+ win->draw(radio);
+
+ return state;
+}
diff --git a/src/ui.h b/src/ui.h
new file mode 100644
index 0000000..16027ae
--- /dev/null
+++ b/src/ui.h
@@ -0,0 +1,46 @@
+#ifndef UI_H
+#define UI_H
+
+#include<SFML/Graphics.hpp>
+
+enum class MouseButton{
+ Left,
+ Mid,
+ Right
+};
+
+class UI{
+ public:
+ int hotItem, activeItem;
+
+ // TODO: Move MouseStatus other class
+ using MousePos = sf::Vector2i;
+ MousePos mousePos;
+ struct MouseState{
+ bool leftDown, rightDown, midDown;
+ MouseState(){
+ leftDown = false;
+ rightDown = false;
+ midDown = false;
+ }
+ } currentMouse, previousMouse;
+
+ bool mouseButtonPressed(MouseButton m);
+ bool mouseButtonReleased(MouseButton m);
+ bool mouseButtonIsDown(MouseButton m);
+ bool mouseButtonIsUp(MouseButton m);
+ sf::Vector2i mousePosition();
+ // end of MouseStatus
+
+ void handleEvent(const sf::Event &event);
+ void initImUI();
+ void finishImUI();
+ bool button(sf::RenderWindow *win, int id, const std::string &text,
+ sf::Vector2i pos, sf::Vector2i size);
+ bool radioButton(sf::RenderWindow *win, int id,
+ const std::string &text, bool state, sf::Vector2i pos,
+ sf::Vector2i size);
+
+};
+
+#endif // UI_H