Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Mouse Plugin - Function collection for better mouse-control

Author
Message
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 10th Jun 2007 01:06 Edited at: 10th Jun 2007 02:18
Hi,

I've made this functions today, because I was a bit bored... this function collection, called "mouse plugin" (short "mop", also the mainly used prefix at the beginning of all used functions, arrays, types and variables), can handle different mouse-operations, and offers different features for your programs.
It's easy to implement, you just have to include the code and then you can use the functions.
At the beginning you should call "StartMop()" to setup all variables etc.

Features:
-Management of Mouse-Position and -Movement
-Click-Management for right mousebutton, left button and the wheel (Key pressed, click started, click stopped, editable doubleclick (time-delay between two clicks, both clicks on the same position etc.))
-Field/Zone-Functions (Create different Trigger-Zones and easily check if they are hovered or clicked)
-Mouse can be locked at a defined position (usefull for shooters etc)
-Simple Drag-and-Drop-System

Anyone can use the code, if he wants, and edit it, and can name me in the credits - if he wants.. doesn't really matter, I'd just be happy if I could help someone with it...

The functions:





My actual example-code is really bad written, but while I'm writing on a new, better one I'll post this here:



The code shows the main-features. For example, you can drag and drop the two buttons, using the right/top mousebutton.
Also much information about the mouse is showed on the screen.
I know, that most of the features are easy to do in dark basic, but for example the doubleclick-management is something I often wished to have, and didn't really know how to do it. (I think it's also possible to do it over windows DLLs.. but I'm not sure)

So I'm going to write another example-code now.

I'd apreciate criticism, questions, feature-ideas etc., thanks.

Mr Kohlenstoff


Edit:

I made a new demo-program, the code is better structured, and most features are used again.

Here the code:


And, finally, an image of this demo.



Have fun!

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 10th Jun 2007 02:16
I've created a Keywords-file for the functions.

If anyone uses them, he should create a file "MousePluginKeywords.ini" or something in "Editor\Keywords\" and paste this lines into it:

Quote: "StartMop=MousePlugin_Help.html=(*No Parameters*)
mop_SetDoubleClickDelay=MousePlugin_Help.html=(time)
mop_SetDoubleClickSamePos=MousePlugin_Help.html=(SamePos as boolean)
mop_Update=MousePlugin_Help.html=(*No Parameters*)
mop_PosX=MousePlugin_Help.html=(*No Parameters*)
mop_PosY=MousePlugin_Help.html=(*No Parameters*)
mop_MoveX=MousePlugin_Help.html=(*No Parameters*)
mop_MoveY=MousePlugin_Help.html=(*No Parameters*)
mop_MoveZ=MousePlugin_Help.html=(*No Parameters*)
mop_Click=MousePlugin_Help.html=(Button)
mop_NewClick=MousePlugin_Help.html=(Button)
mop_DoubleClick=MousePlugin_Help.html=(Button)
mop_InField=MousePlugin_Help.html=(x1,y1,x2,y2)
mop_CreateZone=MousePlugin_Help.html=(x1,y1,w,h)
mop_GetZoneX=MousePlugin_Help.html=(ID)
mop_GetZoneY=MousePlugin_Help.html=(ID)
mop_GetZoneX2=MousePlugin_Help.html=(ID)
mop_GetZoneY2=MousePlugin_Help.html=(ID)
mop_GetZoneWidth=MousePlugin_Help.html=(ID)
mop_GetZoneHeight=MousePlugin_Help.html=(ID)
mop_PositionZone=MousePlugin_Help.html=(ID,X,Y)
mop_RelPositionZone=MousePlugin_Help.html=(ID,xadd,yadd)
mop_InZone=MousePlugin_Help.html=(ID)
mop_ShowZone=MousePlugin_Help.html=(ID,t$,Filled as boolean)
mop_SetZoneColors=MousePlugin_Help.html=(Txt as dword, Fill as dword, Border as dword)
mop_LockMouse=MousePlugin_Help.html=(x,y)
mop_UnlockMouse=MousePlugin_Help.html=(*No Parameters*)
mop_GetLocked=MousePlugin_Help.html=(*No Parameters*)
mop_GetLockX=MousePlugin_Help.html=(*No Parameters*)
mop_GetLockY=MousePlugin_Help.html=(*No Parameters*)
mop_GetDoubleClickSamePos=MousePlugin_Help.html=(*No Parameters*)
mop_GetDoubleClickDelay=MousePlugin_Help.html=(*No Parameters*)
mop_Drag=MousePlugin_Help.html=(*No Parameters*)
mop_DragObject=MousePlugin_Help.html=(ObjX,ObjY)
mop_GetDraggedObjectX=MousePlugin_Help.html=(*No Parameters*)
mop_GetDraggedObjectY=MousePlugin_Help.html=(*No Parameters*)
mop_Drop=MousePlugin_Help.html=(*No Parameters*)
mop_GetDragged=MousePlugin_Help.html=(*No Parameters*)
mop_GetDragX=MousePlugin_Help.html=(*No Parameters*)
mop_GetDragY=MousePlugin_Help.html=(*No Parameters*)
mop_GetDropXDif=MousePlugin_Help.html=(*No Parameters*)
mop_GetDropYDif=MousePlugin_Help.html=(*No Parameters*)
"


