MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / HandleWindowDragging

Function HandleWindowDragging

src/window.cpp:2162–2328  ·  view source on GitHub ↗

* Handle dragging/resizing of a window. * @return State of handling the event. */

Source from the content-addressed store, hash-verified

2160 * @return State of handling the event.
2161 */
2162static EventState HandleWindowDragging()
2163{
2164 /* Get out immediately if no window is being dragged at all. */
2165 if (!_dragging_window) return ES_NOT_HANDLED;
2166
2167 /* If button still down, but cursor hasn't moved, there is nothing to do. */
2168 if (_left_button_down && _cursor.delta.x == 0 && _cursor.delta.y == 0) return ES_HANDLED;
2169
2170 /* Otherwise find the window... */
2171 for (Window *w : Window::Iterate()) {
2172 if (w->flags.Test(WindowFlag::Dragging)) {
2173 /* Stop the dragging if the left mouse button was released */
2174 if (!_left_button_down) {
2175 w->flags.Reset(WindowFlag::Dragging);
2176 break;
2177 }
2178
2179 w->SetDirty();
2180
2181 int x = _cursor.pos.x + _drag_delta.x;
2182 int y = _cursor.pos.y + _drag_delta.y;
2183 int nx = x;
2184 int ny = y;
2185
2186 if (_settings_client.gui.window_snap_radius != 0) {
2187 int hsnap = ScaleGUITrad(_settings_client.gui.window_snap_radius);
2188 int vsnap = ScaleGUITrad(_settings_client.gui.window_snap_radius);
2189 int delta;
2190
2191 for (const Window *v : Window::Iterate()) {
2192 if (v == w) continue; // Don't snap at yourself
2193
2194 if (y + w->height > v->top && y < v->top + v->height) {
2195 /* Your left border <-> other right border */
2196 delta = abs(v->left + v->width - x);
2197 if (delta <= hsnap) {
2198 nx = v->left + v->width;
2199 hsnap = delta;
2200 }
2201
2202 /* Your right border <-> other left border */
2203 delta = abs(v->left - x - w->width);
2204 if (delta <= hsnap) {
2205 nx = v->left - w->width;
2206 hsnap = delta;
2207 }
2208 }
2209
2210 if (w->top + w->height >= v->top && w->top <= v->top + v->height) {
2211 /* Your left border <-> other left border */
2212 delta = abs(v->left - x);
2213 if (delta <= hsnap) {
2214 nx = v->left;
2215 hsnap = delta;
2216 }
2217
2218 /* Your right border <-> other right border */
2219 delta = abs(v->left + v->width - x - w->width);

Callers 1

MouseLoopFunction · 0.85

Calls 7

absFunction · 0.85
EnsureVisibleCaptionFunction · 0.85
ResizeWindowFunction · 0.85
TestMethod · 0.80
ScaleGUITradFunction · 0.70
ResetMethod · 0.45
SetDirtyMethod · 0.45

Tested by

no test coverage detected