void Gui::notify(const SDL_Point& clickLocation) const { for (std::vector<GuiElement*>::const_iterator it = mElements.begin(); it != mElements.end(); ++it) { if (SDL_PointInRect(&clickLocation, &((*it)->getRectangle()))) { (*it)->callback(); return; } } }
bool Rect::contains(const Vector2i& pos) const { #if SDL_VERSION_ATLEAST(2, 0, 4) SDL_Rect sdl_rect; SDL_Point sdl_point; return SDL_PointInRect(pos.copyInto(&sdl_point), this->copyInto(&sdl_rect)); #else const i32_t rw = this->x + this->width; const i32_t rh = this->y + this->height; return pos.x >= this->x && pos.y >= this->y && pos.x < rw && pos.y < rh; #endif }
std::string Gui::notifyBrowser(const SDL_Point & clickLocation) { for (std::vector<BrowserButton>::const_iterator it = mBrowserPanel.mButtons.begin(); it != mBrowserPanel.mButtons.end(); ++it) { if (SDL_PointInRect(&clickLocation, &it->mRect)) { mIsBrowserActive = false; return it->mText; } } return ""; }
void InteractiveSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, entityx::TimeDelta dt) { SDL_Event event; bool mouseUp = false; auto it = entities.entities_with_components<MouseC>().begin(); PositionC *mousePos = (*it).component<PositionC>().get(); while (SDL_PollEvent(&event) != 0) { switch (event.type) { case SDL_MOUSEMOTION: mousePos->x = event.motion.x; mousePos->y = event.motion.y; break; case SDL_MOUSEBUTTONUP: mouseUp = true; break; default: // printf("event %d \n", event.type); break; } } if (mouseUp) { PositionC *positionC; RenderC *renderC; InteractiveC *interactiveC; SDL_Rect sprite; SDL_Point mousePoint = {(int) mousePos->x, (int) mousePos->y}; it = entities.entities_with_components<CameraC>().begin(); PositionC *cameraPos = (*it).component<PositionC>().get(); for (entityx::Entity entity : entities .entities_with_components<PositionC, RenderC, InteractiveC>()) { positionC = entity.component<PositionC>().get(); renderC = entity.component<RenderC>().get(); interactiveC = entity.component<InteractiveC>().get(); sprite.x = (int) (positionC->x - renderC->anchor.x - cameraPos->x); sprite.y = (int) (positionC->y - renderC->anchor.y - cameraPos->y); sprite.h = renderC->h; sprite.w = renderC->w; if (SDL_PointInRect(&mousePoint, &sprite)) { // todo: do depth search to select only upper one (*interactiveC->onClick)(entity, entities, events); return; } } } }
std::shared_ptr<GuiWindow> GuiWindow::getWindowAtPos(SDL_Point pos, WindowList *captured) { std::shared_ptr<GuiWindow> out = NULL; if ( SDL_PointInRect(&pos, &getPosition())) { if (captured != NULL) { captured->push_back(this); } out = this->shared_from_this(); for (auto &child : m_children) { if ( SDL_PointInRect(&pos, &child->getPosition())) { out = child->getWindowAtPos(pos, captured); } } } return out; }
bool Actor::isInActor(const SDL_Point &p) { if (!_currentCostume) { return false; } SDL_Rect rect = _currentCostume->rect(); rect.x *= _scale; rect.y *= _scale; rect.w *= _scale; rect.h *= _scale; rect.x += _pos.x; rect.y += _pos.y; return SDL_PointInRect(&p, &rect); }
void CAnchorScenery::handleGrabAnchor(const SDL_Point* m) { for (int i = CScenery::sceneryList.size() - 1; i >= 0; i--) { if (layer == CScenery::sceneryList[i].layer) { double rel_x = CCamera::CameraControl.trueXToRel(CScenery::sceneryList[i].true_x, anch_z); double rel_y = CCamera::CameraControl.trueYToRel(CScenery::sceneryList[i].true_y, anch_z); int w = CScenery::sceneryList[i].srcR.w; int h = CScenery::sceneryList[i].srcR.h; SDL_Point w_pos = CCamera::CameraControl.GetWinRelPoint(rel_x, rel_y); SDL_Rect w_rec = CAsset::getRect(w_pos.x, w_pos.y, w, h); if (SDL_PointInRect(m, &w_rec)) { anch_x = CScenery::sceneryList[i].true_x; anch_y = CScenery::sceneryList[i].true_y; break; } } } CInterrupt::removeFlag(INTRPT_GRAB_ANCH); }
static SDL_HitTestResult SDLCALL hitTest(SDL_Window *window, const SDL_Point *pt, void *data) { int i; int w, h; for (i = 0; i < numareas; i++) { if (SDL_PointInRect(pt, &areas[i])) { SDL_Log("HIT-TEST: DRAGGABLE\n"); return SDL_HITTEST_DRAGGABLE; } } SDL_GetWindowSize(window, &w, &h); #define REPORT_RESIZE_HIT(name) { \ SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \ return SDL_HITTEST_RESIZE_##name; \ } if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) { REPORT_RESIZE_HIT(TOPLEFT); } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) { REPORT_RESIZE_HIT(TOP); } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) { REPORT_RESIZE_HIT(TOPRIGHT); } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) { REPORT_RESIZE_HIT(RIGHT); } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) { REPORT_RESIZE_HIT(BOTTOMRIGHT); } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) { REPORT_RESIZE_HIT(BOTTOM); } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) { REPORT_RESIZE_HIT(BOTTOMLEFT); } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) { REPORT_RESIZE_HIT(LEFT); } SDL_Log("HIT-TEST: NORMAL\n"); return SDL_HITTEST_NORMAL; }
bool point_in_rect(int x, int y, const SDL_Rect& rect) { SDL_Point p {x, y}; return SDL_PointInRect(&p, &rect) != SDL_FALSE; }
void doMouseEvent(struct _uistate *ui, sim_Sim *s) { static int laststate, clicked; Uint32 buttons; SDL_Point loc; buttons = SDL_GetMouseState(&(loc.x), &(loc.y)); clicked = laststate & SDL_BUTTON(SDL_BUTTON_LEFT) && !(buttons & SDL_BUTTON(SDL_BUTTON_LEFT)); buttons &= SDL_BUTTON(SDL_BUTTON_LEFT); ui->hoverx = -1; if(SDL_PointInRect(&loc, &toolbox)) {//Handle clicks in the ui ui->hovery = (loc.y - toolbox.y) / 100; ui->hoverx = (loc.x - toolbox.x) / 100; switch(ui->hoverx) { case 0://Tools if(buttons) ui->toolselected = ui->hovery; break; case 1://None break; case 2://demo if(buttons) ui->demoselected = ui->hovery; break; case 3://Pause if(clicked && ui->hovery == 0) ui->paused = !ui->paused; break; } //Each button in the toolbox is 100px high } else if(SDL_PointInRect(&loc, &gridviewport)) {//Handle clicks in the wave grid switch(ui->toolselected) { case TOOL_ADD: //Add water if(!clicked) break; loc.x -= gridviewport.x; loc.y -= gridviewport.y; loc.x /= CELLSIZE; loc.y /= CELLSIZE; cellAt(loc.x, loc.y, s).height += 200; //cellAt(loc.x - 1, loc.y, s).height += 200; //cellAt(loc.x + 1, loc.y, s).height += 200; //cellAt(loc.x, loc.y - 1, s).height += 200; //cellAt(loc.x, loc.y + 1, s).height += 200; break; case TOOL_REMOVE: if(!clicked) break; loc.x -= gridviewport.x; loc.y -= gridviewport.y; loc.x /= CELLSIZE; loc.y /= CELLSIZE; cellAt(loc.x, loc.y, s).height = 0; //Remove water break; } } else {//??? } laststate = buttons; }
void Loop::handleMouseEvents(const SDL_Event& e) { //Mouse events if (e.type == SDL_MOUSEBUTTONDOWN) { switch (e.button.button) { case SDL_BUTTON_LEFT: mIsLeftClickPressed = true; break; case SDL_BUTTON_RIGHT: mIsRightClickPressed = true; break; default: break; } } if (e.type == SDL_MOUSEBUTTONUP) { switch (e.button.button) { case SDL_BUTTON_LEFT: mIsLeftClickPressed = false; break; case SDL_BUTTON_RIGHT: mIsRightClickPressed = false; break; default: break; } } int x, y; SDL_GetMouseState(&x, &y); SDL_Point clickLocation = { x, y }; //Left click if (mIsLeftClickPressed) { if (mGui.isBrowserActive()) { if (SDL_PointInRect(&clickLocation, &mGui.getBrowserRect())) { std::string filename = mGui.notifyBrowser(clickLocation); if (filename != "") { mBlocks = loadCoordinates(filename); mIsDelaunayUpToDate = false; mGui.print("Loaded: " + filename); } } } else { if (SDL_PointInRect(&clickLocation, &mGui.mRect)) { mIsLeftClickPressed = false; mGui.notify(clickLocation); } else { mDataStructure.createBoid(x, y); updateBoidCounter(); } } } //Right click if (mIsRightClickPressed) { if (!mGui.isBrowserActive() && !SDL_PointInRect(&clickLocation, &mGui.mRect)) { switch (mObjectPlacement) { case 0: mFood.setPosition(x, y); break; case 1: createBlock(x, y); mIsDelaunayUpToDate = false; break; case 2: if (!isInBlockProximity(x, y)) { mPathFinish = Vector2(x, y); } break; } } } }
void UI_SDL::mainloop() { SDL_Event e; //droppedframes = MAXDROPPEDFRAMES; draw(); SDL_RenderPresent(ren); //fprintf(stderr, "b"); /* if(SDL_GetTicks() - updatetime > 1000) { updatetime = SDL_GetTicks(); if(DEBUGPRINT) fprintf(stderr, "%d\n", tickspersecond); tickspersecond = 0; } */ //Get all events while (SDL_PollEvent(&e) != 0 && !quit) { //Set focus if(e.type == SDL_MOUSEBUTTONDOWN) { SDL_Point p; p.x = e.button.x; p.y = e.button.y; int i; for(i = 0; i < widgets.size(); i++) { if(SDL_PointInRect(&p, &widgets[i]->bounds)) { if(focusedWidget) focusedWidget->loseFocus(); focusedWidget = widgets[i]; focusedWidget->gainFocus(); break; } } //If clicked nowhere if(i == widgets.size()) { if(focusedWidget) focusedWidget->loseFocus(); focusedWidget = NULL; } } if(focusedWidget == canvasArea) { if(doToolEvents(e)) continue; } switch (e.type) { case SDL_QUIT: quit = 1; break; case SDL_USEREVENT: /* if(!droppedframes || uistate.paused) break; //If it drops too many frames, clear the event queue droppedframes--; //fprintf(stderr, "c"); tickspersecond++; for(i = 0; i < STEPSPERTICK; i++) { doDemos(uistate.demoselected, mysim); sim_step(mysim, TICKTIME); } //printGrid(mysim); tparams.timerlock = 0; */ break; case SDL_KEYDOWN: switch(e.key.keysym.sym) { case SDLK_p: paused = !paused; break; } case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: case SDL_KEYUP: case SDL_TEXTINPUT: case SDL_TEXTEDITING: case SDL_WINDOWEVENT: if(focusedWidget) { focusedWidget->doEvent(e); } break; default: //if(DEBUGPRINT) fprintf(stderr, "UNKNOWN EVENT TYPE\n"); break; } } setMeshState(); return; }