How to add a separate `onRightClick` function to `h2d.Interactive`?

What’s the appropriate elegant way to add an onRightClick function to the interactive?

Unfortunately, problem is that this case seems to be neglected, doesn’t it? Because

  • onClick only regards left mouse click (or left+right bunched together with enableRightButton), while
  • onWheel doesn’t help of course, neither does
  • onKeyDown etc. as it only checks the keyboard.

more info

  • before it gets messy and I use the interactives shape/h2d.col.Collider or a getBounds()
  • of course Interactive.enableRightButton is not what I’m searching for.

Research


Thank you! :slight_smile:

This is documented as part of the interactive enableRightButton property here
https://heaps.io/api/h2d/Interactive.html#enableRightButton

1 Like

Thank you for your reply! Now it works :smiley:
I misunderstood this property…


(Code sample below)

import h2d.Interactive;
import hxd.res.DefaultFont;
import hxd.App;

class H2DInteractivePanel extends hxd.App {

    override function init() {

        var b = UI.newButton_standard("Default (no right click)", this.s2d );
        b.onClick = function(e){b.backgroundColor = 0xFF99ff99; };

        var b = UI.newButton_standard("Right click allowed (calls same function)", this.s2d );
        b.enableRightButton = true;
        b.onClick = function(e){b.backgroundColor = 0xFF99ff99; };

        var b = UI.newButton_standard("Behaves differently: left/right/shift-click", this.s2d );
        b.enableRightButton = true;
        b.onClick = function(e){
            if( hxd.Key.isReleased(hxd.Key.MOUSE_LEFT) )
                b.backgroundColor = 0xFF99ff99;
            if( hxd.Key.isReleased(hxd.Key.MOUSE_RIGHT) )
                b.backgroundColor = 0xFF99ccff;
            if( hxd.Key.isDown(hxd.Key.SHIFT) )
                b.backgroundColor = 0xFFffcc66;
        };

    }
    public static function main() {
        new H2DInteractivePanel();
    }
}

// this is just to set up the example

class UI {
    private static var yy = 8;
    public static function newButton_standard( button_name:String, ?parent:Null<h2d.Object> ) : Interactive {
        var bw : Float = 320;
        var bh : Float =  32;
        var b = newButton( bw, bh, button_name, parent );
        b.y = yy; yy += 32; b.x += 8; // set correct position
        return b;
    }

    public static function newButton( width:Float, height:Float, button_name:String, ?parent:Null<h2d.Object> ) : Interactive {
        var b = new Interactive( width, height, parent );
        var color_default = 0xFFb3b3b3;
        var color_active = 0xFFd9d9d9;
        b.backgroundColor = color_default;
        b.onOut = function(e){
            b.backgroundColor = color_default;
        };
        b.onOver = function(e){
            b.backgroundColor = color_active;
        };
        var t = new h2d.Text( DefaultFont.get(), b ); // the text/label is child to the button/interactive
        t.text = button_name;
        t.textColor = 0xFFFFFF; // white
        return b;
    }
}