Processing2 selectInput() P3D clash - opengl

I've been studying this thread
Processing 2.0 - Open file dialog
with regard to the use of selectinput().
I'm hoping to import point data to construct some 3d plots.
I can import the data and construct the plot ok but when trying to use
selectinput() to choose a file I run into trouble.
The difficulty I am having is that selectinput() appears to be incompatible with a P3D
window.
Using OS X10.10
for example This code works
void setup() {
size(400, 400,P3D); //3Dgraphics specified
background(0);
stroke(255);
frameRate(20);
}
void draw() {
noFill();
ellipse(mouseX, mouseY, 90, 90);
and this works
String [] myInputFileContents ;
String myFilePath;
void setup() {
selectInput("Select a file : ", "fileSelected");
while (myInputFileContents == null) {//wait
// println("wait"); //If there is nothing inside the curly brackets
//delay(3000); //this doesn't work
size(400, 400 );/// If P3D is added it won't work
background(0);
//smooth();
stroke(255);
frameRate(25);
}
}
void draw() {
box(mouseX, mouseY, 150);
println("Selected at this point " + myFilePath);
}
void mousePressed() {
selectInput("Select a file : ", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("no selection so far...");
}
else {
myFilePath = selection.getAbsolutePath();
myInputFileContents = loadStrings(myFilePath) ;// this moves here...
println("User selected " + myFilePath);
}
}
but if
size(400, 400);
is changed to
size(400,400,P3D);
The frame displays but it won't draw.
Could someone point me to the answer?

It seems that this problem is some kind of frame loading problem. This "hackish" approached worked for me under OS X 10.10 Yoesemite. It should also work under windows. Oh and whenever you use selectInput() please use mouseReleased so the function only gets called once :)
Setup() gets called twice for whatever reason, this is why we have the didPreSelect boolean.
You call mousePressed instead of mouseReleased
The loop already had the screen selection in it, this is why i changed the loop.
The delay also has problems, this is why i added the delay inside the while-loop.
Man that was quite the challenge, +1 for asking.
PS: I added an exit() if you press cancel the first time you open the application, which should be more convenient.
String [] myInputFileContents ;
String myFilePath;
boolean didPreSelect = false;
boolean secondPress = false;
void setup() {
if (didPreSelect == false)
{
didPreSelect = true;
selectInput("Select a file : ", "fileSelected");
}
//frame = null;
while (myInputFileContents == null || myInputFileContents.length < 2) {//wait
delay(1000);
}
//this doesn't work
size(400, 400, P3D);
background(0);
//smooth();
stroke(255);
frameRate(25);
}
void draw() {
box(mouseX, mouseY, 150);
//println("Selected at this point " + myFilePath);
}
void mouseReleased() {
if(secondPress == false) secondPress = true;
selectInput("Select a file : ", "fileSelected");
}
void fileSelected(File selection) {
if (frameCount < 50)
{
if (selection == null) {
println("no selection so far...");
if(secondPress == false) exit();
} else {
myFilePath = selection.getAbsolutePath();
myInputFileContents = loadStrings(myFilePath) ;// this moves here..
println("User selected " + myFilePath);
}
}
}

Related

Every time I upload my (play melody-)code to the arduino and I press the button, it skips the same notes in the array. How come?

