| 1 | #include <base/color.h> |
| 2 | #include <base/math.h> |
| 3 | |
| 4 | #include <game/client/ui.h> |
| 5 | #include <game/editor/editor.h> |
| 6 | #include <game/editor/editor_ui.h> |
| 7 | |
| 8 | void CEditor::UpdateTooltip(const void *pId, const CUIRect *pRect, const char *pToolTip) |
| 9 | { |
| 10 | if(Ui()->MouseInside(pRect) && !pToolTip) |
| 11 | str_copy(dst&: m_aTooltip, src: "" ); |
| 12 | else if(Ui()->HotItem() == pId && pToolTip) |
| 13 | str_copy(dst&: m_aTooltip, src: pToolTip); |
| 14 | } |
| 15 | |
| 16 | ColorRGBA CEditor::GetButtonColor(const void *pId, int Checked) |
| 17 | { |
| 18 | if(Checked < 0) |
| 19 | return ColorRGBA(0, 0, 0, 0.5f); |
| 20 | |
| 21 | switch(Checked) |
| 22 | { |
| 23 | case EditorButtonChecked::DANGEROUS_ACTION: |
| 24 | if(Ui()->HotItem() == pId) |
| 25 | return ColorRGBA(1.0f, 0.0f, 0.0f, 0.75f); |
| 26 | return ColorRGBA(1.0f, 0.0f, 0.0f, 0.5f); |
| 27 | case 8: // invisible |
| 28 | return ColorRGBA(0, 0, 0, 0); |
| 29 | case 7: // selected + game layers |
| 30 | if(Ui()->HotItem() == pId) |
| 31 | return ColorRGBA(1, 0, 0, 0.4f); |
| 32 | return ColorRGBA(1, 0, 0, 0.2f); |
| 33 | |
| 34 | case 6: // game layers |
| 35 | if(Ui()->HotItem() == pId) |
| 36 | return ColorRGBA(1, 1, 1, 0.4f); |
| 37 | return ColorRGBA(1, 1, 1, 0.2f); |
| 38 | |
| 39 | case 5: // selected + image/sound should be embedded |
| 40 | if(Ui()->HotItem() == pId) |
| 41 | return ColorRGBA(1, 0, 0, 0.75f); |
| 42 | return ColorRGBA(1, 0, 0, 0.5f); |
| 43 | |
| 44 | case 4: // image/sound should be embedded |
| 45 | if(Ui()->HotItem() == pId) |
| 46 | return ColorRGBA(1, 0, 0, 1.0f); |
| 47 | return ColorRGBA(1, 0, 0, 0.875f); |
| 48 | |
| 49 | case 3: // selected + unused image/sound |
| 50 | if(Ui()->HotItem() == pId) |
| 51 | return ColorRGBA(1, 0, 1, 0.75f); |
| 52 | return ColorRGBA(1, 0, 1, 0.5f); |
| 53 | |
| 54 | case 2: // unused image/sound |
| 55 | if(Ui()->HotItem() == pId) |
| 56 | return ColorRGBA(0, 0, 1, 0.75f); |
| 57 | return ColorRGBA(0, 0, 1, 0.5f); |
| 58 | |
| 59 | case 1: // selected |
| 60 | if(Ui()->HotItem() == pId) |
| 61 | return ColorRGBA(1, 0, 0, 0.75f); |
| 62 | return ColorRGBA(1, 0, 0, 0.5f); |
| 63 | |
| 64 | default: // regular |
| 65 | if(Ui()->HotItem() == pId) |
| 66 | return ColorRGBA(1, 1, 1, 0.75f); |
| 67 | return ColorRGBA(1, 1, 1, 0.5f); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int CEditor::DoButtonLogic(const void *pId, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip) |
| 72 | { |
| 73 | if(Ui()->MouseInside(pRect)) |
| 74 | { |
| 75 | if(Flags & BUTTONFLAG_RIGHT) |
| 76 | m_pUiGotContext = pId; |
| 77 | } |
| 78 | |
| 79 | UpdateTooltip(pId, pRect, pToolTip); |
| 80 | return Ui()->DoButtonLogic(pId, Checked, pRect, Flags); |
| 81 | } |
| 82 | |
| 83 | int CEditor::DoButton_Editor(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip) |
| 84 | { |
| 85 | pRect->Draw(Color: GetButtonColor(pId, Checked), Corners: IGraphics::CORNER_ALL, Rounding: 3.0f); |
| 86 | CUIRect NewRect = *pRect; |
| 87 | Ui()->DoLabel(pRect: &NewRect, pText, Size: 10.0f, Align: TEXTALIGN_MC); |
| 88 | Checked %= 2; |
| 89 | return DoButtonLogic(pId, Checked, pRect, Flags, pToolTip); |
| 90 | } |
| 91 | |
| 92 | int CEditor::DoButton_Env(const void *pId, const char *pText, int Checked, const CUIRect *pRect, const char *pToolTip, ColorRGBA BaseColor, int Corners) |
| 93 | { |
| 94 | float Bright = Checked ? 1.0f : 0.5f; |
| 95 | float Alpha = Ui()->HotItem() == pId ? 1.0f : 0.75f; |
| 96 | ColorRGBA Color = ColorRGBA(BaseColor.r * Bright, BaseColor.g * Bright, BaseColor.b * Bright, Alpha); |
| 97 | |
| 98 | pRect->Draw(Color, Corners, Rounding: 3.0f); |
| 99 | Ui()->DoLabel(pRect, pText, Size: 10.0f, Align: TEXTALIGN_MC); |
| 100 | Checked %= 2; |
| 101 | return DoButtonLogic(pId, Checked, pRect, Flags: BUTTONFLAG_LEFT, pToolTip); |
| 102 | } |
| 103 | |
| 104 | int CEditor::DoButton_Ex(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners, float FontSize, int Align) |
| 105 | { |
| 106 | pRect->Draw(Color: GetButtonColor(pId, Checked), Corners, Rounding: 3.0f); |
| 107 | |
| 108 | CUIRect Rect; |
| 109 | pRect->VMargin(Cut: ((Align & TEXTALIGN_MASK_HORIZONTAL) == TEXTALIGN_CENTER) ? 1.0f : 5.0f, pOtherRect: &Rect); |
| 110 | |
| 111 | SLabelProperties Props; |
| 112 | Props.m_MaxWidth = Rect.w; |
| 113 | Props.m_EllipsisAtEnd = true; |
| 114 | Ui()->DoLabel(pRect: &Rect, pText, Size: FontSize, Align, LabelProps: Props); |
| 115 | |
| 116 | return DoButtonLogic(pId, Checked, pRect, Flags, pToolTip); |
| 117 | } |
| 118 | |
| 119 | int CEditor::DoButton_FontIcon(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip, int Corners, float FontSize) |
| 120 | { |
| 121 | pRect->Draw(Color: GetButtonColor(pId, Checked), Corners, Rounding: 3.0f); |
| 122 | |
| 123 | TextRender()->SetFontPreset(EFontPreset::ICON_FONT); |
| 124 | TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING); |
| 125 | Ui()->DoLabel(pRect, pText, Size: FontSize, Align: TEXTALIGN_MC); |
| 126 | TextRender()->SetRenderFlags(0); |
| 127 | TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT); |
| 128 | |
| 129 | return DoButtonLogic(pId, Checked, pRect, Flags, pToolTip); |
| 130 | } |
| 131 | |
| 132 | int CEditor::(const void *pId, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip) |
| 133 | { |
| 134 | if((Ui()->HotItem() == pId && Checked == 0) || Checked > 0) |
| 135 | pRect->Draw(Color: GetButtonColor(pId, Checked), Corners: IGraphics::CORNER_ALL, Rounding: 3.0f); |
| 136 | |
| 137 | CUIRect Rect; |
| 138 | pRect->VMargin(Cut: 5.0f, pOtherRect: &Rect); |
| 139 | |
| 140 | SLabelProperties Props; |
| 141 | Props.m_MaxWidth = Rect.w; |
| 142 | Props.m_EllipsisAtEnd = true; |
| 143 | if(Checked < 0) |
| 144 | { |
| 145 | Props.SetColor(ColorRGBA(0.6f, 0.6f, 0.6f, 1.0f)); |
| 146 | } |
| 147 | Ui()->DoLabel(pRect: &Rect, pText, Size: 10.0f, Align: TEXTALIGN_ML, LabelProps: Props); |
| 148 | |
| 149 | return DoButtonLogic(pId, Checked, pRect, Flags, pToolTip); |
| 150 | } |
| 151 | |
| 152 | int CEditor::DoButton_DraggableEx(const void *pId, const char *pText, int Checked, const CUIRect *pRect, bool *pClicked, bool *pAbrupted, int Flags, const char *pToolTip, int Corners, float FontSize) |
| 153 | { |
| 154 | pRect->Draw(Color: GetButtonColor(pId, Checked), Corners, Rounding: 3.0f); |
| 155 | |
| 156 | CUIRect Rect; |
| 157 | pRect->VMargin(Cut: pRect->w > 20.0f ? 5.0f : 0.0f, pOtherRect: &Rect); |
| 158 | |
| 159 | SLabelProperties Props; |
| 160 | Props.m_MaxWidth = Rect.w; |
| 161 | Props.m_EllipsisAtEnd = true; |
| 162 | Ui()->DoLabel(pRect: &Rect, pText, Size: FontSize, Align: TEXTALIGN_MC, LabelProps: Props); |
| 163 | |
| 164 | if(Ui()->MouseInside(pRect)) |
| 165 | { |
| 166 | if(Flags & BUTTONFLAG_RIGHT) |
| 167 | m_pUiGotContext = pId; |
| 168 | } |
| 169 | |
| 170 | UpdateTooltip(pId, pRect, pToolTip); |
| 171 | return Ui()->DoDraggableButtonLogic(pId, Checked, pRect, pClicked, pAbrupted); |
| 172 | } |
| 173 | |
| 174 | bool CEditor::DoEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners, const char *pToolTip, const std::vector<STextColorSplit> &vColorSplits) |
| 175 | { |
| 176 | UpdateTooltip(pId: pLineInput, pRect, pToolTip); |
| 177 | return Ui()->DoEditBox(pLineInput, pRect, FontSize, Corners, vColorSplits); |
| 178 | } |
| 179 | |
| 180 | bool CEditor::DoClearableEditBox(CLineInput *pLineInput, const CUIRect *pRect, float FontSize, int Corners, const char *pToolTip, const std::vector<STextColorSplit> &vColorSplits) |
| 181 | { |
| 182 | UpdateTooltip(pId: pLineInput, pRect, pToolTip); |
| 183 | return Ui()->DoClearableEditBox(pLineInput, pRect, FontSize, Corners, vColorSplits); |
| 184 | } |
| 185 | |
| 186 | SEditResult<int> CEditor::UiDoValueSelector(void *pId, CUIRect *pRect, const char *pLabel, int Current, int Min, int Max, int Step, float Scale, const char *pToolTip, bool IsDegree, bool IsHex, int Corners, const ColorRGBA *pColor, bool ShowValue) |
| 187 | { |
| 188 | // logic |
| 189 | static bool s_DidScroll = false; |
| 190 | static float s_ScrollValue = 0.0f; |
| 191 | static CLineInputNumber s_NumberInput; |
| 192 | static int s_ButtonUsed = -1; |
| 193 | static void *s_pLastTextId = nullptr; |
| 194 | |
| 195 | const bool Inside = Ui()->MouseInside(pRect); |
| 196 | const int Base = IsHex ? 16 : 10; |
| 197 | |
| 198 | if(Ui()->HotItem() == pId && s_ButtonUsed >= 0 && !Ui()->MouseButton(Index: s_ButtonUsed)) |
| 199 | { |
| 200 | Ui()->DisableMouseLock(); |
| 201 | if(Ui()->CheckActiveItem(pId)) |
| 202 | { |
| 203 | Ui()->SetActiveItem(nullptr); |
| 204 | } |
| 205 | if(Inside && ((s_ButtonUsed == 0 && !s_DidScroll && Ui()->DoDoubleClickLogic(pId)) || s_ButtonUsed == 1)) |
| 206 | { |
| 207 | s_pLastTextId = pId; |
| 208 | s_NumberInput.SetInteger(Number: Current, Base); |
| 209 | s_NumberInput.SelectAll(); |
| 210 | } |
| 211 | s_ButtonUsed = -1; |
| 212 | } |
| 213 | |
| 214 | if(s_pLastTextId == pId) |
| 215 | { |
| 216 | str_copy(dst&: m_aTooltip, src: "Type your number. Press enter to confirm." ); |
| 217 | Ui()->SetActiveItem(&s_NumberInput); |
| 218 | DoEditBox(pLineInput: &s_NumberInput, pRect, FontSize: 10.0f, Corners); |
| 219 | |
| 220 | if(Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ENTER) || ((Ui()->MouseButtonClicked(Index: 1) || Ui()->MouseButtonClicked(Index: 0)) && !Inside)) |
| 221 | { |
| 222 | Current = std::clamp(val: s_NumberInput.GetInteger(Base), lo: Min, hi: Max); |
| 223 | Ui()->DisableMouseLock(); |
| 224 | Ui()->SetActiveItem(nullptr); |
| 225 | s_pLastTextId = nullptr; |
| 226 | } |
| 227 | |
| 228 | if(Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ESCAPE)) |
| 229 | { |
| 230 | Ui()->DisableMouseLock(); |
| 231 | Ui()->SetActiveItem(nullptr); |
| 232 | s_pLastTextId = nullptr; |
| 233 | } |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | if(Ui()->CheckActiveItem(pId)) |
| 238 | { |
| 239 | if(s_ButtonUsed == 0 && Ui()->MouseButton(Index: 0)) |
| 240 | { |
| 241 | s_ScrollValue += Ui()->MouseDeltaX() * (Input()->ShiftIsPressed() ? 0.05f : 1.0f); |
| 242 | |
| 243 | if(absolute(a: s_ScrollValue) >= Scale) |
| 244 | { |
| 245 | int Count = (int)(s_ScrollValue / Scale); |
| 246 | s_ScrollValue = std::fmod(x: s_ScrollValue, y: Scale); |
| 247 | Current += Step * Count; |
| 248 | Current = std::clamp(val: Current, lo: Min, hi: Max); |
| 249 | s_DidScroll = true; |
| 250 | |
| 251 | // Constrain to discrete steps |
| 252 | if(Count > 0) |
| 253 | Current = Current / Step * Step; |
| 254 | else |
| 255 | Current = std::ceil(x: Current / (float)Step) * Step; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if(pToolTip && s_pLastTextId != pId) |
| 260 | str_copy(dst&: m_aTooltip, src: pToolTip); |
| 261 | } |
| 262 | else if(Ui()->HotItem() == pId) |
| 263 | { |
| 264 | if(Ui()->MouseButton(Index: 0)) |
| 265 | { |
| 266 | s_ButtonUsed = 0; |
| 267 | s_DidScroll = false; |
| 268 | s_ScrollValue = 0.0f; |
| 269 | Ui()->SetActiveItem(pId); |
| 270 | Ui()->EnableMouseLock(pId); |
| 271 | } |
| 272 | else if(Ui()->MouseButton(Index: 1)) |
| 273 | { |
| 274 | s_ButtonUsed = 1; |
| 275 | Ui()->SetActiveItem(pId); |
| 276 | } |
| 277 | |
| 278 | if(pToolTip && s_pLastTextId != pId) |
| 279 | str_copy(dst&: m_aTooltip, src: pToolTip); |
| 280 | } |
| 281 | |
| 282 | // render |
| 283 | char aBuf[128]; |
| 284 | if(pLabel[0] != '\0') |
| 285 | { |
| 286 | if(ShowValue) |
| 287 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s %d" , pLabel, Current); |
| 288 | else |
| 289 | str_copy(dst&: aBuf, src: pLabel); |
| 290 | } |
| 291 | else if(IsDegree) |
| 292 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d°" , Current); |
| 293 | else if(IsHex) |
| 294 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "#%06X" , Current); |
| 295 | else |
| 296 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d" , Current); |
| 297 | pRect->Draw(Color: pColor ? *pColor : GetButtonColor(pId, Checked: 0), Corners, Rounding: 3.0f); |
| 298 | Ui()->DoLabel(pRect, pText: aBuf, Size: 10, Align: TEXTALIGN_MC); |
| 299 | } |
| 300 | |
| 301 | if(Inside && !Ui()->MouseButton(Index: 0) && !Ui()->MouseButton(Index: 1)) |
| 302 | Ui()->SetHotItem(pId); |
| 303 | |
| 304 | static const void *s_pEditing = nullptr; |
| 305 | EEditState State = EEditState::NONE; |
| 306 | if(s_pEditing == pId) |
| 307 | { |
| 308 | State = EEditState::EDITING; |
| 309 | } |
| 310 | if(((Ui()->CheckActiveItem(pId) && Ui()->CheckMouseLock() && s_DidScroll) || s_pLastTextId == pId) && s_pEditing != pId) |
| 311 | { |
| 312 | State = EEditState::START; |
| 313 | s_pEditing = pId; |
| 314 | } |
| 315 | if(!Ui()->CheckMouseLock() && s_pLastTextId != pId && s_pEditing == pId) |
| 316 | { |
| 317 | State = EEditState::END; |
| 318 | s_pEditing = nullptr; |
| 319 | } |
| 320 | |
| 321 | return SEditResult<int>{.m_State: State, .m_Value: Current}; |
| 322 | } |
| 323 | |
| 324 | void CEditor::RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) const |
| 325 | { |
| 326 | Graphics()->TextureSet(Texture); |
| 327 | Graphics()->BlendNormal(); |
| 328 | Graphics()->QuadsBegin(); |
| 329 | Graphics()->SetColor(r: Brightness, g: Brightness, b: Brightness, a: 1.0f); |
| 330 | Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: View.w / Size, BottomRightV: View.h / Size); |
| 331 | IGraphics::CQuadItem QuadItem(View.x, View.y, View.w, View.h); |
| 332 | Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1); |
| 333 | Graphics()->QuadsEnd(); |
| 334 | } |
| 335 | |