//======================================================================================================================== // // Mouse // //======================================================================================================================== var mouseGlobal = { name: "mouseGlobal", x: 0, y: 0, w: GetScreenWidth(), h: GetScreenHeight() }; // mouseGlobal for the mouse, in case it is NOT over anything //======================================================================================================================== // Object //======================================================================================================================== function _mouse(image) { this.x = GetMouseX(); // Mouse's X this.y = GetMouseY(); // Mouse's Y this.image = image || LoadImage("Mouse.png"); // Mouse's Image this.idle = 0; // Set mouse idle time this.locked = false; // Used to lock onto one control despite of the mouse's location this.focus = mouseGlobal; // The control the mouse is currently using this.lastFocus = mouseGlobal; // Keeps record of the last control the mouse had used - Used to stop issues with double click ie one click on one control and second click over another would perfom a double click on the second without this this.lastButton = -1; // Keep record of the last button that was pressed - Also used for double click to avoid a left followed by a right click initiating the double click on right click funciton this.held = 0; // Registers if mouse button held and if on click has been performed all ready this.heldLag = 0; // The amount of delays between repeatadly performing the "OnHeld" callback this.time = GetTime() - 150; // Timer used for double click this.doubleClick = false; // Used to default to double click instead of regular click } //======================================================================================================================== // Update //======================================================================================================================== // The mouse is handled and the clicks are controlled here _mouse.prototype.update = function() { if (this.x == GetMouseX() && this.y == GetMouseY() && this.idle < 300000) this.idle++; // If the mouse is idle start counting how long for else this.idle = 0; // If mouse has moved then reset the idle count this.x = GetMouseX(); // Updates the current Mouse co-ords this.y = GetMouseY(); // Updates the current Mouse co-ords while (GetNumMouseWheelEvents()) // If there are mouse wheel events queued { switch (GetMouseWheelEvent()) // Find out what the next event is { case MOUSE_WHEEL_UP: if (this.lastFocus.wheelScrollUp) return this.lastFocus.wheelScrollUp(); // If mouse wheel up and a function exists for that case MOUSE_WHEEL_DOWN: if (this.lastFocus.wheelScrollDown) return this.lastFocus.wheelScrollDown(); // If mouse wheel down and a function exists for that } } if (this.isButtonPressed() != -1)// && IsMouseButtonPressed(this.isButtonPressed())) { this.idle = 0; // Mouse button was pressed so report no-longer idle // If mouse button held and this is the second click do nothing. Cant see a need for ON DOUBLE HOLD function? if (this.held) // If the button is held but was already registered as held the nrun the on hold functions { this.handleButton(1); // If not it is the first instance of a click so perform ON HOLD function } else { // If this is a click and there wasnt a click within 150ms of this perform ON PRESS function as normal if (GetTime() > this.time + 150) this.handleButton(0); // Run on pressed else // If not then this click IS within 150ms of the last so perform ON DOUBLE function { if (this.lastButton == this.isButtonPressed() && this.focus == this.lastFocus) // If the mouse is over the same object and if the button that has been pressed is the same as before double click { this.handleButton(3); // Run on double this.doubleClick = true; // Confirm mouse was double clicked to prevent held and release events running } // If the button that has been pressed is not the same as before forget double click else this.handleButton(0); // Run on pressed } this.held = true; // Confirm mouse held this.locked = true // Lock into that control this.lastButton = this.isButtonPressed(); // Set what button was just pressed this.lastFocus = this.focus; // Set last focus to the current } } else { if (this.held) { // If button released and this is the second click just reset, cant see a need for a on double release function this.handleButton(2); // Run on release this.time = GetTime(); // Store time to find double click if (this.doubleClick) this.time -= 150; // If this was a double click, adjust the time to prevent ANOTHER double click this.heldLag = 0; // If released then reset the delay this.locked = false; // Unlock the mouse this.held = false; // Confirm mouse released this.doubleClick = false; // Reset confirmation of double click } } if (!mouse.locked) mouse.focus = mouseGlobal; return } //======================================================================================================================== // Reset //======================================================================================================================== _mouse.prototype.reset = function() { this.focus = mouseGlobal; // Reset all defaults this.locked = false; // Reset all defaults this.held = false; // Reset all defaults this.time = 0; // Reset all defaults this.doubleClick = undefined; // Reset all defaults } //======================================================================================================================== // Handle Button //======================================================================================================================== _mouse.prototype.handleButton = function(i) { var button = this.numberToButtonName(this.isButtonPressed()); // Get the required reference to the button if (this.focus) // If mouse focus is an appropriate control { switch(i) // Dependent on which mouse button is pressed { case 0: { if (this.focus.heldLag) this.heldLag = this.focus.heldLag; // If the focus object requests a delay between executing, take that delay if (this.focus[button + "OnPress"]) this.focus[button + "OnPress"](); // If on press return } case 1: { if (this.heldLag == 0) // If the minimum delay has passed between executions { if (this.focus.heldLag) this.heldLag = this.focus.heldLag; // If the focus object requests a delay between executing, take that delay if (this.focus[button + "OnHeld"]) this.focus[button + "OnHeld"](); // If on held } else this.heldLag--; // If there is a delay the count the delay down per itteration return } case 2: { if (this.focus.heldLag) this.heldLag = this.focus.heldLag; // If the focus object requests a delay between executing, take that delay if (this.focus[button + "OnRelease"]) this.focus[button + "OnRelease"](); // If on release return } case 3: // Handle if mouse is double clicked for all 3 buttons AND check if double click is applicable else run normal on pressed event { if (this.focus.heldLag) this.heldLag = this.focus.heldLag; // If the focus object requests a delay between executing, take that delay if (!this.focus[button + "OnDouble"] && this.focus[button + "OnPress"]) this.focus[button + "OnPress"](); // If there is no left double function run on press (if on press exists) else if (this.focus[button + "OnDouble"]) this.focus[button + "OnDouble"](); // Else if left on double exists run that return; // Return after running } } } } //======================================================================================================================== // Is Button Pressed //======================================================================================================================== // Will return the number of the button pressed, no button will return -1 // 0 = MOUSE_LEFT // 1 = MOUSE_MIDDLE // 2 = MOUSE_RIGHT _mouse.prototype.isButtonPressed = function() { if (IsMouseButtonPressed(MOUSE_LEFT)) return MOUSE_LEFT; // If the mouse button is pressed return 0 if (IsMouseButtonPressed(MOUSE_MIDDLE)) return MOUSE_MIDDLE; // If the mouse button is pressed return 1 if (IsMouseButtonPressed(MOUSE_RIGHT)) return MOUSE_RIGHT; // If the mouse button is pressed return 2 return -1; // If no button is pressed return -1 - this is because MOUSE_LEFT returns 0 } //======================================================================================================================== // Is Mouse Over //======================================================================================================================== // Will return if the mouse is over selected co-ords - if obj is passed to this function and the button is pressed then the mouse focus // will attach to that control // The way I use this would be to use isMouseOver from within the control and pass 'this' to it eg // menu.prototype.handleMouse = function() //{ // if (mouse.isMouseOver(this.x, this.y, this.w, this.h, this) // Will return true or false if the mouse is over it will set the mouse focus to 'this' also //} _mouse.prototype.isMouseOver = function(x, y, w, h, obj) { if (this.x >= x && this.x < x + w && this.y >= y && this.y < y + h) // Check if mouse is over x, y, w, h { if (obj && !this.locked) this.focus = obj; // If not already locked in change focus to the obj that was passed to it return true; // Return true as the mouse is over the co-ords } return false; // return false if its not over the co-ords } //======================================================================================================================== // Number to Button //======================================================================================================================== // Mouse buttons are returned as numbers in sphere so to this function will tell you what button relates to what number _mouse.prototype.numberToButton = function(number) // For testing purposes, probably have no use { if (number == 0) return "MOUSE_LEFT"; // To the mouse 0 is MOUSE_LEFT if (number == 1) return "MOUSE_MIDDLE"; // To the mouse 1 is MOUSE_MIDDLE if (number == 2) return "MOUSE_RIGHT"; // To the mouse 2 is MOUSE_RIGHT } //======================================================================================================================== // Number to Button //======================================================================================================================== // Mouse buttons are returned as numbers in sphere so to this function will tell you what button relates to what number and gives you a name to reference _mouse.prototype.numberToButtonName = function(number) // For testing purposes, probably have no use { if (number == 0) return "left"; // To the mouse 0 is MOUSE_LEFT if (number == 1) return "middle"; // To the mouse 1 is MOUSE_MIDDLE if (number == 2) return "right"; // To the mouse 2 is MOUSE_RIGHT } //======================================================================================================================== // Render //======================================================================================================================== _mouse.prototype.render = function() { this.image.blit(this.x, this.y); // Blit the mouse cursor }