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.

Dark GDK / Upgraded Mouse class

Author
Message
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jun 2008 23:10 Edited at: 13th Jun 2008 23:12
For those of you interested, I've just made a change in my Mouse class. I've made it a singleton so that only one instance of it exists but you can either create a reference to that one instance or a pointer to it in any scope. When you declare a Mouse object you declare it as a pointer or a reference and use the static Instance function to initialize it. Either of the two methods below work

Mouse *myMouse = Mouse::Instance();
Mouse &myMouse = *Mouse::Instance();

I prefer the reference type myself because the pointer variable requires remembering to use the -> operator.

What happens is that the first call to Instance() creates a new instance of the Mouse object. Thereafter, any call to Mouse::Instance() returns the address of the first instance.

The code only has minor changes from my first release of it. The code follows:

Mouse.h



Mouse.cpp



Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 17th Jun 2008 09:22
Lilith
I reviewed your code and understand how it all works but what advantage do you gain over using this over the standard db commands?

All the best

Codger

System
MacBook Pro
Windows XP Home on Boot Camp
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Jun 2008 15:22
The primary advantage is that it makes it easier to detect transitional states. The db commands will tell you if a button is up or down but it won't tell you if it was just clicked or just released. With the db commands you can't assume that just because button 1 is down that it was just clicked. The next time you loop and check it again, if it's still down, you have the same condition and your code reacts the same way.

With my Mouse class, you call the Sync function near the beginning of of your loop and if any of the states changes it notes it so you'll know if the left mouse button was just clicked.

Making it a singleton allows you to create local references in deeper scopes that still have access to the data that was last Sync()'d.

I have some documentation that I should have included with it. Let me see if I can dig it up.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 17th Jun 2008 20:31
That's awesome, Lilith. What was the hex code for? I'm too stupid to understand it.

If you can do any models for FW, reply to the FleetWars thread.

Click here!
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 17th Jun 2008 20:40


I think you should change that to this:



And add two new values, xOffset and yOffset.



Probably not the best way to do it, but at least it won't snap to the mouse position, it'll go from where you click. I hope.

If you can do any models for FW, reply to the FleetWars thread.

Click here!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Jun 2008 20:59
Quote: "What was the hex code for?"


The hex code isn't really necessary but it sometimes helps to clarify the intent of the number being used. When you ask for the status of the buttons Dark GDK returns that status as a single integer but the bit positions in the low end of the integer are used to indicate which buttons are currently down. I'll use 8 bits for illustration purposes here.

Look at the value returned by the request to get the button status as a series of bits (actually 32 of them but...)

00000000

The LSB (least significant bit) is used to indicate that button #1 is down if that bit is set to 1. If button #2 is down it would indicate so by setting the second LSB to 1 and so forth for button #3 (third LSB) and button #4 (fourth LSB.) If more than one button is down you'll see bit indicators in each off the appropriate position. So if button #1 and button #3 were both pressed you'd get back 00000101 as a pattern. Buttons #1 and #4 both pressed comes back as 00001001.

In order to tell if any one button is pressed you logically and the result with a value equal to the bit position. If you get a non-zero result it means you've got a button down. You can use decimal values but I prefer hex because it's easier for me to visualize if the bit patterns become more complex.

00000001 0x01
00000010 0x02
00000100 0x04
00001000 0x08

If I need to test for buttons #2 and 3 down I can either add or logically or them together before testing against the current status.

int result = dbMouseClick() && (0x02 || 0x04);

If I were actually ambitious I'd do something clever in my headers like define the mouse buttons in text.

#define BUTTON2 0x02

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 17th Jun 2008 21:00
Quote: "I think you should change that to this:"


I'll take a look in a bit and see what happens.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 18th Jun 2008 19:20
Because I promised a rehash of my explanation of the Mouse class, here it is.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Attachments

Login to view attachments
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 18th Jun 2008 22:02
Okay, here's how the code I posted works differently from your code.


Say you have a little smiley. It's offset is 0,0.

You click on its lower right corner with your code.
The upper left corner snaps to where the mouse is.

With mine, it doesn't snap to the mouse, it doesn't move until you move the mouse.

If you can do any models for FW, reply to the FleetWars thread.

Click here!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 18th Jun 2008 22:24
Gotcha. And I was aware of the problem involved there. It was on my list of things to do but for the moment it worked for what I needed. I'll implement your modifications and see if I can't work them into my release of Mouse 3.0.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-09-29 23:18:52
Your offset time is: 2024-09-29 23:18:52