Skip to content

Manipulating an on screen object with the mouse

Brian M edited this page Feb 9, 2016 · 1 revision

User Interface

  1. Begin by creating a button as described in Creating a simple button that changes state.

  2. Create an additional button_id for placing the object. Do not create a item in the pane button map for this; this button_id will only be used as an event, not an actual button.

  3. Create a vector of PAIR to keep track of placed objects.

  4. Create a PAIR variable to keep track of the current mouse position.

  5. In the on_mousemove function set the variable you created in step 4. based on the current mouse position. You will likely want to scale from window coordinates to pane coordinates

    • Example: (assume m_tMouse_Position is the variable created in step 4.)

        PAIR<unsigned int> tMouse = i_tMouse_Position;
        pane_id idMouse_Pane = Find_Pane(tMouse);
        QUAD<unsigned int> qPane_Position = Get_Pane_Position(idMouse_Pane);
        PAIR<unsigned int> tMouse_TR = tMouse - qPane_Position.m_tTR;
        tMouse -= qPane_Position.m_tBL;
        double dPane_Scale = 1.0 / (double) qPane_Position.Get_Size().m_tY;
        PAIR<double> tMouse_Scaled = tMouse * dPane_Scale;
        m_tMouse_Position = tMouse_Scaled
      
  6. In the on_mouse_button_down, detect when the button is in the on state, the user clicks somewhere that is not on another button, then add an event

    • Example

        // if no button has been processed and we are in a select mode, add an event to place the selected item
        if (!bProcessed && m_mMain_Pane_Buttons[TEST_BUTTON].Get_State() == 1)
        {
            m_csEvent_Queue.Set();
            m_qEvent_List.push_back(SELECTED_OBJECT_PLACE);
            m_csEvent_Queue.Unset();
        }
      
  7. In the event processing loop when the placement event occurs add the current mouse position to the back of the vector you have created, and turn off the selection mode.

    • Example:

        case SELECTED_OBJECT_PLACE:
            m_vPlaced_Objects.push_back(m_tMouse_Position);
            m_mMain_Pane_Buttons[TEST_BUTTON].Set_State(0);
            break;
      
  8. In gfx, draw an object at the current mouse position.

    • Example:

        if (m_mMain_Pane_Buttons[TEST_BUTTON].Get_State() == 1)
        {
            glPushMatrix();
                glColor4d(0.0,0.0,1.0,0.0);
                glTranslated(m_tMouse_Position.m_tX,m_tMouse_Position.m_tY,0.0);
                glScaled(0.05,0.05,0.05);
                glBegin(GL_TRIANGLE_FAN);
                    glVertex3d(0.0,0.0,0.0);
                    glVertexList(g_vlEllipse);
                glEnd();
            glPopMatrix();
        }
      
  9. In gfx draw the list of placed objects

    • Example:

        for (std::vector<PAIR<double> >::iterator cI = m_vPlaced_Objects.begin(); cI != m_vPlaced_Objects.end(); cI++)
        {
            glPushMatrix();
                glColor4d(0.0,0.0,1.0,0.0);
                glTranslated(cI->m_tX,cI->m_tY,0.0);
                glScaled(0.05,0.05,0.05);
                glBegin(GL_TRIANGLE_FAN);
                    glVertex3d(0.0,0.0,0.0);
                    glVertexList(g_vlEllipse);
                glEnd();
            glPopMatrix();
        }