Skip to content

Commit

Permalink
feat: implement various system, point and layout script interfaces (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Apr 12, 2024
1 parent b344657 commit 0e22325
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 16 deletions.
142 changes: 139 additions & 3 deletions src/ui/components/abstract/ScriptRegion.script.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import UIRoot from '../UIRoot';
import { stringToFramePointType } from '../../utils';
import {
DDCtoNDCWidth,
NDCtoDDCWidth,
aspectCompensation,
luaValueToBoolean,
maxAspectCompensation,
} from '../../../utils';
import {
LUA_TNIL,
LUA_TSTRING,
LUA_TTABLE,
luaL_error,
lua_State,
lua_isnumber,
lua_isstring,
lua_pushnumber,
lua_rawgeti,
lua_settop,
lua_tolstring,
lua_tonumber,
lua_touserdata,
lua_type,
to_jsstring,
} from '../../scripting/lua';

import LayoutFrame from './LayoutFrame';
import ScriptRegion from './ScriptRegion';
import FramePointType from './FramePointType';

export const IsProtected = () => {
return 0;
Expand Down Expand Up @@ -144,15 +159,136 @@ export const GetPoint = () => {
return 0;
};

export const SetPoint = () => {
export const SetPoint = (L: lua_State): number => {
const region = ScriptRegion.getObjectFromStack(L);

// TODO: Protection logic

if (!lua_isstring(L, 2)) {
return luaL_error(L, 'Usage: %s:SetPoint("point" [, region or nil] [, "relativePoint"] [, offsetX, offsetY])', region.displayName);
}

let relative: LayoutFrame | null = region.layoutParent;

const pointStr = to_jsstring(lua_tolstring(L, 2, 0));
const point = stringToFramePointType(pointStr);
if (point === undefined) {
return luaL_error(L, '%s:SetPoint(): Unknown region point (%s)', region.displayName, pointStr);
}

let argsIndex = 3;
if (lua_type(L, 3) == LUA_TSTRING) {
const name = to_jsstring(lua_tolstring(L, 3, 0));
relative = region.getLayoutFrameByName(name);

argsIndex++;
} else if (lua_type(L, 3) == LUA_TTABLE) {
lua_rawgeti(L, 3, 0);

relative = lua_touserdata(L, -1) || null;

lua_settop(L, -2);

argsIndex++;
} else if (lua_type(L, 3) == LUA_TNIL) {
relative = UIRoot.instance;

argsIndex++;
}

if (!relative) {
const name = lua_tolstring(L, 3, 0);
return luaL_error(L, "%s:SetPoint(): Couldn't find region named '%s'", region.displayName, name);
}

if (relative == region) {
return luaL_error(L, '%s:SetPoint(): trying to anchor to itself', region.displayName);
}

if (relative.isResizeDependency(region)) {
return luaL_error(L, '%s:SetPoint(): %s is dependent on this', region.displayName, (relative as ScriptRegion).displayName);
}

let relativePoint: FramePointType | undefined = point;

if (lua_type(L, argsIndex) == LUA_TSTRING) {
const relativePointStr = to_jsstring(lua_tolstring(L, argsIndex, 0));
relativePoint = stringToFramePointType(relativePointStr);
if (relativePoint === undefined) {
return luaL_error(L, '%s:SetPoint(): Unknown region point', region.displayName);
}

argsIndex++;
}

let offsetX = 0.0;
let offsetY = 0.0;

if (lua_isnumber(L, argsIndex) && lua_isnumber(L, argsIndex + 1)) {
const x = lua_tonumber(L, argsIndex);
const ndcX = x / (aspectCompensation * 1024.0);
const ddcX = NDCtoDDCWidth(ndcX);

const y = lua_tonumber(L, argsIndex + 1);
const ndcY = y / (aspectCompensation * 1024.0);
const ddcY = NDCtoDDCWidth(ndcY);

offsetX = ddcX;
offsetY = ddcY;
}

region.setPoint(point, relative, relativePoint, offsetX, offsetY, true);

return 0;
};

export const SetAllPoints = () => {
export const SetAllPoints = (L: lua_State): number => {
const region = ScriptRegion.getObjectFromStack(L);

// TODO: Protected logic

let relative: LayoutFrame | null = region.layoutParent;

if (lua_isstring(L, 2)) {
const name = to_jsstring(lua_tolstring(L, 2, 0));
relative = region.getLayoutFrameByName(name);
} else if (lua_type(L, 2) == LUA_TTABLE) {
lua_rawgeti(L, 2, 0);

relative = lua_touserdata(L, -1) || null;

lua_settop(L, -2);
} else if (lua_type(L, 2) == LUA_TNIL) {
relative = UIRoot.instance;
}

if (!relative) {
const name = lua_tolstring(L, 2, 0);
return luaL_error(L, "%s:SetAllPoints(): Couldn't find region named '%s'", region.displayName, name);
}

if (relative == region) {
return luaL_error(L, '%s:SetAllPoints(): trying to anchor to itself', region.displayName);
}

if (relative.isResizeDependency(region)) {
return luaL_error(L, '%s:SetAllPoints(): %s is dependent on this', region.displayName, (relative as ScriptRegion).displayName);
}

region.setAllPoints(relative, true);

return 0;
};

export const ClearAllPoints = () => {
export const ClearAllPoints = (L: lua_State): number => {
const region = ScriptRegion.getObjectFromStack(L);

if (region.protectedFunctionsAllowed) {
region.clearAllPoints();
} else {
// TODO: Error handling
}

return 0;
};

Expand Down
33 changes: 30 additions & 3 deletions src/ui/components/simple/Frame.script.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import EventType from '../../scripting/EventType';
import {
lua_State,
lua_isnumber,
lua_isstring,
lua_pushnil,
lua_pushnumber,
lua_tolstring,
lua_tonumber,
luaL_error,
to_jsstring,
} from '../../scripting/lua';
Expand Down Expand Up @@ -55,13 +57,28 @@ export const SetFrameStrata = () => {
return 0;
};

export const GetFrameLevel = (L: lua_State) => {
export const GetFrameLevel = (L: lua_State): number => {
const frame = Frame.getObjectFromStack(L);
lua_pushnumber(L, frame.level);
return 1;
};

export const SetFrameLevel = () => {
export const SetFrameLevel = (L: lua_State): number => {
const frame = Frame.getObjectFromStack(L);

// TODO: Protected logic

if (!lua_isnumber(L, 2)) {
return luaL_error(L, 'Usage: %s:SetFrameLevel(level)', frame.displayName);
}

const level = lua_tonumber(L, 2);
if (level < 0) {
return luaL_error(L, '%s:SetFrameLevel(): Passed negative frame level: %d', frame.displayName, level);
}

frame.setFrameLevel(level, true);

return 0;
};

Expand Down Expand Up @@ -156,7 +173,17 @@ export const GetID = (L: lua_State) => {
return 1;
};

export const SetID = () => {
export const SetID = (L: lua_State): number => {
const frame = Frame.getObjectFromStack(L);

// TODO: Protected logic

if (!lua_isnumber(L, 2)) {
return luaL_error(L, 'Usage: %s:SetID(ID)', frame.displayName);
}

frame.id = lua_tonumber(L, 2);

return 0;
};

Expand Down
12 changes: 10 additions & 2 deletions src/ui/components/simple/Texture.script.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { lua_State } from '../../scripting/lua';

import Texture from './Texture';

export const IsObjectType = () => {
return 0;
};
Expand Down Expand Up @@ -46,11 +50,15 @@ export const GetAlpha = () => {
return 0;
};

export const Show = () => {
export const Show = (L: lua_State): number => {
const texture = Texture.getObjectFromStack(L);
texture.show();
return 0;
};

export const Hide = () => {
export const Hide = (L: lua_State): number => {
const texture = Texture.getObjectFromStack(L);
texture.hide();
return 0;
};

Expand Down
25 changes: 17 additions & 8 deletions src/ui/scripting/globals/glue/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
lua_State,
lua_pushboolean,
lua_pushnil,
lua_pushnumber,
lua_pushstring,
} from '../../../scripting/lua';
Expand Down Expand Up @@ -199,8 +200,10 @@ export const PatchDownloadApply = () => {
return 0;
};

export const GetNumAddOns = () => {
return 0;
export const GetNumAddOns = (L: lua_State) => {
// TODO: Implementation
lua_pushnumber(L, 0);
return 1;
};

export const GetAddOnInfo = () => {
Expand Down Expand Up @@ -395,8 +398,10 @@ export const AcceptChangedOptionWarnings = () => {
return 0;
};

export const ShowChangedOptionWarnings = () => {
return 0;
export const ShowChangedOptionWarnings = (L: lua_State) => {
// TODO: Implementation
lua_pushnil(L);
return 1;
};

export const TokenEntered = () => {
Expand Down Expand Up @@ -467,10 +472,14 @@ export const ReadyForAccountDataTimes = () => {
return 0;
};

export const IsTrialAccount = () => {
return 0;
export const IsTrialAccount = (L: lua_State) => {
// TODO: Implementation
lua_pushnil(L);
return 1;
};

export const IsSystemSupported = () => {
return 0;
export const IsSystemSupported = (L: lua_State) => {
// TODO: Implementation
lua_pushboolean(L, 1);
return 1;
};

0 comments on commit 0e22325

Please sign in to comment.