2121import net .minecraft .client .gui .ComponentPath ;
2222import net .minecraft .client .gui .GuiGraphics ;
2323import net .minecraft .client .gui .components .AbstractWidget ;
24- import net .minecraft .client .gui .components .EditBox ;
2524import net .minecraft .client .gui .components .events .GuiEventListener ;
2625import net .minecraft .client .gui .components .tabs .Tab ;
2726import net .minecraft .client .gui .components .tabs .TabNavigationBar ;
3534import org .lwjgl .glfw .GLFW ;
3635
3736import java .util .*;
37+ import java .util .function .Supplier ;
3838
3939public class ScreenProcessor <T extends Screen > {
4040 public final T screen ;
@@ -111,54 +111,51 @@ protected void handleComponentNavigation(ControllerEntity controller) {
111111 InputBinding guiNaviUp = ControlifyBindings .GUI_NAVI_UP .on (controller );
112112 InputBinding guiNaviDown = ControlifyBindings .GUI_NAVI_DOWN .on (controller );
113113
114- FocusNavigationEvent . ArrowNavigation event = null ;
114+ Supplier < Boolean > navigationFunc = null ;
115115 if (guiNaviRight .digitalNow () && (repeatEventAvailable || !guiNaviRight .digitalPrev ())) {
116- event = accessor . invokeCreateArrowEvent (ScreenDirection .RIGHT );
116+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .RIGHT );
117117
118118 if (!guiNaviRight .digitalPrev ())
119119 holdRepeatHelper .reset ();
120120 } else if (guiNaviLeft .digitalNow () && (repeatEventAvailable || !guiNaviLeft .digitalPrev ())) {
121- event = accessor . invokeCreateArrowEvent (ScreenDirection .LEFT );
121+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .LEFT );
122122
123123 if (!guiNaviLeft .digitalPrev ())
124124 holdRepeatHelper .reset ();
125125 } else if (guiNaviUp .digitalNow () && (repeatEventAvailable || !guiNaviUp .digitalPrev ())) {
126- event = accessor . invokeCreateArrowEvent (ScreenDirection .UP );
126+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .UP );
127127
128128 if (!guiNaviUp .digitalPrev ())
129129 holdRepeatHelper .reset ();
130130 } else if (guiNaviDown .digitalNow () && (repeatEventAvailable || !guiNaviDown .digitalPrev ())) {
131- event = accessor . invokeCreateArrowEvent (ScreenDirection .DOWN );
131+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .DOWN );
132132
133133 if (!guiNaviDown .digitalPrev ())
134134 holdRepeatHelper .reset ();
135135 } else if (state .isButtonDown (GamepadInputs .DPAD_RIGHT_BUTTON ) && (repeatEventAvailable || !prevState .isButtonDown (GamepadInputs .DPAD_RIGHT_BUTTON ))) {
136- event = accessor . invokeCreateArrowEvent (ScreenDirection .RIGHT );
136+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .RIGHT );
137137
138138 if (!prevState .isButtonDown (GamepadInputs .DPAD_RIGHT_BUTTON ))
139139 holdRepeatHelper .reset ();
140140 } else if (state .isButtonDown (GamepadInputs .DPAD_LEFT_BUTTON ) && (repeatEventAvailable || !prevState .isButtonDown (GamepadInputs .DPAD_LEFT_BUTTON ))) {
141- event = accessor . invokeCreateArrowEvent (ScreenDirection .LEFT );
141+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .LEFT );
142142
143143 if (!prevState .isButtonDown (GamepadInputs .DPAD_LEFT_BUTTON ))
144144 holdRepeatHelper .reset ();
145145 } else if (state .isButtonDown (GamepadInputs .DPAD_UP_BUTTON ) && (repeatEventAvailable || !prevState .isButtonDown (GamepadInputs .DPAD_UP_BUTTON ))) {
146- event = accessor . invokeCreateArrowEvent (ScreenDirection .UP );
146+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .UP );
147147
148148 if (!prevState .isButtonDown (GamepadInputs .DPAD_UP_BUTTON ))
149149 holdRepeatHelper .reset ();
150150 } else if (state .isButtonDown (GamepadInputs .DPAD_DOWN_BUTTON ) && (repeatEventAvailable || !prevState .isButtonDown (GamepadInputs .DPAD_DOWN_BUTTON ))) {
151- event = accessor . invokeCreateArrowEvent (ScreenDirection .DOWN );
151+ navigationFunc = this . createScreenNavigationFunc (ScreenDirection .DOWN );
152152
153153 if (!prevState .isButtonDown (GamepadInputs .DPAD_DOWN_BUTTON ))
154154 holdRepeatHelper .reset ();
155155 }
156156
157- if (event != null ) {
158- ComponentPath path = screen .nextFocusPath (event );
159- if (path != null ) {
160- accessor .invokeChangeFocus (path );
161-
157+ if (navigationFunc != null ) {
158+ if (navigationFunc .get ()) {
162159 holdRepeatHelper .onNavigate ();
163160
164161 controller .input ().ifPresent (InputComponent ::notifyGuiPressOutputsOfNavigate );
@@ -176,6 +173,19 @@ protected void handleComponentNavigation(ControllerEntity controller) {
176173 }
177174 }
178175
176+ protected @ Nullable Supplier <Boolean > createScreenNavigationFunc (ScreenDirection direction ) {
177+ var event = new FocusNavigationEvent .ArrowNavigation (direction );
178+ var path = screen .nextFocusPath (event );
179+ if (path == null ) {
180+ return null ;
181+ }
182+
183+ return () -> {
184+ ((ScreenAccessor ) screen ).invokeChangeFocus (path );
185+ return true ;
186+ };
187+ }
188+
179189 protected void handleButtons (ControllerEntity controller ) {
180190 boolean vmouseEnabled = Controlify .instance ().virtualMouseHandler ().isVirtualMouseEnabled ();
181191 InputComponent input = controller .input ().orElseThrow ();
@@ -252,14 +262,6 @@ protected void onTabChanged(ControllerEntity controller) {
252262
253263 public void onWidgetRebuild () {
254264 setInitialFocus ();
255- updateLastInputTypeOnRebild ();
256- }
257-
258- private void updateLastInputTypeOnRebild () {
259- final VirtualMouseHandler virtualMouseBehaviour = Controlify .instance ().virtualMouseHandler ();
260- if (virtualMouseBehaviour != null && !virtualMouseBehaviour .isVirtualMouseEnabled ()) {
261- Minecraft .getInstance ().setLastInputType (InputType .KEYBOARD_ARROW );
262- }
263265 }
264266
265267 public void onVirtualMouseToggled (boolean enabled ) {
@@ -276,12 +278,14 @@ protected void render(ControllerEntity controller, GuiGraphics graphics, float t
276278
277279 protected void setInitialFocus () {
278280 if (screen .getFocused () == null && Controlify .instance ().currentInputMode ().isController () && !Controlify .instance ().virtualMouseHandler ().isVirtualMouseEnabled ()) {
279- var accessor = ( ScreenAccessor ) screen ;
280- ComponentPath path = screen .nextFocusPath (accessor . invokeCreateArrowEvent ( ScreenDirection . DOWN ) );
281+ FocusNavigationEvent . TabNavigation tabNavigation = new FocusNavigationEvent . TabNavigation ( true ) ;
282+ var path = screen .nextFocusPath (tabNavigation );
281283 if (path != null ) {
282- accessor .invokeChangeFocus (path );
284+ (( ScreenAccessor ) screen ) .invokeChangeFocus (path );
283285 holdRepeatHelper .clearDelay ();
284286 }
287+
288+ minecraft .setLastInputType (InputType .KEYBOARD_ARROW );
285289 }
286290 }
287291
0 commit comments