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-2021 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 "GenesisWidget.hxx"
19 
20 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GenesisWidget(GuiObject * boss,const GUI::Font & font,int x,int y,Controller & controller)21 GenesisWidget::GenesisWidget(GuiObject* boss, const GUI::Font& font,
22                              int x, int y, Controller& controller)
23   : ControllerWidget(boss, font, x, y, controller)
24 {
25   const string& label = getHeader();
26 
27   const int fontHeight = font.getFontHeight();
28   int xpos = x, ypos = y, lwidth = font.getStringWidth("Right (Genesis)");
29   StaticTextWidget* t;
30 
31   t = new StaticTextWidget(boss, font, xpos, ypos+2, lwidth,
32                            fontHeight, label, TextAlign::Left);
33   xpos += t->getWidth()/2 - 5;  ypos += t->getHeight() + 20;
34   myPins[kJUp] = new CheckboxWidget(boss, font, xpos, ypos, "",
35                                     CheckboxWidget::kCheckActionCmd);
36   myPins[kJUp]->setID(kJUp);
37   myPins[kJUp]->setTarget(this);
38 
39   ypos += myPins[kJUp]->getHeight() * 2 + 10;
40   myPins[kJDown] = new CheckboxWidget(boss, font, xpos, ypos, "",
41                                       CheckboxWidget::kCheckActionCmd);
42   myPins[kJDown]->setID(kJDown);
43   myPins[kJDown]->setTarget(this);
44 
45   xpos -= myPins[kJUp]->getWidth() + 5;
46   ypos -= myPins[kJUp]->getHeight() + 5;
47   myPins[kJLeft] = new CheckboxWidget(boss, font, xpos, ypos, "",
48                                       CheckboxWidget::kCheckActionCmd);
49   myPins[kJLeft]->setID(kJLeft);
50   myPins[kJLeft]->setTarget(this);
51 
52   xpos += (myPins[kJUp]->getWidth() + 5) * 2;
53   myPins[kJRight] = new CheckboxWidget(boss, font, xpos, ypos, "",
54                                        CheckboxWidget::kCheckActionCmd);
55   myPins[kJRight]->setID(kJRight);
56   myPins[kJRight]->setTarget(this);
57 
58   xpos -= (myPins[kJUp]->getWidth() + 5) * 2;
59   ypos = 30 + (myPins[kJUp]->getHeight() + 10) * 3;
60   myPins[kJBbtn] = new CheckboxWidget(boss, font, xpos, ypos, "B button",
61                                       CheckboxWidget::kCheckActionCmd);
62   myPins[kJBbtn]->setID(kJBbtn);
63   myPins[kJBbtn]->setTarget(this);
64 
65   ypos += myPins[kJBbtn]->getHeight() + 5;
66   myPins[kJCbtn] = new CheckboxWidget(boss, font, xpos, ypos, "C button",
67                                       CheckboxWidget::kCheckActionCmd);
68   myPins[kJCbtn]->setID(kJCbtn);
69   myPins[kJCbtn]->setTarget(this);
70 
71   addFocusWidget(myPins[kJUp]);
72   addFocusWidget(myPins[kJLeft]);
73   addFocusWidget(myPins[kJRight]);
74   addFocusWidget(myPins[kJDown]);
75   addFocusWidget(myPins[kJBbtn]);
76   addFocusWidget(myPins[kJCbtn]);
77 }
78 
79 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
loadConfig()80 void GenesisWidget::loadConfig()
81 {
82   myPins[kJUp]->setState(!getPin(ourPinNo[kJUp]));
83   myPins[kJDown]->setState(!getPin(ourPinNo[kJDown]));
84   myPins[kJLeft]->setState(!getPin(ourPinNo[kJLeft]));
85   myPins[kJRight]->setState(!getPin(ourPinNo[kJRight]));
86   myPins[kJBbtn]->setState(!getPin(ourPinNo[kJBbtn]));
87 
88   myPins[kJCbtn]->setState(
89     getPin(Controller::AnalogPin::Five) == AnalogReadout::connectToGround());
90 }
91 
92 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
handleCommand(CommandSender * sender,int cmd,int data,int id)93 void GenesisWidget::handleCommand(
94     CommandSender* sender, int cmd, int data, int id)
95 {
96   if(cmd == CheckboxWidget::kCheckActionCmd)
97   {
98     switch(id)
99     {
100       case kJUp:
101       case kJDown:
102       case kJLeft:
103       case kJRight:
104       case kJBbtn:
105         setPin(ourPinNo[id], !myPins[id]->getState());
106         break;
107       case kJCbtn:
108         setPin(Controller::AnalogPin::Five,
109           myPins[id]->getState() ? AnalogReadout::connectToGround() :
110                                    AnalogReadout::connectToVcc());
111         break;
112       default:
113         break;
114     }
115   }
116 }
117 
118 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119 constexpr std::array<Controller::DigitalPin, 5> GenesisWidget::ourPinNo;
120