|
| 1 | +import UIRoot from '../UIRoot'; |
| 2 | +import { stringToFramePointType } from '../../utils'; |
1 | 3 | import {
|
2 | 4 | DDCtoNDCWidth,
|
3 | 5 | NDCtoDDCWidth,
|
| 6 | + aspectCompensation, |
4 | 7 | luaValueToBoolean,
|
5 | 8 | maxAspectCompensation,
|
6 | 9 | } from '../../../utils';
|
7 | 10 | import {
|
| 11 | + LUA_TNIL, |
| 12 | + LUA_TSTRING, |
| 13 | + LUA_TTABLE, |
8 | 14 | luaL_error,
|
9 | 15 | lua_State,
|
10 | 16 | lua_isnumber,
|
| 17 | + lua_isstring, |
11 | 18 | lua_pushnumber,
|
| 19 | + lua_rawgeti, |
| 20 | + lua_settop, |
| 21 | + lua_tolstring, |
12 | 22 | lua_tonumber,
|
| 23 | + lua_touserdata, |
| 24 | + lua_type, |
| 25 | + to_jsstring, |
13 | 26 | } from '../../scripting/lua';
|
14 | 27 |
|
| 28 | +import LayoutFrame from './LayoutFrame'; |
15 | 29 | import ScriptRegion from './ScriptRegion';
|
| 30 | +import FramePointType from './FramePointType'; |
16 | 31 |
|
17 | 32 | export const IsProtected = () => {
|
18 | 33 | return 0;
|
@@ -144,15 +159,136 @@ export const GetPoint = () => {
|
144 | 159 | return 0;
|
145 | 160 | };
|
146 | 161 |
|
147 |
| -export const SetPoint = () => { |
| 162 | +export const SetPoint = (L: lua_State): number => { |
| 163 | + const region = ScriptRegion.getObjectFromStack(L); |
| 164 | + |
| 165 | + // TODO: Protection logic |
| 166 | + |
| 167 | + if (!lua_isstring(L, 2)) { |
| 168 | + return luaL_error(L, 'Usage: %s:SetPoint("point" [, region or nil] [, "relativePoint"] [, offsetX, offsetY])', region.displayName); |
| 169 | + } |
| 170 | + |
| 171 | + let relative: LayoutFrame | null = region.layoutParent; |
| 172 | + |
| 173 | + const pointStr = to_jsstring(lua_tolstring(L, 2, 0)); |
| 174 | + const point = stringToFramePointType(pointStr); |
| 175 | + if (point === undefined) { |
| 176 | + return luaL_error(L, '%s:SetPoint(): Unknown region point (%s)', region.displayName, pointStr); |
| 177 | + } |
| 178 | + |
| 179 | + let argsIndex = 3; |
| 180 | + if (lua_type(L, 3) == LUA_TSTRING) { |
| 181 | + const name = to_jsstring(lua_tolstring(L, 3, 0)); |
| 182 | + relative = region.getLayoutFrameByName(name); |
| 183 | + |
| 184 | + argsIndex++; |
| 185 | + } else if (lua_type(L, 3) == LUA_TTABLE) { |
| 186 | + lua_rawgeti(L, 3, 0); |
| 187 | + |
| 188 | + relative = lua_touserdata(L, -1) || null; |
| 189 | + |
| 190 | + lua_settop(L, -2); |
| 191 | + |
| 192 | + argsIndex++; |
| 193 | + } else if (lua_type(L, 3) == LUA_TNIL) { |
| 194 | + relative = UIRoot.instance; |
| 195 | + |
| 196 | + argsIndex++; |
| 197 | + } |
| 198 | + |
| 199 | + if (!relative) { |
| 200 | + const name = lua_tolstring(L, 3, 0); |
| 201 | + return luaL_error(L, "%s:SetPoint(): Couldn't find region named '%s'", region.displayName, name); |
| 202 | + } |
| 203 | + |
| 204 | + if (relative == region) { |
| 205 | + return luaL_error(L, '%s:SetPoint(): trying to anchor to itself', region.displayName); |
| 206 | + } |
| 207 | + |
| 208 | + if (relative.isResizeDependency(region)) { |
| 209 | + return luaL_error(L, '%s:SetPoint(): %s is dependent on this', region.displayName, (relative as ScriptRegion).displayName); |
| 210 | + } |
| 211 | + |
| 212 | + let relativePoint: FramePointType | undefined = point; |
| 213 | + |
| 214 | + if (lua_type(L, argsIndex) == LUA_TSTRING) { |
| 215 | + const relativePointStr = to_jsstring(lua_tolstring(L, argsIndex, 0)); |
| 216 | + relativePoint = stringToFramePointType(relativePointStr); |
| 217 | + if (relativePoint === undefined) { |
| 218 | + return luaL_error(L, '%s:SetPoint(): Unknown region point', region.displayName); |
| 219 | + } |
| 220 | + |
| 221 | + argsIndex++; |
| 222 | + } |
| 223 | + |
| 224 | + let offsetX = 0.0; |
| 225 | + let offsetY = 0.0; |
| 226 | + |
| 227 | + if (lua_isnumber(L, argsIndex) && lua_isnumber(L, argsIndex + 1)) { |
| 228 | + const x = lua_tonumber(L, argsIndex); |
| 229 | + const ndcX = x / (aspectCompensation * 1024.0); |
| 230 | + const ddcX = NDCtoDDCWidth(ndcX); |
| 231 | + |
| 232 | + const y = lua_tonumber(L, argsIndex + 1); |
| 233 | + const ndcY = y / (aspectCompensation * 1024.0); |
| 234 | + const ddcY = NDCtoDDCWidth(ndcY); |
| 235 | + |
| 236 | + offsetX = ddcX; |
| 237 | + offsetY = ddcY; |
| 238 | + } |
| 239 | + |
| 240 | + region.setPoint(point, relative, relativePoint, offsetX, offsetY, true); |
| 241 | + |
148 | 242 | return 0;
|
149 | 243 | };
|
150 | 244 |
|
151 |
| -export const SetAllPoints = () => { |
| 245 | +export const SetAllPoints = (L: lua_State): number => { |
| 246 | + const region = ScriptRegion.getObjectFromStack(L); |
| 247 | + |
| 248 | + // TODO: Protected logic |
| 249 | + |
| 250 | + let relative: LayoutFrame | null = region.layoutParent; |
| 251 | + |
| 252 | + if (lua_isstring(L, 2)) { |
| 253 | + const name = to_jsstring(lua_tolstring(L, 2, 0)); |
| 254 | + relative = region.getLayoutFrameByName(name); |
| 255 | + } else if (lua_type(L, 2) == LUA_TTABLE) { |
| 256 | + lua_rawgeti(L, 2, 0); |
| 257 | + |
| 258 | + relative = lua_touserdata(L, -1) || null; |
| 259 | + |
| 260 | + lua_settop(L, -2); |
| 261 | + } else if (lua_type(L, 2) == LUA_TNIL) { |
| 262 | + relative = UIRoot.instance; |
| 263 | + } |
| 264 | + |
| 265 | + if (!relative) { |
| 266 | + const name = lua_tolstring(L, 2, 0); |
| 267 | + return luaL_error(L, "%s:SetAllPoints(): Couldn't find region named '%s'", region.displayName, name); |
| 268 | + } |
| 269 | + |
| 270 | + if (relative == region) { |
| 271 | + return luaL_error(L, '%s:SetAllPoints(): trying to anchor to itself', region.displayName); |
| 272 | + } |
| 273 | + |
| 274 | + if (relative.isResizeDependency(region)) { |
| 275 | + return luaL_error(L, '%s:SetAllPoints(): %s is dependent on this', region.displayName, (relative as ScriptRegion).displayName); |
| 276 | + } |
| 277 | + |
| 278 | + region.setAllPoints(relative, true); |
| 279 | + |
152 | 280 | return 0;
|
153 | 281 | };
|
154 | 282 |
|
155 |
| -export const ClearAllPoints = () => { |
| 283 | +export const ClearAllPoints = (L: lua_State): number => { |
| 284 | + const region = ScriptRegion.getObjectFromStack(L); |
| 285 | + |
| 286 | + if (region.protectedFunctionsAllowed) { |
| 287 | + region.clearAllPoints(); |
| 288 | + } else { |
| 289 | + // TODO: Error handling |
| 290 | + } |
| 291 | + |
156 | 292 | return 0;
|
157 | 293 | };
|
158 | 294 |
|
|
0 commit comments