How do I make a box that i can move. I am new to wxwidgets and wondering if there is a way to move a box

Viewed 118

The Image is what I currently have made, I plan to make an Elevator that can go up and down. I am unsure of how I would move a box such that it stays at the same x cords but changes the y cords. Any help is appreciated.

This is for a project I am doing. The specifications state

 "In this project, the group is required to 
develop  a  programme  that  simulates  the  operation  of  arrays  (columns)  of  elevators 
running over a multi‐storey building. The program  (application) should be able  to cope 
with user defined number of elevators and number of storeys along with an ability for a 
user to simulate or represent elevator passengers making request for elevator service to 
particular  floors. The program  should have a user interface  that displays  the elevators’ 
states  allows  user  input  and  ideally  shows  (simulates)  the  vertical  movement  of  each 
elevator. "

Simulation Plane

I am looking for something that will move the "Elevator A" box with the buttons up or down, along with a box inside the elevator shaft, both move up and down in unison. The code should do this automatically hopefully without having to drag the boxes and buttons with a mouse.

cMain.hpp (Updated)

#pragma once
#ifndef _cMain_
#define _cMain_
#include <wx/frame.h>
#include <wx/wx.h>
class cMain : public wxFrame {
public:
    cMain(wxWindow* parent,
        wxWindowID id,
        const wxString& title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_FRAME_STYLE,
        const wxString& name = wxASCII_STR(wxFrameNameStr));
    ~cMain();
    wxStaticBox* m_box1 = nullptr;
    wxStaticBox* m_box2 = nullptr;
    wxStaticBox* m_box3 = nullptr;
    wxButton* m_btng = nullptr;
    wxButton* m_btn1 = nullptr;
    wxButton* m_btn2 = nullptr;
    wxButton* m_btn3 = nullptr;
    wxButton* m_btn_up = nullptr;
    wxButton* m_btn_down = nullptr;
    wxListBox* m_list1 = nullptr;
    void Ground_Button(wxCommandEvent& evt);
    void Level_1_Button(wxCommandEvent& evt);
    void Level_2_Button(wxCommandEvent& evt);
    void Level_3_Button(wxCommandEvent& evt);
    void Call_Up_Button(wxCommandEvent& evt);
    void Call_Down_Button(wxCommandEvent& evt);
    DECLARE_EVENT_TABLE();
};

#endif /* */

cMain.cpp (Updated)

#include "cMain.h"
#include "id.h"
#include <wx/dc.h>
#include <wx/graphics.h>
#include <wx/dcbuffer.h>
#include <wx/event.h>
#include <iostream>
#include "cElevator.h"
using namespace std;
cElevator e;
wxBEGIN_EVENT_TABLE(cMain, wxFrame)

wxEND_EVENT_TABLE();
cMain::cMain(wxWindow* parent,
wxWindowID id,
const wxString& title,
const wxPoint& pos,
const wxSize& size,
long style ,
const wxString& name) : wxFrame(parent,id,title,pos,size,style,name)
{   
    this->SetBackgroundColour(*wxWHITE);
    m_box1 = new wxStaticBox(this, wxID_ANY, "Call 1", wxPoint(252, 530), wxSize(50, 80));
    m_btn_up = new wxButton(this, wxID_ANY, "/\\", wxPoint(265, 547), wxSize(25, 25));
    m_btn_down = new wxButton(this, wxID_ANY, "\\/", wxPoint(265, 575), wxSize(25, 25));

    //Inner Panel
    m_box2 = new wxStaticBox(this, wxID_ANY, "Elevator A", wxPoint(164, 530), wxSize(80, 80));

    m_btng = new wxButton(this, wxID_ANY, wxString("G"), wxPoint(177, 575), wxSize(25, 25));
    m_btn1 = new wxButton(this, wxID_ANY, wxString("1"), wxPoint(177, 547), wxSize(25, 25));
    m_btn2 = new wxButton(this, wxID_ANY, wxString("2"), wxPoint(207, 575), wxSize(25, 25));
    m_btn3 = new wxButton(this, wxID_ANY, wxString("3"), wxPoint(207, 547), wxSize(25, 25));

    //Elevator Shafts
    //m_box3 = new wxStaticBox(panel, wxID_ANY, "", wxPoint(80, 10), wxSize(80, 600));

    //Other 
    m_list1 = new wxListBox(this, wxID_ANY, wxPoint(200, 10), wxSize(100, 100));
    m_btn_up->Bind(wxEVT_BUTTON, &cMain::Call_Up_Button, this);
    m_btn_down->Bind(wxEVT_BUTTON, &cMain::Call_Down_Button, this);
    m_btng->Bind(wxEVT_BUTTON, &cMain::Ground_Button, this);
    m_btn1->Bind(wxEVT_BUTTON, &cMain::Level_1_Button, this);
    m_btn2->Bind(wxEVT_BUTTON, &cMain::Level_2_Button, this);
    m_btn3->Bind(wxEVT_BUTTON, &cMain::Level_3_Button, this);
}
void cMain::Ground_Button(wxCommandEvent& evt)
{
    e.GLevel = true;
    
    m_list1->AppendString("Going to Ground Level");
    evt.Skip();

}
void cMain::Level_1_Button(wxCommandEvent& evt)
{
    
    m_list1->AppendString("Going to Level 1");
    evt.Skip();
}
void cMain::Level_2_Button(wxCommandEvent& evt)
{
    
    m_list1->AppendString("Going to Level 2");
    evt.Skip();
}
void cMain::Level_3_Button(wxCommandEvent& evt)
{
    
    m_list1->AppendString("Going to Level 3");
    evt.Skip();
}
void cMain::Call_Up_Button(wxCommandEvent& evt)
{
    
    m_list1->AppendString("Going to Up");
    //evt.Skip();
}
void cMain::Call_Down_Button(wxCommandEvent& evt)
{
    
    m_list1->AppendString("Going Down");
    //evt.Skip();
}

