summaryrefslogtreecommitdiff
path: root/src/ui.cpp
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 /src/ui.cpp
parentbf34d835e4ea49bd4ef2e5de2b1fea2fd3c95adc (diff)
Sketch ImGUI:
- Still needs font rendering
Diffstat (limited to 'src/ui.cpp')
-rw-r--r--src/ui.cpp187
1 files changed, 187 insertions, 0 deletions
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;
+}