| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #ifdef NEWEDITOR |
| 22 | #include "../neweditor/stdafx.h" |
| 23 | #include "../neweditor/globals.h" |
| 24 | #endif |
| 25 | |
| 26 | #include "SelManager.h" |
| 27 | #include "stdafx.h" |
| 28 | |
| 29 | #include "macros.h" |
| 30 | |
| 31 | #ifndef NEWEDITOR |
| 32 | #include "ddio.h" |
| 33 | #endif |
| 34 | |
| 35 | editorSelectorManager SelManager; |
| 36 | |
| 37 | editorSelectorManager::editorSelectorManager() { |
| 38 | m_l = m_t = m_r = m_b = -1; |
| 39 | m_OwnerWnd = NULL; |
| 40 | m_DragX = m_DragY = -1; |
| 41 | m_DragState = 0; |
| 42 | m_EndFunc = NULL; |
| 43 | } |
| 44 | |
| 45 | // sets the owner window of selection |
| 46 | // starts a selection at x,y |
| 47 | void editorSelectorManager::StartSelection(CWnd *wnd, void (*func)(editorSelectorManager *), int x, int y) { |
| 48 | ASSERT(wnd != NULL); |
| 49 | |
| 50 | if (m_DragState != 1) { |
| 51 | POINT pt; |
| 52 | RECT rect; |
| 53 | SIZE size = {2, 2}; |
| 54 | |
| 55 | if (m_DragState == 2) { |
| 56 | CClientDC dc(m_OwnerWnd); |
| 57 | int r_l, r_b, r_t, r_r; |
| 58 | RECT client_rect; |
| 59 | |
| 60 | m_OwnerWnd->GetClientRect(&client_rect); |
| 61 | r_l = m_l; |
| 62 | r_t = m_t; |
| 63 | r_r = m_r; |
| 64 | r_b = m_b; |
| 65 | if (r_r < r_l) |
| 66 | std::swap(r_l, r_r); |
| 67 | if (r_b < r_t) |
| 68 | std::swap(r_t, r_b); |
| 69 | if (r_l < client_rect.left) |
| 70 | r_l = client_rect.left; |
| 71 | if (r_t < client_rect.top) |
| 72 | r_t = client_rect.top; |
| 73 | if (r_r > client_rect.right) |
| 74 | r_r = client_rect.right; |
| 75 | if (r_b > client_rect.bottom) |
| 76 | r_b = client_rect.bottom; |
| 77 | SetRect(&rect, r_l, r_t, r_r, r_b); |
| 78 | dc.DrawDragRect(&rect, size, NULL, size, NULL, NULL); |
| 79 | } |
| 80 | |
| 81 | CClientDC dc(wnd); |
| 82 | m_OwnerWnd = wnd; |
| 83 | |
| 84 | m_DragState = 1; |
| 85 | m_EndFunc = func; |
| 86 | |
| 87 | m_l = x; |
| 88 | m_t = y; |
| 89 | m_r = x; |
| 90 | m_b = y; |
| 91 | SetRect(&rect, m_l, m_t, m_r, m_b); |
| 92 | dc.DrawDragRect(&rect, size, NULL, size, NULL, NULL); |
| 93 | |
| 94 | GetCursorPos(&pt); |
| 95 | m_DragX = pt.x; |
| 96 | m_DragY = pt.y; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // returns the selected rectangle. |
| 101 | void editorSelectorManager::GetSelectedRect(int *l, int *t, int *r, int *b) { |
| 102 | RECT client_rect; |
| 103 | |
| 104 | // make sure selected rectangle is clipped to the owner window and normalized. |
| 105 | m_OwnerWnd->GetClientRect(&client_rect); |
| 106 | |
| 107 | if (m_r < m_l) |
| 108 | std::swap(m_l, m_r); |
| 109 | if (m_b < m_t) |
| 110 | std::swap(m_t, m_b); |
| 111 | if (m_l < client_rect.left) |
| 112 | m_l = client_rect.left; |
| 113 | if (m_t < client_rect.top) |
| 114 | m_t = client_rect.top; |
| 115 | if (m_r > client_rect.right) |
| 116 | m_r = client_rect.right; |
| 117 | if (m_b > client_rect.bottom) |
| 118 | m_b = client_rect.bottom; |
| 119 | |
| 120 | *l = m_l; |
| 121 | *t = m_t; |
| 122 | *r = m_r; |
| 123 | *b = m_b; |
| 124 | } |
| 125 | |
| 126 | // defers control to the selector manager |
| 127 | void editorSelectorManager::Defer() { |
| 128 | int mx, my, mdx, mdy; |
| 129 | #ifndef NEWEDITOR |
| 130 | int mouse_state; |
| 131 | #endif |
| 132 | POINT pt; |
| 133 | |
| 134 | if (m_DragState != 1) |
| 135 | return; |
| 136 | |
| 137 | // get mouse deltas and button state |
| 138 | #ifdef NEWEDITOR |
| 139 | mx = mouse.x; |
| 140 | my = mouse.y; |
| 141 | mdx = mouse.x - mouse.oldx; |
| 142 | mdy = mouse.y - mouse.oldy; |
| 143 | #else |
| 144 | mouse_state = ddio_MouseGetState(&mx, &my, &mdx, &mdy); |
| 145 | #endif |
| 146 | GetCursorPos(&pt); |
| 147 | mx = pt.x; |
| 148 | my = pt.y; |
| 149 | |
| 150 | mdx = mx - m_DragX; |
| 151 | mdy = my - m_DragY; |
| 152 | |
| 153 | #ifdef NEWEDITOR |
| 154 | if (mouse.left && m_DragState == 1) { |
| 155 | #else |
| 156 | if ((mouse_state & MOUSE_LB) && m_DragState == 1) { |
| 157 | #endif |
| 158 | // if dragging mouse, then draw drag rectangle. if dragging outside owner window |
| 159 | // then still do sizing of window within bounds of owner (why we use deltas for mouse) |
| 160 | CClientDC dc(m_OwnerWnd); |
| 161 | RECT old_draw_rect, draw_rect, client_rect; |
| 162 | SIZE size = {2, 2}; |
| 163 | int r_l, r_b, r_t, r_r; |
| 164 | |
| 165 | m_OwnerWnd->GetClientRect(&client_rect); |
| 166 | |
| 167 | // get current selected rect, normalize rectangle if left,top is invalid for drawing. |
| 168 | r_l = m_l; |
| 169 | r_t = m_t; |
| 170 | r_r = m_r; |
| 171 | r_b = m_b; |
| 172 | if (r_r < r_l) |
| 173 | std::swap(r_l, r_r); |
| 174 | if (r_b < r_t) |
| 175 | std::swap(r_t, r_b); |
| 176 | if (r_l < client_rect.left) |
| 177 | r_l = client_rect.left; |
| 178 | if (r_t < client_rect.top) |
| 179 | r_t = client_rect.top; |
| 180 | if (r_r > client_rect.right) |
| 181 | r_r = client_rect.right; |
| 182 | if (r_b > client_rect.bottom) |
| 183 | r_b = client_rect.bottom; |
| 184 | SetRect(&old_draw_rect, r_l, r_t, r_r, r_b); |
| 185 | |
| 186 | m_r = m_r + mdx; |
| 187 | m_b = m_b + mdy; |
| 188 | |
| 189 | r_l = m_l; |
| 190 | r_t = m_t; |
| 191 | r_r = m_r; |
| 192 | r_b = m_b; |
| 193 | if (r_r < r_l) |
| 194 | std::swap(r_l, r_r); |
| 195 | if (r_b < r_t) |
| 196 | std::swap(r_t, r_b); |
| 197 | if (r_l < client_rect.left) |
| 198 | r_l = client_rect.left; |
| 199 | if (r_t < client_rect.top) |
| 200 | r_t = client_rect.top; |
| 201 | if (r_r > client_rect.right) |
| 202 | r_r = client_rect.right; |
| 203 | if (r_b > client_rect.bottom) |
| 204 | r_b = client_rect.bottom; |
| 205 | SetRect(&draw_rect, r_l, r_t, r_r, r_b); |
| 206 | |
| 207 | dc.DrawDragRect(&draw_rect, size, &old_draw_rect, size, NULL, NULL); |
| 208 | } |
| 209 | #ifdef NEWEDITOR |
| 210 | else if (mouse.left && m_DragState == 1) { |
| 211 | #else |
| 212 | else if (!(mouse_state & MOUSE_LB) && m_DragState == 1) { |
| 213 | #endif |
| 214 | // if mouse up then call our ending function |
| 215 | m_DragState = 2; |
| 216 | if (m_EndFunc) |
| 217 | (*m_EndFunc)(this); |
| 218 | } |
| 219 | |
| 220 | m_DragX = mx; |
| 221 | m_DragY = my; |
| 222 | } |
| 223 | |
| 224 | void editorSelectorManager::EndSelection() { |
| 225 | RECT rect; |
| 226 | SIZE size = {2, 2}; |
| 227 | |
| 228 | CClientDC dc(m_OwnerWnd); |
| 229 | int r_l, r_b, r_t, r_r; |
| 230 | RECT client_rect; |
| 231 | |
| 232 | // clip drawing rectangle to owner window. |
| 233 | m_OwnerWnd->GetClientRect(&client_rect); |
| 234 | r_l = m_l; |
| 235 | r_t = m_t; |
| 236 | r_r = m_r; |
| 237 | r_b = m_b; |
| 238 | if (r_r < r_l) |
| 239 | std::swap(r_l, r_r); |
| 240 | if (r_b < r_t) |
| 241 | std::swap(r_t, r_b); |
| 242 | if (r_l < client_rect.left) |
| 243 | r_l = client_rect.left; |
| 244 | if (r_t < client_rect.top) |
| 245 | r_t = client_rect.top; |
| 246 | if (r_r > client_rect.right) |
| 247 | r_r = client_rect.right; |
| 248 | if (r_b > client_rect.bottom) |
| 249 | r_b = client_rect.bottom; |
| 250 | SetRect(&rect, r_l, r_t, r_r, r_b); |
| 251 | dc.DrawDragRect(&rect, size, NULL, size, NULL, NULL); |
| 252 | m_l = -1; |
| 253 | m_t = -1; |
| 254 | m_r = -1; |
| 255 | m_b = -1; |
| 256 | m_DragState = 0; |
| 257 | } |