MZ How to add double click/press support for sprites?

I'm trying to check if there's a better of way of doing this, because while it works it feels a bit sloppy, if i either click/press too much or too little. And also need to have the double click/press have priority over the single click/press without it going off.


Spoiler: My Sprite_Clickable Approach
JavaScript:
const ty_spriteClickable = Sprite_Clickable.prototype.initialize;
Sprite_Clickable.prototype.initialize = function() {
    ty_spriteClickable.call(this);
    this._pressCount = 0;
    this._lastPressed = 0;
};

const ty_processTouch = Sprite_Clickable.prototype.processTouch;
Sprite_Clickable.prototype.processTouch = function() {
    if (this.isBeingTouched() && TouchInput.isTriggered()) {
      
        const currentInput = TouchInput.date;
        const interval = Math.abs(this._lastPressed - currentInput);
        if (interval < 600 && this._pressCount < 2) {
            this._pressCount++;
        } else if (interval > 600) {
            this._pressCount = 0;
        }
        if (this._pressCount == 2) {
            this.onDoublePress();
        }
      
        console.log(interval + ' TouchInput.date');
    }
    ty_processTouch.call(this);
    this._lastPressed = TouchInput.date;
};


Sprite_Clickable.prototype.onDoublePress = function() {
    console.log('Has been double pressed');
};
I think I'd do this by patching the onClick or onPress method instead of processTouch, but your approach should be workable.

Suggestions:
  1. Check this.isClickEnabled() as well, like in the core scripts.
  2. Reset the press count when the picture is no longer hovered (onMouseExit).
  3. Consider timing double-clicks via the engine's frame count (Graphics.frameCount) rather than the system clock?
And also need to have the double click/press have priority over the single click/press without it going off.
A double-click includes two single-clicks, but you cannot know in advance whether a single-click is going to be a double-click. That means you'll have to delay all click responses until after the double-click interval (600 ms, in your case) has elapsed. In that case, I'd suggest using a shorter interval. Alternatively, depending on what happens on single-/double-click, you may be able to fake it by having double-clicks "undo" the single-click action.

Typically, double-clicks do something that adds to the single-click action, e.g. click to select, then click to target, or just double-click to use on self.
@caethyril Thank you very much for the suggestions, i have thought of some ideas based on your input, i'll see how they play out first.
Great, hope it goes well! 👋
For anyone wondering, this is what i came up with:
Spoiler: Single / Double Press
JavaScript:
const TY_spriteClickable = Sprite_Clickable.prototype.initialize;
Sprite_Clickable.prototype.initialize = function() {
    TY_spriteClickable.call(this);
    this._timer = 0;
    this._pressCount = 0;
    this._lastPressed = 0;
};


const TY_clickableUpdate = Sprite_Clickable.prototype.update;
Sprite_Clickable.prototype.update = function() {
    TY_clickableUpdate.call(this);
    this.checkPressEvent();
};


const TY_onSpritePress = Sprite_Clickable.prototype.onPress;
Sprite_Clickable.prototype.onPress = function() {
    TY_onSpritePress.call(this);
    if (this._pressCount > 2) this._pressCount = 0;
    this._pressCount++;
    this._lastPressed = Graphics.frameCount;
};


// Check what Sprite_Clickable event to trigger
Sprite_Clickable.prototype.checkPressEvent = function() {
    if (this._pressCount == 1) {
        this._timer = Graphics.frameCount - this._lastPressed;
    }
   
    if (this._pressCount == 1 && this._timer > 10) {
        this.onSinglePress();
        this._pressCount = 0;
       
    } else if (this._pressCount == 2 && this._timer < 10) {
        this.onDoublePress();
        this._pressCount = 0;
    }
};


Sprite_Clickable.prototype.onSinglePress = function() {
    console.log('was single pressed');
};


Sprite_Clickable.prototype.onDoublePress = function() {
    console.log('was double pressed');
};
I might need to adjust the timing a bit but this works pretty well i would say.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-add-double-click-press-support-for-sprites.152319/