| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "platform_ui_adapter.h" |
| |
|
| | #include <chrono> |
| |
|
| | namespace mujoco { |
| | PlatformUIAdapter::PlatformUIAdapter() { |
| | mjr_defaultContext(&con_); |
| | } |
| |
|
| | void PlatformUIAdapter::FreeMjrContext() { |
| | mjr_freeContext(&con_); |
| | } |
| |
|
| | bool PlatformUIAdapter::RefreshMjrContext(const mjModel* m, int fontscale) { |
| | if (m != last_model_ || fontscale != last_fontscale_) { |
| | mjr_makeContext(m, &con_, fontscale); |
| | last_model_ = m; |
| | last_fontscale_ = fontscale; |
| | return true; |
| | } |
| | return false; |
| | } |
| |
|
| | bool PlatformUIAdapter::EnsureContextSize() { |
| | return false; |
| | } |
| |
|
| | void PlatformUIAdapter::OnFilesDrop(int count, const char** paths) { |
| | state_.type = mjEVENT_FILESDROP; |
| | state_.dropcount = count; |
| | state_.droppaths = paths; |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| |
|
| | |
| | state_.dropcount = 0; |
| | state_.droppaths = nullptr; |
| | } |
| |
|
| | void PlatformUIAdapter::OnKey(int key, int scancode, int act) { |
| | |
| | int mj_key = TranslateKeyCode(key); |
| |
|
| | |
| | if (!IsKeyDownEvent(act)) { |
| | return; |
| | } |
| |
|
| | |
| | UpdateMjuiState(); |
| |
|
| | |
| | state_.type = mjEVENT_KEY; |
| | state_.key = mj_key; |
| | state_.keytime = std::chrono::duration<double>( |
| | std::chrono::steady_clock::now().time_since_epoch()).count(); |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| |
|
| | last_key_ = mj_key; |
| | } |
| |
|
| | void PlatformUIAdapter::OnMouseButton(int button, int act) { |
| | |
| | mjtButton mj_button = TranslateMouseButton(button); |
| |
|
| | |
| | UpdateMjuiState(); |
| |
|
| | |
| | if (state_.alt) { |
| | if (mj_button == mjBUTTON_LEFT) { |
| | mj_button = mjBUTTON_RIGHT; |
| | } else if (mj_button == mjBUTTON_RIGHT) { |
| | mj_button = mjBUTTON_LEFT; |
| | } |
| | } |
| |
|
| | |
| | if (IsMouseButtonDownEvent(act)) { |
| | double now = std::chrono::duration<double>( |
| | std::chrono::steady_clock::now().time_since_epoch()).count(); |
| |
|
| | |
| | if (mj_button == state_.button && now - state_.buttontime < 0.25) { |
| | state_.doubleclick = 1; |
| | } else { |
| | state_.doubleclick = 0; |
| | } |
| |
|
| | |
| | state_.type = mjEVENT_PRESS; |
| | state_.button = mj_button; |
| | state_.buttontime = now; |
| |
|
| | |
| | if (state_.mouserect) { |
| | state_.dragbutton = state_.button; |
| | state_.dragrect = state_.mouserect; |
| | } |
| | } |
| |
|
| | |
| | else { |
| | state_.type = mjEVENT_RELEASE; |
| | } |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| |
|
| | |
| | if (state_.type == mjEVENT_RELEASE) { |
| | state_.dragrect = 0; |
| | state_.dragbutton = 0; |
| | } |
| | } |
| |
|
| | void PlatformUIAdapter::OnMouseMove(double x, double y) { |
| | |
| | if (!state_.left && !state_.right && !state_.middle) { |
| | return; |
| | } |
| |
|
| | |
| | UpdateMjuiState(); |
| |
|
| | |
| | state_.type = mjEVENT_MOVE; |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| | } |
| |
|
| | void PlatformUIAdapter::OnScroll(double xoffset, double yoffset) { |
| | |
| | UpdateMjuiState(); |
| |
|
| | |
| | const double buffer_window_ratio = |
| | static_cast<double>(GetFramebufferSize().first) / GetWindowSize().first; |
| | state_.type = mjEVENT_SCROLL; |
| | state_.sx = xoffset * buffer_window_ratio; |
| | state_.sy = yoffset * buffer_window_ratio; |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| | } |
| |
|
| | void PlatformUIAdapter::OnWindowRefresh() { |
| | state_.type = mjEVENT_REDRAW; |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| | } |
| |
|
| | void PlatformUIAdapter::OnWindowResize(int width, int height) { |
| | auto [buf_width, buf_height] = GetFramebufferSize(); |
| | state_.rect[0].width = buf_width; |
| | state_.rect[0].height = buf_height; |
| | if (state_.nrect < 1) state_.nrect = 1; |
| |
|
| | |
| | if (layout_callback_) { |
| | layout_callback_(&state_); |
| | } |
| |
|
| | |
| | UpdateMjuiState(); |
| |
|
| | |
| | state_.type = mjEVENT_RESIZE; |
| |
|
| | |
| | state_.dragbutton = 0; |
| | state_.dragrect = 0; |
| |
|
| | |
| | if (event_callback_) { |
| | event_callback_(&state_); |
| | } |
| | } |
| |
|
| | void PlatformUIAdapter::UpdateMjuiState() { |
| | |
| | state_.left = IsLeftMouseButtonPressed(); |
| | state_.right = IsRightMouseButtonPressed(); |
| | state_.middle = IsMiddleMouseButtonPressed(); |
| |
|
| | |
| | state_.control = IsCtrlKeyPressed(); |
| | state_.shift = IsShiftKeyPressed(); |
| | state_.alt = IsAltKeyPressed(); |
| |
|
| | |
| | if (state_.alt) { |
| | int tmp = state_.left; |
| | state_.left = state_.right; |
| | state_.right = tmp; |
| | } |
| |
|
| | |
| | auto [x, y] = GetCursorPosition(); |
| | const double buffer_window_ratio = |
| | static_cast<double>(GetFramebufferSize().first) / GetWindowSize().first; |
| | x *= buffer_window_ratio; |
| | y *= buffer_window_ratio; |
| |
|
| | |
| | y = state_.rect[0].height - y; |
| |
|
| | |
| | state_.dx = x - state_.x; |
| | state_.dy = y - state_.y; |
| | state_.x = x; |
| | state_.y = y; |
| |
|
| | |
| | state_.mouserect = mjr_findRect(mju_round(x), mju_round(y), state_.nrect-1, state_.rect+1) + 1; |
| | } |
| | } |
| |
|