Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Solved] [Wurst] Issues creating my first package (Mouse Event)

Discussion in 'Triggers & Scripts' started by apsyll, Feb 27, 2019.

  1. apsyll

    apsyll

    Joined:
    Aug 28, 2015
    Messages:
    212
    Resources:
    1
    Maps:
    1
    Resources:
    1
    Hey Hivers,
    after couple of days writing on this system and reading references manuals and more I still can't get my head around this.
    I try to create a mouse event package that handles the newly added mouse events in a much easier way
    due to this I created two packages but I get stuck somehow to get it running.
    The main issue is that I don't understand the language enough and the way to imagine its processing.

    The Idea is to have a system that can track
    Button Click& Release Left/Right Multi clicks(double click, triple clicks...) Fires actions associated to each event and
    Mouse Move saves the mouse position so it can be always looked up
    and Drag Saves a 'path' array of mouse positions
    (I also want to use it later for ray casting but this later when I get actually to something)

    Code (vJASS):

    package MouseUtils
    import ClosureEvents

    public tuple mouseTuple(boolean left, boolean right, boolean middle, boolean move, vec2 mousePosition)

    mouseTuple array mouseStatuses

    public function player.getMouseButtons() returns mouseTuple
        return mouseStatuses[this.getId()]

    EventListener firstKeyListener = null

    public function addMouseListener(EventListener listener) returns EventListener
        if firstKeyListener != null
            firstKeyListener.prev = listener
            listener.next = firstKeyListener

        firstKeyListener = listener
        return listener

    public function removeMouseListener(EventListener listener)
        if firstKeyListener == listener
            firstKeyListener = null
        destroy listener

    function onMouseEvent()
        var mouseStatus = mouseStatuses[GetTriggerPlayer().getId()]
        let id = GetTriggerEventId()
        let mb = BlzGetTriggerPlayerMouseButton()
        if id == EVENT_PLAYER_MOUSE_DOWN
            if mb == MOUSE_BUTTON_TYPE_LEFT
                mouseStatus.left = true
            else if mb == MOUSE_BUTTON_TYPE_RIGHT
                mouseStatus.right = true
            else if mb == MOUSE_BUTTON_TYPE_MIDDLE
                mouseStatus.middle = true
        else if id == EVENT_PLAYER_MOUSE_UP
            if mb == MOUSE_BUTTON_TYPE_LEFT
                mouseStatus.left = false
            else if mb == MOUSE_BUTTON_TYPE_RIGHT
                mouseStatus.right = false
            else if mb == MOUSE_BUTTON_TYPE_MIDDLE
                mouseStatus.middle = false
        else if id == EVENT_PLAYER_MOUSE_MOVE
            mouseStatus.move = true
        mouseStatus.mousePosition = vec2(BlzGetTriggerPlayerMouseX(),BlzGetTriggerPlayerMouseY())

        mouseStatuses[GetTriggerPlayer().getId()] = mouseStatus

        var listener = firstKeyListener
        while listener != null
            listener.onEvent()
            listener = listener.next

    init
        for i = 0 to 23
            mouseStatuses[i] = mouseTuple(false, false, false, false, vec2(0., 0.))
        EventListener.add(EVENT_PLAYER_MOUSE_DOWN, () -> onMouseEvent())
        EventListener.add(EVENT_PLAYER_MOUSE_UP, () -> onMouseEvent())
        EventListener.add(EVENT_PLAYER_MOUSE_MOVE, () -> onMouseEvent())
     
    Code (vJASS):

    package inputTest

    import MouseUtils
    import FxEntity

    public class MouseEvent
        EventListener klistener

        function onBegin()
            klistener = addMouseListener() ->
                foo()

        function onEnd()
            if klistener != null
                removeMouseListener(klistener)

        function foo()
            let downButtons = GetTriggerPlayer().getMouseButtons()
            if downButtons.left
                print("Left")
                //left button is pressed
            if downButtons.right
                //right button is pressed
                print("Right")
    init
        MouseEvent.onBegin() //<== Error "Cannot call dynamic function onBegin from static context (undefined)
     
    Every help is welcome.
    Thank you in advance.
     
  2. Watsuby

    Watsuby

    Joined:
    Dec 16, 2011
    Messages:
    12
    Resources:
    0
    Resources:
    0
    Looks like you didn't create an instance of MouseEvent. You're trying to use onBegin() like its a static function.

    Probably what you want to do:
    Code (Text):
    init
        MouseEvent mouseEvent = new MouseEvent()
        mouseEvent.onBegin()
     
  3. apsyll

    apsyll

    Joined:
    Aug 28, 2015
    Messages:
    212
    Resources:
    1
    Maps:
    1
    Resources:
    1
    Yes this worked for me thank you.
     
  4. Lake

    Lake

    Joined:
    Mar 5, 2015
    Messages:
    135
    Resources:
    0
    Resources:
    0
    Another way to write it could also be like this:

    Code (Text):

    new MouseEvent().onBegin()
     
    ... As the variable is redundant ;)

    And btw, really cool idea ;)