Skip to content

Commit

Permalink
Refactor|Client|libgui: Use Binder to manage script bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 16, 2014
1 parent e6e1dd5 commit 8758931
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 60 deletions.
27 changes: 6 additions & 21 deletions doomsday/client/src/clientapp.cpp
Expand Up @@ -76,7 +76,7 @@ static void continueInitWithEventLoopRunning()
ClientApp::updater().setupUI();
}

Value *Binding_App_GamePlugin(Context &, Function::ArgumentValues const &)
static Value *Binding_App_GamePlugin(Context &, Function::ArgumentValues const &)
{
if(App_CurrentGame().isNull())
{
Expand All @@ -88,7 +88,7 @@ Value *Binding_App_GamePlugin(Context &, Function::ArgumentValues const &)
return new TextValue(name);
}

Value *Binding_App_LoadFont(Context &, Function::ArgumentValues const &args)
static Value *Binding_App_LoadFont(Context &, Function::ArgumentValues const &args)
{
LOG_AS("ClientApp");

Expand Down Expand Up @@ -125,6 +125,7 @@ Value *Binding_App_LoadFont(Context &, Function::ArgumentValues const &args)

DENG2_PIMPL(ClientApp)
{
Binder binder;
QScopedPointer<Updater> updater;
SettingsRegister audioSettings;
SettingsRegister logSettings;
Expand Down Expand Up @@ -208,24 +209,6 @@ DENG2_PIMPL(ClientApp)
delete inputSys;
delete menuBar;
clientAppSingleton = 0;

deinitScriptBindings();
}

void initScriptBindings()
{
Function::registerNativeEntryPoint("App_GamePlugin", Binding_App_GamePlugin);
Function::registerNativeEntryPoint("App_LoadFont", Binding_App_LoadFont);

Record &appModule = self.scriptSystem().nativeModule("App");
appModule.addFunction("gamePlugin", refless(new Function("App_GamePlugin"))).setReadOnly();
appModule.addFunction("loadFont", refless(new Function("App_LoadFont", Function::Arguments() << "fileName"))).setReadOnly();
}

void deinitScriptBindings()
{
Function::unregisterNativeEntryPoint("App_GamePlugin");
Function::unregisterNativeEntryPoint("App_LoadFont");
}

/**
Expand Down Expand Up @@ -317,7 +300,9 @@ ClientApp::ClientApp(int &argc, char **argv)
// We must presently set the current game manually (the collection is global).
setGame(d->games.nullGame());

d->initScriptBindings();
d->binder.init(scriptSystem().nativeModule("App"))
<< DENG2_FUNC_NOARG (App_GamePlugin, "gamePlugin")
<< DENG2_FUNC (App_LoadFont, "loadFont", "fileName");
}

void ClientApp::initialize()
Expand Down
22 changes: 5 additions & 17 deletions doomsday/client/src/ui/inputsystem.cpp
Expand Up @@ -53,6 +53,7 @@ static Value *Binding_InputSystem_BindEvent(Context &, Function::ArgumentValues

DENG2_PIMPL(InputSystem)
{
Binder binder;
SettingsRegister settings;
Record *scriptBindings;

Expand All @@ -68,23 +69,14 @@ DENG2_PIMPL(InputSystem)
.define(SettingsRegister::IntCVar, "input-sharp", 1);

// Initialize script bindings.
Function::registerNativeEntryPoint("InputSystem_BindEvent", Binding_InputSystem_BindEvent);
binder.initNew()
<< DENG2_FUNC(InputSystem_BindEvent, "bindEvent", "event" << "command");

scriptBindings = new Record;
scriptBindings->addFunction("bindEvent",
refless(new Function("InputSystem_BindEvent",
Function::Arguments() << "event" << "command"))).setReadOnly();

App::scriptSystem().addNativeModule("Input", *scriptBindings);
App::scriptSystem().addNativeModule("Input", binder.module());

// Initialize the system.
DD_InitInput();

if(!I_Init())
{
Con_Error("Failed to initialize Input subsystem.\n");
}

I_Init();
I_InitVirtualInputDevices();
}

Expand All @@ -93,10 +85,6 @@ DENG2_PIMPL(InputSystem)
// Shutdown.
I_ShutdownInputDevices();
I_Shutdown();

// Deinit script bindings.
delete scriptBindings; // App observes
Function::unregisterNativeEntryPoint("InputSystem_BindEvent");
}
};

Expand Down
27 changes: 5 additions & 22 deletions doomsday/libgui/src/displaymode.cpp
Expand Up @@ -37,7 +37,7 @@

static bool inited = false;
static DisplayColorTransfer originalColorTransfer;
static de::Record *bindings;
static de::Binder binder;

static float differenceToOriginalHz(float hz);

Expand Down Expand Up @@ -174,25 +174,6 @@ static de::Value *Binding_DisplayMode_OriginalMode(de::Context &, de::Function::
return dict;
}

static void initBindings()
{
de::Function::registerNativeEntryPoint("DisplayMode_OriginalMode", Binding_DisplayMode_OriginalMode);

bindings = new de::Record;
bindings->addFunction("originalMode",
de::refless(new de::Function("DisplayMode_OriginalMode"))).setReadOnly();

de::App::scriptSystem().addNativeModule("DisplayMode", *bindings);
}

static void deinitBindings()
{
delete bindings; // App observes
bindings = 0;

de::Function::unregisterNativeEntryPoint("DisplayMode_OriginalMode");
}

int DisplayMode_Init(void)
{
if(inited) return true;
Expand Down Expand Up @@ -223,7 +204,9 @@ int DisplayMode_Init(void)
i->debugPrint();
}

initBindings();
// Script bindings.
binder.initNew() << DENG2_FUNC_NOARG(DisplayMode_OriginalMode, "originalMode");
de::App::scriptSystem().addNativeModule("DisplayMode", binder.module());

inited = true;
return true;
Expand All @@ -233,7 +216,7 @@ void DisplayMode_Shutdown(void)
{
if(!inited) return;

deinitBindings();
binder.deinit();

LOG_GL_NOTE("Restoring original display mode due to shutdown");

Expand Down

0 comments on commit 8758931

Please sign in to comment.