Button.cpp:
#include "vex.h"
#include "button.h"
#include "utils.h"
typedef struct {
int x;
int y;
int w;
int h;
}
square;
typedef struct {
int x;
int y;
}
point;
typedef char *string;
square s;
bool clicked = false;
void (*functionpointer)(void);
bool checkCollsion(point p, square s)
{
if(p.x >= s.x && p.x <= s.x + s.w && p.y >= s.y && p.y <= s.y + s.h){
return true;
}
return false;
}
int checkCollisions()
{
while(true)
{
waitUntil(Brain.Screen.pressing()); // Waits until the screen is being pressed
do {
point p = {Brain.Screen.xPosition(), Brain.Screen.yPosition()};
if(checkCollsion(p, s))
{
functionpointer();
return EXIT_SUCCESS;
}
}
while(Brain.Screen.pressing());
}
return EXIT_SUCCESS;
}
void button::setup(int x, int y, int width, int height, string text, void (*ptr)(void))
{
Brain.Screen.drawRectangle(x, y, width, height);
s = {x, y, width, height};
int tw = vexDisplayStringWidthGet(text);
int th = vexDisplayStringHeightGet(text);
int tl = s.x + (s.w / 2) - (tw/2);
int tt = s.y + (s.h / 2) - (th / 2);
Brain.Screen.printAt(tl, tt, false, text);
functionpointer = ptr;
task check(checkCollisions);
return;
}
void button::setup(square s, string text, void (*ptr)(void)) {button::setup(s.x, s.y, s.w, s.h, text, ptr);}
main.cpp
#include "vex.h"
#include "button.h"
#include "utils.h"
using namespace vex;
typedef char *string;
button b, b2;
button::square x = {200, 0, 150, 50};
button::square y = {0, 0, 150, 50};
void Autonomous(void)
{
Drivetrain.drive(forward);
}
void buttonFunction()
{
Brain.Screen.clearScreen();
}
int main()
{
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
b.setup(x, "Drivercontrol?", buttonFunction);
b2.setup(y, "Autonomous?", buttonFunction);
Autonomous();
return EXIT_SUCCESS;
}
ALSO yes I know I’m not doing the autonomous/drivercontrol stuff incorrectly that is for a reason.
Anyways the button I call setup first works but the second one does not.
I don’t know why???