Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/ui/components/simple/Button.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@ import {

import Button from './Button';

export const Enable = () => {
export const Enable = (L: lua_State) => {
const button = Button.getObjectFromStack(L);

if (button.protectedFunctionsAllowed) {
button.enable(true);
} else {
// TODO: Disallowed logic
}

return 0;
};

export const Disable = () => {
export const Disable = (L: lua_State) => {
const button = Button.getObjectFromStack(L);

if (button.protectedFunctionsAllowed) {
button.enable(false);
} else {
// TODO: Disallowed logic
}

return 0;
};

Expand Down
67 changes: 66 additions & 1 deletion src/ui/components/simple/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Status } from '../../../utils';

import ButtonState from './ButtonState';
import FontString from './FontString';
import Frame from './Frame';
import Frame, { FrameFlag } from './Frame';
import Texture from './Texture';

import * as scriptFunctions from './Button.script';
Expand All @@ -28,6 +28,7 @@ class Button extends Frame {
fontString: FontString | null;

state: ButtonState;
isStateLocked: boolean;

constructor(parent: Frame | null) {
super(parent);
Expand All @@ -51,6 +52,11 @@ class Button extends Frame {
this.fontString = null;

this.state = ButtonState.DISABLED;
this.isStateLocked = false;

this.enable(true);
// TODO: Enable input events
this.setFrameFlag(FrameFlag.Ox10000, true);
}

loadXML(node: XMLNode, status: Status) {
Expand Down Expand Up @@ -93,6 +99,39 @@ class Button extends Frame {
// TODO: Text, click registration and motion scripts
}

enable(enabled: boolean) {
if (enabled) {
if (this.state !== ButtonState.DISABLED) {
return;
}

this.setState(ButtonState.NORMAL, false);

// TODO: Mouse focus

if (this.isHighlightLocked) {
this.enableDrawLayer(DrawLayerType.HIGHLIGHT);
}
} else {
if (this.state === ButtonState.DISABLED) {
return;
}

// TODO: Mouse focus

this.disableDrawLayer(DrawLayerType.HIGHLIGHT);
this.setState(ButtonState.DISABLED, false);
}

this.setFrameFlag(FrameFlag.Ox400, !enabled);

const script = enabled ? this.scripts.get('OnEnable') : this.scripts.get('OnDisable');

if (script && script.luaRef !== null && !this.loading) {
this.runScript(script);
}
}

setHighlight(texture: Texture | null, _blendMode: BlendMode | null) {
if (this.highlightTexture === texture) {
return;
Expand All @@ -110,6 +149,28 @@ class Button extends Frame {
this.highlightTexture = texture;
}

setState(state: ButtonState, locked: boolean) {
this.isStateLocked = locked;

if (state == this.state) {
return;
}

if (this.activeTexture && (this.textures[state] || state == ButtonState.NORMAL)) {
this.activeTexture.hide();
this.activeTexture = null;
}

if (this.textures[state]) {
this.activeTexture = this.textures[state];
this.activeTexture.show();
}

this.updateTextState(state);

this.state = state;
}

setStateTexture(state: ButtonState, texture: Texture | null) {
const stored = this.textures[state];
if (stored === texture) {
Expand All @@ -135,6 +196,10 @@ class Button extends Frame {
texture.show();
}
}

updateTextState(_state: ButtonState) {
// TODO
}
}

export default Button;
14 changes: 14 additions & 0 deletions src/ui/components/simple/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Frame extends ScriptRegion {
level: number;
frameScale: number;

isHighlightLocked: boolean;

layersEnabled: EnumRecord<DrawLayerType, boolean>;
backdrop: Backdrop | null;

Expand All @@ -81,6 +83,8 @@ class Frame extends ScriptRegion {
this.level = 0;
this.frameScale = 1.0;

this.isHighlightLocked = false;

this.layersEnabled = enumRecordFor(DrawLayerType, (type) => type !== DrawLayerType.HIGHLIGHT);
this.backdrop = null;

Expand Down Expand Up @@ -456,6 +460,16 @@ class Frame extends ScriptRegion {
}
}

disableDrawLayer(drawlayer: DrawLayerType) {
this.layersEnabled[drawlayer] = false;
this.notifyDrawLayerChanged(drawlayer);
}

enableDrawLayer(drawlayer: DrawLayerType) {
this.layersEnabled[drawlayer] = true;
this.notifyDrawLayerChanged(drawlayer);
}

setBackdrop(backdrop: Backdrop | null) {
if (this.backdrop) {
// TODO: Destructor
Expand Down
2 changes: 2 additions & 0 deletions src/ui/components/simple/FrameFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export default {
OCCLUDED: 0x10,
MOVABLE: 0x100,
RESIZABLE: 0x200,
Ox400: 0x400,
Ox10000: 0x10000,
DONT_SAVE_POSITION: 0x80000,
};
36 changes: 32 additions & 4 deletions src/ui/scripting/FrameScriptObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
lua_State,
lua_createtable,
lua_getglobal,
lua_pushboolean,
lua_pushcclosure,
lua_pushlightuserdata,
lua_pushnumber,
Expand All @@ -29,6 +30,8 @@ import { This, ThisConstructor } from '../../utils';
const scriptMetaTables = new Map<typeof FrameScriptObject, lua_Ref>();
const objectTypes = new Map<typeof FrameScriptObject, number>();

type ScriptArg = string | number | boolean;

class FrameScriptObject {
luaRef: lua_Ref | null;
scripts: ScriptRegistry;
Expand Down Expand Up @@ -112,12 +115,37 @@ class FrameScriptObject {
return true;
}

runScript(name: string, argsCount = 0) {
runScript(script: Script, ...args: ScriptArg[]): void
runScript(name: string, ...args: ScriptArg[]): void
runScript(nameOrScript: string | Script, ...args: ScriptArg[]) {
// TODO: This needs to be moved to the caller
const script = this.scripts.get(name);
let script: Script | undefined = nameOrScript as Script;
if (typeof nameOrScript === 'string') {
script = this.scripts.get(nameOrScript);
}

if (script && script.luaRef !== null) {
// TODO: Pass in remaining arguments
ScriptingContext.instance.executeFunction(script.luaRef, this, argsCount);
const scripting = ScriptingContext.instance;
const L = scripting.state;

const argsCount = args.length;
for (const arg of args) {
switch (typeof arg) {
case 'string':
lua_pushstring(L, arg);
break;
case 'number':
lua_pushnumber(L, arg);
break;
case 'boolean':
lua_pushboolean(L, arg);
break;
default:
throw new Error(`invalid argument ${arg} for script ${name}`);
}
}

scripting.executeFunction(script.luaRef, this, argsCount);
}
}

Expand Down
4 changes: 2 additions & 2 deletions vendor/fengari.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare module 'fengari' {
type lua_String = Uint8Array;
type lua_InputString = Uint8Array | string;

type lua_State = unknown
type lua_State = object & { __brand: 'lua_State' }

function to_luastring(s: string): lua_String;
function to_jsstring(s: lua_String): string;
Expand Down Expand Up @@ -214,7 +214,7 @@ declare module 'fengari' {
}

namespace lauxlib {
type luaL_Buffer = unknown
type luaL_Buffer = object & { __brand: 'luaL_Buffer' }

function luaL_addchar(B: luaL_Buffer, c: number): void;
function luaL_addlstring(B: luaL_Buffer, s: string, l: number): void;
Expand Down