Skip to content

Commit 0e22325

Browse files
authored
feat: implement various system, point and layout script interfaces (#58)
1 parent b344657 commit 0e22325

File tree

4 files changed

+196
-16
lines changed

4 files changed

+196
-16
lines changed

src/ui/components/abstract/ScriptRegion.script.ts

+139-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
import UIRoot from '../UIRoot';
2+
import { stringToFramePointType } from '../../utils';
13
import {
24
DDCtoNDCWidth,
35
NDCtoDDCWidth,
6+
aspectCompensation,
47
luaValueToBoolean,
58
maxAspectCompensation,
69
} from '../../../utils';
710
import {
11+
LUA_TNIL,
12+
LUA_TSTRING,
13+
LUA_TTABLE,
814
luaL_error,
915
lua_State,
1016
lua_isnumber,
17+
lua_isstring,
1118
lua_pushnumber,
19+
lua_rawgeti,
20+
lua_settop,
21+
lua_tolstring,
1222
lua_tonumber,
23+
lua_touserdata,
24+
lua_type,
25+
to_jsstring,
1326
} from '../../scripting/lua';
1427

28+
import LayoutFrame from './LayoutFrame';
1529
import ScriptRegion from './ScriptRegion';
30+
import FramePointType from './FramePointType';
1631

1732
export const IsProtected = () => {
1833
return 0;
@@ -144,15 +159,136 @@ export const GetPoint = () => {
144159
return 0;
145160
};
146161

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+
148242
return 0;
149243
};
150244

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+
152280
return 0;
153281
};
154282

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+
156292
return 0;
157293
};
158294

src/ui/components/simple/Frame.script.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import EventType from '../../scripting/EventType';
22
import {
33
lua_State,
4+
lua_isnumber,
45
lua_isstring,
56
lua_pushnil,
67
lua_pushnumber,
78
lua_tolstring,
9+
lua_tonumber,
810
luaL_error,
911
to_jsstring,
1012
} from '../../scripting/lua';
@@ -55,13 +57,28 @@ export const SetFrameStrata = () => {
5557
return 0;
5658
};
5759

58-
export const GetFrameLevel = (L: lua_State) => {
60+
export const GetFrameLevel = (L: lua_State): number => {
5961
const frame = Frame.getObjectFromStack(L);
6062
lua_pushnumber(L, frame.level);
6163
return 1;
6264
};
6365

64-
export const SetFrameLevel = () => {
66+
export const SetFrameLevel = (L: lua_State): number => {
67+
const frame = Frame.getObjectFromStack(L);
68+
69+
// TODO: Protected logic
70+
71+
if (!lua_isnumber(L, 2)) {
72+
return luaL_error(L, 'Usage: %s:SetFrameLevel(level)', frame.displayName);
73+
}
74+
75+
const level = lua_tonumber(L, 2);
76+
if (level < 0) {
77+
return luaL_error(L, '%s:SetFrameLevel(): Passed negative frame level: %d', frame.displayName, level);
78+
}
79+
80+
frame.setFrameLevel(level, true);
81+
6582
return 0;
6683
};
6784

@@ -156,7 +173,17 @@ export const GetID = (L: lua_State) => {
156173
return 1;
157174
};
158175

159-
export const SetID = () => {
176+
export const SetID = (L: lua_State): number => {
177+
const frame = Frame.getObjectFromStack(L);
178+
179+
// TODO: Protected logic
180+
181+
if (!lua_isnumber(L, 2)) {
182+
return luaL_error(L, 'Usage: %s:SetID(ID)', frame.displayName);
183+
}
184+
185+
frame.id = lua_tonumber(L, 2);
186+
160187
return 0;
161188
};
162189

src/ui/components/simple/Texture.script.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { lua_State } from '../../scripting/lua';
2+
3+
import Texture from './Texture';
4+
15
export const IsObjectType = () => {
26
return 0;
37
};
@@ -46,11 +50,15 @@ export const GetAlpha = () => {
4650
return 0;
4751
};
4852

49-
export const Show = () => {
53+
export const Show = (L: lua_State): number => {
54+
const texture = Texture.getObjectFromStack(L);
55+
texture.show();
5056
return 0;
5157
};
5258

53-
export const Hide = () => {
59+
export const Hide = (L: lua_State): number => {
60+
const texture = Texture.getObjectFromStack(L);
61+
texture.hide();
5462
return 0;
5563
};
5664

src/ui/scripting/globals/glue/shared.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import {
88
lua_State,
99
lua_pushboolean,
10+
lua_pushnil,
1011
lua_pushnumber,
1112
lua_pushstring,
1213
} from '../../../scripting/lua';
@@ -199,8 +200,10 @@ export const PatchDownloadApply = () => {
199200
return 0;
200201
};
201202

202-
export const GetNumAddOns = () => {
203-
return 0;
203+
export const GetNumAddOns = (L: lua_State) => {
204+
// TODO: Implementation
205+
lua_pushnumber(L, 0);
206+
return 1;
204207
};
205208

206209
export const GetAddOnInfo = () => {
@@ -395,8 +398,10 @@ export const AcceptChangedOptionWarnings = () => {
395398
return 0;
396399
};
397400

398-
export const ShowChangedOptionWarnings = () => {
399-
return 0;
401+
export const ShowChangedOptionWarnings = (L: lua_State) => {
402+
// TODO: Implementation
403+
lua_pushnil(L);
404+
return 1;
400405
};
401406

402407
export const TokenEntered = () => {
@@ -467,10 +472,14 @@ export const ReadyForAccountDataTimes = () => {
467472
return 0;
468473
};
469474

470-
export const IsTrialAccount = () => {
471-
return 0;
475+
export const IsTrialAccount = (L: lua_State) => {
476+
// TODO: Implementation
477+
lua_pushnil(L);
478+
return 1;
472479
};
473480

474-
export const IsSystemSupported = () => {
475-
return 0;
481+
export const IsSystemSupported = (L: lua_State) => {
482+
// TODO: Implementation
483+
lua_pushboolean(L, 1);
484+
return 1;
476485
};

0 commit comments

Comments
 (0)