My try at an on-screen mouse cursor script

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
HeXaGoN
Posts: 161
Joined: Sat Dec 04, 2010 3:07 pm
Location: Texas, United States

My try at an on-screen mouse cursor script

Post by HeXaGoN »

This level here contains the ACS script below, and allows you to try my script out!
https://dl.dropboxusercontent.com/u/55657765/cursor.wad

You're absolutely free to use this in your mods, just give me credit. :)
The reason I made this was I've noticed that in so many mods, their on-screen mouse scripts feel awkward to use.
It's just because it deviates from what I'm used to in my operating system. So I felt a need to put an end to that!
It's impossible for mouse look being turned off to be accounted for though. You need mouse look on!

Edit: Added support for keyboard binds to control cursor.

I forget if I've shown anybody anything like this before.
If I've come late to the making of this, this has already been done, etc. etc. I apologize. :(
I can't really seem to find anything like this on the forums though.

Math involved:
Image
Δx is the offset of our cursor's x position, Δy is the offset of our cursor's y position.
θ₁ is our player's yaw input, θ₂ is our player's pitch input.
Δ₁ is our player's mouse turning speed (m_yaw), Δ₂ is our player's mouselook speed (m_pitch).
s is our player's overall mouse sensitivity.

ACS source taken directly from the level:

Code: Select all

#include "ZCommon.acs"

// This is an on-screen mouse script written by wtg62.
// I've written it so that it tries to match your operating system mouse sensitivity.
// So mess with your mouse settings as you like!
// No matter what you do, you won't experience awkward cursor movement in game!

// Mouse's coordinates
// Starting values don't matter that much, but I prefer it to start in the center of the screen.
int mx = 0.5;
int my = 0.5;

// Pixels to move every tic when using keyboard to move cursor.
int kbSpeed = 10.0;

// Cursor graphic
str cursorGraphic = "cursor";

// Hud size. The numbers are big because I want a small cursor graphic.
int HUDX = 960;
int HUDY = 600;

// This function will take any value from 0.0 to 1.0.
// It will then convert it to a number between 0 and the "scale" variable.
// It will return a fixed number.
function int fixedToPixel(int x, int scale)
{
    // Here, I multiply 'x' by our scale to get the appropriate number.
    // However, I must round down (basically truncate digits).
    // The best way to do this is to shift our bits to the right, and back to the left.
    // This effectively rounds down for us.
    return (FixedMul(x,scale<<16)>>16)<<16;
}

