Reputation: 269
So I have set up a system where my GUI Class, when initiated, sends a config object to the Focus class constructor. The GUI class creates an instance of the Focus class, and the class constructor has an argument. My Code:
var GUI = function(){
this.isWorking = "GUI(LOCAL) IS WORKING";
this.controlBox = new ControlBox();
this.input1 = new Input({
X: 350, Y: 50, SIZE: 50
});
this.input2 = new Input({
X: 350, Y: 125, SIZE: 50
});
this.focus = new Focus({
input1: this.input1,
input2: this.input2
});
this.drawGUI = function(){
// Draw the control box first?
//println("GLOBAL IS WORKING!");
this.controlBox.drawControlBox();
this.input1.drawInput(false);
this.input2.drawInput(false);
fill(255, 0, 0);
text("Global is working", 80, 25);
};
};
var Focus = function(config){
for(var focus in config){
}
this.getFocus = function(){
return;
};
};
Using this config file in the constructor. I want the Focus class to be able to return the key name that I send in the config file.
This might be a little confusing so I'll explain the process I want it to go through: 1. Put input1 as a key to the config, with the value as GUI.input1 (An actual Input object). 2. The focus constructor will somehow get the key name and value and store it with the same name I passed into it. Now it should be referencable from the GUI class. 3. I will be able to call GUI.focus.getfocus() and it return the object input1 or at least the same name.
EDIT: Another reason I want to use this config parameter is that when I want to add another focus point besides input1 and input2, I can just put the objName: this.objName right below the others, instead of hardcoding each one in.
Upvotes: 0
Views: 39
Reputation: 4047
maybe this is what you are looking for:
var Focus = function(config){
this.points = config || {};
this._focus = undefined;
};
Focus.prototype.add = function(point){
this.points[point.name] = point
};
/* or if you dont want the property name
Focus.prototype.add = function(id, point){
this.points[id] = point
};
*/
Focus.prototype.setFocus = function(focus){
this._focus = focus;
};
Focus.prototype.getFocus = function(focus){
return this.points[focus || this._focus];
};
// usage:
this.focus = new Focus({
input1 : { name : 'input1', point : this.input1 },
input2 : { name : 'input2', point : this.input2 }
});
this.focus.setFocus('input2');
this.focus.getFocus('input2');
Upvotes: 2