Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joystick Slider #5186

Closed
TYSON-Alii opened this issue Apr 12, 2022 · 0 comments
Closed

Joystick Slider #5186

TYSON-Alii opened this issue Apr 12, 2022 · 0 comments

Comments

@TYSON-Alii
Copy link

TYSON-Alii commented Apr 12, 2022

Hello,

Example Usage:

im::Begin("debug");
static float bg_scale = 100.f, b_scale = 15.f;
static vex2f v = 0.f;
joystick(v, bg_scale, b_scale);
im::DragFloat("bg scale", &bg_scale, 0.5, b_scale + 5, 500.f);
im::DragFloat("b scale", &b_scale, 0.5, 2, bg_scale - 5);
im::Text("%f, %f", v.x, v.y);
im::End();

Ekran görüntüsü 2022-04-12 135348

C++

void JoystickSlider(float& _x, float& _y, float scale = 100.f, float b_scale = 15.f, uint bg_color = IM_COL32(0, 0, 0, 155), uint button_color = IM_COL32(215, 215, 215, 255), const uint& mouse_button = 0u) {
	namespace im = ImGui;
	ImDrawList* draw_list = im::GetWindowDrawList();
	const auto& p = im::GetCursorScreenPos();
	static bool button_clicked = false;
	const auto& mouse = im::GetIO().MousePos;
	draw_list->AddCircleFilled(ImVec2(p.x + scale, p.y + scale), scale, bg_color, 50);
	auto button_x = _x * (scale - b_scale) + p.x + scale;
	auto button_y = _y * (scale - b_scale) + p.y + scale;
	static float toward = 0.f;
	im::ButtonBehavior(ImRect({ button_x - b_scale, button_y - b_scale}, { button_x + b_scale, button_y + b_scale }), im::GetCurrentWindow()->ID, nullptr, nullptr, 0);
	if (sqrtf(pow(mouse.x - button_x, 2) + pow(mouse.y - button_y, 2)) < b_scale)
		if (im::GetIO().MouseClicked[mouse_button])
			button_clicked = true;
	if (!im::GetIO().MouseDown[mouse_button])
		button_clicked = false;
	if (button_clicked) {
		button_x = mouse.x;
		button_y = mouse.y;
	};
	toward = -atan2(button_x - p.x - scale, button_y - p.y - scale);
	if (sqrtf(pow(p.x - button_x + scale, 2) + pow(p.y - button_y + scale, 2)) > scale - b_scale) {
		button_x = p.x + scale + cos(toward - 0.5f * 3.14f) * -(scale-b_scale);
		button_y = p.y + scale + sin(toward - 0.5f * 3.14f) * -(scale-b_scale);
	};
	draw_list->AddCircleFilled(ImVec2(button_x, button_y), b_scale, button_color, 25);
	im::Dummy(ImVec2(scale*2, scale*2));
	_x = (button_x - p.x - scale) / (scale - b_scale);
	_y = (button_y - p.y - scale) / (scale - b_scale);
};

>=C++17

void JoystickSlider(auto& _vector2f, float scale = 100.f, float b_scale = 15.f, uint bg_color = IM_COL32(0, 0, 0, 155), uint button_color = IM_COL32(215, 215, 215, 255), const uint& mouse_button = 0u) {
	namespace im = ImGui;
	ImDrawList* draw_list = im::GetWindowDrawList();
	const auto& p = im::GetCursorScreenPos();
	static bool button_clicked = false;
	const auto& mouse = im::GetIO().MousePos;
	draw_list->AddCircleFilled(ImVec2(p.x + scale, p.y + scale), scale, bg_color, 50);
	auto button_x = _vector2f.x * (scale - b_scale) + p.x + scale;
	auto button_y = _vector2f.y * (scale - b_scale) + p.y + scale;
	static float toward = 0.f;
	im::ButtonBehavior(ImRect({ button_x - b_scale, button_y - b_scale}, { button_x + b_scale, button_y + b_scale }), im::GetCurrentWindow()->ID, nullptr, nullptr, 0);
	if (sqrtf(pow(mouse.x - button_x, 2) + pow(mouse.y - button_y, 2)) < b_scale)
		if (im::GetIO().MouseClicked[mouse_button])
			button_clicked = true;
	if (!im::GetIO().MouseDown[mouse_button])
		button_clicked = false;
	if (button_clicked) {
		button_x = mouse.x;
		button_y = mouse.y;
	};
	toward = -atan2(button_x - p.x - scale, button_y - p.y - scale);
	if (sqrtf(pow(p.x - button_x + scale, 2) + pow(p.y - button_y + scale, 2)) > scale - b_scale) {
		button_x = p.x + scale + cos(toward - 0.5f * 3.14f) * -(scale-b_scale);
		button_y = p.y + scale + sin(toward - 0.5f * 3.14f) * -(scale-b_scale);
	};
	draw_list->AddCircleFilled(ImVec2(button_x, button_y), b_scale, button_color, 25);
	im::Dummy(ImVec2(scale*2, scale*2));
	_vector2f.x = (button_x - p.x - scale) / (scale - b_scale);
	_vector2f.y = (button_y - p.y - scale) / (scale - b_scale);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants