customcontrols.adoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. = customcontrols
  2. :revnumber: 2.0
  3. :revdate: 2020/07/27
  4. == Creating Custom Controls
  5. In this section, I'm going to walk you through creating a custom control and hopefully pass along a few tip and help standardize how controls are built. This way (if you feel so inclined) you can share your controls with other and all will know exactly how to use your control.
  6. === A Reusable Contextual Menu
  7. We're going to be building a reusable contextual right-click menu that will:
  8. * lock and unlock certain features of a standard window/panel
  9. * Allow you to add/remove menu items specific to each window as it is called.
  10. ==== STEP 1: Adding the framework for the new class.
  11. First, we'll set up the class and add the three standard constructors. We'll be using the default Menu style information. The class will remain abstract to leverage the existing callback method of the super class Menu.
  12. [source,java]
  13. ----
  14. public abstract class ContextualMenu extends Menu {
  15. public ContextualMenu(Screen screen, String UID, Vector2f position, boolean isScrollable) {
  16. this(screen, UID, position,
  17. screen.getStyle("Menu").getVector2f("defaultSize"),
  18. screen.getStyle("Menu").getVector4f("resizeBorders"),
  19. screen.getStyle("Menu").getString("defaultImg"),
  20. isScrollable
  21. );
  22. }
  23. public ContextualMenu(Screen screen, String UID, Vector2f position, Vector2f dimensions,
  24. boolean isScrollable) {
  25. this(screen, UID, position, dimensions,
  26. screen.getStyle("Menu").getVector4f("resizeBorders"),
  27. screen.getStyle("Menu").getString("defaultImg"),
  28. isScrollable
  29. );
  30. }
  31. public ContextualMenu(Screen screen, String UID, Vector2f position, Vector2f dimensions,
  32. Vector4f resizeBorders, String defaultImg, boolean isScrollable) {
  33. // Call the super class to construct our basic menu
  34. super(screen, UID, position, dimensions, resizeBorders, defaultImg, false);
  35. }
  36. }
  37. ----
  38. ==== STEP 2: Adding a public method for building standard options
  39. This method will be used by calling windows to ensure the menu starts with only the default options. If the particular calling window needs extra options, we can add them once this method is called.
  40. [source,java]
  41. ----
  42. public void resetMenuOptions() {
  43. // First, clear the current menuItems
  44. getMenuItems().clear();
  45. // Now add the default items
  46. /** Parameters:
  47. * String caption
  48. * Object value - We'll store a String that gives us a hint as to the menu item function
  49. * Menu subMenu - null, because there isn't one
  50. * boolean isToggleItem - adds a CheckBox to the menu item
  51. */
  52. addMenuItem("Lock window position", "position", null, true);
  53. addMenuItem("Lock window size", "dimensions", null, true);
  54. addMenuItem("Lock current alpha setting", "alpha", null, true);
  55. }
  56. ----
  57. === Adding Our New Control to the Screen
  58. [source,java]
  59. ----
  60. // Create a single instance of the reusable contextual menu
  61. ContextualMenu rcMenu = new ContextualMenu(screen, "rcMenu", Vector2f.ZERO) {
  62. // Override the abstract event method
  63. @Override
  64. public void onMenuItemClicked(int index, Object value, boolean isToggled) {
  65. // Now we will pass this through to our calling window.
  66. getCallerElement().setOption((String)value, isToggled);
  67. }
  68. };
  69. rcMenu.resetMenuOptions();
  70. screen.add(rcMenu);
  71. ----
  72. === Extending the Window Class to Utilize Our Control
  73. ==== STEP 1: Extend the window class and add a few methods
  74. We need to extend the window class in order to add right-click event handling and handle the setOption() method call from our ContextualMenu class.
  75. [source,java]
  76. ----
  77. public abstract class ContextualWindow extends Window implements MouseButtonListener {
  78. // Add the 3 standard constructors from the Window class and rename them
  79. // implement all abstract methods from the listener and add the following to right mouse button up:
  80. @Override
  81. public void onRightMouseReleased(MouseButtonEvent evt) {
  82. ContextualMenu rcMenu = screen.getElementById("rcMenu");
  83. // set the toggle state for each of the options
  84. rcMenu.getMenuItem(0).setIsToggled(getIsMovable());
  85. rcMenu.getMenuItem(1).setIsToggled(getIsResizable());
  86. rcMenu.getMenuItem(2).setIsToggled(getIgnoreGlobalAlpha());
  87. // Show the menu
  88. rcMenu.showMenu(this, screen.getMouseXY().x, screen.getMouseXY().y);
  89. }
  90. // Add the setOption method tohandle menu item events
  91. public void setOption(String value, boolean isToggled) {
  92. if (value.equals("position")) {
  93. setIsMovable(isToggled);
  94. } else if (value.equals("dimensions")) {
  95. setIsResizable(isToggled);
  96. } else if (value.equals("alpha")) {
  97. setIgnoreGlobalAlpha(isToggled);
  98. }
  99. }
  100. }
  101. ----