2021-02-27 19:02:30 +00:00
|
|
|
package net.shadowfacts.cacao
|
|
|
|
|
|
|
|
import net.shadowfacts.cacao.util.KeyModifiers
|
|
|
|
import net.shadowfacts.cacao.window.Window
|
|
|
|
|
|
|
|
/**
|
2021-02-28 17:19:09 +00:00
|
|
|
* A responder is an object which participates in the responder chain, a mechanism for propagating indirect events (i.e.
|
|
|
|
* events for which there is no on-screen position to determine the targeted view, such as key presses) through the
|
|
|
|
* view hierarchy.
|
|
|
|
*
|
2021-02-27 19:02:30 +00:00
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
interface Responder {
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* The window that this responder is part of.
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
val window: Window?
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Whether this responder is the first responder of its window.
|
|
|
|
*
|
|
|
|
* `false` if [window] is null.
|
|
|
|
*
|
|
|
|
* @see Window.firstResponder
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
val isFirstResponder: Boolean
|
|
|
|
get() = window?.firstResponder === this
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* The next responder in the chain after this. The next responder will receive an event if this responder did not
|
|
|
|
* accept it.
|
|
|
|
*
|
|
|
|
* The next responder may be `null` if this responder is at the end of the chain.
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
val nextResponder: Responder?
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Makes this responder become the window's first responder.
|
|
|
|
* @throws RuntimeException if [window] is null
|
|
|
|
* @see Window.firstResponder
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
fun becomeFirstResponder() {
|
|
|
|
if (window == null) {
|
|
|
|
throw RuntimeException("Cannot become first responder while not in Window")
|
|
|
|
}
|
|
|
|
window!!.firstResponder = this
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Called immediately after this responder has become the window's first responder.
|
|
|
|
* @see Window.firstResponder
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
fun didBecomeFirstResponder() {}
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Removes this object as the window's first responder.
|
|
|
|
* @throws RuntimeException if [window] is null
|
|
|
|
* @see Window.firstResponder
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
fun resignFirstResponder() {
|
|
|
|
if (window == null) {
|
|
|
|
throw RuntimeException("Cannot resign first responder while not in Window")
|
|
|
|
}
|
|
|
|
window!!.firstResponder = null
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Called immediately before this object is removed as the window's first responder.
|
|
|
|
* @see Window.firstResponder
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
fun didResignFirstResponder() {}
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Called when a character has been typed.
|
|
|
|
*
|
|
|
|
* @param char The character that was typed.
|
|
|
|
* @param modifiers The key modifiers that were held down when the character was typed.
|
|
|
|
* @return Whether this responder accepted the event. If `true`, it will not be passed to the next responder.
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
fun charTyped(char: Char, modifiers: KeyModifiers): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
/**
|
|
|
|
* Called when a keyboard key is pressed.
|
|
|
|
*
|
|
|
|
* If the pressed key is a typed character, [charTyped] will also be called. The order in which the methods are
|
|
|
|
* invoked is undefined and should not be relied upon.
|
|
|
|
*
|
|
|
|
* @param keyCode The integer code of the key that was pressed.
|
|
|
|
* @param modifiers The key modifiers that were held down when the character was typed.
|
|
|
|
* @return Whether this responder accepted the event. If `true`, it will not be passed to the next responder.
|
|
|
|
* @see org.lwjgl.glfw.GLFW for key code constants
|
|
|
|
*/
|
2021-02-27 19:02:30 +00:00
|
|
|
fun keyPressed(keyCode: Int, modifiers: KeyModifiers): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:19:09 +00:00
|
|
|
}
|