cMain::~cMain() {

}




cElevator.hpp

#include <wx/wx.h>

class cElevator : public wxFrame
{
public:
    cElevator(wxWindow* parent,
        wxWindowID id,
        const wxString& title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_FRAME_STYLE,
        const wxString& name = wxASCII_STR(wxFrameNameStr));
    ~cElevator();

    bool GLevel;
    void OnPaint(wxPaintEvent& evt);


    wxDECLARE_EVENT_TABLE();
};

cElevator.cpp

#include "cMain.h"
#include "cElevator.h"
#include <wx/dc.h>
#include <wx/graphics.h>
#include <wx/dcbuffer.h>
#include <wx/event.h>
#include <iostream>
using namespace std;

wxBEGIN_EVENT_TABLE(cElevator,wxPanel)
EVT_PAINT(cElevator::OnPaint)
wxEND_EVENT_TABLE();

cElevator::cElevator(wxWindow* parent,
    wxWindowID id,
    const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style,
    const wxString& name) : wxFrame(parent,id,title,pos,size,style,name)
{
    this->SetBackgroundColour(*wxBLACK);

}

void cElevator::OnPaint(wxPaintEvent& evt)
{
    wxClientDC cdc(this);
    wxBufferedDC dc(&cdc);
    wxGraphicsContext* gc = wxGraphicsContext::Create(dc);


    // make a path that contains a circle and some lines
    gc->SetPen(*wxWHITE_PEN);
    wxGraphicsPath path = gc->CreatePath();
    path.CloseSubpath();
    path.AddRectangle(80.0, 10.0, 80.0, 800.0);
    path.MoveToPoint(80, 600);
    path.AddLineToPoint(160, 600);
    path.MoveToPoint(80, 400);
    path.AddLineToPoint(160, 400);
    path.MoveToPoint(80, 200);
    path.AddLineToPoint(160, 200);
    //path.AddRectangle(90, 740, 60, 65);
    //path.AddRectangle(90, 530, 60, 65);
    //path.AddRectangle(90, 330, 60, 65);
    //path.AddRectangle(90, 130, 60, 65);
    gc->StrokePath(path);

    delete gc;
}

cElevator::~cElevator() {

}

Paintting part

void cElevator::OnPaint(wxPaintEvent& evt)
{
    int CurrentPosy = 130;
    wxPaintDC dc(this);
    dc.DrawRectangle(80.0, 10.0, 80.0, 800.0);//overall rectangle for  elevator shaft
    dc.Clear();
    dc.SetPen(*wxWHITE);
    dc.SetBrush(*wxLIGHT_GREY_BRUSH);
    while (CurrentPosy != ExpectedPosy)
    {
        dc.DrawRectangle(90, CurrentPosy, 60, 65);
            //CurrentPosy--; // or currentPOs.y++;
        wxSleep(5);
        dc.Clear();
    }
    dc.SetPen(wxNullPen);
    dc.SetBrush(wxNullBrush);

The ExpectedPosy is in the header file as an int, that should get a value when the ground button (or other level buttons) is pressed.

1 Answers

I presume you will have some kind of box drawn inside the elevator shaft and then this shaft will be equally divided by 5.

Now what you do is following:

You keep the vector of positions for each floor. You can get it if you call GetSize() on the elevator box and divide the height by 5.

Next you will have an initial position of the shaft at (0,0), which will be your current position.

You will need to catch the EVT_PAINT() messqage of the elevator shaft.

When the user press the button you will calculate the new position and set the flag to repaint the shaft.

In the handler you will need to create a wxPaintDC object (not a pointer), then do:

dc.Clear();
dc.SetPen();
dc.SetBrush();
while( currentPos.y != expectedPos.y )
{
    dc.DrawLine();
    dc.DrawLIne();
    dc.DrawLine();
    dc.DrawLione();
    currentPos.y--; // or currentPOs.y++;
    wxSleep( 5 );
    dc.Clear();
}
dc.SetPen( wxNullPen );
dc.SetBrush( wxNullBrush );

or something to that extent.

But I hope you get the idea.

Let us know if that is not what you are looking for.

Related