Here is a basic example I of an autonomous selector in Vexcode…
int autonToRun = 0;
class Button
{
public:
int x, y, width, height;
std::string text;
vex::color buttonColor, textColor;
Button(int x, int y, int width, int height, std::string text, vex::color buttonColor, vex::color textColor)
: x(x), y(y), width(width), height(height), text(text), buttonColor(buttonColor), textColor(textColor){}
void render()
{
Brain.Screen.drawRectangle(x, y, width, height, buttonColor);
Brain.Screen.printAt(x + 10, y + 10, false, text.c_str());
}
bool isClicked()
{
if(Brain.Screen.pressing() && Brain.Screen.xPosition() >= x && Brain.Screen.xPosition() <= x + width &&
Brain.Screen.yPosition() >= y && Brain.Screen.yPosition() <= y + width) return true;
return false;
}
};
Button autonButtons[] = {
Button(10, 10, 150, 50, "Auton Red 1", vex::green, vex::black),
Button(170, 10, 150, 50, "Auton Red 2", vex::white, vex::black),
Button(10, 70, 150, 50, "Auton Blue 1", vex::white, vex::black),
Button(170, 70, 150, 50, "Auton Blue 2", vex::white, vex::black)
};
int main()
{
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);
while(true)
{
Brain.Screen.clearScreen(vex::white);
if(!Competition.isEnabled())
{
for(int i = 0; i < 4; i++)
{
autonButtons[i].render();
if(autonButtons[i].isClicked())
{
autonButtons[autonToRun].buttonColor = vex::white;
autonButtons[i].buttonColor = vex::green;
autonToRun = i;
}
}
}
Brain.Screen.render();
vex::task::sleep(7);
}
}
The first thing is a button class to draw the buttons and detect clicks. It hold parameters for the dimensions, text, and colors, which are also changed to signify which autonomous has been selected. Then I created an array of four buttons to handle them in a loop. The loop goes through each button, rending it, then checking if it is being clicked. If it is it updates the colors and changes the autonToRun variable. Also the Brain.Screen.render() is needed if you are going to be rending really fast.
This a relatively basic example, but it should get the job done. If you don’t understand aspects feel free to ask. Also please take the time to understand how it work and not just copy and paste it. If you want, and probably should, make improvements which might include, only rendering when something changes, better button class, ability to run selector then autonomous from drivercontrol, etc.
When programming it helps to have API docs so here are the relevant links…
VEX Help
VEX Help
https://api.vexcode.cloud/v5/html/index.html