I think I'm also going to create a small help file, including an overview about all the functions in one table.

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
vorconan
18
Years of Service
User Offline
Joined: 4th Nov 2006
Location: Wales
Posted: 10th Jun 2007 08:36
Looks really really good, thanks, I'll need these.
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 11th Jun 2007 18:13
OK, thanks for the comment.. any ideas for new features?

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
vorconan
18
Years of Service
User Offline
Joined: 4th Nov 2006
Location: Wales
Posted: 13th Jun 2007 23:57
I think it's fairly well built at the moment, maybe some sort of rotate command to keep you going? Just an idea, might come in useful
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 14th Jun 2007 00:48
I like what you've done from examining the code. The code is clean enough to read and seems fairly easy to understand - which is a credit to you and in no way takes away from the work that you put into it.

In Short: Good Job!

Why I popped into your thread is because I'm writing my own input routines, and I was looking for a programmable user input source - so you can program a game to map almost ANY CONTROL to ALMOST any game action - JoyX, MouseY, Left Arrow Key etc.

I have a decent start, and seeing how your code is in here - I presume its ok to nag it and do what I want with it right?

I'm thinkning I already have MANY of your global variables defined already - for things like MouseX, Y etc. By merging the two "systems" to share variables, etc. It could become QUITE powerful as a user input solution.

