1 | //============================================================================ |
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | //============================================================================ |
17 | |
18 | #include "OSystem.hxx" |
19 | #include "EventHandler.hxx" |
20 | #include "Widget.hxx" |
21 | #include "Font.hxx" |
22 | #include "EditTextWidget.hxx" |
23 | #include "StringListWidget.hxx" |
24 | #include "Variant.hxx" |
25 | #include "JoystickDialog.hxx" |
26 | |
27 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
28 | JoystickDialog::JoystickDialog(GuiObject* boss, const GUI::Font& font, |
29 | int max_w, int max_h) |
30 | : Dialog(boss->instance(), boss->parent(), font, "Joystick database" , 0, 0, max_w, max_h) |
31 | { |
32 | int xpos, ypos; |
33 | WidgetArray wid; |
34 | |
35 | int buttonWidth = font.getStringWidth("Remove " ) + 20, |
36 | buttonHeight = font.getLineHeight() + 4; |
37 | |
38 | // Joystick list |
39 | xpos = 10; ypos = 10 + _th; |
40 | int w = _w - 2 * xpos; |
41 | int h = _h - buttonHeight - ypos - 20; |
42 | myJoyList = new StringListWidget(this, font, xpos, ypos, w, h); |
43 | myJoyList->setEditable(false); |
44 | wid.push_back(myJoyList); |
45 | |
46 | // Joystick ID |
47 | ypos = _h - buttonHeight - 10; |
48 | StaticTextWidget* t = new StaticTextWidget(this, font, xpos, ypos+2, "Joystick ID " ); |
49 | xpos += t->getWidth() + 4; |
50 | myJoyText = new EditTextWidget(this, font, xpos, ypos, |
51 | font.getStringWidth("Unplugged" )+8, font.getLineHeight(), "" ); |
52 | myJoyText->setEditable(false); |
53 | |
54 | // Add buttons at bottom |
55 | xpos = _w - buttonWidth - 10; |
56 | myCloseBtn = new ButtonWidget(this, font, xpos, ypos, |
57 | buttonWidth, buttonHeight, "Close" , GuiObject::kCloseCmd); |
58 | addOKWidget(myCloseBtn); addCancelWidget(myCloseBtn); |
59 | |
60 | buttonWidth = font.getStringWidth("Remove" ) + 20; |
61 | xpos -= buttonWidth + 8; |
62 | myRemoveBtn = new ButtonWidget(this, font, xpos, ypos, |
63 | buttonWidth, buttonHeight, "Remove" , kRemoveCmd); |
64 | myRemoveBtn->clearFlags(Widget::FLAG_ENABLED); |
65 | |
66 | // Now we can finally add the widgets to the focus list |
67 | wid.push_back(myRemoveBtn); |
68 | wid.push_back(myCloseBtn); |
69 | addToFocusList(wid); |
70 | } |
71 | |
72 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
73 | void JoystickDialog::loadConfig() |
74 | { |
75 | myJoyIDs.clear(); |
76 | |
77 | StringList sticks; |
78 | for(const auto& i: instance().eventHandler().physicalJoystickDatabase()) |
79 | { |
80 | sticks.push_back(i.first); |
81 | myJoyIDs.push_back(i.second.toInt()); |
82 | } |
83 | myJoyList->setList(sticks); |
84 | myJoyList->setSelected(0); |
85 | if(sticks.size() == 0) |
86 | { |
87 | myRemoveBtn->setEnabled(false); |
88 | myJoyText->setText("" ); |
89 | } |
90 | } |
91 | |
92 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
93 | void JoystickDialog::handleCommand(CommandSender* sender, int cmd, int data, int id) |
94 | { |
95 | switch(cmd) |
96 | { |
97 | case GuiObject::kOKCmd: |
98 | close(); |
99 | break; |
100 | |
101 | case kRemoveCmd: |
102 | instance().eventHandler().removePhysicalJoystickFromDatabase( |
103 | myJoyList->getSelectedString()); |
104 | loadConfig(); |
105 | break; |
106 | |
107 | case ListWidget::kSelectionChangedCmd: |
108 | if(myJoyIDs[data] >= 0) |
109 | { |
110 | myRemoveBtn->setEnabled(false); |
111 | ostringstream buf; |
112 | buf << "J" << myJoyIDs[data]; |
113 | myJoyText->setText(buf.str()); |
114 | } |
115 | else |
116 | { |
117 | myRemoveBtn->setEnabled(true); |
118 | myJoyText->setText("Unplugged" ); |
119 | } |
120 | break; |
121 | |
122 | default: |
123 | Dialog::handleCommand(sender, cmd, data, id); |
124 | break; |
125 | } |
126 | } |
127 | |