Script 1 Enter
{
    // Level set up, not an important part of the script at all.
    SetMusic("");
    SetPlayerProperty(0,1,PROP_TOTALLYFROZEN);
    ChangeCamera(1,0,0);
    
    // Set up
    SetHudSize(HUDX,HUDY,0); // I just use these numbers for a small cursor graphic.
    SetFont(cursorGraphic); // Optional, but looks better than using a character such as "+" or "."
    
    while(true)
    {
        // We need to support keybinds for keyboard look.
        // So let's check if we're using the keyboard or mouse, and then respond appropriately.
        if(GetPlayerInput(0,INPUT_BUTTONS) & (BT_LEFT|BT_RIGHT|BT_LOOKUP|BT_LOOKDOWN))
        {
            // Keyboard controlled cursor
            // Should be slow enough for user to be able to precisely hover over stuff.
            // 'kbSpeed' pixels per tic.
            // I use our previously set hud size to make movement speed on both axes look the same.
            if(GetPlayerInput(0,INPUT_BUTTONS) & BT_LEFT)       mx -= kbSpeed/HUDX;
            if(GetPlayerInput(0,INPUT_BUTTONS) & BT_RIGHT)      mx += kbSpeed/HUDX;
            if(GetPlayerInput(0,INPUT_BUTTONS) & BT_LOOKUP)     my -= kbSpeed/HUDY;
            if(GetPlayerInput(0,INPUT_BUTTONS) & BT_LOOKDOWN)   my += kbSpeed/HUDY;
        }
        else
        {
            // So here's our mouse controlled cursor.
            // Basically, these 2 variables below will take our player's yaw and pitch input and scale it.
            // Input is scaled so that our in-game "cursor" movement accurately matches operating system mouse movement.
            // 'pitchscale' is also basically multiplied by 2.
            // Why? Because pitch input is 2x slower than yaw input.
            int yawScale   = FixedDiv(2.5,FixedMul(GetCVar("m_yaw"),GetCVar("mouse_sensitivity")));
            int pitchScale = FixedDiv(5.0,FixedMul(GetCVar("m_pitch"),GetCVar("mouse_sensitivity")));
            
            // Now we get our delta x and delta y. 'Delta' means 'change in'. So 'change in x', and 'change in y'.
            // We'll use these to offset our current mouse position.
            int dmx = -FixedMul(GetPlayerInput(0,INPUT_YAW),yawScale);
            int dmy = -FixedMul(GetPlayerInput(0,INPUT_PITCH),pitchScale);
            
            // Apply our offsets.
            mx += dmx;
            my += dmy;
        }
        
        // Clamp our mouse position, so our mouse doesn't go offscreen and into the abyss forever.
        if(mx < 0)      mx = 0;
        if(my < 0)      my = 0;
        if(mx > 0.999)  mx = 0.999;
        if(my > 0.999)  my = 0.999;
        
        // Time to display our cursor!
        // I have to remember that we used the SetHudSize function.
        // I can't use a value from 0.0 to 1.0 for our display coordinates.
        // I have to use a value from 0.0 to 960.0 for x, and 0.0 to 600.0 for y.
        // So that's why I have my fixedToPixel function defined above!
        // I also have to add 0.1 to each of our coordinates.
        // This signals to ZDoom I want the upper left corner of my image to be at the integer part of the coordinates.
        HudMessage(s:"a"; HUDMSG_PLAIN, 1, CR_UNTRANSLATED, fixedToPixel(mx,HUDX)+0.1, fixedToPixel(my,HUDY)+0.1, 0);
        Delay(1);
    }
}
Hope it's useful! Modify as needed!
Last edited by HeXaGoN on Mon Jun 08, 2015 7:51 pm, edited 9 times in total.
User avatar
Nash
 
 
Posts: 17501
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: My try at an on-screen mouse cursor script

Post by Nash »

Cool stuff, commenting to check it out when I go back home to my country.
Spoiler:
User avatar
Isle
Posts: 687
Joined: Fri Nov 21, 2003 1:30 am
Location: Arizona, USA

Re: My try at an on-screen mouse cursor script

Post by Isle »

looks good but I'm not sure if it accounts for inverted mouse setups.
User avatar
Apothem
Posts: 2070
Joined: Sat Nov 29, 2003 7:13 pm
Location: Performing open heart surgery on an ACS compiler.

Re: My try at an on-screen mouse cursor script

Post by Apothem »

Isle wrote:looks good but I'm not sure if it accounts for inverted mouse setups.
I dont think anything ever will without some new commands being implemented in zdoom.

EDIT:
Gave the wad a try, looks great! Really smooth and precise I've noticed! Very impressive work!

I hope you dont mind if I adapt some of this for one of my mods? :D
HeXaGoN
Posts: 161
Joined: Sat Dec 04, 2010 3:07 pm
Location: Texas, United States

Re: My try at an on-screen mouse cursor script

Post by HeXaGoN »

Of course you can use it, anybody is free to use it as long as they give credit! :)
I'm pretty sure "inverted mouse" is a CVar so it wouldn't take me more than a couple of seconds to add a check for that.

In fact, I just did! Thanks for pointing that out.
User avatar
kodi
 
 
Posts: 1361
Joined: Mon May 06, 2013 8:02 am

Re: My try at an on-screen mouse cursor script

Post by kodi »

