How to put a function in a constructor and call it later in the class
in
Programming Questions
•
7 months ago
Hi,
I'm trying to make a few class files that could be copy/pasted into one of my sketches (Or someone else's) that would quickly let me make a button with custom/existing functions that could be easily defined in the main sketch, with only minimal other work to the main sketch, and no other modifications to the copy/pasted files besides possibly extending the class besides extending the class with the list of functions to add custom ones.
So basically, I would have a class called Functions which would be defined in the sketch as, lets say, Functions function, which the Button class would grab functions from. It would have a long series of
void ...(...,...,...)s, each of which would do a certain thing, lets say,
void ChangeBackground(
color c), which would be called on in the Button class.
I know how to make a list of functions using a variable in the constructor that would be strained through a series of conditionals to find the right function, but I'm trying to do it where someone could add new functions by simply extending the Functions class/adding new
void ...s to the existing one, instead of modifying the Button. The best way I can think to do this would be putting a variable in the Button class that would be set to a function, which would then be used to call on part of the Functions class which the button of pressed.
Define the Functions class in the sketch (Functions function =
new Functions()) --> Constructor (For example: test =
new Button(...,...,...,function.ChangeBackground(
color(...,...,...)))) --> Have a variable in the class be set to the function --> Call on the function using the variable in a part of the class
I'm not sure if this is possible, and what I've done so far hasn't resulted in anything more than constructor errors, function errors, and the occasional NPE. If anyone has any ideas to help, they would be much appreciated.
Main sketch:
- Button test;
- Functions function;
- void setup() {
- imageMode(CENTER);
- size(500,500);
- test = new Button(width/2,height/2,50,50,loadImage("Musketeer.png"),loadImage("Galleon.png"),"Hello!",function.ChangeBackground(color(255,0,0)));
- function = new Functions();
- }
- void draw() {
- test.Display();
- }
- void mousePressed() {
- test.mousePressed();
- }
Button class:
- class Button {
- PVector location; //Location of the button
- int xSize,ySize; //Size ofthe button
- PImage on,off; //Images for mosue overlap
- String type; //Text to be displayed in the button
- boolean mouseOver;
- void callFuntion;
- Button(float x, float y, int xs, int ys, PImage ion, PImage ioff, String t, void a) {
- location = new PVector(x,y);
- xSize = xs;
- ySize = ys;
- on = ion;
- off = ioff;
- type = t;
- callFuntion = a;
- }
- boolean mouseOver(float x, float y, float sizeX, float sizeY) {
- x-=sizeX/2;
- y-=sizeY/2;
- if(mouseX>x&&mouseX<x+sizeX&&mouseY>y&&mouseY<y+sizeY) {
- return true;
- } else {
- return false;
- }
- }
- void Display() {
- if(mouseOver(location.x,location.y,xSize,ySize)) {
- image(off,location.x,location.y,xSize,ySize);
- }
- else if(!mouseOver(location.x,location.y,xSize,ySize)) {
- image(on,location.x,location.y,xSize,ySize);
- }
- text(type,location.x-(xSize/2),location.y);
- }
- void mousePressed() {
- if(mouseOver(location.x,location.y,xSize,ySize)) {
- }
- }
- }
Functions class:
- class Functions {
- Functions() {
- }
- void ChangeBackground(color c) {
- background(c);
- }
- void ChangeFill(color c) {
- fill(c);
- }
- void ChangeTint(color c) {
- tint(c);
- }
- void ChangeStroke(color c) {
- stroke(c);
- }
- void ChangeFont(PFont s) {
- textFont(s);
- }
- void ChangeFontSize(int a) {
- textSize(a);
- }
- void DrawShape(float x, float y) {
- point(x,y);
- }
- void DrawShape(String s, float x, float y, float w, float h) {
- if(s=="RECT") {
- rect(x,y,w,h);
- }
- else if(s=="ELLIPSE") {
- ellipse(x,y,w,h);
- }
- else if(s=="LINE") {
- line(x,y,w,h);
- }
- }
- void DrawShape(float x1, float y1, float x2, float y2, float x3, float y3) {
- triangle(x1,y1,x2,y2,x3,y3);
- }
- void DrawText(String s, float x, float y) {
- text(s,x,y);
- }
- }
1