I made a code where, for every time I press the button, it plays the next note in the song. But the problem is, for some reason it keeps skipping the same notes in the array.
For example (my code):
int mariomelody[] = {
NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_C5, NOTE_E5, NOTE_G5, NOTE_G4,
//THERE ARE TWO 'NOTE_E5's BECAUSE THE THIRD NOTE DOESNT PLAY
NOTE_C5, NOTE_G4, NOTE_E4, NOTE_A4, NOTE_B5, NOTE_AS4, NOTE_A4,
NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5, NOTE_E5,
NOTE_C5, NOTE_D5, NOTE_B4,
//buttonPin sits at DIG. 12
int buttonPin = 12;
void setup()
{
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
for (int i = 0; i < sizeof(mariomelody) / sizeof(mariomelody[0]); i++)
{
while (digitalRead(buttonPin) == HIGH)
{
tone(8, mariomelody[i], 20);
}
while (digitalRead(buttonPin) == LOW)
{
}
}
Everybody knows the mario melody right. The first three notes are the same at different speeds (4 is half the speed of 8); E(4) E(4) E(8). For some reason it just skips the third NOTE_E5 so I just put another NOTE_E5 there and now it works "just fine".
Does anybody know why it skips the notes? Is it my code?
I don't know the mario melody, but anyway I think your code is wrong:
Try this:
void loop()
{
for (int i = 0; i < sizeof(mariomelody) / sizeof(mariomelody[0]); i++)
{
while (digitalRead(buttonPin) == LOW)
{
// wait until button is pressed
}
tone(8, mariomelody[i], 20);
while (digitalRead(buttonPin) == HIGH)
{
// wait until button is released
}
}
}

OpenGL - Left mouse click keeps removing my grid

I have created a drawGrid() function that draws a squared grid along my X and Y axis, which works fine. I have then created a menu() function (called in the main()), that toggles the grid on and off, here's the code for that:
void menu(int item)
{
switch (item)
{
case MENU_SWITCH_OFF_GRID:
{
if (gridActive == true)
{
gridActive = true;
}
}
break;
case MENU_SWITCH_ON_GRID:
{
if (gridActive == true)
{
gridActive = false;
}
}
break;
default:
{ /* Nothing */ }
break;
}
glutPostRedisplay();
return;
}
}
The menu switch works fine, as I have created a global variable called gridActive without a true or false value so it doesn't reset each time, that way it can be accessed in my display() function like so:
if (gridActive != true)
{
drawGrid();
gridActive = true;
}
All of this works just fine.
What's my issue?
My issue is, whenever I click the left mouse button, my grid disappears, which I don't want. So I've made a mouse() function like this:
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN)
{
exit(0); // this has been added to see if
// my program will exit!
}
break;
To test if my program exits when I click the left mouse, and it does.
So instead of using exit(0); what code can i put here so that my grid doesn't disappear when I click the left mouse button? Or is the issue beyond that?
UPDATE:
Here's the mouse function:
void mouse(int button, int state, int x, int y)
{
// these have simply been set-up for me to use
// in the future
switch (button)
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN)
{
}
break;
case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN)
{
}
break;
default: break;
}
}
Based on your code:
if (gridActive != true)
{
drawGrid();
gridActive = true;
}
You only draw the grid when gridActive is false. However, after every time you draw it, you set gridActive=true which will then stop it from being drawn.
Without more of your code, it's impossible to tell exactly what's going on, but these lines may not be doing what you think they are, and that may be causing some issues.
This never does anything.
if (gridActive == true)
{
gridActive = true;
}
This:
if (gridActive == true)
{
gridActive = false;
}
is the same as:
gridActive = false;
In order to tell what's going on, though, we need to know what happens when you click your mouse button when the exit call isn't there, but you didn't post that code yet.
Also, i don't quite know what you mean by:
I have created a global variable called gridActive without a true or false value so it doesn't reset each time
but it sounds like you made an uninitialized global variable and expect that it has some specific meaning because it's uninitialized?

Cannot Adapt BackgroundWorker progress percentage to the real percentage of the task

