control a toggle by a button click - processing

i needed to create a gui where a toggle would be ON when we click on a specified button , the problem is i cant find the right command to do so , here is an example of how i added my toggle :
cp5.addToggle("condenseur")
.setPosition(700,585)
.setSize(70,70)
.setValue(false)
.setLabelVisible(false);
and here is the function (the button click) where i want it to return a char AND change the toggle state (makes it ON)
void marche(){
port.write('a');
i tried to use setValue() or condensateur = true but none worked

The following code uses two buttons to toggle a cp5 toggle button. Reference: https://www.kasperkamperman.com/blog/processing-code/controlp5-library-example2/
import controlP5.*;
ControlP5 cp5;
void setup() {
size(400, 300);
cp5 = new ControlP5(this);
cp5.addToggle("toggle")
.setPosition(200, 90)
.setSize(70, 70)
.setValue(false)
.setLabelVisible(false)
;
cp5.addButton("on")
.setValue(0)
.setPosition(50, 100)
.setSize(50, 19)
;
cp5.addButton("off")
.setValue(0)
.setPosition(120, 100)
.setSize(50, 19)
;
}
void draw() {
}
void controlEvent(ControlEvent theEvent) {
if (theEvent.isController()) {
print("control event from : " + theEvent.getController().getName());
println(", value : " + theEvent.getController().getValue());
if (theEvent.getController().getName()=="on") {
cp5.getController("toggle").setValue(1);
}
if (theEvent.getController().getName()=="off") {
cp5.getController("toggle").setValue(0);
}
}
}

Related

enable and disable textfield in Processing using Cp5 library

Using Cp5, I'm attempting to create a textfield. I want the user to only be able to edit the textfield after clicking a button. The user is unable to edit the textfield if the button is not pressed.
Does Textfield have a method that can assist me with this function?
I am asking because I didn't find any documentation for the methods in the library.
setLock() should do what you want to do:
import controlP5.*;
ControlP5 cp5;
String textValue = "";
public void lock() {
cp5.get(Textfield.class, "textField").setLock(true);
}
public void unlock() {
cp5.get(Textfield.class, "textField").setLock(false);
}
void setup() {
size(700, 400);
background(0);
fill(255);
PFont font = createFont("arial", 20);
cp5 = new ControlP5(this);
cp5.addTextfield("textField")
.setPosition(60, 100)
.setSize(150, 30)
.setFont(createFont("arial", 18))
.setAutoClear(false)
.setLock(false);
;
cp5.addBang("lock")
.setPosition(240, 100)
.setSize(60, 30)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
cp5.addBang("unlock")
.setPosition(330, 100)
.setSize(60, 30)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
textFont(font);
}
void draw() {
}

Alter Textfield functionality ControlP5

I would like to remove some of the functionality of a Textfield.
When the enter or return keys are pressed, the field becomes empty. I would like the value entered to stay within the field.
I have tried overriding the submit method but this hasn't done the job:
widthTopField = new Textfield(controlP5, "widthField"){
# Override public Textfield submit(){
return this;
}
};
Demonstrates using .setAutoClear() to retain edit field contents after hitting return button. Field 1 is set to true and the other is set to false.
import controlP5.*;
ControlP5 cp5;
void setup() {
size(400,400);
PFont font = createFont("arial",18);
cp5 = new ControlP5(this);
cp5.addTextfield("Field 1")
.setPosition(40,50)
.setSize(200,40)
.setFont(font)
.setFocus(true)
.setColor(color(255))
.setAutoClear(true);
;
cp5.addTextfield("Field 2")
.setPosition(40,130)
.setSize(200,40)
.setFont(font)
.setAutoClear(false);
;
}
void draw() {
background(0);
}

How to change button image on click Unity

I got unlock button for game, it works,but I want to user himself can press button, and then button image change. As you can see in code below when score is >= 10, buttons image change immediately. How can I achieve that?
public GameObject lockBtn;
Image lockComp;
public Sprite myLockImage;
public Sprite myLockSecondImage;
/////////////////////////////////////////
lockComp = lockBtn.GetComponent<Image> ();
if (bestScore >= 10) {
lockComp.sprite = myLockImage;
}
else
{
lockComp.sprite = myLockSecondImage;
}
Simply add an event listener to your button so as to change the image.
lockComp.sprite = myLockSecondImage;
if (bestScore >= 10) {
lockBtn.GetComponent<Button>().onClick.addEventListener( OnLockButtonClicked ) ;
}
// ...
private void OnLockButtonClicked()
{
lockComp.sprite = myLockImage ;
}
Be carefull to not add this snippet of code into the Update function. Otherwise, you will add a new event listener every frame
Saving the "unlocked state" of the button into a file so as to not show the unlocked sprite when the user starts the game again could be a good idea.
Put your code in a file and class named ButtonScript and in a method named OnButtonClick()
public class ButtonScript : MonoBehaviour {
public void OnButtonClick(){
lockComp = lockBtn.GetComponent<Image> ();
if (bestScore >= 10) {
lockComp.sprite = myLockImage;
}
else
{
lockComp.sprite = myLockSecondImage;
}
}
}
Then add this script on your Button and in the OnClick event add a reference to the Button and his Method:

