void DisplayWindow::mouseCallback(int event, int x, int y, int flags, void* param){ switch (event){ case EVENT_LBUTTONDOWN: dragStartL = Point(x,y); leftDrag = true; clickTime = std::chrono::system_clock::now(); break; case EVENT_RBUTTONDOWN: dragStartR = Point(x,y); rightDrag = true; clickTime = std::chrono::system_clock::now(); break; case EVENT_LBUTTONUP: if (dragging) {onDragStop();} else {onLeftClick();} leftDrag = false; dragging = false; break; case EVENT_RBUTTONUP: if (dragging) {onDragStop();} else {onRightClick();} rightDrag = false; dragging = false; break; case EVENT_MOUSEMOVE: currentPos = Point(x,y); auto diff = std::chrono::system_clock::now()-clickTime; std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(diff); std::chrono::milliseconds oneClick(250); if ((leftDrag || rightDrag) && ms > oneClick) {dragging = true;} break; } }
void GameObject::update(const sf::Time& frameTime) { for (auto& component : m_components) { component->update(frameTime); } if (g_inputController->isMouseOver(&m_boundingBox, m_isInputInDefaultView)) { onMouseOver(); // if the inputcontroller has locked actions, skip these methods. if (g_inputController->isActionLocked()) return; if (g_inputController->isRightClicked(&m_boundingBox, m_isInputInDefaultView)) { onRightClick(); } else if (g_inputController->isRightJustPressed(&m_boundingBox, m_isInputInDefaultView)) { onRightJustPressed(); } else if (g_inputController->isLeftClicked(&m_boundingBox, m_isInputInDefaultView)) { onLeftClick(); } else if (g_inputController->isLeftJustPressed(&m_boundingBox, m_isInputInDefaultView)) { onLeftJustPressed(); } } }
void Element::handleQueuedEvent(std::shared_ptr<Events::Event> event) { switch(event->getEventType()) { case Events::EventType::MOUSE_CURSOR: { auto casted_event = std::static_pointer_cast<Input::MouseCursorEvent>(event); if(cursor_within == false && isActive() && isPointWithin(casted_event->getPosition())) { cursor_within = true; onCursorEnter(); } else if(cursor_within == true && isActive() && !isPointWithin(casted_event->getPosition())) { cursor_within = false; onCursorLeave(); } break; } case Events::EventType::MOUSE_SCROLL: { auto casted_event = std::static_pointer_cast<Input::MouseScrollEvent>(event); auto change = last_mouse_scroll_position - casted_event->getPosition(); last_mouse_scroll_position = casted_event->getPosition(); onScroll(change); break; } case Events::EventType::MOUSE_BUTTON_DOWN: { auto casted_event = std::static_pointer_cast<Input::MouseButtonDownEvent>(event); if(cursor_within && isActive() && casted_event->getButton() == GLFW_MOUSE_BUTTON_LEFT) { onLeftClick(); } else if(cursor_within && isActive() && casted_event->getButton() == GLFW_MOUSE_BUTTON_RIGHT) onRightClick(); break; } case Events::EventType::MOUSE_BUTTON_UP: { auto casted_event = std::static_pointer_cast<Input::MouseButtonUpEvent>(event); if(cursor_within && isActive() && casted_event->getButton() == GLFW_MOUSE_BUTTON_LEFT) onLeftClickRelease(); else if(cursor_within && isActive() && casted_event->getButton() == GLFW_MOUSE_BUTTON_RIGHT) onRightClickRelease(); break; } case Events::EventType::KEY_DOWN: { auto casted_event = std::static_pointer_cast<Input::KeyDownEvent>(event); onKeyDown(casted_event->getKey()); break; } case Events::EventType::KEY_UP: { auto casted_event = std::static_pointer_cast<Input::KeyUpEvent>(event); onKeyUp(casted_event->getKey()); break; } case Events::EventType::KEY_REPEAT: { auto casted_event = std::static_pointer_cast<Input::KeyRepeatEvent>(event); onKeyRepeat(casted_event->getKey()); break; } default: { Renderable::handleQueuedEvent(event); break; } } }
LRESULT WDL_CursesEditor::onMouseMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static int s_mousedown[2]; switch (uMsg) { case WM_CAPTURECHANGED: break; case WM_MOUSEMOVE: if (GetCapture()==hwnd && CURSES_INSTANCE->m_font_w && CURSES_INSTANCE->m_font_h) { POINT pt = { (short)LOWORD(lParam), (short)HIWORD(lParam) }; int cx=pt.x/CURSES_INSTANCE->m_font_w; int cy=pt.y/CURSES_INSTANCE->m_font_h; int paney[2], paneh[2]; const int pane_divy=GetPaneDims(paney, paneh); if (m_scrollcap) { int i=m_scrollcap-1; if (i == 2) // divider { int divy=cy-paney[0]; if (divy != pane_divy) { if (divy <= 0 || divy >= LINES-m_top_margin-m_bottom_margin-1) { if (divy <= 0.0) m_paneoffs_y[0]=m_paneoffs_y[1]; m_curpane=0; m_pane_div=1.0; } else { m_pane_div=(double)divy/(double)(LINES-m_top_margin-m_bottom_margin-1); } draw(); draw_status_state(); } } else { int prevoffs=m_paneoffs_y[i]; int pxy=paney[i]*CURSES_INSTANCE->m_font_h; int pxh=paneh[i]*CURSES_INSTANCE->m_font_h; if (pxh > CURSES_INSTANCE->scroll_h[i]) { m_paneoffs_y[i]=(m_text.GetSize()-paneh[i])*(pt.y-m_scrollcap_yoffs-pxy)/(pxh-CURSES_INSTANCE->scroll_h[i]); int maxscroll=m_text.GetSize()-paneh[i]+4; if (m_paneoffs_y[i] > maxscroll) m_paneoffs_y[i]=maxscroll; if (m_paneoffs_y[i] < 0) m_paneoffs_y[i]=0; } if (m_paneoffs_y[i] != prevoffs) { draw(); draw_status_state(); const int col=m_curs_x-m_offs_x; int line=m_curs_y+paney[m_curpane]-m_paneoffs_y[m_curpane]; if (line >= paney[m_curpane] && line < paney[m_curpane]+paneh[m_curpane]) move(line, col); } } return 0; } if (!m_selecting && (cx != s_mousedown[0] || cy != s_mousedown[1])) { m_select_x2=m_select_x1=m_curs_x; m_select_y2=m_select_y1=m_curs_y; m_selecting=1; } int x=cx+m_offs_x; int y=cy+m_paneoffs_y[m_curpane]-paney[m_curpane]; if (m_selecting && (m_select_x2!=x || m_select_y2 != y)) { if (y < m_paneoffs_y[m_curpane] && m_paneoffs_y[m_curpane] > 0) { m_paneoffs_y[m_curpane]--; } else if (y >= m_paneoffs_y[m_curpane]+paneh[m_curpane] && m_paneoffs_y[m_curpane]+paneh[m_curpane] < m_text.GetSize()) { m_paneoffs_y[m_curpane]++; } if (x < m_offs_x && m_offs_x > 0) { m_offs_x--; } else if (x > m_offs_x+COLS) { int maxlen=0; int a; for (a=0; a < paneh[m_curpane]; a++) { WDL_FastString* s=m_text.Get(m_paneoffs_y[m_curpane]+a); if (s && s->GetLength() > maxlen) maxlen=s->GetLength(); } if (maxlen > m_offs_x+COLS-8) m_offs_x++; } m_select_y2=y; m_select_x2=x; if (m_select_y2<0) m_select_y2=0; else if (m_select_y2>=m_text.GetSize()) { m_select_y2=m_text.GetSize()-1; WDL_FastString *s=m_text.Get(m_select_y2); if (s) m_select_x2 = s->GetLength(); } if (m_select_x2<0)m_select_x2=0; WDL_FastString *s=m_text.Get(m_select_y2); if (s && m_select_x2>s->GetLength()) m_select_x2 = s->GetLength(); draw(); int y=m_curs_y+paney[m_curpane]-m_paneoffs_y[m_curpane]; if (y >= paney[m_curpane] && y < paney[m_curpane]+paneh[m_curpane]) setCursor(); } } break; case WM_LBUTTONDBLCLK: case WM_RBUTTONDOWN: case WM_LBUTTONDOWN: if (CURSES_INSTANCE->m_font_w && CURSES_INSTANCE->m_font_h) { int mousex=(short)LOWORD(lParam); int mousey=(short)HIWORD(lParam); int cx=mousex/CURSES_INSTANCE->m_font_w; int cy=mousey/CURSES_INSTANCE->m_font_h; if (cx > COLS) cx=COLS; if (cx < 0) cx=0; if (cy > LINES) cy=LINES; if (cy < 0) cy=0; m_state=0; // any click clears the state s_mousedown[0]=cx; s_mousedown[1]=cy; int paney[2], paneh[2]; const int pane_divy=GetPaneDims(paney, paneh); if (uMsg == WM_LBUTTONDOWN && m_pane_div > 0.0 && m_pane_div < 1.0 && cy == m_top_margin+pane_divy) { SetCapture(hwnd); m_scrollcap=3; return 0; } int pane=-1; if (cy >= paney[0] && cy < paney[0]+paneh[0]) pane=0; else if (cy >= paney[1] && cy < paney[1]+paneh[1]) pane=1; if ((uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK) && pane >= 0 && cx >= COLS-CURSES_INSTANCE->drew_scrollbar[pane]) { const int st=paney[pane]*CURSES_INSTANCE->m_font_h+CURSES_INSTANCE->scroll_y[pane]; const int sb=st+CURSES_INSTANCE->scroll_h[pane]; int prevoffs=m_paneoffs_y[pane]; if (mousey < st) { m_paneoffs_y[pane] -= paneh[pane]; if (m_paneoffs_y[pane] < 0) m_paneoffs_y[pane]=0; } else if (mousey < sb) { if (uMsg == WM_LBUTTONDOWN) { SetCapture(hwnd); m_scrollcap=pane+1; m_scrollcap_yoffs=mousey-st; } } else { m_paneoffs_y[pane] += paneh[pane]; int maxscroll=m_text.GetSize()-paneh[pane]+4; if (m_paneoffs_y[pane] > maxscroll) m_paneoffs_y[pane]=maxscroll; } if (prevoffs != m_paneoffs_y[pane]) { draw(); draw_status_state(); int y=m_curs_y+paney[m_curpane]-m_paneoffs_y[m_curpane]; if (y >= paney[m_curpane] && y < paney[m_curpane]+paneh[m_curpane]) setCursor(); return 0; } } if (uMsg == WM_LBUTTONDOWN) m_selecting=0; if (pane >= 0) m_curpane=pane; m_curs_x=cx+m_offs_x; m_curs_y=cy+m_paneoffs_y[m_curpane]-paney[m_curpane]; bool end = (m_curs_y > m_text.GetSize()-1); if (end) m_curs_y=m_text.GetSize()-1; if (m_curs_y < 0) m_curs_y = 0; WDL_FastString *s=m_text.Get(m_curs_y); if (m_curs_x < 0) m_curs_x = 0; if (s && (end || m_curs_x > s->GetLength())) m_curs_x=s->GetLength(); if (uMsg == WM_LBUTTONDBLCLK && s && s->GetLength()) { if (m_curs_x < s->GetLength()) { int x1=m_curs_x; int x2=m_curs_x+1; const char* p=s->Get(); while (x1 > 0 && (isalnum(p[x1-1]) || p[x1-1] == '_')) --x1; while (x2 < s->GetLength()-1 && (isalnum(p[x2]) || p[x2] == '_')) ++x2; if (x2 > x1) { m_select_x1=x1; m_curs_x=m_select_x2=x2; m_select_y1=m_select_y2=m_curs_y; m_selecting=1; } } } draw(); setCursor(); if (uMsg == WM_LBUTTONDOWN) { SetCapture(hwnd); } else if (uMsg == WM_RBUTTONDOWN) { onRightClick(hwnd); } } return 0; case WM_LBUTTONUP: ReleaseCapture(); m_scrollcap=0; m_scrollcap_yoffs=0; return 0; case WM_MOUSEWHEEL: if (CURSES_INSTANCE->m_font_h) { POINT p; GetCursorPos(&p); ScreenToClient(hwnd, &p); p.y /= CURSES_INSTANCE->m_font_h; int paney[2], paneh[2]; int pane_divy=GetPaneDims(paney, paneh); int pane=-1; if (p.y >= paney[0] && p.y < paney[0]+paneh[0]) pane=0; else if (p.y >= paney[1] && p.y < paney[1]+paneh[1]) pane=1; if (pane < 0) pane=m_curpane; m_paneoffs_y[pane] -= ((short)HIWORD(wParam))/20; int maxscroll=m_text.GetSize()-paneh[pane]+4; if (m_paneoffs_y[pane] > maxscroll) m_paneoffs_y[pane]=maxscroll; if (m_paneoffs_y[pane] < 0) m_paneoffs_y[pane]=0; draw(); int y=m_curs_y+paney[m_curpane]-m_paneoffs_y[m_curpane]; if (y >= paney[m_curpane] && y < paney[m_curpane]+paneh[m_curpane]) setCursor(); else draw_status_state(); } break; } return 0; }
LRESULT WDL_CursesEditor::onMouseMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CAPTURECHANGED: break; case WM_MOUSEMOVE: if (GetCapture()==hwnd && CURSES_INSTANCE->m_font_w && CURSES_INSTANCE->m_font_h) { int x = ((short)LOWORD(lParam)) / CURSES_INSTANCE->m_font_w + m_offs_x; int y = ((short)HIWORD(lParam)) / CURSES_INSTANCE->m_font_h + m_offs_y - m_top_margin; if (!m_selecting && (x != m_curs_x || y != m_curs_y)) { m_select_x2=m_select_x1=m_curs_x; m_select_y2=m_select_y1=m_curs_y; m_selecting=1; } if (m_selecting && (m_select_x2!=x || m_select_y2 != y)) { if (y < m_offs_y && m_offs_y > 0) { m_offs_y--; } else if (y > m_offs_y + getVisibleLines() && m_offs_y + getVisibleLines() < m_text.GetSize()) { m_offs_y++; } if (x < m_offs_x && m_offs_x > 0) { m_offs_x--; } else if (x > m_offs_x+COLS) { int sz=0; int a; const int vl=getVisibleLines(); for (a=0;a<vl;a++) if (m_text.Get(m_offs_y + a) && m_text.Get(m_offs_y + a)->GetLength() > sz) sz=m_text.Get(m_offs_y + a)->GetLength(); if (sz > m_offs_x + COLS - 8) m_offs_x++; } m_select_y2=y; m_select_x2=x; if (m_select_y2<0) m_select_y2=0; else if (m_select_y2>=m_text.GetSize()) { m_select_y2=m_text.GetSize()-1; WDL_FastString *s=m_text.Get(m_select_y2); if (s) m_select_x2 = s->GetLength(); } if (m_select_x2<0)m_select_x2=0; WDL_FastString *s=m_text.Get(m_select_y2); if (s && m_select_x2>s->GetLength()) m_select_x2 = s->GetLength(); draw(); if (m_curs_y >= m_offs_y && m_curs_y < m_offs_y + getVisibleLines()) setCursor(); } } break; case WM_RBUTTONDOWN: case WM_LBUTTONDOWN: if (CURSES_INSTANCE->m_font_w && CURSES_INSTANCE->m_font_h) { if (uMsg == WM_LBUTTONDOWN) m_selecting=0; m_curs_x = m_offs_x + LOWORD(lParam) / CURSES_INSTANCE->m_font_w; int a = HIWORD(lParam)/CURSES_INSTANCE->m_font_h - m_top_margin; if (a>=getVisibleLines()) a=getVisibleLines()-1; if (a<0)a=0; m_curs_y = m_offs_y + a; if (m_curs_y > m_text.GetSize()-1) { m_curs_y = m_text.GetSize()-1; WDL_FastString *s=m_text.Get(m_curs_y); if (s) m_curs_x=s->GetLength(); } if (m_curs_x < 0) m_curs_x = 0; WDL_FastString *s=m_text.Get(m_curs_y); if (s && m_curs_x > s->GetLength()) m_curs_x=s->GetLength(); if (m_curs_y < 0) m_curs_y = 0; draw(); setCursor(); if (uMsg == WM_LBUTTONDOWN) { SetCapture(hwnd); } else { onRightClick(); } } return 0; case WM_LBUTTONUP: ReleaseCapture(); return 0; case WM_MOUSEWHEEL: m_offs_y -= ((short)HIWORD(wParam))/20; if(m_offs_y > m_text.GetSize() - getVisibleLines()) m_offs_y=m_text.GetSize() - getVisibleLines(); if (m_offs_y < 0) m_offs_y=0; draw(); if (m_curs_y >= m_offs_y && m_curs_y < m_offs_y + getVisibleLines()) setCursor(); else draw_status_state(); break; } return 0; }