bool checkForMouseTrigger(sf::Sprite &sprite, sf::RenderWindow &window) { int mouseX = sf::Mouse::getPosition().x; int mouseY = sf::Mouse::getPosition().y; sf::Vector2i windowPosition = window.getPosition(); if (mouseX > sprite.getPosition().x + windowPosition.x && mouseX < (sprite.getPosition().x + sprite.getGlobalBounds().width + windowPosition.x) && mouseY > sprite.getPosition().y + windowPosition.y + 30 && mouseY < (sprite.getPosition().y + sprite.getGlobalBounds().height + windowPosition.y + 30)) { if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { return true; } return false; } return false; }
bool ClickTrigger(sf::Sprite &av_Sprite, sf::RenderWindow &av_Window) { int mouseX = sf::Mouse::getPosition().x; int mouseY = sf::Mouse::getPosition().y; sf::Vector2i windowPosition = av_Window.getPosition(); if(mouseX > av_Sprite.getPosition().x + windowPosition.x && mouseX < ( av_Sprite.getPosition().x + av_Sprite.getGlobalBounds().width + windowPosition.x) && mouseY > av_Sprite.getPosition().y + windowPosition.y + 30 && mouseY < ( av_Sprite.getPosition().y + av_Sprite.getGlobalBounds().height + windowPosition.y + 30) ) { if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) { return true; } return false; } return false; }
//Transforms mouseclicks to Map positions sf::Vector2f screenToMapTransform(sf::Vector2i click, sf::RenderWindow& renderWindow, sf::Vector2f mapOffset) { int spriteWidth = 100; int spriteHeight = 64; //adapt click to top of map sf::Vector2f screenClick; screenClick.x = click.x - renderWindow.getPosition().x - (SCREENWIDTH/2) + mapOffset.x; screenClick.y = click.y - renderWindow.getPosition().y - (SCREENHEIGHT/2) + mapOffset.y; //screenClick.x = renderWindow.convertCoords(click).x; //screenClick.y = renderWindow.convertCoords(click).y; //transform screen position to map position float XPos = ((screenClick.x/(spriteWidth/2)) + (screenClick.y/(spriteHeight/2)))/2; float YPos = ((screenClick.y/(spriteHeight/2)) - (screenClick.x/(spriteWidth/2)))/2; sf::Vector2f position(XPos, YPos); return position; }
void MapEditor::update(sf::RenderWindow &window){ // Events sf::Event gEvent; while (window.pollEvent(gEvent)){ if (gEvent.type == sf::Event::Closed) window.close(); if (gEvent.type == sf::Event::MouseButtonPressed){ sf::Vector2i pixel_pos = sf::Mouse::getPosition(window); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); if (mMeny.menyClicked(sf::Vector2i(coord_pos))){ MapEditor::updateInsertType(); } else{ int i; sf::Sprite clickedTile; switch (gEvent.mouseButton.button){ case sf::Mouse::Left: if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)){ MapEditor::insertObject(sf::Vector2f(coord_pos.x, coord_pos.y)); } else{ sf::Vector2i pixel_pos = sf::Mouse::getPosition(window); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); clickedTile = determineSelectedTileInGrid(coord_pos, &mGrid); // Offset applied in all tiles, counteroffset needed here to match grid. MapEditor::insertObject(sf::Vector2f( clickedTile.getPosition().x + clickedTile.getLocalBounds().width / 2 , clickedTile.getPosition().y + clickedTile.getLocalBounds().height / 2)); } break; case sf::Mouse::Right: for (i = mEntities.size() - 1; i > -1; i--){ if (MapEditor::isSpriteClicked(mEntities[i]->getSprite(), &coord_pos)){ MapEditor::eraseEntity(i); break; } } for (i = mTerrains.size() - 1; i > -1; i--){ if (MapEditor::isSpriteClicked(mTerrains[i]->getSprite(), &coord_pos)){ MapEditor::eraseTerrain(i); break; } } for (i = mDecorations.size() - 1; i > -1; i--) { if (MapEditor::isSpriteClicked(mDecorations[i]->getSprite(), &coord_pos)) { MapEditor::eraseDecoration(i); break; } } break; case sf::Mouse::Middle: MapEditor::changeInsertType(); break; default: break; } } } if (gEvent.type == sf::Event::MouseWheelMoved){ mCamera.zoomCameraEDITOR(gEvent); sf::Vector2i pixel_pos = window.getPosition(); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); mMeny.resetMenusPos(coord_pos); } if (gEvent.type == sf::Event::KeyPressed){ switch (gEvent.key.code){ case sf::Keyboard::S: MapEditor::saveMap(); std::cout << "Saved..." << std::endl; break; case sf::Keyboard::Num0: MapEditor::changeInsertType(); break; case sf::Keyboard::L: MapEditor::changeLayer(); break; case sf::Keyboard::Delete: MapEditor::internalClear(); break; case sf::Keyboard::R: MapEditor::changeRotDirection(); break; case sf::Keyboard::E: MapEditor::changeEventType(); break; case sf::Keyboard::P: mEventSize.y += 30; break; case sf::Keyboard::O: mEventSize.y -= 30; break; case sf::Keyboard::I: mEventSize.x += 30; break; case sf::Keyboard::U: mEventSize.x -= 30; break; default: break; } } } // Camera movement & following menu if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W)){ mCamera.updateCamEDITOR(window, "Up"); sf::Vector2i pixel_pos = window.getPosition(); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); mMeny.resetMenusPos(coord_pos); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S)){ mCamera.updateCamEDITOR(window, "Down"); sf::Vector2i pixel_pos = window.getPosition(); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); mMeny.resetMenusPos(coord_pos); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)){ mCamera.updateCamEDITOR(window, "Right"); sf::Vector2i pixel_pos = window.getPosition(); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); mMeny.resetMenusPos(coord_pos); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A)){ mCamera.updateCamEDITOR(window, "Left"); sf::Vector2i pixel_pos = window.getPosition(); sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos); mMeny.resetMenusPos(coord_pos); } window.setView(mCamera.getTileView()); sf::Vector2f tileViewCoordPos = Toolbox::findCoordPos(sf::Vector2i(mCamera.getTileView().getCenter().x, 0), window); mLayerHandler.updateHud(mCamera.getTileView().getCenter(), tileViewCoordPos); }
//Common window get functions// sf::Vector2i window_get_position(){ return window.getPosition(); }