MousePressed drawing ellipse in tuio

Hi if anyone could help me out i would be very grateful, i have a sketch that will enable a user to draw graffiti on a screen with a wireless spray can. At the minute, with the tuio code installed, when the user presses the mouse button, a spray sound is made.. but i am having difficulty in the sketch creating an ellipse when presses the mouse button.
This is my code;
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer player;
AudioInput input;
/*TUIO processing demo - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten#iua.upf.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You shgould have greceived a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
TuioProcessing tuioClient;
import java.util.*; //ADD THIS LINE TO BE ABLE TO USE TUIOCLIENT WITH PROCESSING 2+
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
PFont font;
//declare a boolean to check mouse click
boolean drag = false;
int n=0;
int size[]= {20,40};
int sizeChosen;
boolean inside = false;
PImage bg;
PImage img;
PGraphics pg;
import controlP5.*;
ControlP5 cp5;
boolean mp = true;
void setup ()
{
size(1000,1000);//size(screen.width, screen.height).
smooth();
noStroke();
fill(0);
loop();
frameRate(30);
hint(ENABLE_NATIVE_FONTS);
font = createFont("Arial",18);
scale_factor = height/table_size;
// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);
ellipseMode( CENTER);
//smooth();
noCursor();
background(170);
bg = loadImage("brickwall.jpg");
background(bg);
img = loadImage("instructions.jpg");
image (img,30,40,THUMB_SIZE, THUMB_SIZE);
cp5 = new ControlP5(this);//screenshot button
cp5.addButton("Save Graffiti Artwork").setPosition(0,650).setSize(200,100);//screenshot details
minim = new Minim(this);
player = minim.loadFile("spray_close.wav");
input = minim.getLineIn();
}
void draw() {
if (mp = true);
ellipse(255,0,255,0);
background(bg);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
noStroke();
fill(0);
pushMatrix();
translate(tobj.getScreenX(width), tobj.getScreenY(height));
rotate(tobj.getAngle());
// ellipse(-obj_size/2, -obj_size/2, obj_size, obj_size);
popMatrix();
//fill(255);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
if (mousePressed) {
ellipse(255,0,255,0);
// printIn("Pink");
//mouse press 1
if (tobj.getSymbolID()==12) {
ellipse(255,0,255,0);
}
}
}
Vector tuioCursorList = tuioClient.getTuioCursors();
for (int i=0;i<tuioCursorList.size();i++) {
TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
Vector pointList = tcur.getPath();
if (pointList.size()>0) {
stroke(0, 0, 255);
TuioPoint start_point = (TuioPoint)pointList.firstElement();
;
for (int j=0;j<pointList.size();j++) {
TuioPoint end_point = (TuioPoint)pointList.elementAt(j);
line(start_point.getScreenX(width), start_point.getScreenY(height), end_point.getScreenX(width), end_point.getScreenY(height));
start_point = end_point;
}
stroke(192, 192, 192);
fill(192, 192, 192);
ellipse( tcur.getScreenX(width), tcur.getScreenY(height), cur_size, cur_size);
fill(0);
text(""+tcur.getCursorID(), tcur.getScreenX(width)-5, tcur.getScreenY(height)+5);
}
}
if (drag) //if drag = true, i-e if mouse click is holding, ellipse are drawing according the mouse's position
{
fill(#FF00FF); //black color
ellipse(mouseX, mouseY, 50,50); //draw ellipse with x and y mouse's position + size 10*10
//or line strokeWeight(3);stroke(0);line(mouseX,mouseY,25,25);
}
//draw palette size
for(n=0;n<2;n++)
{
fill(0);
ellipse(360,10+n*40,20*(n+1),20*(n+1));
}
}
//size selector
void mousePressed() {
//bDrawFullSize = true;
if (inside==true){
sizeChosen=size[n];
}
player.play();
mp = true;
}
void mouseReleased() {
//bDrawFullSize = true;
drag = false;
player.close();
//since close closes the file, we need to load the sound effect again.
player = minim.loadFile("spray_close.wav");
}
//function "drag and drop"
void mouseDragged() {
drag = true;
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called when a cursor is added to the scene
void addTuioCursor(TuioCursor tcur) {
println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
}
// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur) {
println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
}
// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur) {
println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void keyPressed() {
endRecord();
background(bg);
// exit();
}
public void saveScreen() {
saveFrame();
player.pause();
}
// returns true if mouse is inside this rectangle
boolean inside(int left, int top, int right, int bottom ) {
if (mouseX>left && mouseX<right && mouseY>top && mouseY<bottom ) {
return true;
}
else {
return false;
}
}
Anything under the function:
void mousePressed() {
//bDrawFullSize = true;
if (inside==true){
sizeChosen=size[n];
}
player.play();
mp = true;
}
is run every frame while mouse is pressed. So you would create an ellipse (or whatever you want) here.
You can put the ellipse generation function inside branching condition that's checking on the mouse pressed state:
if (mousePressed) {
ellipse(random(0,width),random(0,height),random(0,100),random(0,100));
}
put the condition at the end of the Draw function so that it overwrites any background element

javafx, TableView: detect a doubleclick on a cell

Given a TableView, i need to detect the doubleclick on a cell.
tableView.setOnMouseClicked(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
if(event.getClickCount()>1)
{
System.out.println("double clicked!");
}
}
});
How to determine the cell on which the mouse has been clicked?
Code example.
Run the "Example 12-11: Alternative Solution Of Cell Editing" of official tableview tutorial.
Replace the followings:
table.setEditable(false);
Callback<TableColumn, TableCell> cellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<Person, String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() > 1) {
System.out.println("double clicked!");
TableCell c = (TableCell) event.getSource();
System.out.println("Cell text: " + c.getText());
}
}
});
return cell;
}
};
No need to EditingCell since your cells are uneditable. Cell factory is used for cell rendering. So one can put any node/control other than default Labeled using cell's setGraphics() method. IMO you cannot access the default cell directly so you should define your own cell factory to be able to put event filter on cell.
JavaFX allows you to set up multiple listeners per cell (I'm not saying that this is good or bad, just that you can). Each listener will execute your code if you have code set to execute a response to the specific listener for the specific column/row. To capture cell mouse clicks, I use the following:
table.setEditable(true);
table.getSelectionModel().setCellSelectionEnabled(true); // selects cell only, not the whole row
table.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent click) {
if (click.getClickCount() == 2) {
#SuppressWarnings("rawtypes")
TablePosition pos = table.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
int col = pos.getColumn();
#SuppressWarnings("rawtypes")
TableColumn column = pos.getTableColumn();
String val = column.getCellData(row).toString(); System.out.println("Selected Value, " + val + ", Column: " + col + ", Row: " + row);
if ( col == 2 ) { ... do something ... }
if ( col == 5 ) { ... do something ... }
if ( col == 6 ) { ... do something ... }
if ( col == 8 ) { ... do something ... }
}
}
});
You can see from the above code, on the columns I want to do something based on a mouse click, I have code:
if ( col == <int> ) { ... do something ... }
I also have those columns set to not allow editing:
thisCol.setEditable(false);
The rows that I want to edit I have .setEditable(true) but don't have a response included with a mouse click.
Cell editing defaults to 2 mouse clicks. You can change the above code to capture different mouse events on a cell, so you can still edit the cell with 2 mouse clicks, or open a URL, dialog box, etc., with any other mouse event determined by you. TableView allows you to determine your own functionality based on your imagination and programming skills. You're not stuck with "I can either edit it, or fire a mouse event with it." You can do both :)
Add the following in the body of your listener, with T the type of your table record :
#SuppressWarnings("rawtypes")
ObservableList<TablePosition> cells = tableView.getSelectionModel().getSelectedCells();
for( TablePosition< T, ? > cell : cells )
{
System.out.println( cell.getColumn());
}// for
Create your cell using a cell factory and in the cell factory which creates the cell node, place an mouse event handler or filter on the node rather than the tableView.
In my case i use next code
tableViewObject.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (t.getClickCount() == 2 && getSelectedItem() != null) {
SMPBLogger.logInfo("Double cliked", Boolean.TRUE);
if (listener != null) {
listener.doubleClicked(tableViewObject.this,getSelectedItem());
}
}
}
});

Resources