Modifier.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * This file is part of Cloth Config.
  3. * Copyright (C) 2020 - 2021 shedaniel
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.clothconfig2.api;
  20. import net.fabricmc.api.EnvType;
  21. import net.fabricmc.api.Environment;
  22. import net.minecraft.client.gui.screens.Screen;
  23. import java.util.Objects;
  24. /**
  25. * Taken from amecs for a lightweight modifers api:
  26. * https://github.com/Siphalor/amecs
  27. *
  28. * @author Siphalor
  29. */
  30. @Environment(EnvType.CLIENT)
  31. public class Modifier {
  32. private final short value;
  33. /**
  34. * Constructs a new modifier object by a raw value
  35. *
  36. * @param value the raw value with flags set
  37. */
  38. private Modifier(short value) {
  39. this.value = value;
  40. }
  41. public static Modifier none() {
  42. return of((short) 0);
  43. }
  44. /**
  45. * Constructs a new modifier object by all modifier bits
  46. *
  47. * @param alt sets whether the alt flag should be set
  48. * @param control sets whether the control flag should be set
  49. * @param shift sets whether the shift flag should be set
  50. */
  51. public static Modifier of(boolean alt, boolean control, boolean shift) {
  52. short value = setFlag((short) 0, (short) 1, alt);
  53. value = setFlag(value, (short) 2, control);
  54. value = setFlag(value, (short) 4, shift);
  55. return of(value);
  56. }
  57. public static Modifier of(short value) {
  58. return new Modifier(value);
  59. }
  60. public static Modifier current() {
  61. return Modifier.of(Screen.hasAltDown(), Screen.hasControlDown(), Screen.hasShiftDown());
  62. }
  63. private static short setFlag(short base, short flag, boolean val) {
  64. return val ? setFlag(base, flag) : removeFlag(base, flag);
  65. }
  66. private static short setFlag(short base, short flag) {
  67. return (short) (base | flag);
  68. }
  69. private static short removeFlag(short base, short flag) {
  70. return (short) (base & (~flag));
  71. }
  72. private static boolean getFlag(short base, short flag) {
  73. return (base & flag) != 0;
  74. }
  75. /**
  76. * Compares this object with the current pressed keys
  77. *
  78. * @return whether the modifiers match in the current context
  79. */
  80. public boolean matchesCurrent() {
  81. return equals(current());
  82. }
  83. /**
  84. * Gets the raw value
  85. *
  86. * @return the value with all flags set
  87. */
  88. public short getValue() {
  89. return value;
  90. }
  91. /**
  92. * Gets the state of the alt flag
  93. *
  94. * @return whether the alt key needs to be pressed
  95. */
  96. public boolean hasAlt() {
  97. return getFlag(value, (short) 1);
  98. }
  99. /**
  100. * Gets the state of the control flag
  101. *
  102. * @return whether the control key needs to be pressed
  103. */
  104. public boolean hasControl() {
  105. return getFlag(value, (short) 2);
  106. }
  107. /**
  108. * Gets the state of the shift flag
  109. *
  110. * @return whether the shift key needs to be pressed
  111. */
  112. public boolean hasShift() {
  113. return getFlag(value, (short) 4);
  114. }
  115. /**
  116. * Returns whether no flag is set
  117. *
  118. * @return value == 0
  119. */
  120. public boolean isEmpty() {
  121. return value == 0;
  122. }
  123. /**
  124. * Returns whether this object equals another one
  125. *
  126. * @param other another modifier object
  127. * @return whether both values are equal
  128. */
  129. @Override
  130. public boolean equals(Object other) {
  131. if (other == null)
  132. return false;
  133. if (!(other instanceof Modifier))
  134. return false;
  135. return value == ((Modifier) other).value;
  136. }
  137. @Override
  138. public int hashCode() {
  139. return Objects.hash(value);
  140. }
  141. }