Great job - a lot that it is much cleaner than stupid way I came up with :P . I'll borrow this too with due credit (if I ever manage to finish a release lol)
User avatar
phantombeta
Posts: 2180
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: My try at an on-screen mouse cursor script

Post by phantombeta »

wtg62 wrote:In fact, I just did!
You shouldn't do that. The invert mouse CVar is NOT sent to other players, it only inverts the input you send! This will desync demos and multiplayer completely.
HeXaGoN
Posts: 161
Joined: Sat Dec 04, 2010 3:07 pm
Location: Texas, United States

Re: My try at an on-screen mouse cursor script

Post by HeXaGoN »

Yikes, that's weird. IMO it should be sent to other players. I'll have to not do that then.
Edit: Fixed it. Sorry to anybody who uses an inverted mouse!
Last edited by HeXaGoN on Mon Jun 08, 2015 7:54 pm, edited 2 times in total.
User avatar
jdredalert
Posts: 1681
Joined: Sat Jul 13, 2013 10:13 pm
Contact:

Re: My try at an on-screen mouse cursor script

Post by jdredalert »

Nice and smooth pointer, well done!
User avatar
phantombeta
Posts: 2180
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: My try at an on-screen mouse cursor script

Post by phantombeta »

wtg62 wrote:Yikes, that's weird. IMO it should be sent to other players. I'll have to not do that then.
It shouldn't. Why send data that will be useless to other players?
This is a thing that has no reason at all to be done at every client. It's just a waste of cycles. It makes MUCH more sense to just invert the input before sending it if you have the CVAR on, which is what ZDoom does.
User avatar
edward850
Posts: 5890
Joined: Tue Jul 19, 2005 9:06 pm
Location: New Zealand
Contact:

Re: My try at an on-screen mouse cursor script

Post by edward850 »

Does somone need to bring up the "Pitch and Yaw != mouse input" issue, also? This is a very defeatist "mouse cursor" you have, here.
phantombeta wrote:It makes MUCH more sense to just invert the input before sending it if you have the CVAR on, which is what ZDoom does.
That's actually exactly why it's not attached to the buffer. It's part of the interface, and could cause all manner of problems if it was controlled by the playsim. Imagine if we attached rendering values to it?
Nash wrote:inb4 Edward850 in 3... 2... 1... :mrgreen: :mrgreen: :mrgreen:
It's as if you want my attention. :P
HeXaGoN
Posts: 161
Joined: Sat Dec 04, 2010 3:07 pm
Location: Texas, United States

Re: My try at an on-screen mouse cursor script

Post by HeXaGoN »

Yeah, I guess it was silly of me to think that way. I can see why it's better as a client side thing.
I guess this is only good for singleplayer...
User avatar
edward850
Posts: 5890
Joined: Tue Jul 19, 2005 9:06 pm
Location: New Zealand
Contact:

Re: My try at an on-screen mouse cursor script

Post by edward850 »

Is it even then? How can you tell if someone is using a mouse? What if they are playing using an oculus rift? Or some 6 axis controller? You cannot possibly know or predict if this system is even viable for the input a player is using, and the concept of input is a very dynamic one.
HeXaGoN
Posts: 161
Joined: Sat Dec 04, 2010 3:07 pm
Location: Texas, United States

Re: My try at an on-screen mouse cursor script

Post by HeXaGoN »

That's a very good point you make.

So I guess all that can be said is that this script isn't going to work very well for much,
It would take some certain new features in ZDoom to make something like this that functions 100%.
User avatar
solarsnowfall
Posts: 1581
Joined: Thu Jun 30, 2005 1:44 am

Re: My try at an on-screen mouse cursor script

Post by solarsnowfall »

It's certainly been done before by querying the player's angle and pitch directly. We didn't always have GetPlayerInput(), you know. This approach gets you around the issue raised by edward850, however, is not without it's own set of drawbacks. It's a very thoroughly thought out and well documented piece of scripting otherwise. Nice work.
Locked

Return to “Editing (Archive)”