ImagineEngine icon indicating copy to clipboard operation
ImagineEngine copied to clipboard

Question adding gestures

Open Alex88WH opened this issue 7 years ago • 6 comments

Hello there! Imagine Engine 0.9.0 🎉 Game Jam on its 1.0.0 release? haha Imagine Engine needs a showcase!! Question: There's a way to add common gestures (like swipes) on the scene's game.view? (game.view is just to attach to the app's view, right?) Should I build a plugin for that? (like the ClickPlugin). Thanks a lot!

PS: Dialogue plugin progress plugin

Alex88WH avatar Jan 26 '18 20:01 Alex88WH

Let's totally do a game jam once we hit 1.0.0 😄 Need to start defining the criteria for 1.0.0 🤔

Plugin looks amazing @Alex88WH! 🚀 I love the effect on the exclamation marks 😄

In Revazendo I use a plugin to control the ship. It simply adds a UIPanGestureRecognizer to the game view that I then observe and move the ship accordingly. I was thinking the other day if we should add a panned event on Scene, similar to how there's a clicked event. Question is what it would translate to on macOS, a mouse drag event maybe? We could also make it iOS + tvOS only. What do you think? (cc @mattiashagstrand, @aranasaurus, @insidegui, @Loyolny, @lcs-rgordon)

JohnSundell avatar Jan 26 '18 21:01 JohnSundell

Ok, swipe plugin is working! 🎉 Thank you! Messing around with that pan gesture. It's great and I think is a must have!

Alex88WH avatar Jan 30 '18 18:01 Alex88WH

@JohnSundell Yeah, I think panning should be translated to mouse drag on macOS. I think it should be useful to start adding plugins that are platform specific. For example, I created a plugin that handles key events on macOS (and a iOS specific one for handling tilting the phone). A collection of plugins for common things you need to do in games would further lower the learning curve for using ImagineEngine.

mattiashagstrand avatar Feb 04 '18 11:02 mattiashagstrand

Yeah let's add a panned event, think it'd be super useful 👍

Perhaps we could do something like:

scene.events.panned(includingMouseDrags: Bool).observe {
    ...
}

That way the user can decide how this event should be treated on macOS?

Regarding plugins, we should totally make that plugin repo (and maybe some tooling to query that repo for plugins?), I'll get on that within the next few weeks 😄

JohnSundell avatar Feb 05 '18 22:02 JohnSundell

Just getting started with ImageEngine (mainly for macOS) and I just know it will be faster than what I'm currently getting from SpriteKit! That said, it's fairly nascent at this point and I'd like to contribute... first I need to figure some stuff out :) like how to create a custom events for say keyUp/keyDown? Seems we need to get into GameViewController and dispatch the events via a Scene Event? (and/or using a PlugIn) Any help would be appreciated!

chessboy avatar Apr 26 '20 00:04 chessboy

ok, I get it... please LMK know if this is in the spirit of the framework:

//
//  KeyEventPlugin.swift
//  (for Imagine Engine)
//
//  Created by Rob Silverman on 4/25/20.
//  Copyright © 2020 Rob Silverman. All rights reserved.
//

import ImagineEngine

final class KeyEventPlugin: Plugin {
    private weak var scene: Scene?
    private var keyDownObserver: Any?
    private var keyUpObserver: Any?

    func activate(for object: Scene, in game: Game) {
        scene = object
        
        keyDownObserver = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event -> NSEvent? in
            self.keyDown(with: event)
            return event
        }
    
        keyUpObserver = NSEvent.addLocalMonitorForEvents(matching: .keyUp) { event -> NSEvent? in
            self.keyUp(with: event)
            return event
        }
    }
    
    func deactivate() {
        scene = nil
        if let keyDownObserver = keyDownObserver {
            NSEvent.removeMonitor(keyDownObserver)
        }
        if let keyUpObserver = keyUpObserver {
            NSEvent.removeMonitor(keyUpObserver)
        }
    }

    func keyDown(with event: NSEvent?) {
        guard let event = event, let scene = scene else {
            return
        }
        //print("key down: \(event.keyCode)")
        scene.events.keyDown.trigger(with: event)
     }
        
    func keyUp(with event: NSEvent?) {
        guard let event = event, let scene = scene else {
            return
        }
        //print("key up: \(event.keyCode)")
        scene.events.keyUp.trigger(with: event)
    }
}

/** usage:
scene.events.keyDown.observe { scene, event in
    if event.keyCode == Keycode.space {
        self.isPaused.toggle()
    }
}
*/

extension SceneEventCollection {
    var keyDown: Event<Scene, NSEvent> {
        return makeEvent()
    }
    
    var keyUp: Event<Scene, NSEvent> {
        return makeEvent()
    }
}

chessboy avatar Apr 26 '20 01:04 chessboy