StickIt: Construct a new BJoystick object to pass to the window.

Previously the same BJoystick object was reused when opening a new
window, which caused all windows to show the data of the same device.
This commit is contained in:
Michael Lotz
2015-02-15 10:07:05 +01:00
parent ecbca3feec
commit e2d7e6c4be
4 changed files with 18 additions and 8 deletions
@@ -24,8 +24,9 @@ rgb_color rgb_grey = {216, 216, 216};
int32 hatX[9] = {10, 10, 20, 20, 20, 10, 0, 0, 0}; int32 hatX[9] = {10, 10, 20, 20, 20, 10, 0, 0, 0};
int32 hatY[9] = {10, 0, 0, 10, 20, 20, 20, 10, 0}; int32 hatY[9] = {10, 0, 0, 10, 20, 20, 20, 10, 0};
JoystickWindow::JoystickWindow(BJoystick *stick, BRect rect) JoystickWindow::JoystickWindow(const char *deviceName, BJoystick *stick,
: BWindow(rect, "StickIt", B_TITLED_WINDOW, BRect rect)
: BWindow(rect, deviceName, B_TITLED_WINDOW,
B_NOT_RESIZABLE|B_NOT_ZOOMABLE) B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
{ {
fView = new JoystickView(Bounds(), stick); fView = new JoystickView(Bounds(), stick);
@@ -64,6 +65,11 @@ JoystickView::JoystickView(BRect frame, BJoystick *stick)
} }
JoystickView::~JoystickView() {
delete fStick;
}
void void
JoystickView::Pulse(void) { JoystickView::Pulse(void) {
Window()->Lock(); Window()->Lock();
@@ -13,6 +13,7 @@ class BJoystick;
class JoystickView : public BView { class JoystickView : public BView {
public: public:
JoystickView(BRect bounds, BJoystick *stick); JoystickView(BRect bounds, BJoystick *stick);
virtual ~JoystickView();
virtual void Draw(BRect updateRect); virtual void Draw(BRect updateRect);
virtual void Pulse(void); virtual void Pulse(void);
@@ -29,7 +30,8 @@ class JoystickView : public BView {
class JoystickWindow : public BWindow { class JoystickWindow : public BWindow {
public: public:
JoystickWindow(BJoystick *stick, BRect rect); JoystickWindow(const char *deviceName,
BJoystick *stick, BRect rect);
virtual bool QuitRequested(void); virtual bool QuitRequested(void);
private: private:
@@ -103,19 +103,22 @@ StickItWindow::MessageReceived(BMessage *message)
temp1 << "BJoystick::GetDeviceName(), id = " << id temp1 << "BJoystick::GetDeviceName(), id = " << id
<< ", name = " << devName; << ", name = " << devName;
temp1 = AddToList(fListView1, temp1.String()); temp1 = AddToList(fListView1, temp1.String());
err = fJoystick->Open(devName); BJoystick *joystick = new BJoystick();
err = joystick->Open(devName);
if (err != B_ERROR) { if (err != B_ERROR) {
temp1 = AddToList(fListView1, "BJoystick::Open()"); temp1 = AddToList(fListView1, "BJoystick::Open()");
temp1 = AddToList(fListView1, "BJoystick::Open()"); temp1 = AddToList(fListView1, "BJoystick::Open()");
if(fJoystick->IsCalibrationEnabled()) if (joystick->IsCalibrationEnabled())
temp1 = AddToList(fListView1, temp1 = AddToList(fListView1,
"BJoystick::IsCalibrationEnabled() - True"); "BJoystick::IsCalibrationEnabled() - True");
else else
temp1 = AddToList(fListView1, temp1 = AddToList(fListView1,
"BJoystick::IsCalibrationEnabled() - False"); "BJoystick::IsCalibrationEnabled() - False");
fJoystickWindow = new JoystickWindow(fJoystick, JoystickWindow *window = new(std::nothrow)
JoystickWindow(devName, joystick,
BRect(50, 50, 405, 350)); BRect(50, 50, 405, 350));
fJoystickWindow->Show(); if (window != NULL)
window->Show();
} else } else
AddToList(fListView1, AddToList(fListView1,
"No controller connected on that port. Try again."); "No controller connected on that port. Try again.");
@@ -26,5 +26,4 @@ class StickItWindow : public BWindow {
BListView* fListView1; BListView* fListView1;
BListView* fListView2; BListView* fListView2;
BJoystick* fJoystick; BJoystick* fJoystick;
JoystickWindow* fJoystickWindow;
}; };