Pergunta

I am stuck, I need help, I can't post all files here because this is a project with almost 20 files of c++. But it basically all boils down to movements on a screen.

-The class "MyClass" is the screen, it has a constructor which calls the functions. the screen has a small-box at the bottom. when you press the mouse down,up, or move it, it sends a signal or 'Event" and one of the three functions below is called;

-function-mouseMotion, it should move the small-box to where your mouse is pointing when you are moving the mouse, sort of drag. this function should be active whenever you press down your mouse, say whenever mouse is down -function-mouseButtonDown, when u press your mouse down, the small-box should move from the bottom of screen to where your cursor is. -function-mouseButtonUp, when you unclick your mouse, this function will be called and all the movements of small box should stop.

Right now as the code is, whenever I bring the ouse to the screen, the small box immediately jumps and starts following the mouse. But our professor wants us to change the functions such that when I introduce the moue on the screen, nothing should happen. When I click down the mouse, the box should then come to where the mouse is. If I click and hold the mouse down as I move it, the box should keep following the mouse. Finally, if i unclick the mouse, the box needs to remain where i unclicked it from.

What alograithm would you use, am new to c++

using namespace WALY;

class MyClass : public Frame {
   private:
   void(*switchAction) (void);

   Frame* box;

   static void mouseButtonDown (Frame* f, const Event* e);
   static void mouseButtonUp (Frame* f, const Event* e);
   static void mouseMotion (Frame* f, const Event* e);


   public:
   MyClass (Frame* parent, void (*switchFunc) (void));
   void activate (void);
   void deactivate (void);

};


MyClass::MyClass(Frame *parent, void (*switchFunc)(void)) : Frame (parent),       switchAction (switchFunc)
{
box = new Frame (this, 250, 700);     // creating the box on the screen
box->setAlign (ALIGN_C);                              
Rect boxRect;
boxRect.x = boxRect.y = 0;
boxRect.w = 80;
boxRect.h = 50;
box->setScrollRect (boxRect);
box->useSolidBackground (0x808080);


setCallbackFunc (MOUSE_DOWN, mouseButtonDown);        // registering functions into wally library
setCallbackFunc (MOUSE_UP, mouseButtonUp);
setCallbackFunc (MOUSE_MOTION, mouseMotion);

deactivate ();

}

void MyClass::activate(void)
{
   setActive(true);
   setVisible(true);
}

void MyClass::deactivate(void)
{
   setVisible(false);
   setActive(false);
}
void
MyClass::mouseMotion (Frame* f, const Event* e)
{
   MyClass* wywtcyc = (MyClass*)f;
   wywtcyc->box->setX (e->motion.x);
   wywtcyc->box->setY (e->motion.y);
}

 void MyClass::mouseButtonDown (Frame* f, const Event* e)
{
  // (e->button.x)
}

void MyClass::mouseButtonUp (Frame* f, const Event* e)
{
}
Foi útil?

Solução

The logic could be like below:

Declare variables:

  • mouseIsDown = False;
  • released = False;

In mouseMotion:

if (mouseIsDown && ! released) {
    // your current code
}

In mouseButtonDown:

if (mouseIsDown) {
    released = True;
} else {
    mouseIsDown = True;
    released = False;
}

In mouseButtonUp:

if (mouseIsDown) {
    mouseIsDown = False;
    released = True;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top