tavurth / godot-radial-menu

A radial menu for Godot, supports Mobile & Desktop

Home Page:https://godotengine.org/asset-library/asset/1885

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snap to each segment

fire opened this issue · comments

Hi I was looking at your code,

Do you know how to make it is snap to each section and provide a line from the center to the cursor?

commented

Hey I'm on a motorcycle tour at the moment but I can give some pointers.

The current index of the cursor is determined by the following function:

func get_index():
if not self.count: return 0
var normalized_cursor = cursor.normalized()
var angle = PI - atan2(normalized_cursor.y, normalized_cursor.x)
# Whole circle in radians divided by the number of items
# will give us the radian step of each item
var index_offset = (2 * PI) / self.count
# Calculate the angle as a percentage of the entire circle
# divided up into equal sized arcs based on the number of items (count)
var to_return = angle / index_offset
# Clip to the min-max of our array of buttons
to_return = clamp(to_return - 1, 0, self.count - 1)
return round(to_return)

However this is for code logic, to actually render the cursor position we use shader code, which can be found here:

vec4 find_color(vec2 uv) {
// Bevel color logic
if (bevel_enabled && is_in_bevel(uv) > 0.) {
return bevel_color;
}
float current_degree = abs(find_deg(uv) - cursor_deg);
if (current_degree < cursor_size || current_degree > PI_2 - cursor_size) {
return color_fg;
}
return color_bg;
}

To snap to Index, you would probably just convert the position of the current degree to snapped percentage based on the number of items you have in your selection. That can be found in this line:

float current_degree = abs(find_deg(uv) - cursor_deg);

About a line from the center, perhaps you can draw a small diagram to help me understand what you're going for?

commented

If the line should reach the cursor wherever it is on screen, then I would use a canvas layer and just draw that line in a shader.

The Shader in my code only applies to the circle, so it would be easier to use an overlay. Or you could just use a Line2D node on the overlay layer and set the end point to the mouse position.