Prevent MouseUp when click starts outside of object - events

I have Sprite object that is a poker chip. When the user clicks it the bid increase. I am increasing the bid on the MouseUp event. But if the user starts a click outside of the object/chip and then drags the mouse over the chip and does MouseUp, it fires the function mapped to MouseUp. How can I prevent it from firing if the click starts outside of the object.
this.addEventListener(MouseEvent.MOUSE_DOWN, pressChipDown)
this.addEventListener(MouseEvent.MOUSE_UP, pressChipUp)
this.addEventListener(MouseEvent.MOUSE_OUT, pressChipOut)
private function pressChipDown(e:MouseEvent):void
{
super.x = startX + 5;
super.y = startY + 5;
}
private function pressChipUp(e:MouseEvent):void
{
increaseBid();
super.x = startX;
super.y = startY;
}
private function pressChipOut(e:MouseEvent):void
{
if(e.buttonDown) //if mouse button is down, move chip back but dont increase bid
{
pressChipUp(MouseEvent.MOUSE_OUT as MouseEvent)
}
}
Thanks

On roll over, start listening to mouse down and mouse up, if up before down, it's a "fake"
this.addeventlistener(MouseEvent.ROLL_OVER, onOver)
...

Related

Stop rotating for a click right on the game object

I have a unity game and in it, a rotating game object, which increases its speed when it is clicked.
My problem is that the game does not work as I want it to. Right now, if I click any part of the screen, it increases the game object's speed of rotation. On the other hand, if I keep my finger on the screen, the game object starts to slow down and then starts rotating in the opposite direction.
I want the rotation of the object to increase when I click on it, not just if I click on any part of the screen. Furthermore, I don't know why holding down reverses the direction of rotation.
var speed = 1;
var click = 0;
Screen.orientation = ScreenOrientation.LandscapeLeft;
function Update (){
{
transform.Rotate(0,0,speed);
}
if(Input.GetMouseButton(0))
{
if(speed != 0)
{
speed = 0;
} else {
click++;
speed = click;
}
You must use Input.GetMouseButtonUp or Input.GetMouseButtonDown, NOT A Input.GetMouseButton, this method used for clamping.
Try use this code:
var speed = 1;
var click = 0;
Screen.orientation = ScreenOrientation.LandscapeLeft;
function Update (){
{
transform.Rotate(0,0,speed);
}
if(Input.GetMouseButtonDown(0))
{
if(speed != 0)
{
speed = 0;
} else {
click++;
speed = click;
}
A few issues here:
Firstly:
To increase speed upon clicking on the object only, use raycasting from camera, and check if it hits your object. Your object needs need a collider component on it for this to work.
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Transform objectHit = hit.transform;
objectHit.Rotate(0,0,speed);
}
Refer to https://docs.unity3d.com/Manual/CameraRays.html -> Raycasting section, for more information.
Secondly:
Input.GetMouseButtonDown(..) // returns true at the instance your mouse button transits from up to down
Input.GetMouseButton(..) // returns true as long as your mouse button is held down
Use Input.GetMouseButtonDown(..) in your Update() method if you want to to do something when you click on it.

How to enable or disable an event hander in MFC?

I have searched the web a lot but couldn't find exactly what I want!
suppose that I have a class derived from CWnd. In fact, it is the class COpenGLControl here in codeguru customized by me for my own purposes.
the event handler for the WM_MOUSEMOVE button is written as follows:
void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
{
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;
// Left mouse button
if (nFlags & MK_LBUTTON)
{
m_fRotX += (float)0.5f * diffY;
if ((m_fRotX > 360.0f) || (m_fRotX < -360.0f))
{
m_fRotX = 0.0f;
}
m_fRotY += (float)0.5f * diffX;
if ((m_fRotY > 360.0f) || (m_fRotY < -360.0f))
{
m_fRotY = 0.0f;
}
}
// Right mouse button
else if (nFlags & MK_RBUTTON)
{
m_fZoom -= (float)0.1f * diffY;
}
// Middle mouse button
else if (nFlags & MK_MBUTTON)
{
m_fPosX += (float)0.05f * diffX;
m_fPosY -= (float)0.05f * diffY;
}
OnDraw(NULL);
CWnd::OnMouseMove(nFlags, point);
}
But I don't want this event handler to be active or enabled at all times. I want to put three buttons on my dialog named pan,rotate and zoom.
when I click pan I want OnMouseMove get active just for middle button
when I click rotate I want middle button get inactive and left button get active
when I click zoom I want left button get inactive and right one get active and so.
and finally when I click another button like Zoom extent, select and etc, I want the OnMouseMove event handler get inactive in such a way that even if I'm on the opengl window the Maya-style mouse won't be active?
How can I implement something like this whether in my customized COpenGLControl class or in My MFC dialog?
Please give some instructions to me to begin my search to find out more.
------------------------------------------------------------------------------------------ Edited part of my question
I also thought about adding an event-handler manually to my class just like the OnDraw function in the COpenGLControl class so did something like this:
OpenGLContro.h
afx_msg void Pan(UINT nFlags, CPoint point);
OpenGLControl.cpp
void COpenGLControl::Pan(UINT nFlags, CPoint point)
{
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
if (nFlags & MK_MBUTTON)
{
m_fPosX += (float)0.05f * diffX;
m_fPosY -= (float)0.05f * diffY;
}
OnDraw(NULL);
}
and whenever the button pan is clicked I will call this function but there I'm not still on the OpenGL Window and don't know hat to pass as the parameters to the function Pan?
Add a state/mode member variable to the class and for each "mode" a dedicated handler function. The event handlers you use the mode variable to decide, which of the mode dependent handlers you call from the event handler, passing all the parameters.

How to move an image randomly on the screen of a view in mobile flex?

I have to create an mobile flex application with tabbed navigator view. One of the view must satisfy this condition: when the view is selected, an image will appear for one second and then disappear for half a second, then reappear at a random position on the screen of the view. This will repeat until another view is selected.
I'm new to Mobile Flex and I need your help.
Thanks you in advance.
Best regards,
HBLE
Use enterFrame event or a Timer to hide/show the image.
Set the image x and y properties to show the image at a specific position
Use Math.random() to generate a random number in the interval [0,1]
Important:
When tab is active call init();
When changing to other tab do not forget to stop the timer and remove event listeners.
(for performance reasons and to avoid memory leaks)
Sample code:
var isVisible:Boolean = false;
function init():void
{
// we show / hide with a delay of 1 second
var t:timer = new Timer(1000);
t.addEventListener(TimerEvent.Timer, onTimer);
t.start();
}
function onTimer(event:TimerEvent):void
{
if(isVisible)
{
hideImage();
}
else
{
showAndMoveImage();
}
isVisible = !isVisible;
}
function hideImage():void
{
myImage.visible = false;
}
function showAndMoveImage():void
{
// we reposition image in screen, assume image size is smaller then screen
myImage.x = Math.random() * (stage.width - myImage.width);
myImage.y = Math.random() * (stage.height - myImage.height);
myImage.visible = true;
}

Wp7 Camera Button Event

Within a Windows Phone 7 app, is it possible to capture the hardware camera button pressed event in code?
Right now when I press the camera button nothing happens and I can't figure out how to hook up to the event.
Yes you can. Check this link. This is an example of the events:
// The event is fired when the shutter button receives a half press.
CameraButtons.ShutterKeyHalfPressed += OnButtonHalfPress;
// The event is fired when the shutter button receives a full press.
CameraButtons.ShutterKeyPressed += OnButtonFullPress;
// The event is fired when the shutter button is released.
CameraButtons.ShutterKeyReleased += OnButtonRelease;
// Provide auto-focus with a half button press using the hardware shutter button.
private void OnButtonHalfPress(object sender, EventArgs e)
{
if (cam != null)
{
// Focus when a capture is not in progress.
try
{
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Half Button Press: Auto Focus";
});
cam.Focus();
}
catch (Exception focusError)
{
// Cannot focus when a capture is in progress.
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = focusError.Message;
});
}
}
}

