Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature for SDK: AbstractControl + Button #518

Open
capdevon opened this issue Aug 9, 2023 · 0 comments
Open

New feature for SDK: AbstractControl + Button #518

capdevon opened this issue Aug 9, 2023 · 0 comments
Labels

Comments

@capdevon
Copy link
Contributor

capdevon commented Aug 9, 2023

I opened this Issue to keep track of forum discussions to add the ability to create in custom AbstractControls, functions that can be executed by button clicks.

https://hub.jmonkeyengine.org/t/new-feature-for-sdk-abstractcontrol-buttonproperty/46927

It requires the creation of a dedicated library, for example jme-sdk-devtools, to be published on the MAVEN repository. In this way, the library can be used by users in their Maven or Gradle projects to add graphical functions to AbstractControls and write their own editors with the SDK.

Here is an example:

  1. ButtonProperty
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ButtonProperty {

    String name() default "";
    
    String tooltip() default "";
}

  1. MyControl
public class MyControl extends AbstractControl {

    @Override
    protected void controlUpdate(float tpf) {
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
    
    @ButtonProperty(name="Click Me", tooltip="sayHello")
    public void sayHello() {
        System.out.println("Hello: " + MyControl.class.getSimpleName());
    }

}
  1. UIBuilder

import java.lang.reflect.Method;
import org.apache.commons.lang3.reflect.MethodUtils;
import com.jme3.scene.control.Control;

import javax.swing.*;

...

    public static JPanel buildPanel(Control control) {

        JPanel container = new JPanel(false);
        container.setLayout(new MigLayout("fillx"));

        ...
        
        List<Method> methods = MethodUtils.getMethodsListWithAnnotation(control.getClass(), ButtonProperty.class);
        for (Method method : methods) {
            ButtonProperty bp = method.getAnnotation(ButtonProperty.class);
            JButton button = new JButton(bp.name());
            button.setToolTipText(bp.tooltip());
            button.addActionListener(e ->  {
                try {
                    method.invoke(control);
                } catch (ReflectiveOperationException ex) {
                    ex.printStackTrace();
                }
            }));
            
            container.add(new JLabel(""), "align righ");
            container.add(button, "wrap, pushx, growx");
        }

        return container;
    }
@neph1 neph1 added the feature label Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants