PDA

View Full Version : How to program the mouse functions



TBProgramer52
15-09-2011, 20:15
This post is for everyone interested, I was wondering how to program the mouse function in all the demo that is installed along with the ThinBasic? Meant all the sample programs within ThinBasic. Just wondering though! To Petr, nice work on the demos you have created! I really enjoyed them alot!

Petr Schreiber
16-09-2011, 14:12
Hi TBProgramer52,

thanks for the kind words! :)

I am not sure I understand your question fully - there are multiple levels of functionality for handling mouse input in ThinBASIC.

When working with UI module:

Win_GetCursorPos allows retrieving position of mouse in window
You can catch %WM_MOUSEMOVE, %WM_LBUTTONDOWN, %WM_LBUTTONUP, %WM_RBUTTONDOWN, %WM_RBUTTONUP and other events allow detecting mouse activity

When working with TBGL module, you can pick some alternative functions as well:

TBGL_MouseGetPosX, TBGL_MouseGetPosY allow retrieving mouse position
TBGL_MouseGetLButton, TBGL_MouseGetMButton, TBGL_MouseGetRButton return non zero value in case the apropriate mouse button is pressed
(TBGL_GetWindowKeyOnce(hWnd, %VK_LBUTTON) and others can be used for similar purpose)
TBGL_MouseGetWheelDelta returns mousewheel delta
TBGL_GetPixelInfo(TBGL_MouseGetPosX, TBGL_MouseGetPosY, ... ) allows picking 3D coordinate or color from screen


Petr

TBProgramer52
16-09-2011, 23:16
Hello Petr,
What I meant to say is to replace the keyboard functions with in the code, to actually transform from keyboard into a mouse format. To actually control the object with a mouse instead of keyboard (arrows keys). Understand now Petr. I'm sorry I didn't make myself clear on this subject. Or maybe demo on how to use the mouse within ThinBasic.

from TBProgramer52

zak
17-09-2011, 09:11
here are some examples i have found about using the mouse
some examples in the thinbasic distribution
1-DragnDropDemo.tbasic in "C:\thinBasic\SampleScripts\UI\Canvas":
you can move the circle by left mouse button or Right mouse button or the middle button.
2-TBGL_2D_Sprites_CheckMouse.tbasic in "C:\thinBasic\SampleScripts\TBGL\Sprites":
you can rotate the sprite to the right by clicking left mouse button, and to the right by right click
3-TBGL_Demo19_MouseDemo.tbasic in "C:\thinBasic\SampleScripts\TBGL\Basic":
you can move the balls with mouse.
4- the following example "demo_of_0192.TBASIC" in the TBGL Bonus Pack Version: 1.6.0.10 downloaded from here http://www.thinbasic.com/index.php?option=com_jdownloads&Itemid=95&task=viewcategory&catid=5 IN FOLDER "TBGL_BonusPack_1_6_0_10\GUIandText\Selection" :
you can select a ball and place a toy over it.
7500
5- FunctionDesigner3D.tbasic‎ by Petr in this thread http://www.thinbasic.com/community/showthread.php?10887-A-challenge-porting-from-FreeBasic-(and-originally-from-C) you can rotate the figure by mouse in any direction.
7501
6- the next example (the original code by sblank ), move the mouse in any direction and the figure will move according to that.

Petr Schreiber
17-09-2011, 09:25
Hello,

here is one simple example on how to replace arrow keys movement with mouse movement:

left mouse button = forward
right mouse button = backward
moving mouse left/right = turning left/right




'---Load needed modules
Uses "TBGL"

Begin Const

' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eGrid

End Const

Function TBMain()
Dim hWnd As DWord

'---Creates OpenGL window and returns handle
hWnd = TBGL_CreateWindowEx("Moving using mouse - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED )
'---Shows TBGL window
TBGL_ShowWindow

' -- First we will create world as place to contain entities
TBGL_SceneCreate(%sScene)

' -- Our world will have two entities

' .. Camera
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos (%sScene, %eCamera, 0.0, 1.7, 0.0 )

' .. Grid
TBGL_EntityCreateDLSlot(%sScene, %eGrid, 0, CreateGridAsDList() )

Dim FrameRate As Double
Dim Sheeps As Long

Dim mouseCenterX, mouseCenterY As Long

'---Resets key status before checking
TBGL_ResetKeyState()

'---Main script loop
While TBGL_IsWindow(hWnd)

'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movements relative to frame rate
FrameRate = TBGL_GetFrameRate

' -- Center cursors and retrieve its position
TBGL_CenterCursor
mouseCenterX = TBGL_MouseGetPosX
mouseCenterY = TBGL_MouseGetPosY

Sleep 10

'---Prepares clear frame
TBGL_ClearFrame

TBGL_SceneRender(%sScene)

TBGL_DrawFrame ' Swaps the buffers - displays rendered image

' -- If mouse pressed, push the camera
If TBGL_MouseGetLButton Then TBGL_EntityPush(%sScene, %eCamera, 0, 0, 5/FrameRate) ' 5 meters/second
If TBGL_MouseGetRButton Then TBGL_EntityPush(%sScene, %eCamera, 0, 0, -5/FrameRate)

' -- If mouse is not in center anymore, turn
TBGL_EntityTurn(%sScene, %eCamera, 0, -5*(TBGL_MouseGetPosX - mouseCenterX)/FrameRate, 0)

If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) Then Exit While

Wend

'---Closes OpenGL window
TBGL_DestroyWindow
End Function

Function CreateGridAsDList() As Long ' Returns to which display list we save
local i, j as long

tbgl_NewList 1
'---Let's build a grid
TBGL_BeginPoly %GL_LINES ' Starts polygon definition based on 2 vertex lines
TBGL_Color 0,255,0 ' Defines color

For i = -20 To 20
For j = -20 To 20
TBGL_Vertex -20, 0, j ' Adds vertex
TBGL_Vertex 20, 0, j ' Adds vertex

TBGL_Vertex i, 0, -20 ' Adds vertex
TBGL_Vertex i, 0, 20 ' Adds vertex

Next
Next

TBGL_EndPoly
tbgl_EndList

function = 1
End Function


Petr

TBProgramer52
09-03-2014, 06:22
Thanks Petr,

Thank for sharing that code, it helps a lot! Now I can use this code in all the examples, that would help me to practice to code correctly.

From,
TBProgramer52
(Richard L. West)

RobbeK
10-03-2014, 21:03
Hi Richard,

I tried something like ;.. (attached).

The mouse coordinates always are screen coordinates and not related with any window or any other object.



best Rob