How do I limit mouse pointer movement in wxWidgets?

Is there a way to limit mouse pointer movement to a specific area in wxWidgets? I know there is an API function ClipCursor() in Windows, but is there a method in wxWidgets for all platforms?
No. There is no such function in wx by all i know. Start up a timer (say 50ms) checking the global mouse position. If the mouse is outside the region, then set it into again.
If you want to restrict the mouse for some certain reason, for example to make some sort of game, then you can capture the mouse (see wxWindow::CaptureMouse). You will get mouse events even if the pointer is outside your window. Then you could react to mouse-motion events and do the check for the position there, without a timer. Downside of this is that the mouse won't be able to be used somewhere else for other programs since they won't receive events.
wxWidgets manual states that OSX guidelines forbid the programs to set the mouse pointer to a certain position programmatically. That might contribute to the reason there is not much support for such stuff in wx, especially since wx tries really hard to be compatible to everything possible.
Small sample. Click on the button to restrict the mouse to area 0,0,100,100. Click somewhere to release it.
#include <wx/wx.h>
namespace sample {
class MyWin : public wxFrame {
public:
MyWin()
:wxFrame(0, wxID_ANY, wxT("haha title")) {
mRestricted = wxRect(0, 0, 100, 100);
mLast = mRestricted.GetTopLeft();
wxButton * button = new wxButton(this, wxID_ANY, wxT("click this"));
}
private:
void OnClicked(wxCommandEvent& event) {
if(!HasCapture()) {
CaptureMouse();
CheckPosition();
}
}
void OnMotion(wxMouseEvent& event) {
CheckPosition();
}
void OnLeft(wxMouseEvent& event) {
if(HasCapture())
ReleaseMouse();
}
void CheckPosition() {
wxPoint pos = wxGetMousePosition();
if(!mRestricted.Contains(pos)) {
pos = ScreenToClient(mLast);
WarpPointer(pos.x, pos.y);
} else {
mLast = pos;
}
}
wxRect mRestricted;
wxPoint mLast;
DECLARE_EVENT_TABLE();
};
BEGIN_EVENT_TABLE(MyWin, wxFrame)
EVT_BUTTON(wxID_ANY, MyWin::OnClicked)
EVT_MOTION(MyWin::OnMotion)
EVT_LEFT_DOWN(MyWin::OnLeft)
END_EVENT_TABLE()
class MyApp : public wxApp {
virtual bool OnInit() {
MyWin * win = new MyWin;
win -> Show();
SetTopWindow(win);
return true;
}
};
} /* sample:: */
IMPLEMENT_APP(sample::MyApp)

Resources