Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
ButtonActionComponent.c
Go to the documentation of this file.
1
3{
4 [Attribute(defvalue: "0", uiwidget: UIWidgets.ComboBox, enums: { ParamEnum("LMB", "0", ""), ParamEnum("RMB", "1", ""), ParamEnum("MMB", "2", ""), ParamEnum("ALL", "3", "") }, desc: "ID of mouse button which activates the action upon click.")]
5 private int m_iMouseButton;
6
7 [Attribute(desc: "Name of input action which activates the action.")]
8 private string m_bActionName;
9
10 [Attribute(defvalue: "2", uiwidget: UIWidgets.ComboBox, enums: ParamEnumArray.FromEnum(EActionTrigger))]
11 private EActionTrigger m_ActionTrigger;
12
13 [Attribute(desc: "Name of the widget which shows key assigned to the input action.")]
14 private string m_bWidgetHintName;
15
16 private Widget m_wWidget;
17 private ref ScriptInvoker m_OnAction = new ScriptInvoker();
18
19 //------------------------------------------------------------------------------------------------
23 {
24 return m_OnAction;
25 }
26
27 //------------------------------------------------------------------------------------------------
30 int GetMouseButton()
31 {
32 return m_iMouseButton;
33 }
34
35 //------------------------------------------------------------------------------------------------
48 static ScriptInvoker GetOnAction(Widget w, string buttonName, bool canCreateComponent = false, int mouseButton = 0)
49 {
50 Widget button = w.FindAnyWidget(buttonName);
51 if (!button)
52 {
53 Print(string.Format("Widget '%1' not found in widget '%2'!", buttonName, w.GetName()), LogLevel.ERROR);
54 return new ScriptInvoker();
55 }
56
57 return GetOnAction(button, canCreateComponent, mouseButton);
58 }
59
60 //------------------------------------------------------------------------------------------------
72 static ScriptInvoker GetOnAction(Widget button, bool canCreateComponent = false, int mouseButton = 0)
73 {
74 ButtonActionComponent component;
75 int countHandlers = button.GetNumHandlers();
76 for (int i = 0; i < countHandlers; i++)
77 {
78 ButtonActionComponent componentCandidate = ButtonActionComponent.Cast(button.GetHandler(i));
79 if (componentCandidate && componentCandidate.GetMouseButton() == mouseButton)
80 {
81 component = componentCandidate;
82 break;
83 }
84 }
85
86 if (!component)
87 {
88 if (canCreateComponent)
89 {
90 component = new ButtonActionComponent();
91 component.m_iMouseButton = mouseButton;
92 button.AddHandler(component);
93 }
94 else
95 {
96 Print(string.Format("Widget '%1' is missing ButtonActionComponent component!", button.GetName()), LogLevel.ERROR);
97 return new ScriptInvoker();
98 }
99 }
100
101 return component.GetOnAction();
102 }
103
104 //------------------------------------------------------------------------------------------------
105 protected void Execute(float value, EActionTrigger reason)
106 {
107 if (!m_wWidget.IsVisible() || !m_wWidget.IsEnabledInHierarchy())
108 return;
109
110 m_OnAction.Invoke(m_wWidget, value, reason);
111 }
112
113 //------------------------------------------------------------------------------------------------
114 protected void OnAction(float value, EActionTrigger reason)
115 {
116 Execute(value, reason);
117 }
118
119 //------------------------------------------------------------------------------------------------
120 override bool OnClick(Widget w, int x, int y, int button)
121 {
122 if (button == m_iMouseButton || m_iMouseButton == 3)
123 Execute(1, EActionTrigger.DOWN);
124
125 return false;
126 }
127
128 //------------------------------------------------------------------------------------------------
129 override void HandlerAttached(Widget w)
130 {
131 m_wWidget = w;
132
133 //--- Get hint widget
134 RichTextWidget widgetIcon;
135 if (m_bWidgetHintName.IsEmpty())
136 widgetIcon = RichTextWidget.Cast(w);
137 else
138 widgetIcon = RichTextWidget.Cast(w.FindAnyWidget(m_bWidgetHintName));
139
140 if (widgetIcon)
141 {
142 if (m_bActionName != "")
143 widgetIcon.SetText(string.Format("<action name=\"%1\"/>", m_bActionName));
144 else
145 widgetIcon.SetText("");
146 }
147 else
148 {
149 if (m_bWidgetHintName != "")
150 Print(string.Format("ButtonActionComponent: RichTextWidget widget '%1' not found on widget '%2'!", m_bWidgetHintName, w.GetName()), LogLevel.ERROR);
151 }
152
153 //--- Add action listener
154 if (m_bActionName == "")
155 return;
156
157 ArmaReforgerScripted game = GetGame();
158 if (!game)
159 return;
160
161 InputManager inputManager = game.GetInputManager();
162 if (inputManager)
163 inputManager.AddActionListener(m_bActionName, m_ActionTrigger, OnAction);
164 }
165
166 //------------------------------------------------------------------------------------------------
167 override void HandlerDeattached(Widget w)
168 {
169 if (m_bActionName == "")
170 return;
171
172 ArmaReforgerScripted game = GetGame();
173 if (!game)
174 return;
175
176 InputManager inputManager = game.GetInputManager();
177 if (inputManager)
178 inputManager.RemoveActionListener(m_bActionName, m_ActionTrigger, OnAction);
179 }
180}
ref DSGameConfig game
Definition DSConfig.c:81
ArmaReforgerScripted GetGame()
Definition game.c:1398
ScriptInvokerAction GetOnAction()
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Component to execute action when the button or its shortcut is pressed.
void Execute(float value, EActionTrigger reason)
override bool OnClick(Widget w, int x, int y, int button)
override void HandlerAttached(Widget w)
override void HandlerDeattached(Widget w)
void OnAction(float value, EActionTrigger reason)
Input management system for user interactions.
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
EActionTrigger
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134