I wrote a program that reads a short video then writes the couple hundred frames with reduced FPS. It worked fine, but the "if" loop was blocking the UI. I tried to create a backgroudWorker to handle the "if" Loop , while the UI values could be displayed and the process could be interrupted by a "Cancel" button.
My problem is: I cannot make the "if" loop go through all the frames in:
for (i = 0; i < frames_number; i++)
to the last frame! it stops at 100, even though I tried multiple solutions to convert the progress of "i" to percentage like:
progress = System::Convert::ToInt32((100*(i / frames_number)));
or
progress = (int)((float)i / (float)frames_number *100);
Here are the important snippets of the code:
private: System::Void CreateSlowMo_Click(System::Object^ sender, System::EventArgs^ e) {
VideoCapture inputVideo(videoToOpenNameStr);
if (!inputVideo.isOpened()) {
MessageBox::Show(L"Error");
}
fps = int(inputVideo.get(CAP_PROP_FPS)); // get the frame rate of the video
frames_number = int(inputVideo.get(CAP_PROP_FRAME_COUNT)); // get the total frames in the video
this->progressBar1->Maximum = frames_number; // Initilize the progress bar's maximum to the number of frames
outputVideo.open(name, CV_FOURCC('M', 'J', 'P', 'G'), fps_wanted, resolution, true); // Create an output video
if (outputVideo.isOpened())
{
this->backgroundWorker1->RunWorkerAsync(progress); // Delegating the blocking operations to the background worker
}
else {
MessageBox::Show(L"Error creating the video");
CreateSlowMo_button->Enabled = true;
}
}
backgroundWorker1_DoWork:
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
progress = (int)e->Argument; // get the intial value of the argument
for (i = 0; i < frames_number; i++)
{
inputVideo >> source; // read frame
if (backgroundWorker1->CancellationPending) { // check if the User clicked on "Cancel"
e->Cancel = true;
break; // Stop the convertion
}
if (source.empty()) { // we reached the last frame
break;
// CreateSlowMo_button->Enabled = true;
}
if (progress > 100)
{ // verify we didn't exceed 100% of the task
backgroundWorker1->ReportProgress(100);
break;
}
outputVideo << source; // write the frame to the output video
progress = (int)(((float)i / (float)frames_number)* 100);
backgroundWorker1->ReportProgress(progress);
}
this->backgroundWorker1->ReportProgress(100);
e->Result = progress;
}
backgroundWorker1_ProgressChanged:
private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e) {
this->progressBar1->Value = e->ProgressPercentage;
toolStripStatusLabel1->Text = "Processing..." + progressBar1->Value.ToString() + "%";
richTextBox6->Text = e->ProgressPercentage.ToString();
}
backgroundWorker1_RunWorkerCompleted:
private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e) {
// in case the background process is finished with a cancel
if (e->Cancelled)
{
toolStripStatusLabel1->Text = "Task Cancelled";
}
// check if an error occurred in the backgroud process
else if (e->Error != nullptr)
{
toolStripStatusLabel1->Text = "Error while creating the SlowMo";
}
else
{ // task completed normally
this->toolStripStatusLabel1->Text = "SlowMo Created successfully!";
this->cancel_button->Enabled = false;
MessageBox::Show(L"Finished creating the sloMo");
this->toolStripStatusLabel1->Text = "Done";
this->CreateSlowMo_button->Enabled = true;
this->richTextBox5->Text = "value " + e->Result;
}
}
Here are some screenshots from the resulats (I added text boxes to visualise the values of 'i', 'progress' and e->result) 'i' and 'progress' are int=0, definded in files 'variables.h'
extern int i ;
extern int progress;
and 'variables.cpp':
int i = 0;
int progress =0;
Have you checked the value of frames_number? Or if any of your if statements within the for loop becomes true anytime during the execution?
I'm suspecting frames_number gets an incorrect value prior to the for loop, or source.empty() becomes true.

Connecting SLOTS in Qt

I have a context menu and edit menu. I want to connect context menu slots with the edit menu.
Like EDIT menu has menu Items: cut, copy and paste
My context menu slots are:
void CadGraphicsScene::cut(getEntity *obj)
{
// id of item pasted is kept same as that of the item being cut
removeItem(obj);
clipboardStack::instance()->push(obj->clone(contextItemId));
}
void CadGraphicsScene::copy(getEntity *obj)
{
// id of item pasted is one more than total number of items in the scene
clipboardStack::instance()->push(obj->clone(++id));
}
void CadGraphicsScene::paste(const QPointF &pos)
{
// gets the items cut/copy from clipboardStack to paste
getEntity *pasteEntity = clipboardStack::instance()->pop();
if (pasteEntity->type() == Point::Type)
{
Point *itemPtr = dynamic_cast<Point *>(pasteEntity);
itemPtr->position = pos;
drawEntity(itemPtr);
}
if (pasteEntity->type() == Line::Type)
{
Line *itemPtr = dynamic_cast<Line *>(pasteEntity);
itemPtr->startP = pos;
/* calculates difference between startP of line being cut/copy and line
* being pasted for proper pasting of line
*/
differenceX = itemPtr->startP.x() - lineStartPoint.x();
differenceY = itemPtr->startP.y() - lineStartPoint.y();
itemPtr->endP = QPointF(lineEndPoint.x() + differenceX,
lineEndPoint.y() + differenceY);
drawEntity(itemPtr);
}
if (pasteEntity->type() == Circle::Type)
{
Circle *itemPtr = dynamic_cast<Circle *>(pasteEntity);
itemPtr->centerP = pos;
drawEntity(itemPtr);
}
if (pasteEntity->type() == Ellipse::Type)
{
Ellipse *itemPtr = dynamic_cast<Ellipse *>(pasteEntity);
itemPtr->p1 = pos;
drawEntity(itemPtr);
}
if (pasteEntity->type() == Text::Type)
{
Text *itemPtr = dynamic_cast<Text *>(pasteEntity);
itemPtr->position = pos;
drawEntity(itemPtr);
}
setMode(NoMode);
}
//context menu actions
void CadGraphicsScene::menuAction(QAction *action)
{
if (action == cutAction)
{
cut(static_cast<getEntity *>(contextItem));
}
else if (action == copyAction)
{
copy(static_cast<getEntity *>(contextItem));
}
else if (action == pasteAction)
{
paste(contextPosition);
}
}
How can the same be done from edit menu? How can the same slots be used?
for cut in the edit menu I made another slot:
void CadgraphicsScene::cut()
{
cutAction
}
connect(actionCut, SIGNAL(triggered), this, SLOT(cut()));
If you want to reuse the same slots in your application's Edit menu, just use existing cutAction, copyAction and pasteAction actions when constructing it. So if you has established the connections for that actions the same slots will be called both when user triggers actions from context menu and from Edit menu.