One of the biggest drawbacks I found in User Input in DarkBasic - is that in order to get SMOOTH KEYBOARD (often functioning smooth multi-key press stuff also ..cuz a$=inkey$() doesn't cut it for many gample situations...i digress) ... In order to get smoother Keyboard input you need to use SCANCODE and KEYSTATE commands. Point is that to scan the WHOLE keyboard (101 KeyS) its SLOW - So part of my Whole Mapping thing is to allow you to set up say 10 actions that you are looking for to keep the overhead low as possible - by limiting the "scan for input" routine to only scan for what you need/expect. Note that you can scan for as much as you want also... like the part where you actually ask the user to enter the key, mouse, or joystick you want to control Shooting.

well, good day.

Know way to many languages - Master of none
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 14th Jun 2007 14:36 Edited at: 14th Jun 2007 14:45
@vorcanon: What exactly do you mean with rotation.. I'm not sure what I could rotate there.

@jason: You can, of course, do with my code what you want, and include it in your project if you want.
Do you check the keystates everytime an update-function is called? Then, of course, it would be slow, but you could just call the keystate-function if the code uses your, for example, "CapslockKeyPressed"-Function.. as far as I understood this could maybe solve the performance-problem, but I'm not sure. ^^

Thanks for the comments so far. I've just seen another thread (I think in the DBP-Board) where I read that it's possible to change the function calls with constants, so that you can e.g. write "mop_SetDoubleClickDelay 200" instead of using this brackets...
(Was this thread btw: http://forum.thegamecreators.com/?m=forum_view&t=108125&b=1&p=0 )
So I think I'm going to include some of this constants now, and then maybe create a small overview-helpfile.

Edit: I testet using the constant function-names, but theres a problem when you call other functions in the parameters, so for example "mop_PositionZone mop_PosX(), mop_PosY()" causes an error... so I think I won't include this constants and let it as it is.

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 14th Jun 2007 18:48
Hello Mr Kohlenstoff!

You understand just find. Scanning the whole keyboard for all possible scancodes is slow.

So, to answer your question (sort of) "(Jason,) Do you check the keystates everytime an update-function is called?"

I did once. It was to slow. So I did some research and modified things a bit.

First of all - my goal is to make one "Get User Input" function that handles EVERYTHING I need handled in whatever MAIN LOOP I'm using to run a game or menu etc. Tall Order? Yeah - but I think I nailed it!

I managed to create a small group of functions that REALLY are great for managing user input of any sort, the ability to map any user input to any "action" and this "action" separation from the ACTUAL user input makes it a PERFECT system for being able to write code that can be shared by the game player and other game characters - which implies its a cool way to control AI and game plyer in similiar fashion!

This has come together nicely and its WAY easier than it sounds (I SWEAR!) Now all I need to do is merge your mouse "helper" functions with the infrastructure I've built. I am VERY excited about this.

The key to the whole speed thing of checking all keyboard codes etc. is to obviously limit the number of "checks" to only those you absolutely need.

Searching for 5 to 10 keys is MUCH faster than searching for 101 when in the context of a game play main loop.

I implemented dynamic arrays to allow adding as little or as many user input things you want to check for.

I found code similiar to what I was doing on a lesser scale that was very helpful. (By "lesser" I only mean that mine includes keyboard, joystick, mouse and control devices and the example I'm referring to only did the keys and did not have the "User Input" to "Action" mapping I'm referring to above.

(this is the one "Keystate Monitor" : http://forum.thegamecreators.com/?m=forum_view&t=107531&b=6)

So in short: You add the stuff you want to scan/poll albeit keyboard keys, mousex, mousey, mousez, mousemovedx, joystick x, joystick button 1, etc. and you just call GetUserInput() and it handles it for you - but in a simplified manner compared to the above referenced KeyState Monitor which has KeyEnabled/KeyDelay, KeyIsAToggleSwitch, etc.

I took this KeyEnabled/KeyDelay, KeyIsAToggleSwitch, functionality and implemented it in my "Action" area. So, how it all works it you FIRST add ACTIONS to your game:
Shoot Gun, Shoot Laser, Forward, Backward, "Sprint/Run", etc. Each Action You create gets a #.




You THEN add things for the "GetUserInput()" to poll using " syntax like this:



Then in your main loop you check the actions you are immediately
concerned with - like things the user can do - such as Shoot gun.

You simply check if garAction(iShootGun).bActionRequested=TRUE

What is nice is that bActionRequested is only TRUE IF all conditions met, in this case, when a bullet has not been fired or the half second (500 milli sec) delay has expired... etc.

Then its your responsiblity to clear this flag because you MIGHT not want to clear this flag if its a toggle action - depend on implementation - but so far the code is looking good and I'm willing to share it after I implement your mouse functions and do some more testing and do any optimizations that are necessary to make it easy to use and fast.

So I'm mid way done accomplishing the following:

1ne User Input routine for all devices that is fast
2:Ability to manage game actions for both player and AI
3:Ability to map any user input control to any game action (think of those config screens in decent games that let you pick what keys do what and if you want to use joystick or mouse etc)

For those of you who might be reading this and are not programming gurus - which I'm NOT claiming to be by the way - one of the main programming matras is to separate "functionality" from "User Interface" and in a game this seems like it could not possibly apply but in many ways these routines accomplish this to some degree and to a larger degree they put a line in the sand between "what the game does and the player can do in the game" and "how the user actually interacts with the game".

Know way to many languages - Master of none
The ARRAYinator
19
Years of Service
User Offline
Joined: 13th Aug 2005
Location:
Posted: 18th Jun 2007 22:59
Very Nice! Im pretty sure I can find a use for this in gamescape as there's a ton of mouse stuff left to be dealt with. Thanks!

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 19th Jun 2007 15:29
@Jason: Sounds really interesting, I think now I understand what your functions are made for.. Looking forward to see my mouse-functions cooperating with it.

@Arrayinator: Thanks for the comment.. no problem. Also nice to see the functions in such a big, promising project.

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
The ARRAYinator
19
Years of Service
User Offline
Joined: 13th Aug 2005
Location:
Posted: 19th Jun 2007 20:22 Edited at: 19th Jun 2007 20:23
thanks alot MR. K The functions are actually quite helpful and have been used in my latest editon to Gamscape which is a true 3d mouse where you basically can drag the mouse across the terrains and raise points from any angle which makes the workflow 1000 times better. Im going to post a little video demonstration later for the heck of it so be sure to check it out. These will definetly be useful functions to have around thanks!

Login to post a reply

Server time is: 2024-11-22 19:09:18
Your offset time is: 2024-11-22 19:09:18