mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-25 19:02:07 -05:00
MouseDemo: A more visual approach for MouseEvents :^)
This commit is contained in:
parent
21712ed0a3
commit
662e47e140
1 changed files with 106 additions and 52 deletions
|
@ -9,78 +9,138 @@
|
|||
#include <LibGUI/Widget.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Path.h>
|
||||
|
||||
static unsigned s_mouse_button_state;
|
||||
#include <math.h>
|
||||
|
||||
class MouseButtonIndicator final : public GUI::Frame {
|
||||
C_OBJECT(MouseButtonIndicator);
|
||||
class MainFrame final : public GUI::Frame {
|
||||
C_OBJECT(MainFrame);
|
||||
|
||||
public:
|
||||
virtual ~MouseButtonIndicator() {}
|
||||
virtual void timer_event(Core::TimerEvent&) override
|
||||
{
|
||||
|
||||
m_show_scroll_wheel = false;
|
||||
stop_timer();
|
||||
update();
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void paint_event(GUI::PaintEvent& event) override
|
||||
{
|
||||
Frame::paint_event(event);
|
||||
|
||||
GUI::Painter painter(*this);
|
||||
painter.add_clip_rect(event.rect());
|
||||
painter.fill_rect(frame_inner_rect(), Color::White);
|
||||
|
||||
Color background_color;
|
||||
Color foreground_color;
|
||||
Gfx::Path path;
|
||||
// draw mouse outline
|
||||
path.move_to({ 30, 140 });
|
||||
path.line_to({ 30, 20 });
|
||||
path.line_to({ 65, 12 });
|
||||
path.line_to({ 95, 12 });
|
||||
path.line_to({ 130, 20 });
|
||||
path.line_to({ 130, 140 });
|
||||
path.line_to({ 30, 140 });
|
||||
|
||||
if (s_mouse_button_state & m_button) {
|
||||
background_color = Color::Black;
|
||||
foreground_color = Color::White;
|
||||
} else {
|
||||
background_color = Color::White;
|
||||
foreground_color = Color::Black;
|
||||
// draw button separator
|
||||
path.move_to({ 30, 65 });
|
||||
path.line_to({ 130, 65 });
|
||||
|
||||
path.move_to({ 65, 65 });
|
||||
path.line_to({ 65, 13 });
|
||||
|
||||
path.move_to({ 95, 65 });
|
||||
path.line_to({ 95, 13 });
|
||||
|
||||
// draw fw and back button outlines
|
||||
path.move_to({ 30, 43 });
|
||||
path.line_to({ 25, 43 });
|
||||
path.line_to({ 25, 60 });
|
||||
path.line_to({ 30, 60 });
|
||||
|
||||
path.move_to({ 30, 70 });
|
||||
path.line_to({ 25, 70 });
|
||||
path.line_to({ 25, 87 });
|
||||
path.line_to({ 30, 87 });
|
||||
|
||||
painter.stroke_path(path, Color::Black, 1);
|
||||
|
||||
if (m_buttons & GUI::MouseButton::Left) {
|
||||
painter.fill_rect({ 31, 21, 34, 44 }, Color::Blue);
|
||||
painter.draw_triangle({ 30, 21 }, { 65, 21 }, { 65, 12 }, Color::Blue);
|
||||
}
|
||||
|
||||
painter.fill_rect(frame_inner_rect(), background_color);
|
||||
painter.draw_text(frame_inner_rect(), m_name, Gfx::TextAlignment::Center, foreground_color);
|
||||
if (m_buttons & GUI::MouseButton::Right) {
|
||||
painter.fill_rect({ 96, 21, 34, 44 }, Color::Blue);
|
||||
painter.draw_triangle({ 96, 12 }, { 96, 21 }, { 132, 21 }, Color::Blue);
|
||||
}
|
||||
|
||||
if (m_buttons & GUI::MouseButton::Middle)
|
||||
painter.fill_rect({ 66, 13, 29, 52 }, Color::Blue);
|
||||
|
||||
if (m_buttons & GUI::MouseButton::Forward)
|
||||
painter.fill_rect({ 26, 44, 4, 16 }, Color::Blue);
|
||||
|
||||
if (m_buttons & GUI::MouseButton::Back)
|
||||
painter.fill_rect({ 26, 71, 4, 16 }, Color::Blue);
|
||||
|
||||
if (m_show_scroll_wheel) {
|
||||
auto radius = 10;
|
||||
auto off_x = 80;
|
||||
auto off_y = 38;
|
||||
|
||||
Gfx::Point p1;
|
||||
Gfx::Point p2;
|
||||
Gfx::Point p3;
|
||||
Gfx::Point p4;
|
||||
|
||||
p1.set_x(radius * cos(M_PI * m_wheel_delta_acc / 18) + off_x);
|
||||
p1.set_y(radius * sin(M_PI * m_wheel_delta_acc / 18) + off_y);
|
||||
|
||||
p2.set_x(radius * cos(M_PI * (m_wheel_delta_acc + 18) / 18) + off_x);
|
||||
p2.set_y(radius * sin(M_PI * (m_wheel_delta_acc + 18) / 18) + off_y);
|
||||
|
||||
p3.set_x(radius * cos(M_PI * (m_wheel_delta_acc + 9) / 18) + off_x);
|
||||
p3.set_y(radius * sin(M_PI * (m_wheel_delta_acc + 9) / 18) + off_y);
|
||||
|
||||
p4.set_x(radius * cos(M_PI * (m_wheel_delta_acc + 27) / 18) + off_x);
|
||||
p4.set_y(radius * sin(M_PI * (m_wheel_delta_acc + 27) / 18) + off_y);
|
||||
|
||||
painter.draw_line(p1, p2, Color::Red, 2);
|
||||
painter.draw_line(p3, p4, Color::Red, 2);
|
||||
}
|
||||
}
|
||||
|
||||
void mousedown_event(GUI::MouseEvent& event) override
|
||||
{
|
||||
event.ignore();
|
||||
m_buttons = event.buttons();
|
||||
update();
|
||||
}
|
||||
|
||||
void mouseup_event(GUI::MouseEvent& event) override
|
||||
{
|
||||
event.ignore();
|
||||
}
|
||||
|
||||
MouseButtonIndicator(const String& name, GUI::MouseButton button)
|
||||
: m_name(name)
|
||||
, m_button(button)
|
||||
{
|
||||
}
|
||||
|
||||
String m_name;
|
||||
GUI::MouseButton m_button;
|
||||
};
|
||||
|
||||
class MainWidget final : public GUI::Widget {
|
||||
C_OBJECT(MainWidget);
|
||||
|
||||
public:
|
||||
void mousedown_event(GUI::MouseEvent& event) override
|
||||
{
|
||||
s_mouse_button_state = event.buttons();
|
||||
m_buttons = event.buttons();
|
||||
update();
|
||||
Widget::mousedown_event(event);
|
||||
}
|
||||
|
||||
void mouseup_event(GUI::MouseEvent& event) override
|
||||
void mousewheel_event(GUI::MouseEvent& event) override
|
||||
{
|
||||
s_mouse_button_state = event.buttons();
|
||||
m_wheel_delta_acc = (m_wheel_delta_acc + event.wheel_delta() + 36) % 36;
|
||||
m_show_scroll_wheel = true;
|
||||
update();
|
||||
Widget::mouseup_event(event);
|
||||
if (!has_timer())
|
||||
start_timer(500);
|
||||
}
|
||||
|
||||
private:
|
||||
MainWidget() {}
|
||||
unsigned m_buttons;
|
||||
unsigned m_wheel_delta_acc;
|
||||
bool m_show_scroll_wheel;
|
||||
MainFrame()
|
||||
: m_buttons { 0 }
|
||||
, m_wheel_delta_acc { 0 }
|
||||
, m_show_scroll_wheel { false }
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
|
@ -88,18 +148,11 @@ int main(int argc, char** argv)
|
|||
GUI::Application app(argc, argv);
|
||||
auto window = GUI::Window::construct();
|
||||
window->set_title("Mouse button demo");
|
||||
window->resize(160, 155);
|
||||
|
||||
auto& main_widget = window->set_main_widget<MainWidget>();
|
||||
auto& main_widget = window->set_main_widget<MainFrame>();
|
||||
main_widget.set_fill_with_background_color(true);
|
||||
|
||||
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||
|
||||
main_widget.add<MouseButtonIndicator>("Left", GUI::MouseButton::Left);
|
||||
main_widget.add<MouseButtonIndicator>("Middle", GUI::MouseButton::Middle);
|
||||
main_widget.add<MouseButtonIndicator>("Right", GUI::MouseButton::Right);
|
||||
main_widget.add<MouseButtonIndicator>("Back", GUI::MouseButton::Back);
|
||||
main_widget.add<MouseButtonIndicator>("Forward", GUI::MouseButton::Forward);
|
||||
|
||||
auto menubar = GUI::MenuBar::construct();
|
||||
auto& app_menu = menubar->add_menu("Mouse Demo");
|
||||
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app.quit(); }));
|
||||
|
@ -110,6 +163,7 @@ int main(int argc, char** argv)
|
|||
}));
|
||||
|
||||
app.set_menubar(move(menubar));
|
||||
window->set_resizable(false);
|
||||
window->show();
|
||||
return app.exec();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue