【问题标题】:Jmonkey Nifty. detect if user stops slidingJmonkey 漂亮。检测用户是否停止滑动
【发布时间】:2023-03-19 12:21:01
【问题描述】:

有没有办法检测用户是否停止滑动?也许bij鼠标输入检查或其他东西。我在 onSliderChange 事件中尝试了鼠标 isButton0Release 和 hasFocus 来检查用户是否停止滑动,但这不起作用。

到目前为止我得到了什么

@NiftyEventSubscriber(id = "speedSlider")
public void onSliderChange(String id, SliderChangedEvent event) 
{   boolean test2 = event.getSlider().hasFocus();
    boolean test = nifty.getMouseInputEventQueue().getLastMouseDownEvent().isButton0Release();
    System.out.println("before " + test2);
    int speed = (int)event.getValue();
    speedTextField.getRenderer(TextRenderer.class).setText(speed + "");
    if(test2){
        System.out.println("after " + test2);
        main.setSimSpeed(speed);
    }
}

还有xml中的滑块

                <panel id="speed_up_down_panel" height="30px" width="180px" childLayout="center">         
                <control id="speedSlider" name="horizontalSlider" width="150px" min="1" initial="1" buttonStepSize="1">
                    <image id="#position" filename="Interface/sliderbutton.png" visibleToMouse="true" width="40px" height="40px"></image>
                </control>
            </panel> 

现在代码有点乱,因为我在测试。

【问题讨论】:

  • 你能解释一下为什么你需要这个活动吗?在滑动操作结束时做出反应并不是那么容易。主要是因为执行该操作的方法不止一种。您可以拖动手柄,单击轨道,使用键盘。除了拖动手柄之外,如何定义“停止滑动”?
  • 我需要这个的原因是因为我必须通过套接字将滑块的值发送到控制器。如果我只使用 onSliderChange 事件,该值将被发送多次,所以我认为当用户停止滑动时,该值发送一次会更好。
  • 固定超时时间不是更安全吗?如果值已更改并且在x 毫秒内不再更改,则发送更新的值?这将涵盖通过鼠标单击/按下轨道和键盘更改的变化。

标签: java jmonkeyengine nifty-gui


【解决方案1】:

我能想到的最简单的方法是定义自己的控件,添加一个onRelease 事件处理程序:

<?xml version="1.0" encoding="UTF-8"?>
<nifty-controls xmlns="http://nifty-gui.lessvoid.com/nifty-gui">
<controlDefinition name="horizontalSlider" style="nifty-horizontal-slider"
    controller="de.lessvoid.nifty.controls.slider.SliderControl"
    inputMapping="de.lessvoid.nifty.controls.scrollbar.ScrollbarInputMapping">

    <panel style="#panel">
        <interact onMouseWheel="mouseWheel()"/>
        <image style="#left">
            <interact onClickRepeat="upClick()"/>
        </image>
        <image id="#background" style="#background">
            <interact onClick="mouseClick()" onClickMouseMove="mouseClick()"/>
            <image id="#position" style="#position">
                <interact onClick="mouseClick()" onClickMouseMove="mouseClick()"
                    onRelease="onRelease()"/>
                    <!-- ^ ^ ^ ADD THIS ^ ^ ^ -->
            </image>
        </image>
        <image style="#right">
            <interact onClickRepeat="downClick()"/>
        </image>
    </panel>
</controlDefinition>

注意:我只是复制了 SliderControl 的原始定义并将onRelease 添加到&lt;interact&gt; 标记中。 见:https://github.com/nifty-gui/nifty-gui/blob/1.4/nifty-controls/src/main/resources/nifty-controls/nifty-slider.xml

现在使用 XML 中的这一行加载这个新控件:
(在加载nifty-default-controls.xml的那一行之后插入)

<useControls filename="Interface/NewSlider.xml" />

然后将方法public void onRelease() 添加到您的控制器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多