class Input { int selectedSquare; int selectedSquare2; void Initialize() { selectedSquare = -1; selectedSquare2 = -1; } void Update() { //reset the secondary selection every frame selectedSquare2 = -1; //check for secondary hover if( selectedSquare != -1 ) { selectedSquare2 = CheckForSquareHover( mouseX, mouseY ); if( selectedSquare2 != -1 ) { //if we're hovering over a second square, turn on the one frame selection COLORS[ INPUT.selectedSquare2 ].m_HoverSelected = true; } } } void Draw() { if( selectedSquare != -1 ) { stroke( GAME_COLORS[ COLORS[selectedSquare].m_ColorIndex ] ); fill( GAME_COLORS[ COLORS[selectedSquare].m_ColorIndex ] ); line( COLORS[selectedSquare].m_Position.x + COLORS[selectedSquare].m_HalfWidth, COLORS[selectedSquare].m_Position.y + COLORS[selectedSquare].m_HalfHeight, mouseX, mouseY ); } } int CheckForSquareHover( int mX, int mY ) { //determine which grid cell we clicked in int cellIndexX = mX / GRID.m_CellWidth; int cellIndexY = mY / GRID.m_CellWidth; cellIndexX = CapInt( cellIndexX, 0, GRID.m_NumCellsX - 1 ); cellIndexY = CapInt( cellIndexY, 0, GRID.m_NumCellsY - 1 ); int cellIndex = cellIndexY * GRID.m_NumCellsX + cellIndexX; //check for colored square int i; for( i = 0; i < SQUARES_PER_GRID_CELL; ++i ) { if( GRID.m_Cells[cellIndex].m_ColorSquares[i] != -1 ) { int squareIndex = GRID.m_Cells[cellIndex].m_ColorSquares[i]; //check for the bounds of this square if( mX > COLORS[squareIndex].m_Position.x && mX < COLORS[squareIndex].m_Position.x + COLORS[squareIndex].m_Width && mY > COLORS[squareIndex].m_Position.y && mY < COLORS[squareIndex].m_Position.y + COLORS[squareIndex].m_Height ) { //yup, we're over it.. return the index return( squareIndex ); } } } return( -1 ); } } void mousePressed() { if( mouseButton == LEFT ) { INPUT.selectedSquare = -1; INPUT.selectedSquare = INPUT.CheckForSquareHover( mouseX, mouseY ); if( INPUT.selectedSquare != -1 ) { COLORS[ INPUT.selectedSquare ].m_Selected = true; COLORS[ INPUT.selectedSquare ].m_SelectedFrame = 0; } } if( mouseButton == RIGHT ) { } } void mouseReleased() { //if we have a square selected, we have some work to do if( INPUT.selectedSquare != -1 ) { if( INPUT.selectedSquare2 != -1 ) { int combinedIndex = CombineColors( COLORS[INPUT.selectedSquare].m_ColorIndex, COLORS[INPUT.selectedSquare2].m_ColorIndex ); if( combinedIndex != -1 && combinedIndex != COLORS[INPUT.selectedSquare].m_ColorIndex ) { COLORS[INPUT.selectedSquare].m_ColorIndex = combinedIndex; COLORS[INPUT.selectedSquare].m_Changed = true; COLORS[INPUT.selectedSquare2].m_ColorIndex = combinedIndex; COLORS[INPUT.selectedSquare2].m_Changed = true; } COLORS[INPUT.selectedSquare].m_Selected = false; } else { //turn off selection on square COLORS[INPUT.selectedSquare].m_Selected = false; } } INPUT.selectedSquare = -1; } void mouseDragged() { } void keyReleased() { if( keyCode == ' ' ) { PAUSED ^= true; } if( key == 'r' ) { RESET(); } }