Permalink
Browse files

new controls deletion api, todo

  • Loading branch information...
1 parent c583b98 commit ea556b110703bdabdf5f9f39bbfd6cd93d318a60 Andrey Borunov committed Apr 3, 2012
@@ -4,7 +4,7 @@ import handlers.RendererLib
import support._
import support.messages._
-trait ScageLib extends ScagePropertiesTrait with ScageMessageTrait with ScageXMLTrait with RendererLib with LWJGLKeyboard with ScageColorTrait {
+trait ScageLib extends ScagePropertiesTrait with ScageMessageTrait with ScageXMLTrait with RendererLib with LWJGLKeyboard with ScageColorTrait with ScageIdTrait {
def property[A : Manifest](key:String, default:A):A = ScageProperties.property(key, default)
def property[A : Manifest](key:String, default:A, condition:(A => (Boolean, String))):A = ScageProperties.property(key, default, condition)
@@ -18,6 +18,8 @@ trait ScageLib extends ScagePropertiesTrait with ScageMessageTrait with ScageXML
def xmlInterfaceStrings(interface_id:String, parameters:Any*):Array[String] = ScageXML.xmlInterfaceStrings(interface_id, parameters:_*)
def stopApp() {Scage.stopApp()}
+
+ def nextId = ScageId.nextId
}
object ScageLib extends ScageLib
@@ -3,6 +3,7 @@ package net.scage.handlers.controller2
import net.scage.support.Vec
import org.lwjgl.input.{Keyboard, Mouse}
import collection.mutable.{HashMap, ArrayBuffer}
+import net.scage.support.ScageId._
case class MultiKeyEvent(var was_pressed:Boolean, var last_pressed_time:Long, repeat_time: () => Long, onKeyDown: () => Any, onKeyUp: () => Any)
case class MultiMouseButtonEvent(var was_pressed:Boolean, var last_pressed_time:Long, repeat_time: () => Long, onButtonDown: Vec => Any, onButtonUp: Vec => Any)
@@ -16,124 +17,137 @@ trait MultiController extends ScageController {
private var mouse_wheel_ups = ArrayBuffer[Vec => Any]()
private var mouse_wheel_downs = ArrayBuffer[Vec => Any]()
- def key(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {}) {
- if(keyboard_keys.contains(key_code)) keyboard_keys(key_code) += MultiKeyEvent(false, 0, () => repeat_time, () => if(!on_pause) onKeyDown, () => if(!on_pause) onKeyUp)
- else keyboard_keys(key_code) = ArrayBuffer(MultiKeyEvent(false, 0, () => repeat_time, () => if(!on_pause) onKeyDown, () => if(!on_pause) onKeyUp))
- }
- def keyNoPause(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {}) {
- if(keyboard_keys.contains(key_code)) keyboard_keys(key_code) += MultiKeyEvent(false, 0, () => repeat_time, () => onKeyDown, () => onKeyUp)
- else keyboard_keys(key_code) = ArrayBuffer(MultiKeyEvent(false, 0, () => repeat_time, () => onKeyDown, () => onKeyUp))
- }
-
- def anykey(onKeyDown: => Any) {
- anykeys += (() => if(!on_pause) onKeyDown)
- }
- def anykeyNoPause(onKeyDown: => Any) {
- anykeys += (() => onKeyDown)
+ def key(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {}) = {
+ val control_id = nextId
+ val event = MultiKeyEvent(false, 0, () => repeat_time, () => if(!on_pause) onKeyDown, () => if(!on_pause) onKeyUp)
+ if(keyboard_keys.contains(key_code)) keyboard_keys(key_code) += event
+ else keyboard_keys(key_code) = ArrayBuffer(event)
+ deletion_operations += control_id -> (() => keyboard_keys(key_code) -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
+ }
+ def keyNoPause(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {}) = {
+ val control_id = nextId
+ val event = MultiKeyEvent(false, 0, () => repeat_time, () => onKeyDown, () => onKeyUp)
+ if(keyboard_keys.contains(key_code)) keyboard_keys(key_code) += event
+ else keyboard_keys(key_code) = ArrayBuffer(event)
+ deletion_operations += control_id -> (() => keyboard_keys(key_code) -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
+ }
+
+ def anykey(onKeyDown: => Any) = {
+ val control_id = nextId
+ val event = () => if(!on_pause) onKeyDown
+ anykeys += event
+ deletion_operations += control_id -> (() => anykeys -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
+ }
+ def anykeyNoPause(onKeyDown: => Any) = {
+ val control_id = nextId
+ val event = () => onKeyDown
+ anykeys += event
+ deletion_operations += control_id -> (() => anykeys -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
def mouseCoord = Vec(Mouse.getX, Mouse.getY)
def isMouseMoved = Mouse.getDX != 0 || Mouse.getDY != 0
- private def mouseButton(button_code:Int, repeat_time: => Long = 0, onButtonDown: Vec => Any, onButtonUp: Vec => Any = Vec => {}) {
- if(mouse_buttons.contains(button_code)) mouse_buttons(button_code) += MultiMouseButtonEvent(false, 0, () => repeat_time, onButtonDown, onButtonUp)
- else mouse_buttons(button_code) = ArrayBuffer(MultiMouseButtonEvent(false, 0, () => repeat_time, onButtonDown, onButtonUp))
+ private def mouseButton(button_code:Int, repeat_time: => Long = 0, onButtonDown: Vec => Any, onButtonUp: Vec => Any = Vec => {}) = {
+ val control_id = nextId
+ val event = MultiMouseButtonEvent(false, 0, () => repeat_time, onButtonDown, onButtonUp)
+ if(mouse_buttons.contains(button_code)) mouse_buttons(button_code) += event
+ else mouse_buttons(button_code) = ArrayBuffer(event)
+ deletion_operations += control_id -> (() => mouse_buttons(button_code) -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- def leftMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) {
+ def leftMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) = {
mouseButton(0, repeat_time, mouse_coord => if(!on_pause) onBtnDown(mouse_coord), mouse_coord => if(!on_pause) onBtnUp(mouse_coord))
}
- def leftMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) {
+ def leftMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) = {
mouseButton(0, repeat_time, onBtnDown, onBtnUp)
}
- def rightMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) {
+ def rightMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) = {
mouseButton(1, repeat_time, mouse_coord => if(!on_pause) onBtnDown(mouse_coord), mouse_coord => if(!on_pause) onBtnUp(mouse_coord))
}
- def rightMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) {
+ def rightMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}) = {
mouseButton(1, repeat_time, onBtnDown, onBtnUp)
}
- def mouseMotion(onMotion: Vec => Any) {
- mouse_motions += (mouse_coord => if(!on_pause) onMotion(mouse_coord))
+ def mouseMotion(onMotion: Vec => Any) = {
+ val control_id = nextId
+ val event = {mouse_coord:Vec => if(!on_pause) onMotion(mouse_coord)}
+ mouse_motions += event
+ deletion_operations += control_id -> (() => mouse_motions -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- def mouseMotionNoPause(onMotion: Vec => Any) {
+ def mouseMotionNoPause(onMotion: Vec => Any) = {
+ val control_id = nextId
mouse_motions += onMotion
+ deletion_operations += control_id -> (() => mouse_motions -= onMotion)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- private def mouseDrag(button_code:Int, onDrag: Vec => Any) {
+ private def mouseDrag(button_code:Int, onDrag: Vec => Any) = {
+ val control_id = nextId
if(mouse_drag_motions.contains(button_code)) mouse_drag_motions(button_code) += onDrag
else mouse_drag_motions(button_code) = ArrayBuffer(onDrag)
+ deletion_operations += control_id -> (() => mouse_drag_motions(button_code) -= onDrag)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- def leftMouseDrag(onDrag: Vec => Any) {
+ def leftMouseDrag(onDrag: Vec => Any) = {
mouseDrag(0, mouse_coord => if(!on_pause) onDrag(mouse_coord))
}
- def leftMouseDragNoPause(onDrag: Vec => Any) {
+ def leftMouseDragNoPause(onDrag: Vec => Any) = {
mouseDrag(0, onDrag)
}
- def rightMouseDrag(onDrag: Vec => Any) {
+ def rightMouseDrag(onDrag: Vec => Any) = {
mouseDrag(1, mouse_coord => if(!on_pause) onDrag(mouse_coord))
}
- def rightMouseDragNoPause(onDrag: Vec => Any) {
+ def rightMouseDragNoPause(onDrag: Vec => Any) = {
mouseDrag(1, onDrag)
}
- def mouseWheelUp(onWheelUp: Vec => Any) {
- mouse_wheel_ups += (mouse_coord => if(!on_pause) onWheelUp(mouse_coord))
+ def mouseWheelUp(onWheelUp: Vec => Any) = {
+ val control_id = nextId
+ val event = {mouse_coord:Vec => if(!on_pause) onWheelUp(mouse_coord)}
+ mouse_wheel_ups += event
+ deletion_operations += control_id -> (() => mouse_wheel_ups -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- def mouseWheelUpNoPause(onWheelUp: Vec => Any) {
+ def mouseWheelUpNoPause(onWheelUp: Vec => Any) = {
+ val control_id = nextId
mouse_wheel_ups += onWheelUp
+ deletion_operations += control_id -> (() => mouse_wheel_ups -= onWheelUp)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- def mouseWheelDown(onWheelDown: Vec => Any) {
- mouse_wheel_downs += (mouse_coord => if(!on_pause) onWheelDown(mouse_coord))
+ def mouseWheelDown(onWheelDown: Vec => Any) = {
+ val control_id = nextId
+ val event = {mouse_coord:Vec => if(!on_pause) onWheelDown(mouse_coord)}
+ mouse_wheel_downs += event
+ deletion_operations += control_id -> (() => mouse_wheel_downs -= event)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
- def mouseWheelDownNoPause(onWheelDown: Vec => Any) {
+ def mouseWheelDownNoPause(onWheelDown: Vec => Any) = {
+ val control_id = nextId
mouse_wheel_downs += onWheelDown
- }
-
- def delKeys(key_codes_to_delete: Int*) {
- keyboard_keys --= key_codes_to_delete
- }
- def delAnyKey() {
- anykeys.clear()
- }
- def delAllKeys() {
- keyboard_keys.clear()
- }
- def delAllKeysExcept(key_codes_to_save: Int*) {
- delKeys(keyboard_keys.filter(key => !key_codes_to_save.contains(key._1)).map(_._1).toSeq:_*)
- }
-
- def delMouseButtons(mouse_buttons_to_delete:Int*) {
- mouse_buttons --= mouse_buttons_to_delete
- }
- def delAllMouseButtons() {
- mouse_buttons.clear()
- }
- def delAllMouseButtonsExcept(mouse_buttons_to_save:Int*) {
- delMouseButtons(mouse_buttons.filter(btn => !mouse_buttons_to_save.contains(btn._1)).map(_._1).toSeq:_*)
- }
-
- def delMouseMotion() {
- mouse_motions.clear()
- }
- def delMouseDrags(mouse_buttons_to_delete:Int*) {
- mouse_drag_motions --= mouse_buttons_to_delete
- }
- def delAllMouseDrags() {
- mouse_drag_motions.clear()
- }
- def delAllMouseDragsExcept(mouse_buttons_to_save:Int*) {
- delMouseDrags(mouse_drag_motions.filter(btn => !mouse_buttons_to_save.contains(btn._1)).map(_._1).toSeq:_*)
- }
-
- def delMouseWheelUp() {
- mouse_wheel_ups.clear()
- }
- def delMouseWheelDown() {
- mouse_wheel_downs.clear()
+ deletion_operations += control_id -> (() => mouse_wheel_downs -= onWheelDown)
+ operations_mapping += control_id -> ControlOperations.Control
+ control_id
}
def checkControls() {
@@ -3,6 +3,7 @@ package net.scage.handlers.controller2
import net.scage.support.Vec
import net.scage.Scage
import collection.mutable.HashMap
+import com.weiglewilczek.slf4s.Logger
case class KeyPress(key_code:Int, var was_pressed:Boolean, var last_pressed_time:Long)
case class MouseButtonPress(button_code:Int, var was_pressed:Boolean, var last_pressed_time:Long)
@@ -13,6 +14,8 @@ object ScageController {
}
trait ScageController extends Scage {
+ private val log = Logger(this.getClass.getName)
+
protected def keyPress(key_code:Int):KeyPress = {
ScageController.key_presses.get(key_code) match {
case Some(kp:KeyPress) => kp
@@ -33,69 +36,88 @@ trait ScageController extends Scage {
}
}
- def key(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {})
- def keyNoPause(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {})
+ def key(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {}):Int
+ def keyNoPause(key_code:Int, repeat_time: => Long = 0, onKeyDown: => Any, onKeyUp: => Any = {}):Int
- def anykey(onKeyDown: => Any)
- def anykeyNoPause(onKeyDown: => Any)
+ def anykey(onKeyDown: => Any):Int
+ def anykeyNoPause(onKeyDown: => Any):Int
def mouseCoord:Vec
def isMouseMoved:Boolean
- def leftMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {})
- def leftMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {})
-
- def rightMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {})
- def rightMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {})
+ def leftMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}):Int
+ def leftMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}):Int
- def mouseMotion(onMotion: Vec => Any)
- def mouseMotionNoPause(onMotion: Vec => Any)
+ def rightMouse(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}):Int
+ def rightMouseNoPause(repeat_time: => Long = 0, onBtnDown: Vec => Any, onBtnUp: Vec => Any = Vec => {}):Int
- def leftMouseDrag(onDrag: Vec => Any)
- def leftMouseDragNoPause(onDrag: Vec => Any)
+ def mouseMotion(onMotion: Vec => Any):Int
+ def mouseMotionNoPause(onMotion: Vec => Any):Int
- def rightMouseDrag(onDrag: Vec => Any)
- def rightMouseDragNoPause(onDrag: Vec => Any)
+ def leftMouseDrag(onDrag: Vec => Any):Int
+ def leftMouseDragNoPause(onDrag: Vec => Any):Int
- def mouseWheelUp(onWheelUp: Vec => Any)
- def mouseWheelUpNoPause(onWheelUp: Vec => Any)
+ def rightMouseDrag(onDrag: Vec => Any):Int
+ def rightMouseDragNoPause(onDrag: Vec => Any):Int
- def mouseWheelDown(onWheelDown: Vec => Any)
- def mouseWheelDownNoPause(onWheelDown: Vec => Any)
+ def mouseWheelUp(onWheelUp: Vec => Any):Int
+ def mouseWheelUpNoPause(onWheelUp: Vec => Any):Int
- def delKeys(key_codes_to_delete: Int*)
- def delAnyKey()
- def delAllKeys()
+ def mouseWheelDown(onWheelDown: Vec => Any):Int
+ def mouseWheelDownNoPause(onWheelDown: Vec => Any):Int
- def delMouseButtons(mouse_buttons_to_delete:Int*)
- def delAllMouseButtons()
- def delMouseMotion()
- def delMouseDrags(mouse_buttons_to_delete:Int*)
- def delAllMouseDrags()
- def delMouseWheelUp()
- def delMouseWheelDown()
-
- def delAllMouseWheelEvents() {
- delMouseWheelUp()
- delMouseWheelDown()
+ def checkControls()
+
+ private[controller2] val deletion_operations = HashMap[Int, () => Any]()
+
+ def delControls(control_ids:Int*):Boolean = {
+ val result = control_ids.foldLeft(true)((overall_result, control_id) => {
+ val deletion_result = deletion_operations.get(control_id) match {
+ case Some(op) =>
+ op()
+ log.debug("deleted control with id "+control_id)
+ true
+ case None =>
+ log.warn("control with id "+control_id+" not found so wasn't deleted")
+ false
+ }
+ overall_result && deletion_result
+ })
+ deletion_operations --= control_ids
+ result
+ }
+ def delAllControls() {
+ for(op <- deletion_operations.values) op()
+ deletion_operations.clear()
+ log.info("deleted all control operations")
}
- def delAllMouseEvents() {
- delAllMouseButtons()
- delMouseMotion()
- delAllMouseDrags()
- delAllMouseWheelEvents()
+ def delAllControlsExcept(control_ids:Int*) {
+ delControls(deletion_operations.keys.filter(!control_ids.contains(_)).toSeq:_*)
}
- def delAllKeysAndMouseEvents() {
- delAllKeys()
- delAnyKey()
- delAllMouseEvents()
+
+ object ControlOperations extends Enumeration {
+ val Control = Value
}
+ override def delOperation(operation_id:Int) = {
+ operations_mapping.get(operation_id) match {
+ case Some(operation_type) => {
+ operation_type match {
+ case ControlOperations.Control => delControls(operation_id)
+ case _ => super.delOperation(operation_id)
+ }
+ }
+ case None => super.delOperation(operation_id)
+ }
+ }
+
override def delAllOperations() {
- delAllKeysAndMouseEvents()
+ delAllControls()
super.delAllOperations()
}
-
- def checkControls()
+ override def delAllOperationsExcept(operation_ids:Int*) {
+ delAllControlsExcept(operation_ids:_*)
+ super.delAllOperationsExcept(operation_ids:_*)
+ }
}
Oops, something went wrong.

0 comments on commit ea556b1

Please sign in to comment.