SFML - Rendering a character when the key is pressed?

So I've got this program that is supposed to imitate a console (with a little coding help from this user):
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
sf::Color fontColor;
sf::Font mainFont;
sf::Clock myClock;
bool showCursor = true;
void LoadFont() {
mainFont.loadFromFile("dos.ttf");
fontColor.r = 0;
fontColor.g = 203;
fontColor.b = 0;
}
int main() {
sf::RenderWindow wnd(sf::VideoMode(1366, 768), "SFML Console");
wnd.setSize(sf::Vector2u(1366, 768));
LoadFont();
sf::Text myTxt;
myTxt.setColor(fontColor);
myTxt.setString("System Module:");
myTxt.setFont(mainFont);
myTxt.setCharacterSize(18);
myTxt.setStyle(sf::Text::Regular);
myTxt.setPosition(0, 0);
while(wnd.isOpen()) {
sf::Event myEvent;
while (wnd.pollEvent(myEvent)) {
if (myEvent.type == sf::Event::Closed) {
wnd.close();
}
if (myEvent.type == sf::Event::KeyPressed) {
if (myEvent.key.code == sf::Keyboard::Escape) {
wnd.close();
}
}
}
wnd.clear();
if (myClock.getElapsedTime() >= sf::milliseconds(500)) {
myClock.restart();
showCursor = !showCursor;
if(showCursor == true) {
myTxt.setString("System Module:_");
} else {
myTxt.setString("System Module:");
}
}
wnd.draw(myTxt);
wnd.display();
}
}
I need to be able to let the user type a key on the keyboard, and then render that key on the screen. I'm thinking about using an std::vector of sf::Keyboard::Key, and use a while loop to check what the key is (looping through the std::vector<sf::Keyboard::Key>) without using a whole bunch of if statements, but I don't exactly know how to handle that yet, so I'd like to know if there is an easier way to accomplish my main goal. Suggestions? Comments?
Thank you for your time,
~Mike
SFML has a nice feature for this, sf::Event::TextEntered (tutorial). That is typically what you want and it avoids you to do crazy things to interpret the text entered by the user.
Stock your text entered by adding every character into a sf::String (rather than std::string, it may deal better with sfml's unicode types ~ not sure, but that would need a little check) which is then the perfect type for sf::Text::setString !
Don't hesitate to look at the docs, it has further documentation in every classes' page.
Example:
sf::String userInput;
// ...
while( wnd.pollEvent(event))
{
if(event.type == sf::Event::TextEntered)
{
/* Choose one of the 2 following, and note that the insert method
may be more efficient, as it avoids creating a new string by
concatenating and then copying into userInput.
*/
// userInput += event.text.unicode;
userInput.insert(userInput.getSize(), event.text.unicode);
}
else if(event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::BackSpace) // delete the last character
{
userInput.erase(userInput.getSize() - 1);
}
}
}