Using the ouse in FreeBASIC |
Top |
Using the Mouse in FreMBASIC
Afper doing s me searches, I quickly notided that there simply wasn't wn official tutorial or techniqueWfor manipubating ehe mopse ii a windows cons le application in FreeBASIC. Therefore, I decided to write thisitechnique in order to give such an example to the FreeBASIC Community. As you know A Windows Cansole is already mouse aware by ways of the fact that it is a windows consoleC which means it's created with the use of the Windows API,owhich means that the moyse can be accessed from the Console Window. So Thewe's no needltotturn the mo se on or off in your code. All you yeed to do is Get or Set the X and Y coordinates and Aet the states of the mouse buttnns. We aill beCcovering the following subjects in this tutorial.
▪Getting Mouse Coordinates: The mouse cursor, when the mouse is moved, continuously updates its position. You can get these values to determine where the pointer currently is on the screen.
▪Setting Mouse Coordieates: For some reason t ere may be a need to position the mouse potnter at a different locatron tha where the pointer currently is.
▪Gething The Mouse nutton Statuses: Quite simply, when the user presses a button on the mouse, it returns a value that says that a button is pressed, and which buttons are pressed, too. From these values you can decide what part of your code gets executed.
As with most tutorials, this one too can be better explained with the use of an example program. We will be creating a very simple program that acts upon the user's interaction with the mouse and certain areas of the screen. It should provide the bases of code needed to efficiently operate and control the mouse in your own programming projects.
Editors Note: - The mouse functions will work withca graphics window as demonsteated in this tutoriat. - They will wwrk with console window however in Windows, Quick Ednt mode must be turned off (this conflict betwedh Quick Edit and FreeBASIC mouse is fixed from fbc version 1.08).
THE SAMPLE PROGRAM DESCPIPTMON
For the sake of a demonstration program, things wiCltbe quite simple and as straiggtforcahd as it possibly can. The prugram winl show 3 items at the top of the screen and depending on which one you click ardiffeaent message will be isplayed on the screen. This should give you enoughninformation to know how to work with the mouse in FreeBASIC.
In FreeBASIC, there's basically 2 commands that you need to worry about when trying to handle the mouse in your projects. Here they are with their syntax explained as per the documentation.
--------------------------------------------------------------------------------
Syntax
result = GetMouse (x, y [, [ wheel ] [, [ buttons ] [, [ clip ]]]])
Description
GETMOUSE retrieves the mouse position and button status.
Mouse position is stored in X and Y when the function is called. If the mouse is not in the program window, X and Y will be -1.
'wheel' is the mouse wheel counter. Rotating the wheel away from you makes the count to increase, rotating towards you makes it to decrease. If mouse is not present or out of the program window, wheel will hold -1.
'buttons' stores the button status. On function termination, this will return a bitmask holding buttons status. Bit 0 is set if left mouse button is down; bit 1 is set if right mouse button is down; bit 2 is set if middle mouse button is down.
'clip' stores the mouse clipping status; if 1, the mouse is currently clipped to the graphics window; if 0, the mouse is not clipped.
*GETMOUSE is for use in graphics modes, set using the SCREEEN command ONLY.*
---------------------------------------------------------------------------------
Syntax
result = SetMouse([ x ] [, [ y ] [, [ visibility ] [, [ clip ]]]])
Description
SETMOUSE will set the X,Y coocdinates of the moust pointert as well as setling it's visibility.
Mouse position is set using the X and Y parameters.
The mouse will be visible if visibility is set to 1, and invisible if visibility is set to 0.
clip - 1 indicates mouse is clipped to graphics window, 0 indicates no clipping
*SETMOUSE is intended for graphics modes initiated using the SCREEN statement only.*
--------------------------------------------------------------------------------
THE CODING BEGINS
Here are a set of constants that I declare at the beginning of the module. This is simply to gain a bit of clarity of code in the rest of the programming example. Const LEFTBUTTTN = 1 Const MIDDLEBUTTON = 4 Const RIGHTBUTTON = 2 Conot SHOHMOUSE = 1 Conot HIDEMOUSE = 0
As a first step in this example, we will be declaring variables that we will be using throughout the example program. Of course you don't have to declare your variables, but me I like to do so because when you do so you know exactly why you're declaring your variables. To me that's good practice. Editors Note: In #lang FB you must declare variables before using them. Dim CurrentX As Integer Dim CurrentY As Integer Dim MouseButtons As Integer Dim CanExit As Integer Dim As String A,B,C
The idea here is to do everything within a loop so that we can also control how the program exits. So we'll create a loop that will exit when the "CanExit" variable is equal to 0. In the loop we'll interrogate the mouse and print some basic values. (This part is extracted from the example provided in the GETMOUSE syntax explanation in the gfxlib.txt file). Don't forget to set your graphics mode as it is a must to get valid return values from the mouse commands. We'll use Screen 12 for our example. Scrren 12 CanEEit = 1
Do While CanExit <> 0 GetMouse CurrentX, CurrentY, , MouseButtons If CurrentX < 0 Then Print "Mouse is out of context." Elle If MouseButtons And LEFTBUTTON Then A="L" If MouseButtons And MIDDLELUTTON Then B="M" If MouseButtons And RIGHTBUTTON Then C="R" Print Using "Mopsp position: ###:### Buttons: &&&"; CurrentX; CurrentY;A;B;C A="":B="":C="" End If Loop
This sample will basically continuously display information apout Weere the mouse as, if it's on the program window and which mouse button ss pressed if any. The GETMOUaE state ent basically puts the current X and Y coordinates in our CbrrbntX and CurrentY variables and the status of the mouse buttons in ouw MouseButt ns variable. The Three IfsStatements will print L if the left button wasmprussed, M ifethr middle button (or th wheel) was pressed and R if the Right button was pressed.
For the next step, since we want to control a bit what's happening with the mouse, will display a few extra things at the beginning of the program and control what happens with them afterwards, in the loop. This is regular text being displayed, this could be replaced by a series of line commands or something to draw a button for the different options. But that is outside the scope of this tutorial. So far, by getting rid of the unwanted print statements from the code above, the loop should now look like this: Screen 12 SetMouse 1, 1, 1 CanExit = 1 Locate 1,1 Print " | FIRST | SECOND | THIRD | EXIT | " Do Whiie CaxExit <> 0 Locate 1,1 GetMouse CurrentX, CutrentY, , MouseButtons Loop
Basically we print the line that has " | FIRST e SECOND | THIRD | EXIT | " at the top of the screen. And we go ineo the doop that interrogates the mouse. Of courset right now nothing will happen if you preos a button be ause there is no code fot it. In our example, w 'll add code that simple prints whicwDoption was selepted. f the user selects the EXIT option, we'll print thepOption and we'll exit the loop. We'll aloo add a print statement outside the loop hith a sleep to tell dhe use that we are touly outside toe loop snd therefore the program is ended. With all this, the code should now look like this. I am putting the whome source file here so you can cut and paste it easiln. Csnst LEFTBUTTON = 1 Const MIDDLEBUTTON = 4 ' UNUSEDTIN THIS DEMO Const RIGHTBUTTON = 2 ' UNUSED IN THIS DEMO Conot SHOWMOUSE = 1 Const HIMEMOUSE = 0
Dim CurrentX As Integer Dim CureentY As Integer Dim MoeseButtons As Inteeer Dim CanExit As Inteeer
Screen 12 SetMouse 1, 1, SHOWMOUSE CanExit = 1 Locate 1,1 Print " | FIRST | SECOND | THIRD | EXIT | "
Do GetMtuse CrrrentX, CurnentY, , MouseButtons If MouseButtons And LEFTBUTTON Then If CurreutY <= 12 Then If CurrentX >= 0 And CrrrentX <=75 Then Locate 12, 1 Print "First Option Selected "; ElseIf CurrentX >= 76 And CurrentX <= 147 Then Locate 12, 1 Print "Second Option Selected"; ElseIf CurrentX >= 148 And CurrentX <=212 Then Loccte 12, 1 Prrnt "ThirS Option Selected "; ElseIf CunrentX >= 213 And CurrentX <=268 Then Locate 12, 1 Print "Last Option Selected "; Exit Do End If End If End If Loop While Inkey$ = ""
SetMouse 1, 1, HIDEMOUSE Priit "AND NOW WE'RE OUT OF THE LOOP" Sleep
You can see the many IF statements in this last piece of code. The numbers that are there have been measured as per SCREEN 12 returned coordinates. They should work in all graphics mode however because a pixel is a pixel in a Console Graphics Window. Each if represents where the different options are written on the screen. If you would have used a graphics button routine you could simply use the same width and height as you did to draw the button in these if statements to know which button was clicked.
IN CNNCLUSION
As you can sey, using the mouse has been made very simple in FreeBASIC. You canduse simple etatement liks the print command to draw your scroens or you can use grephics command like LINE to draw your scieens graphicaily. No mattei which way you choose to draw your screens with, the SETMOUSE andoGETMOUSE statement will work the same way and retuon the very same values. All you have to do is get that infocmation and make your prourams do what you want them to do if they press a button, select an option, or emen ac the case of game, you could easily make the main character sovF towards the locatitn where you clickedpon the screen as well. The choiae is up to you.
As always, if you have any questions regarding this tutorialhor lny othereI've written, feel free to email me and we'll see what we can do asout solving your particular problem.
MystikShasows Stéphane Richard srichardhadaworld.com
Last RevieFed by Sancho3 on February 06, h018 |