File size: 17,935 Bytes
24b81cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
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;
//widget
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";
//controls
protected RadialMenuControlType m_ControlType;
private UAIDWrapper m_SelectInputWrapper;
private UAIDWrapper m_BackInputWrapper;
protected float m_ControllerAngle;
protected float m_ControllerTilt;
//controller
protected int m_ControllerTimout;
protected bool m_IsControllerTimoutEnabled = true; //enables/disables controller deselect timeout reset
protected const float CONTROLLER_DESELECT_TIMEOUT = 1000; //timeout [ms] after which selection is automatically deselect when controller is not active
protected const float CONTROLLER_TILT_TRESHOLD_SELECT = 0.8; //tilt value (0.0-1.0) for controller sticks after which the selection will be selected
protected const float CONTROLLER_TILT_TRESHOLD_EXECUTE = 1.0; //tilt value (0.0-1.0) for controller sticks after which the selection will be executed
//mouse
protected bool m_WidgetInitialized;
protected const float MOUSE_SAFE_ZONE_RADIUS = 120; //Radius [px] of safe zone where every previous selection is deselected
//References
protected float m_RadiusOffset; //Radius [% of the main container size]
protected float m_ExecuteDistanceOffset; //Distance offset [% of the main container size] after which the selection will be automatically executed
protected float m_OffsetFromTop; //first item in the menu won't be directly on top but offset by a rad angle value (clock-wise)
protected float m_ItemCardRadiusOffset; //Radius [% of the main container size] for item cards
protected string m_DelimiterLayout; //layout file name with path
ref UIScriptedMenu m_RegisteredClass;
ref static RadialMenu m_Instance;
/*
RADIAL MENU EVENTS
Mouse:
OnMouseSelect
OnMouseDeselect
OnMouseExecute - unused, press events used instead
OnMousePressLeft
OnMousePressRight
Controller:
OnControllerSelect
OnControllerDeselect
OnControllerExecute - unused, press events used instead
OnControllerPressSelect
OnControllerPressBack
Common:
OnControlsChanged - controls has been changed (mouse<->controller)
*/
//============================================
// RadialMenu
//============================================
void RadialMenu()
{
m_Instance = this;
//set default control type
#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();
//radial cards
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;
}
//Set handler
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);
//parent
m_Parent = w;
m_Parent.SetHandler(this);
}
//controls
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;
}
//============================================
// Setup
//============================================
void RegisterClass(UIScriptedMenu class_name)
{
m_RegisteredClass = class_name;
if (m_UpdateTimer && !m_UpdateTimer.IsRunning())
m_UpdateTimer.Run(0.01, this, "Update", NULL, true);
}
//Set radial menu parameters
//Radius offset [% of the main container size]
void SetRadiusOffset(float radius_offset)
{
m_RadiusOffset = radius_offset;
}
//Distance offset [% of the main container size] after which the selection will be automatically executed
void SetExecuteDistOffset(float execute_dist_offset)
{
m_ExecuteDistanceOffset = execute_dist_offset;
}
//First item in the menu won't be directly on top but offset by a rad angle value (clock-wise)
void SetOffsetFromTop(float offset_from_top)
{
m_OffsetFromTop = offset_from_top;
}
//Radius [% of the main container size] for item cards
void SetItemCardRadiusOffset(float item_card_radius_offset)
{
m_ItemCardRadiusOffset = item_card_radius_offset;
}
//Enable/Disable controller timeout
void ActivateControllerTimeout(bool state)
{
m_IsControllerTimoutEnabled = state;
}
void SetWidgetProperties(string delimiter_layout)
{
m_DelimiterLayout = delimiter_layout;
}
//============================================
// Visual
//============================================
//hide_selector => shows/hides radial selector when refreshing radial menu
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;
//--PARAM top offset--
if (m_OffsetFromTop != 0)
{
angle_rad = angle_rad + m_OffsetFromTop;
}
//--------------------
//delete all delimiters
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;
}
}
//Position item cards, crate radial delimiters
Widget item_cards_panel = m_Parent.FindAnyWidget(RADIAL_ITEM_CARD_CONTAINER);
Widget item_card = item_cards_panel.GetChildren();
//get radius
float original_r = GetRadius();
float item_cards_r = original_r;
//--PARAM top offset--....
if (m_ItemCardRadiusOffset != 0)
{
item_cards_r = item_cards_r * m_ItemCardRadiusOffset;
if (item_cards_r < 0) item_cards_r = 0; //min radius is 0
}
m_RadialItemCards.Clear();
for (int i = 0; i < item_cards_count; ++i)
{
//position item cards
if (item_card)
{
//creates circle from simple widget items
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);
//store item card
m_RadialItemCards.Insert(item_card, angle_rad);
//get next child
item_card = item_card.GetSibling();
}
//-------------------------
//create delimiter
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);
}
//calculate next angle
angle_rad += m_AngleRadOffset;
}
//hide selector on refresh
if (hide_selector)
{
HideRadialSelector();
}
}
//Radial selector
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); //rotate widget according to its desired rotation
//set radial selector size
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);
}
}
//============================================
// Widget size calculations
//============================================
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);
//PARAM --radius--
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
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;
}
//============================================
// Angle calculations
//============================================
//get object by angle (degrees)
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; //clamp 0-360
if (max_angle > 360) max_angle -= 360;
if (min_angle > max_angle) //angle radius is in the cycling point 360->
{
if (min_angle <= deg_angle) //is cursor position also before this point
{
if (deg_angle > max_angle)
{
return w;
}
}
else //is cursor position after this point
{
if (deg_angle < max_angle)
{
return w;
}
}
}
else
{
if (deg_angle >= min_angle && deg_angle < max_angle) //min, max angles are within 0-360 radius
{
return w;
}
}
}
return NULL;
}
//returns GUI compatible mouse-to-parent angle
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;
}
//returns distance from parent center
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;
}
//return angle 0-360 deg
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;
}
//============================================
// Update
//============================================
//mouse
int last_time = -1;
protected void Update()
{
if (this && !m_RegisteredClass)
{
m_UpdateTimer.Stop();
return;
}
//get delta time
if (last_time < 0)
{
last_time = GetGame().GetTime();
}
int delta_time = GetGame().GetTime() - last_time;
last_time = GetGame().GetTime();
//controls
if (this && m_RegisteredClass && m_RegisteredClass.IsVisible())
{
//mouse controls
if (IsUsingMouse() && m_WidgetInitialized)
{
float mouse_angle = GetMousePointerAngle();
float mouse_distance = GetMouseDistance();
//--PARAM --safe zone radius--
if (mouse_distance <= MOUSE_SAFE_ZONE_RADIUS)
{
//Deselect
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMouseDeselect", NULL, m_SelectedObject);
m_SelectedObject = NULL;
//hide selector
HideRadialSelector();
}
else
{
//Deselect
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMouseDeselect", NULL, m_SelectedObject);
//Select
m_SelectedObject = GetObjectByDegAngle(GetAngleInDegrees(mouse_angle));
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMouseSelect", NULL, m_SelectedObject);
//show selector
ShowRadialSelector(m_SelectedObject);
}
}
//controller controls
else if (IsUsingController())
{
UpdataControllerInput();
//Controller tilt
if (m_ControllerAngle > -1 && m_ControllerTilt > -1)
{
//Right analogue stick
Widget w_selected = GetObjectByDegAngle(m_ControllerAngle);
//Select
if (w_selected)
{
if (w_selected != m_SelectedObject)
{
if (m_ControllerTilt >= CONTROLLER_TILT_TRESHOLD_SELECT)
{
//Deselect
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerDeselect", NULL, m_SelectedObject);
//Select new object
m_SelectedObject = w_selected;
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerSelect", NULL, m_SelectedObject);
//show selector
ShowRadialSelector(m_SelectedObject);
}
}
}
else
{
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerDeselect", NULL, m_SelectedObject);
m_SelectedObject = NULL;
//hide selector
HideRadialSelector();
}
}
//if controller is giving no feedback
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;
//hide selector
HideRadialSelector();
m_ControllerTimout = 0; //reset controller timeout
}
}
}
m_ControllerAngle = -1; //reset angle and tilt
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;
}
//============================================
// Controls
//============================================
void UpdataControllerInput()
{
Input input = GetGame().GetInput();
//Controller radial
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; //reset controller timeout
//Controller buttons
//Select (A,cross)
if (m_SelectInputWrapper.InputP().LocalPress())
{
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnControllerPressSelect", NULL, m_SelectedObject);
}
//Back (B,circle)
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/* && w == m_SelectedObject*/)
{
//Execute
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMousePressLeft", NULL, m_SelectedObject);
return true;
}
if (button == MouseState.RIGHT)
{
//Back one level
GetGame().GameScript.CallFunction(m_RegisteredClass, "OnMousePressRight", NULL, NULL);
return true;
}
return false;
}
} |