1#ifndef GAME_CLIENT_COMPONENTS_MENUS_INGAME_TOUCH_CONTROLS_H
2#define GAME_CLIENT_COMPONENTS_MENUS_INGAME_TOUCH_CONTROLS_H
3#include <game/client/component.h>
4#include <game/client/components/touch_controls.h>
5#include <game/client/ui.h>
6
7#include <memory>
8
9class CMenusIngameTouchControls : public CComponentInterfaces
10{
11public:
12 static constexpr const float BUTTON_EDITOR_WIDTH = 700.0f;
13 enum class EBehaviorType
14 {
15 BIND,
16 BIND_TOGGLE,
17 PREDEFINED,
18 NUM_BEHAVIORS
19 };
20
21 enum class EPredefinedType
22 {
23 INGAME_MENU,
24 EXTRA_MENU,
25 EMOTICON,
26 SPECTATE,
27 SWAP_ACTION,
28 USE_ACTION,
29 JOYSTICK_ACTION,
30 JOYSTICK_AIM,
31 JOYSTICK_AIM_RELATIVE,
32 JOYSTICK_FIRE,
33 JOYSTICK_HOOK,
34 NUM_PREDEFINEDTYPES
35 };
36
37 // Which menu is selected.
38 enum class EMenuType
39 {
40 MENU_FILE,
41 MENU_BUTTONS,
42 MENU_SETTINGS,
43 MENU_PREVIEW,
44 NUM_MENUS
45 };
46 EMenuType m_CurrentMenu = EMenuType::MENU_FILE;
47
48 enum class ESortType
49 {
50 LABEL,
51 X,
52 Y,
53 W,
54 H,
55 NUM_SORTS
56 };
57 ESortType m_SortType = ESortType::LABEL;
58
59 enum class EElementType
60 {
61 LAYOUT,
62 VISIBILITY,
63 BEHAVIOR,
64 NUM_ELEMENTS
65 };
66 EElementType m_EditElement = EElementType::LAYOUT;
67
68 enum class EVisibilityType
69 {
70 EXCLUDE,
71 INCLUDE,
72 IGNORE,
73 NUM_VISIBILITIES
74 };
75 std::array<EVisibilityType, (size_t)CTouchControls::EButtonVisibility::NUM_VISIBILITIES> m_aCachedVisibilities; // 0:-, 1:+, 2:Ignored.
76
77 // Mainly for passing values in popups.
78 CTouchControls::CTouchButton *m_pOldSelectedButton = nullptr;
79 CTouchControls::CTouchButton *m_pNewSelectedButton = nullptr;
80
81 // Storing everything you are editing.
82 CLineInputNumber m_InputX;
83 CLineInputNumber m_InputY;
84 CLineInputNumber m_InputW;
85 CLineInputNumber m_InputH;
86 CTouchControls::EButtonShape m_CachedShape;
87
88 EBehaviorType m_EditBehaviorType = EBehaviorType::BIND;
89 EPredefinedType m_PredefinedBehaviorType = EPredefinedType::EXTRA_MENU;
90 int m_CachedExtraMenuNumber = 0;
91
92 class CBehaviorElements
93 {
94 public:
95 CLineInputBuffered<1024> m_InputCommand;
96 CLineInputBuffered<1024> m_InputLabel;
97 CTouchControls::CBindToggleTouchButtonBehavior::CCommand m_CachedCommands;
98 CButtonContainer m_BindToggleAddButtons;
99 CButtonContainer m_BindToggleDeleteButtons;
100 CButtonContainer m_aLabelTypeRadios[(int)CTouchControls::CButtonLabel::EType::NUM_TYPES];
101
102 CBehaviorElements() noexcept;
103 CBehaviorElements(CBehaviorElements &&Other) noexcept;
104 ~CBehaviorElements();
105 CBehaviorElements &operator=(CBehaviorElements &&Other) noexcept;
106
107 CBehaviorElements &operator=(const CBehaviorElements &) = delete;
108 CBehaviorElements(const CBehaviorElements &Other) = delete;
109
110 std::string ParseLabel(const char *pLabel) const;
111 void UpdateInputs();
112 void UpdateLabel() { m_CachedCommands.m_Label = ParseLabel(pLabel: m_InputLabel.GetString()); }
113 void UpdateCommand() { m_CachedCommands.m_Command = m_InputCommand.GetString(); }
114 void Reset();
115 };
116 std::vector<std::unique_ptr<CBehaviorElements>> m_vBehaviorElements;
117
118 unsigned m_ColorActive = 0;
119 unsigned m_ColorInactive = 0;
120
121 // Used for creating ui elements.
122 std::array<CButtonContainer, (unsigned)CTouchControls::EButtonVisibility::NUM_VISIBILITIES> m_aVisibilityIds = {};
123 std::array<CButtonContainer, (unsigned)ESortType::NUM_SORTS> m_aSortHeaderIds = {};
124 std::array<CButtonContainer, (unsigned)EElementType::NUM_ELEMENTS> m_aEditElementIds = {};
125
126 // Functional variables.
127 bool m_FirstEnter = true; // Execute something when first opening the editor.
128 bool m_CloseMenu = false; // Decide if closing menu after the popup confirm.
129 bool m_NeedUpdatePreview = true; // Whether to reload the button being previewed.
130 bool m_NeedSort = true; // Whether to sort all previewed buttons.
131 bool m_NeedFilter = false; // Whether to exclude some buttons from preview.
132 std::vector<CTouchControls::CTouchButton *> m_vpButtons;
133 std::vector<CTouchControls::CTouchButton *> m_vpMutableButtons;
134 CLineInputBuffered<1024> m_FilterInput;
135 int m_SelectedPreviewButtonIndex = -1;
136
137 void RenderTouchButtonEditor(CUIRect MainView);
138 bool RenderLayoutSettingBlock(CUIRect Block);
139 bool RenderBehaviorSettingBlock(CUIRect Block);
140 bool RenderVisibilitySettingBlock(CUIRect Block);
141 void RenderTouchButtonBrowser(CUIRect MainView);
142 void RenderPreviewButton(CUIRect MainView);
143
144 void RenderSelectingTab(CUIRect SelectingTab);
145 void RenderConfigSettings(CUIRect MainView);
146 void RenderPreviewSettings(CUIRect MainView);
147 void RenderTouchControlsEditor(CUIRect MainView);
148
149 // Confirm, Cancel only decide if saving cached settings.
150 void DoPopupType(CTouchControls::CPopupParam PopupParam);
151 void ChangeSelectedButtonWhileHavingUnsavedChanges() const;
152 void NoSpaceForOverlappingButton() const;
153 void SelectedButtonNotVisible();
154
155 // Getter and setters.
156 bool UnsavedChanges() const;
157 void SetUnsavedChanges(bool UnsavedChanges);
158
159 // Convenient functions.
160 bool CheckCachedSettings() const;
161 void ResetCachedSettings();
162 void CacheAllSettingsFromTarget(CTouchControls::CTouchButton *pTargetButton);
163 void SaveCachedSettingsToTarget(CTouchControls::CTouchButton *pTargetButton) const;
164 void SetPosInputs(CTouchControls::CUnitRect MyRect);
165 CTouchControls::CUnitRect GetPosInputs() const;
166 void InputPosFunction(CLineInputNumber *pInput);
167 void UpdateSampleButton();
168 void ResetButtonPointers();
169 bool NewButton(CTouchControls::CUnitRect Rect, CTouchControls::EButtonShape Shape);
170 void ResolveIssues();
171 int CalculatePredefinedType(const char *pType) const;
172 std::string DetermineTouchButtonCommandLabel(CTouchControls::CTouchButton *pButton) const;
173 const char **VisibilityNames() const;
174 const char **PredefinedNames() const;
175 const char *HelpMessageForBehaviorType(EBehaviorType Type) const;
176 const char *HelpMessageForPredefinedType(EPredefinedType Type) const;
177 const char *HelpMessageForVisibilityType(CTouchControls::EButtonVisibility Type) const;
178
179 class CBehaviorFactoryEditor
180 {
181 public:
182 const char *m_pId;
183 std::function<std::unique_ptr<CTouchControls::CPredefinedTouchButtonBehavior>()> m_Factory;
184 };
185
186 static const CBehaviorFactoryEditor BEHAVIOR_FACTORIES_EDITOR[11];
187};
188
189#endif
190