diff options
-rw-r--r-- | include/gui/Button.hpp | 1 | ||||
-rw-r--r-- | src/gui/Button.cpp | 30 | ||||
-rw-r--r-- | src/main.cpp | 72 |
3 files changed, 75 insertions, 28 deletions
diff --git a/include/gui/Button.hpp b/include/gui/Button.hpp index 145d869..84098b2 100644 --- a/include/gui/Button.hpp +++ b/include/gui/Button.hpp @@ -15,5 +15,6 @@ namespace gsr { private: mgl::vec2f size; bool mouse_inside = false; + bool pressed_inside = false; }; }
\ No newline at end of file diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 2250560..d7df26a 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -10,7 +10,6 @@ namespace gsr { } void Button::on_event(mgl::Event &event, mgl::Window&) { - /* if(event.type == mgl::Event::MouseMoved) { const bool inside = mgl::FloatRect(position, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); if(mouse_inside && !inside) { @@ -18,34 +17,25 @@ namespace gsr { } else if(!mouse_inside && inside) { mouse_inside = true; } - } else if(event.type == mgl::Event::MouseButtonPressed && mouse_inside) { - - } - */ - if(event.type == mgl::Event::MouseButtonPressed && mouse_inside) { - if(on_click) + } else if(event.type == mgl::Event::MouseButtonPressed) { + pressed_inside = mouse_inside; + } else if(event.type == mgl::Event::MouseButtonReleased) { + const bool clicked_inside = pressed_inside && mouse_inside; + pressed_inside = false; + if(clicked_inside && on_click) on_click(); } } void Button::draw(mgl::Window &window) { - const bool inside = mgl::FloatRect(position, size).contains(window.get_mouse_position().to_vec2f()); - if(mouse_inside && !inside) { - mouse_inside = false; - } else if(!mouse_inside && inside) { - mouse_inside = true; - } - if(mouse_inside) { // Background - /* { mgl::Rectangle rect(size); rect.set_position(position); rect.set_color(mgl::Color(20, 20, 20, 255)); window.draw(rect); } - */ const int border_size = 5; const mgl::Color border_color(118, 185, 0); @@ -68,16 +58,16 @@ namespace gsr { // Green line at left { - mgl::Rectangle rect({ border_size, size.y }); - rect.set_position(position); + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(position + mgl::vec2f(0, border_size)); rect.set_color(border_color); window.draw(rect); } // Green line at right { - mgl::Rectangle rect({ border_size, size.y }); - rect.set_position(position + mgl::vec2f(size.x - border_size, 0.0f)); + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(position + mgl::vec2f(size.x - border_size, border_size)); rect.set_color(border_color); window.draw(rect); } diff --git a/src/main.cpp b/src/main.cpp index ac0b7dd..b9985e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -252,9 +252,6 @@ int main(int argc, char **argv) { main_buttons[2].mode = gsr::GsrMode::Stream; - const int per_button_width = 425 * get_config().scale; - const mgl::vec2i overlay_desired_size(per_button_width * (int)main_buttons.size(), 300 * get_config().scale); - XGCValues xgcv; xgcv.foreground = WhitePixel(display, DefaultScreen(display)); xgcv.line_width = 1; @@ -265,6 +262,12 @@ int main(int argc, char **argv) { auto update_overlay_shape = [&]() { const int main_button_margin = 20 * get_config().scale; + const int spacing = 10 * get_config().scale; + const int combined_spacing = spacing * std::max(0, (int)main_buttons.size() - 1); + + const int per_button_width = 425 * get_config().scale; + const mgl::vec2i overlay_desired_size(per_button_width * (int)main_buttons.size() + combined_spacing, 300 * get_config().scale); + const mgl::vec2i main_buttons_start_pos = target_window_size/2 - overlay_desired_size/2; mgl::vec2i main_button_pos = main_buttons_start_pos; @@ -302,12 +305,12 @@ int main(int argc, char **argv) { shapes[i] = { (short)main_button_pos.x, (short)main_button_pos.y, (unsigned short)per_button_width, (unsigned short)overlay_desired_size.y }; - main_button_pos.x += per_button_width; + main_button_pos.x += per_button_width + combined_spacing; } const mgl::vec2i settings_button_size(128 * get_config().scale, 128 * get_config().scale); shapes[main_buttons.size()] = { - (short)(main_buttons_start_pos.x + overlay_desired_size.x), (short)(main_buttons_start_pos.y - settings_button_size.y), + (short)(main_buttons_start_pos.x + overlay_desired_size.x + 50 * get_config().scale), (short)(main_buttons_start_pos.y - settings_button_size.y - 50 * get_config().scale), (unsigned short)settings_button_size.x, (unsigned short)settings_button_size.y }; @@ -342,13 +345,57 @@ int main(int argc, char **argv) { Cursor default_cursor = XCreateFontCursor(display, XC_arrow); - // TODO: Retry if these fail - XGrabPointer(display, window.get_system_handle(), True, ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, default_cursor, CurrentTime); + // TODO: Capture target window texture and display that as background, but that doesn't work right now because capture will capture our window which makes the capture recursive. + // TODO: Capture end ui should not show up in the recording, but it does because the ui becomes part of the target window texture because the ui is a child of the target window. + + // TODO: Retry if these fail. + // TODO: Hmm, these dont work in owlboy. Maybe owlboy uses xi2 and that breaks this (does it?). + XGrabPointer(display, window.get_system_handle(), True, + ButtonPressMask | ButtonReleaseMask | PointerMotionMask | + Button1MotionMask | + Button2MotionMask | + Button3MotionMask | + Button4MotionMask | + Button5MotionMask | + ButtonMotionMask, + GrabModeAsync, GrabModeAsync, None, default_cursor, CurrentTime); XGrabKeyboard(display, window.get_system_handle(), True, GrabModeAsync, GrabModeAsync, CurrentTime); + //XGrabServer(display); + + WindowTexture target_window_texture; + if(window_texture_init(&target_window_texture, display, target_window) != 0) { + fprintf(stderr, "Error: failed to capture window\n"); + return 1; + } + + int target_window_texture_width = 0; + int target_window_texture_height = 0; + window_texture_get_size_or(&target_window_texture, &target_window_texture_width, &target_window_texture_height, target_window_size.x, target_window_size.y); + + mgl_texture window_texture_ref = { + window_texture_get_opengl_texture_id(&target_window_texture), + target_window_texture_width, + target_window_texture_height, + MGL_TEXTURE_FORMAT_RGB, + 32768, + 32768, + true + }; + + mgl::Texture window_texture = mgl::Texture::reference(window_texture_ref); + mgl::Sprite window_texture_sprite(&window_texture); + XEvent xev; mgl::Event event; + event.type = mgl::Event::MouseMoved; + event.mouse_move.x = window.get_mouse_position().x; + event.mouse_move.y = window.get_mouse_position().y; + for(auto &main_button : main_buttons) { + main_button.button.on_event(event, window); + } + while(window.is_open()) { if(XCheckTypedWindowEvent(display, target_window, DestroyNotify, &xev)) { window.close(); @@ -357,7 +404,10 @@ int main(int argc, char **argv) { if(XCheckTypedWindowEvent(display, target_window, VisibilityNotify, &xev)) { if(xev.xvisibility.state) { - + window_texture_on_resize(&target_window_texture); + window_texture_ref.id = window_texture_get_opengl_texture_id(&target_window_texture); + window_texture_get_size_or(&target_window_texture, &window_texture_ref.width, &window_texture_ref.height, target_window_size.x, target_window_size.y); + window_texture = mgl::Texture::reference(window_texture_ref); } } @@ -367,6 +417,11 @@ int main(int argc, char **argv) { target_window_size.y = xev.xconfigure.height; window.set_size(target_window_size); update_overlay_shape(); + + window_texture_on_resize(&target_window_texture); + window_texture_ref.id = window_texture_get_opengl_texture_id(&target_window_texture); + window_texture_get_size_or(&target_window_texture, &window_texture_ref.width, &window_texture_ref.height, target_window_size.x, target_window_size.y); + window_texture = mgl::Texture::reference(window_texture_ref); } if(window.poll_event(event)) { @@ -383,6 +438,7 @@ int main(int argc, char **argv) { } window.clear(mgl::Color(37, 43, 47)); + //window.draw(window_texture_sprite); for(auto &main_button : main_buttons) { main_button.button.draw(window); window.draw(main_button.icon); |