|
enum RadialMenuControlType |
|
{ |
|
MOUSE, |
|
CONTROLLER |
|
} |
|
|
|
class RadialMenu : ScriptedWidgetEventHandler |
|
{ |
|
protected Widget m_Parent; |
|
protected Widget m_ItemCardsContainer; |
|
protected Widget m_RadialSelector; |
|
protected ImageWidget m_RadialSelectorImage; |
|
protected ImageWidget m_RadialSelectorPointerImage; |
|
protected int m_RadialSelectorOriginalColor; |
|
protected int m_RadialSelectorDisabledColor; |
|
protected Widget m_SelectedObject; |
|
protected ref map<Widget, float> m_RadialItemCards; |
|
|
|
protected float m_AngleRadOffset; |
|
protected ref Timer m_UpdateTimer; |
|
|
|
|
|
static const string RADIAL_SELECTOR = "RadialSelector"; |
|
static const string RADIAL_SELECTOR_IMAGE = "SelectorImage"; |
|
static const string RADIAL_SELECTOR_POINTER = "SelectorPointer"; |
|
static const string RADIAL_DELIMITER_CONTAINER = "RadialDelimiterContainer"; |
|
static const string RADIAL_ITEM_CARD_CONTAINER = "RadialItemCardContainer"; |
|
|
|
|
|
protected RadialMenuControlType m_ControlType; |
|
private UAIDWrapper m_SelectInputWrapper; |
|
private UAIDWrapper m_BackInputWrapper; |
|
protected float m_ControllerAngle; |
|
protected float m_ControllerTilt; |
|
|
|
|
|
protected int m_ControllerTimout; |
|
protected bool m_IsControllerTimoutEnabled = true; |
|
protected const float CONTROLLER_DESELECT_TIMEOUT = 1000; |
|
protected const float CONTROLLER_TILT_TRESHOLD_SELECT = 0.8; |
|
protected const float CONTROLLER_TILT_TRESHOLD_EXECUTE = 1.0; |
|
|
|
|
|
protected bool m_WidgetInitialized; |
|
protected const float MOUSE_SAFE_ZONE_RADIUS = 120; |
|
|
|
|
|
protected float m_RadiusOffset; |
|
protected float m_ExecuteDistanceOffset; |
|
protected float m_OffsetFromTop; |
|
protected float m_ItemCardRadiusOffset; |
|
protected string m_DelimiterLayout; |
|
|
|
ref UIScriptedMenu m_RegisteredClass; |
|
ref static RadialMenu m_Instance; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RadialMenu() |
|
{ |
|
m_Instance = this; |
|
|
|
|
|
#ifdef PLATFORM_CONSOLE |
|
Input inp = GetGame().GetInput(); |
|
if (inp && inp.IsEnabledMouseAndKeyboardEvenOnServer()) |
|
{ |
|
m_ControlType = RadialMenuControlType.MOUSE; |
|
} |
|
else |
|
{ |
|
m_ControlType = RadialMenuControlType.CONTROLLER; |
|
} |
|
#endif |
|
#ifdef PLATFORM_WINDOWS |
|
m_ControlType = RadialMenuControlType.MOUSE; |
|
#endif |
|
|
|
m_SelectInputWrapper = GetUApi().GetInputByID(UAUISelect).GetPersistentWrapper(); |
|
m_BackInputWrapper= GetUApi().GetInputByID(UAUIBack).GetPersistentWrapper(); |
|
|
|
|
|
m_RadialItemCards = new map<Widget, float>; |
|
m_UpdateTimer = new Timer(); |
|
m_UpdateTimer.Run(0.01, this, "Update", NULL, true); |
|
} |
|
|
|
void ~RadialMenu() |
|
{ |
|
} |
|
|
|
static RadialMenu GetInstance() |
|
{ |
|
return m_Instance; |
|
} |
|
|
|
|
|
void OnWidgetScriptInit(Widget w) |
|
{ |
|
m_ItemCardsContainer = w.FindAnyWidget(RADIAL_ITEM_CARD_CONTAINER); |
|
m_RadialSelector = w.FindAnyWidget(RADIAL_SELECTOR); |
|
m_RadialSelectorImage = ImageWidget.Cast(m_RadialSelector.FindAnyWidget(RADIAL_SELECTOR_IMAGE)); |
|
m_RadialSelectorPointerImage = ImageWidget.Cast(m_RadialSelector.FindAnyWidget(RADIAL_SELECTOR_POINTER)); |
|
|
|
m_RadialSelectorOriginalColor = m_RadialSelectorImage.GetColor(); |
|
m_RadialSelectorDisabledColor = ARGB(255,150,150,150); |
|
|
|
|
|
m_Parent = w; |
|
m_Parent.SetHandler(this); |
|
} |
|
|
|
|
|
void SetControlType(RadialMenuControlType type) |
|
{ |
|
if (m_ControlType != type) |
|
{ |
|
m_ControlType = type; |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControlsChanged", NULL, type); |
|
} |
|
} |
|
|
|
bool IsUsingMouse() |
|
{ |
|
if (m_ControlType == RadialMenuControlType.MOUSE) |
|
{ |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
bool IsUsingController() |
|
{ |
|
if (m_ControlType == RadialMenuControlType.CONTROLLER) |
|
{ |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
void SetWidgetInitialized(bool state) |
|
{ |
|
m_WidgetInitialized = state; |
|
} |
|
|
|
bool IsWidgetInitialized() |
|
{ |
|
return m_WidgetInitialized; |
|
} |
|
|
|
|
|
|
|
|
|
void RegisterClass(UIScriptedMenu class_name) |
|
{ |
|
m_RegisteredClass = class_name; |
|
if (m_UpdateTimer && !m_UpdateTimer.IsRunning()) |
|
m_UpdateTimer.Run(0.01, this, "Update", NULL, true); |
|
} |
|
|
|
|
|
|
|
void SetRadiusOffset(float radius_offset) |
|
{ |
|
m_RadiusOffset = radius_offset; |
|
} |
|
|
|
|
|
void SetExecuteDistOffset(float execute_dist_offset) |
|
{ |
|
m_ExecuteDistanceOffset = execute_dist_offset; |
|
} |
|
|
|
|
|
void SetOffsetFromTop(float offset_from_top) |
|
{ |
|
m_OffsetFromTop = offset_from_top; |
|
} |
|
|
|
|
|
void SetItemCardRadiusOffset(float item_card_radius_offset) |
|
{ |
|
m_ItemCardRadiusOffset = item_card_radius_offset; |
|
} |
|
|
|
|
|
void ActivateControllerTimeout(bool state) |
|
{ |
|
m_IsControllerTimoutEnabled = state; |
|
} |
|
|
|
void SetWidgetProperties(string delimiter_layout) |
|
{ |
|
m_DelimiterLayout = delimiter_layout; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Refresh(bool hide_selector = true) |
|
{ |
|
int item_cards_count = GetItemCardsCount(); |
|
if (item_cards_count > 0) |
|
m_AngleRadOffset = 2 * Math.PI / item_cards_count; |
|
float angle_rad = -Math.PI / 2; |
|
|
|
|
|
if (m_OffsetFromTop != 0) |
|
{ |
|
angle_rad = angle_rad + m_OffsetFromTop; |
|
} |
|
|
|
|
|
|
|
Widget delimiters_panel = m_Parent.FindAnyWidget(RADIAL_DELIMITER_CONTAINER); |
|
if (delimiters_panel) |
|
{ |
|
Widget del_child = delimiters_panel.GetChildren(); |
|
while (del_child) |
|
{ |
|
Widget child_to_destroy1 = del_child; |
|
del_child = del_child.GetSibling(); |
|
|
|
delete child_to_destroy1; |
|
} |
|
} |
|
|
|
|
|
Widget item_cards_panel = m_Parent.FindAnyWidget(RADIAL_ITEM_CARD_CONTAINER); |
|
Widget item_card = item_cards_panel.GetChildren(); |
|
|
|
|
|
float original_r = GetRadius(); |
|
float item_cards_r = original_r; |
|
|
|
|
|
if (m_ItemCardRadiusOffset != 0) |
|
{ |
|
item_cards_r = item_cards_r * m_ItemCardRadiusOffset; |
|
if (item_cards_r < 0) item_cards_r = 0; |
|
} |
|
|
|
m_RadialItemCards.Clear(); |
|
for (int i = 0; i < item_cards_count; ++i) |
|
{ |
|
|
|
if (item_card) |
|
{ |
|
|
|
float pos_x = item_cards_r * Math.Cos(angle_rad); |
|
float pos_y = item_cards_r * Math.Sin(angle_rad); |
|
|
|
pos_x = pos_x / original_r; |
|
pos_y = pos_y / original_r; |
|
|
|
item_card.SetPos(pos_x, pos_y); |
|
|
|
|
|
m_RadialItemCards.Insert(item_card, angle_rad); |
|
|
|
|
|
item_card = item_card.GetSibling(); |
|
} |
|
|
|
|
|
|
|
if (item_cards_count > 1 && delimiters_panel && m_DelimiterLayout) |
|
{ |
|
Widget delimiter_widget = GetGame().GetWorkspace().CreateWidgets(m_DelimiterLayout, delimiters_panel); |
|
float delim_angle_rad = angle_rad + (m_AngleRadOffset / 2); |
|
delimiter_widget.SetPos(0, 0); |
|
delimiter_widget.SetRotation(0, 0, GetAngleInDegrees(delim_angle_rad) + 90); |
|
} |
|
|
|
|
|
angle_rad += m_AngleRadOffset; |
|
} |
|
|
|
|
|
if (hide_selector) |
|
{ |
|
HideRadialSelector(); |
|
} |
|
} |
|
|
|
|
|
protected void ShowRadialSelector(Widget selected_item) |
|
{ |
|
if (m_RadialSelector && selected_item) |
|
{ |
|
int item_count = m_RadialItemCards.Count(); |
|
if (item_count > 1) |
|
{ |
|
int angle_deg = GetAngleInDegrees(m_RadialItemCards.Get(selected_item)); |
|
m_RadialSelector.SetRotation(0, 0, angle_deg + 90); |
|
|
|
|
|
float progress = (1 / item_count) * 2; |
|
m_RadialSelectorImage.SetMaskProgress(progress); |
|
|
|
m_RadialSelector.Show(true); |
|
|
|
bool grey_selector = selected_item.GetFlags() & WidgetFlags.DISABLED; |
|
if (!grey_selector) |
|
{ |
|
m_RadialSelectorImage.SetColor(m_RadialSelectorDisabledColor); |
|
m_RadialSelectorPointerImage.SetColor(m_RadialSelectorDisabledColor); |
|
} |
|
else |
|
{ |
|
m_RadialSelectorImage.SetColor(m_RadialSelectorOriginalColor); |
|
m_RadialSelectorPointerImage.SetColor(m_RadialSelectorOriginalColor); |
|
} |
|
} |
|
} |
|
} |
|
|
|
protected void HideRadialSelector() |
|
{ |
|
if (m_RadialSelector) |
|
{ |
|
m_RadialSelector.Show(false); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
protected int GetItemCardsCount() |
|
{ |
|
Widget child = m_ItemCardsContainer.GetChildren(); |
|
int count = 0; |
|
|
|
while (child) |
|
{ |
|
++count; |
|
|
|
child = child.GetSibling(); |
|
} |
|
|
|
return count; |
|
} |
|
|
|
protected float GetRadius() |
|
{ |
|
float radius = Math.AbsFloat(GetParentMinSize() * 0.5); |
|
|
|
|
|
if (m_RadiusOffset > 0) |
|
{ |
|
return radius * m_RadiusOffset; |
|
} |
|
|
|
|
|
return radius; |
|
} |
|
|
|
protected void GetParentCenter(out float center_x, out float center_y) |
|
{ |
|
if (m_Parent) |
|
{ |
|
float wx; |
|
float wy; |
|
m_Parent.GetScreenPos(wx, wy); |
|
|
|
float ww; |
|
float wh; |
|
m_Parent.GetScreenSize(ww, wh); |
|
|
|
center_x = wx + ww / 2; |
|
center_y = wy + wh / 2; |
|
} |
|
} |
|
|
|
protected float GetParentMinSize() |
|
{ |
|
if (m_Parent) |
|
{ |
|
float size_x; |
|
float size_y; |
|
m_Parent.GetScreenSize(size_x, size_y); |
|
|
|
return Math.Min(size_x, size_y); |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected Widget GetObjectByDegAngle(float deg_angle) |
|
{ |
|
for (int i = 0; i < m_RadialItemCards.Count(); ++i) |
|
{ |
|
Widget w = m_RadialItemCards.GetKey(i); |
|
float w_angle = GetAngleInDegrees(m_RadialItemCards.Get(w)); |
|
float offset = GetAngleInDegrees(m_AngleRadOffset) / 2; |
|
float min_angle = w_angle - offset; |
|
float max_angle = w_angle + offset; |
|
|
|
if (min_angle < 0) min_angle += 360; |
|
if (max_angle > 360) max_angle -= 360; |
|
|
|
if (min_angle > max_angle) |
|
{ |
|
if (min_angle <= deg_angle) |
|
{ |
|
if (deg_angle > max_angle) |
|
{ |
|
return w; |
|
} |
|
} |
|
else |
|
{ |
|
if (deg_angle < max_angle) |
|
{ |
|
return w; |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
if (deg_angle >= min_angle && deg_angle < max_angle) |
|
{ |
|
return w; |
|
} |
|
} |
|
} |
|
|
|
return NULL; |
|
} |
|
|
|
|
|
protected float GetMousePointerAngle() |
|
{ |
|
int mouse_x; |
|
int mouse_y; |
|
GetMousePos(mouse_x, mouse_y); |
|
|
|
float center_x; |
|
float center_y; |
|
GetParentCenter(center_x, center_y); |
|
|
|
float tan_x = mouse_x - center_x; |
|
float tan_y = mouse_y - center_y; |
|
float angle = Math.Atan2(tan_y, tan_x); |
|
|
|
return angle; |
|
} |
|
|
|
|
|
protected float GetMouseDistance() |
|
{ |
|
int mouse_x; |
|
int mouse_y; |
|
GetMousePos(mouse_x, mouse_y); |
|
|
|
float center_x; |
|
float center_y; |
|
GetParentCenter(center_x, center_y); |
|
|
|
float distance = vector.Distance(Vector(mouse_x, mouse_y, 0), Vector(center_x, center_y, 0)); |
|
|
|
return distance; |
|
} |
|
|
|
|
|
protected float GetAngleInDegrees(float rad_angle) |
|
{ |
|
float rad_deg = rad_angle * Math.RAD2DEG; |
|
|
|
int angle_mp = rad_deg / 360; |
|
|
|
if (rad_deg < 0) |
|
{ |
|
rad_deg = rad_deg - (360 * angle_mp); |
|
rad_deg += 360; |
|
} |
|
|
|
return rad_deg; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
int last_time = -1; |
|
protected void Update() |
|
{ |
|
if (this && !m_RegisteredClass) |
|
{ |
|
m_UpdateTimer.Stop(); |
|
return; |
|
} |
|
|
|
|
|
if (last_time < 0) |
|
{ |
|
last_time = GetGame().GetTime(); |
|
} |
|
int delta_time = GetGame().GetTime() - last_time; |
|
last_time = GetGame().GetTime(); |
|
|
|
|
|
if (this && m_RegisteredClass && m_RegisteredClass.IsVisible()) |
|
{ |
|
|
|
if (IsUsingMouse() && m_WidgetInitialized) |
|
{ |
|
float mouse_angle = GetMousePointerAngle(); |
|
float mouse_distance = GetMouseDistance(); |
|
|
|
|
|
if (mouse_distance <= MOUSE_SAFE_ZONE_RADIUS) |
|
{ |
|
|
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMouseDeselect", NULL, m_SelectedObject); |
|
m_SelectedObject = NULL; |
|
|
|
HideRadialSelector(); |
|
} |
|
else |
|
{ |
|
|
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMouseDeselect", NULL, m_SelectedObject); |
|
|
|
|
|
m_SelectedObject = GetObjectByDegAngle(GetAngleInDegrees(mouse_angle)); |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMouseSelect", NULL, m_SelectedObject); |
|
|
|
ShowRadialSelector(m_SelectedObject); |
|
} |
|
} |
|
|
|
else if (IsUsingController()) |
|
{ |
|
UpdataControllerInput(); |
|
|
|
|
|
if (m_ControllerAngle > -1 && m_ControllerTilt > -1) |
|
{ |
|
|
|
Widget w_selected = GetObjectByDegAngle(m_ControllerAngle); |
|
|
|
if (w_selected) |
|
{ |
|
if (w_selected != m_SelectedObject) |
|
{ |
|
if (m_ControllerTilt >= CONTROLLER_TILT_TRESHOLD_SELECT) |
|
{ |
|
|
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerDeselect", NULL, m_SelectedObject); |
|
|
|
|
|
m_SelectedObject = w_selected; |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerSelect", NULL, m_SelectedObject); |
|
|
|
ShowRadialSelector(m_SelectedObject); |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerDeselect", NULL, m_SelectedObject); |
|
m_SelectedObject = NULL; |
|
|
|
HideRadialSelector(); |
|
} |
|
} |
|
|
|
else |
|
{ |
|
if (m_IsControllerTimoutEnabled) |
|
{ |
|
m_ControllerTimout += delta_time; |
|
|
|
if (m_ControllerTimout >= CONTROLLER_DESELECT_TIMEOUT) |
|
{ |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerDeselect", NULL, m_SelectedObject); |
|
m_SelectedObject = NULL; |
|
|
|
HideRadialSelector(); |
|
|
|
m_ControllerTimout = 0; |
|
} |
|
} |
|
} |
|
|
|
m_ControllerAngle = -1; |
|
m_ControllerTilt = -1; |
|
} |
|
|
|
m_WidgetInitialized = true; |
|
} |
|
} |
|
|
|
float NormalizeInvertAngle(float angle) |
|
{ |
|
float new_angle = 360 - angle; |
|
int angle_mp = new_angle / 360; |
|
|
|
new_angle = new_angle - (360 * angle_mp); |
|
|
|
return new_angle; |
|
} |
|
|
|
|
|
|
|
|
|
void UpdataControllerInput() |
|
{ |
|
Input input = GetGame().GetInput(); |
|
|
|
|
|
float angle; |
|
float tilt; |
|
input.GetGamepadThumbDirection(GamepadButton.THUMB_RIGHT, angle, tilt); |
|
angle = NormalizeInvertAngle(angle * Math.RAD2DEG); |
|
|
|
m_ControllerAngle = angle; |
|
m_ControllerTilt = tilt; |
|
m_ControllerTimout = 0; |
|
|
|
|
|
|
|
if (m_SelectInputWrapper.InputP().LocalPress()) |
|
{ |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerPressSelect", NULL, m_SelectedObject); |
|
} |
|
|
|
|
|
if (m_BackInputWrapper.InputP().LocalPress()) |
|
{ |
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerPressBack", NULL, m_SelectedObject); |
|
} |
|
} |
|
|
|
override bool OnMouseButtonUp(Widget w, int x, int y, int button) |
|
{ |
|
if (button == MouseState.LEFT && m_SelectedObject) |
|
{ |
|
|
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMousePressLeft", NULL, m_SelectedObject); |
|
|
|
return true; |
|
} |
|
|
|
if (button == MouseState.RIGHT) |
|
{ |
|
|
|
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMousePressRight", NULL, NULL); |
|
|
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
} |