Here is what i am trying to do,
step 1 get the color of the mouse location
step 2 enter the loop
step 3 get the second color of the new mouse location
step 4 compare the two colors
error = no matter what the output of color 1 or color 2 the script below is stating true.
!^b::
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%
sleep, 5000
loop
{
MouseGetPos, MouseX, MouseY
PixelGetColor, color2, %MouseX%, %MouseY%
if (%color%=%color2%)
{
MsgBox, it matchs %color% = %color2%
sleep,5000
}
else
{
MsgBox, It dosnt match %color% != %color2%
sleep, 5000
}
}
Your problem is with if (%color%=%color2%)
Once you declare an if statement like this, you do not need to enclose variables with the %var%. Instead, you can replace the line with if (color==color2).
Full code:
!^b::
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%
sleep, 5000
loop
{
MouseGetPos, MouseX, MouseY
PixelGetColor, color2, %MouseX%, %MouseY%
if (color==color2)
{
MsgBox, it matchs %color% = %color2%
sleep,5000
}
else
{
MsgBox, It dosnt match %color% != %color2%
sleep, 5000
}
}
Related
I have an if statement that checks whether the group(consists of a rectangle and a text) I'm dragging is over other group(I check only "y" coordinate because "x" axis is locked). If so, change the color of the stationary group(rectangle) to "red". Else make all rectangles blue. Every group is in groupfield called "groups" which I'm iterating and adding events like this 'dragmove' + others. For some reason both blocks of code executes, idk why. Height of all rectangles is set by text height which is same everywhere(and is accessed by groups[].children[1].height(). Groups[i] is the moving(dragging) group and groups[j] are stationary. In my other event("dragstart") I change the "name" of that group to "true" others are false by default so when i iterate in my if doesn't triggers the dragging group. The code works partially, colors change how they should, but "else" is running even if I drag rectangle over other rectangle which is unwanted and needs fixing. Here's my code:
groups[i].on('dragmove', () => {
//lock top stage&bottom
if(groups[i].y() <= 0){
groups[i].y(0);
}
else if (groups[i].y() + groups[i].children[1].height() >= stage.height()){
groups[i].y(stage.height() - groups[i].children[1].height());
}
//end lock top stage&bottom
for (let j = 0; j < groups.length; j++) {
if(groups[i].y() + groups[i].children[1].height() >= groups[j].y() &&
groups[i].y() <= groups[j].y() + groups[j].children[1].height() &&
groups[j].name !== "true"){
if (groups[j].children[0].fill() === "red"){
continue;
}
console.log("if dragmove");
groups[j].children[0].fill("red");
}
else {
groups[j].children[0].fill("#aaf");
}
}
})
I want to use MouseDown in SuperCollider and am having a helluva time. Is it the case that only mouseDownAction actually works with anything? I want to be able to click anywhere on the screen, and have the mouse coordinates print, e.g., to the post window:
Server.default=s=Server.local;
s.boot;
s.mouseDownAction = { x = {|t_poll=0| var x_val, y_val;
x_val = {MouseX.kr};
y_val = {MouseY.kr};
Poll.kr(t_poll, [x_val, y_val], ["x_val", "y_val"]);
}.play };
s.mouseUpAction = { x.set(\t_poll,1) };
Of course, this does not work, because mouseDownAction seems reserved as a property of the 'View' class i.e. only clicking within a specific window, as the below working [albeit not quite what I want] code:
w = Window.new("Mouse Coordinates", Rect(1300,600,50,50));
b = Button.new(w,Rect(10,10,40,25)).states_([["off"],["on"]]);
b.mouseDownAction = { x = {|t_poll=0| var x_val, y_val;
x_val = {MouseX.kr};
y_val = {MouseY.kr};
Poll.kr(t_poll, [x_val, y_val], ["x_val", "y_val"]);
}.play };
b.mouseUpAction = { x.set(\t_poll,1) };
w.front;
Some things I want to know:
Can I modify the first snippet to actually work?
Is there a way to get MouseDown to work to give me these coordinates as I click anywhere on the screen?
How can I figure out how to 'get' the mouse coordinates (calling on which functions [already tried 'output', 'postln', &.c])?
Thanks!!!
There is a Ugen for the mouse button https://doc.sccode.org/Classes/MouseButton.html
This is the example from the linked helpfile:
(
SynthDef( \mousexyb, { |out=0|
var mousex, mousey, mousebutton;
mousex = MouseX.kr( 500, 1000 ); // this will determine the frequency of the sound (minimum value, maximum value, warp, lag)
mousey = MouseY.kr( 0, 0.3 ); // this will determine the amplitude of the sound
mousebutton = MouseButton.kr( 0, 1, 2 ); // this will turn the sound on or off (minimum value, maximum value, lag)
Out.ar( out, SinOsc.ar( mousex, 0, mousey ) * mousebutton );
}).add
)
What you could do is have the server generate an OSC message when the button is pressed and have the language listen for that.
Alright so I have this tic tac toe game I'm making with SDL and C++. I'm trying to implement AI into the game. I don't have a problem setting up the AI, but I have a problem making it where you can take turns. My problem is that when I make my move, I can just move as many times as I want before the AI moves. I want it so I can make my move, and I can't make my move again until the AI makes a move. No matter what I do it seems the turn taking doesn't work properly.
This is the class header
class Buttons
{
private:
SDL_Rect squares[8];
public:
Buttons();
void handle_input();
void load_Squares(SDL_Rect sqRects, SDL_Texture* squarTexs);
void show_Squares();
void AI_move();
int grid[9];
bool moveMade = true;
};
Here I check for mouse input, and depending on the location during the left button press, it sets the according grid value to equal 1, meaning it becomes displayed as a circle on the screen. I also make sure that the AI has made a move before it allows me to click.
void Buttons::handle_input()
{
double mouseX = 0, mouseY = 0;
if((event.type == SDL_MOUSEBUTTONDOWN))
{
//If left mouse button was clicked and AI has made a move
if(event.button.button == SDL_BUTTON_LEFT && moveMade == true)
{
//Get mouse location
mouseX = event.button.x;
mouseY = event.button.y;
//If mouse location is in particular square, set according grid value to 1
if((mouseX >= 0) && (mouseX < SCREEN_WIDTH / 3) && (mouseY >= 0) && (mouseY < SCREEN_HEIGHT / 3) && (grid[0] == 0))
{
grid[0] = 1;
moveMade = false;
}
//Basically does this for all other 9 grids
Here is my AI function, where I check to make sure the moveMade variable = false. Every time I make a move in the input function above, it sets moveMade to false, which means it should access this function, and only until it finishes this AI_move function should I be able to make a move again, because moveMade is set back equal to true.
void Buttons::AI_move()
{
if(moveMade == false)
{
AI_block(&moveMade);
AI_complete(&moveMade);
AI_rand(&moveMade);
moveMade = true;
}
}
Last is my show function, where I show a Circle(player) if the grid array value = 1, and I show the X(AI) if the grid value = 2.
void Buttons::show_Squares()
{
switch(grid[0])
{
case 1:
load_Squares(squares[0], circleTexture); break;
case 2:
load_Squares(squares[0], xTexture); break;
}
switch(grid[1])
{
//Does this all the way to grid[8]
}
Alright so my problem doesn't have to do with the AI dealing accordingly, as I haven't even set up my defense and offense functions. My problem is that I can make another move before the AI moves. Sorry if this is way too long, but if I could get any feedback on this that would be great.
Have you tried putting breakpoints at various points such as if(event.button.button == SDL_BUTTON_LEFT && moveMade == true) and then following the program through the see if moveMade ever actually gets changed to false?
You should also look at changing show_Squares() into a loop as there is a lot of repeated code using incremented indexes. Something like this:
void Buttons::show_Squares()
{
size_t array_size = sizeof(squares) / sizeof(int); //gets the number of elements in the array
for(size_t i = 0; i < array_size; i++)
{
switch(grid[i])
{
case 1:
load_Squares(squares[i], circleTexture); break;
case 2:
load_Squares(squares[i], xTexture); break;
}
}
}
Hi so I'm trying to make it so a little UFO bitmap (drawing/painting already taken care of) can be dragged around the screen. I can't seem to make the UFO position update and then redraw repeatedly from the MouseButtonDown() function (simplified code for mouse event handler). Any suggestions on detecting the dragging and redrawing accordingly? Code is below for relevant functions:
void MouseButtonDown(int x, int y, BOOL bLeft)
{
if (bLeft)
{
while(_bMouseMoving == true && _bMouseDragRelease == false) {
_iSaucerX = x - (_pSaucer->GetWidth() / 2);
_iSaucerY = y - (_pSaucer->GetHeight() / 2);
InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
}
// Set the saucer position to the mouse position
_iSaucerX = x - (_pSaucer->GetWidth() / 2);
_iSaucerY = y - (_pSaucer->GetHeight() / 2);
}
else
{
// Stop the saucer
_iSpeedX = 0;
_iSpeedY = 0;
}
}
void MouseButtonUp(int x, int y, BOOL bLeft)
{
_bMouseDragRelease = true;
}
void MouseMove(int x, int y)
{
_bMouseMoving = true;
}
To clarify what chris said, you're only going to get the WM_xBUTTONDOWN message once, and you'll need to use that to toggle a dragging state that you can query when you recieve a WM_MOUSEMOVE message.
When you get the mouse move message during a dragging state, you'll want to invalidate the rectangle surrounding where the ufo was, and the rectangle surrounding where it is.
Invalidating a rectangle causes WM_PAINT messages, where you redraw whatever was behind the ufo, and the ufo in it's new place.
Or you could cheat and make the UFO a cursor when you're dragging :)
I am trying to change a C++ project, which is currently drawing some lines when I click on the view port. This functionality is perfectly fine, but what I am trying to change is when I click on "UP" or "Down" keys the color for next lines to change. Currently if I click on those keys the color changes for all the lines including the old ones (already drawn).
Please give me an idea of what to do. Here is some of the code:
void drawPrimitive() {
Vertex *temp;
// Set the primitive color
glColor3fv(primitiveColor);
// Set the point size in case we are drawing a point
if (type == POINT)
glPointSize(pointSize);
// Display results depending on the mode
glBegin(mode);
for(temp = head; temp != NULL; temp = temp->np)
{
if (smoothShading)
glColor3f(temp->r, temp->g, temp->b);
glVertex2f(temp->x, temp->y);
}
glEnd(); }
void mouse(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
float pointX, pointY;
pointX = (float)x/window_width * world_width;
pointY = (float)(window_height - y)/window_height * world_height;
// Add a vertex to the list of vertices...
addVertex(&head, &tail, pointX, pointY, 0.0f, primitiveColor[0], primitiveColor[1], primitiveColor[2]);
// automatically calls the display function
glutPostRedisplay();
}
else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
{
deleteVertex(&head, &tail);
glutPostRedisplay();
} }
void special(int key, int x, int y) {
switch (key)
{
// change primitive color
case GLUT_KEY_UP :
changePrimitiveColor(1);
break;
case GLUT_KEY_DOWN :
changePrimitiveColor(-1);
break;
}
glutPostRedisplay(); }
void changePrimitiveColor(int step) {
primitiveColorId += step;
if (primitiveColorId < 0)
primitiveColorId = COLOR_COUNT - 1;
if (primitiveColorId >= COLOR_COUNT)
primitiveColorId = 0;
setColor(primitiveColor, primitiveColorId); }
Your code is sort of unclear; is primitiveColor a global variable?
Assuming you're calling drawPrimitive() for all your lines each redraw, the same primitiveColor would be used for all the lines. As soon as you change the color when you press up or down, the redisplay function is called, and all the lines will be redrawn using the same color.
What you might want to do is have a list containing both the primitives and their respective colors. When you iterate through this list, you can set a color for each line.
Keep in mind that OpenGL behaves like a statemachine. If you set a color, everything you draw after it will be drawn with that color. OpenGL will not remember that your other elements had a different color and you want to keep it like that. You have to do the bookkeeping.
So each time your content is drawn, you will have to explicitly state which elements have which color.