|
| 1 | +#include <libkiwi.h> |
| 2 | + |
| 3 | +namespace kiwi { |
| 4 | + |
| 5 | +/****************************************************************************** |
| 6 | + * |
| 7 | + * DebugOptionBase |
| 8 | + * |
| 9 | + ******************************************************************************/ |
| 10 | + |
| 11 | +/** |
| 12 | + * @brief Sets whether the option is enabled |
| 13 | + * |
| 14 | + * @param enable Enable status |
| 15 | + */ |
| 16 | +void DebugOptionBase::SetEnabled(bool enable) { |
| 17 | + mIsEnabled = enable; |
| 18 | + UpdateString(); |
| 19 | +} |
| 20 | + |
| 21 | +/****************************************************************************** |
| 22 | + * |
| 23 | + * DebugIntOption |
| 24 | + * |
| 25 | + ******************************************************************************/ |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief Constructor |
| 29 | + * |
| 30 | + * @param rName Option name |
| 31 | + * @param min Minimum value (inclusive) |
| 32 | + * @param max Maximum value (inclusive) |
| 33 | + * @param initial Initial value (optional) |
| 34 | + */ |
| 35 | +DebugIntOption::DebugIntOption(const String& rName, int min, int max, |
| 36 | + Optional<int> initial) |
| 37 | + : DebugOptionBase(rName), mMin(min), mMax(max) { |
| 38 | + |
| 39 | + K_ASSERT(max >= min); |
| 40 | + |
| 41 | + // Initial value defaults to minimum |
| 42 | + SetValue(initial ? *initial : min); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Increments the option value |
| 47 | + * @return Action result |
| 48 | + */ |
| 49 | +DebugMenu::EResult DebugIntOption::Increment() { |
| 50 | + if (!IsEnabled()) { |
| 51 | + return DebugMenu::EResult_Invalid; |
| 52 | + } |
| 53 | + |
| 54 | + // Value wraps around |
| 55 | + SetValue(mValue == mMax ? mMin : mValue + 1); |
| 56 | + return DebugMenu::EResult_Change; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Decrements the option value |
| 61 | + * @return Action result |
| 62 | + */ |
| 63 | +DebugMenu::EResult DebugIntOption::Decrement() { |
| 64 | + if (!IsEnabled()) { |
| 65 | + return DebugMenu::EResult_Invalid; |
| 66 | + } |
| 67 | + |
| 68 | + // Value wraps around |
| 69 | + SetValue(mValue == mMin ? mMax : mValue - 1); |
| 70 | + return DebugMenu::EResult_Change; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @brief Sets the maximum value (inclusive) |
| 75 | + * @details The current option value is validated upon range changes. |
| 76 | + * |
| 77 | + * @param max New maximum value (inclusive) |
| 78 | + */ |
| 79 | +void DebugIntOption::SetMax(int max) { |
| 80 | + K_ASSERT(max >= mMin); |
| 81 | + |
| 82 | + mMax = max; |
| 83 | + Validate(); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Sets the minimum value (inclusive) |
| 88 | + * @details The current option value is validated upon range changes. |
| 89 | + * |
| 90 | + * @param min New minimum value (inclusive) |
| 91 | + */ |
| 92 | +void DebugIntOption::SetMin(int min) { |
| 93 | + K_ASSERT(min <= mMax); |
| 94 | + |
| 95 | + mMin = min; |
| 96 | + Validate(); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Sets the range (inclusive) of acceptable values |
| 101 | + * @details The current option value is validated upon range changes. |
| 102 | + * |
| 103 | + * @param min New minimum value (inclusive) |
| 104 | + * @param max New maximum value (inclusive) |
| 105 | + */ |
| 106 | +void DebugIntOption::SetRange(int min, int max) { |
| 107 | + K_ASSERT(max >= min); |
| 108 | + |
| 109 | + mMin = min; |
| 110 | + mMax = max; |
| 111 | + Validate(); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief Validates the option value |
| 116 | + */ |
| 117 | +void DebugIntOption::Validate() { |
| 118 | + if (mValue > mMax) { |
| 119 | + SetValue(mMax); |
| 120 | + } else if (mValue < mMin) { |
| 121 | + SetValue(mMin); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * @brief Updates the option value string |
| 127 | + */ |
| 128 | +void DebugIntOption::UpdateString() { |
| 129 | + if (IsEnabled()) { |
| 130 | + mValueText = kiwi::ToString(mValue); |
| 131 | + } else { |
| 132 | + mValueText = "DISABLED"; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief Sets the integer option value |
| 138 | + * |
| 139 | + * @param value New value |
| 140 | + */ |
| 141 | +void DebugIntOption::SetValue(int value) { |
| 142 | + mValue = Clamp(mValue, mMin, mMax); |
| 143 | + UpdateString(); |
| 144 | +} |
| 145 | + |
| 146 | +/****************************************************************************** |
| 147 | + * |
| 148 | + * DebugBoolOption |
| 149 | + * |
| 150 | + ******************************************************************************/ |
| 151 | + |
| 152 | +/** |
| 153 | + * @brief Updates the option value string |
| 154 | + */ |
| 155 | +void DebugBoolOption::UpdateString() { |
| 156 | + if (IsEnabled()) { |
| 157 | + mValueText = GetValue() ? "True" : "False"; |
| 158 | + } else { |
| 159 | + mValueText = "DISABLED"; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/****************************************************************************** |
| 164 | + * |
| 165 | + * DebugEnumOption |
| 166 | + * |
| 167 | + ******************************************************************************/ |
| 168 | + |
| 169 | +/** |
| 170 | + * @brief Sets the option enum value strings |
| 171 | + * |
| 172 | + * @param ppValues Enum value strings |
| 173 | + */ |
| 174 | +void DebugEnumOption::SetEnumValues(const char** ppValues) { |
| 175 | + K_ASSERT(ppValues != nullptr); |
| 176 | + |
| 177 | + mppValues = ppValues; |
| 178 | + UpdateString(); |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * @brief Updates the option value string |
| 183 | + */ |
| 184 | +void DebugEnumOption::UpdateString() { |
| 185 | + K_ASSERT(mppValues != nullptr); |
| 186 | + |
| 187 | + if (IsEnabled()) { |
| 188 | + mValueText = mppValues[GetValue()]; |
| 189 | + } else { |
| 190 | + mValueText = "DISABLED"; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +/****************************************************************************** |
| 195 | + * |
| 196 | + * DebugProcOption |
| 197 | + * |
| 198 | + ******************************************************************************/ |
| 199 | + |
| 200 | +/** |
| 201 | + * @brief Performs the option selection logic |
| 202 | + * @return Action result |
| 203 | + */ |
| 204 | +DebugMenu::EResult DebugProcOption::Select() { |
| 205 | + if (!IsEnabled()) { |
| 206 | + return DebugMenu::EResult_Invalid; |
| 207 | + } |
| 208 | + |
| 209 | + if (mpCallback != nullptr) { |
| 210 | + return mpCallback(mpCallbackArg); |
| 211 | + } |
| 212 | + |
| 213 | + return DebugMenu::EResult_None; |
| 214 | +} |
| 215 | + |
| 216 | +} // namespace kiwi |
0 commit comments