Commit f93acbfc by NTAuthority

some reshuffling of console management/binding contexts, key catchers for the gfx console

parent 3c8d8ce7
......@@ -166,7 +166,7 @@ enum ConsoleVariableFlags
ConVar_Modified = 2
};
class ConsoleVariableManager : public ConsoleVariableManagerProxy
class ConsoleVariableManager
{
public:
using THandlerPtr = std::shared_ptr<internal::ConsoleVariableEntryBase>;
......
......@@ -5,13 +5,14 @@
namespace krt
{
class ConsoleVariableManagerProxy
class ConsoleManagersBase
{
public:
virtual ~ConsoleVariableManagerProxy() = default;
virtual ~ConsoleManagersBase() = default;
};
class ConsoleVariableManager;
class BindingManager;
namespace console
{
......@@ -34,15 +35,11 @@ public:
void SetVariableModifiedFlags(int flags);
inline ConsoleCommandManager* GetCommandManager()
{
return m_commandManager.get();
}
ConsoleCommandManager* GetCommandManager();
inline ConsoleVariableManager* GetVariableManager()
{
return (ConsoleVariableManager*)m_variableManager.get();
}
ConsoleVariableManager* GetVariableManager();
BindingManager* GetBindingManager();
inline Context* GetFallbackContext()
{
......@@ -54,9 +51,7 @@ private:
int m_variableModifiedFlags;
std::unique_ptr<ConsoleCommandManager> m_commandManager;
std::unique_ptr<ConsoleVariableManagerProxy> m_variableManager;
std::unique_ptr<ConsoleManagersBase> m_managers;
std::string m_commandBuffer;
......
......@@ -13,6 +13,15 @@ namespace krt
{
namespace console
{
struct ConsoleManagers : public ConsoleManagersBase
{
std::unique_ptr<BindingManager> bindingManager;
std::unique_ptr<ConsoleCommandManager> commandManager;
std::unique_ptr<ConsoleVariableManager> variableManager;
};
Context::Context()
: Context(GetDefaultContext())
{
......@@ -22,12 +31,40 @@ Context::Context()
Context::Context(Context* fallbackContext)
: m_fallbackContext(fallbackContext)
{
m_commandManager = std::make_unique<ConsoleCommandManager>(this);
m_variableManager = std::make_unique<ConsoleVariableManager>(this);
m_managers = std::make_unique<ConsoleManagers>();
ConsoleManagers* managers = static_cast<ConsoleManagers*>(m_managers.get());
managers->commandManager = std::make_unique<ConsoleCommandManager>(this);
managers->variableManager = std::make_unique<ConsoleVariableManager>(this);
if (m_fallbackContext == nullptr)
{
managers->bindingManager = std::make_unique<BindingManager>(this);
}
m_variableModifiedFlags = 0;
}
ConsoleVariableManager* Context::GetVariableManager()
{
return static_cast<ConsoleManagers*>(m_managers.get())->variableManager.get();
}
ConsoleCommandManager* Context::GetCommandManager()
{
return static_cast<ConsoleManagers*>(m_managers.get())->commandManager.get();
}
BindingManager* Context::GetBindingManager()
{
if (m_fallbackContext)
{
return m_fallbackContext->GetBindingManager();
}
return static_cast<ConsoleManagers*>(m_managers.get())->bindingManager.get();
}
void Context::ExecuteSingleCommand(const std::string& command)
{
ProgramArguments arguments = Tokenize(command);
......@@ -48,7 +85,7 @@ void Context::ExecuteSingleCommand(const ProgramArguments& arguments)
std::string command = localArgs.Shift();
// run the command through the command manager
m_commandManager->Invoke(command, localArgs);
GetCommandManager()->Invoke(command, localArgs);
}
void Context::AddToBuffer(const std::string& text)
......@@ -114,7 +151,7 @@ void Context::ExecuteBuffer()
}
}
static void SaveConfiguration(const std::string& path, ConsoleVariableManager* manager)
static void SaveConfiguration(const std::string& path, ConsoleVariableManager* varMan, BindingManager* bindMan)
{
vfs::DevicePtr device = vfs::GetDevice(path);
......@@ -136,8 +173,8 @@ static void SaveConfiguration(const std::string& path, ConsoleVariableManager* m
writeLine("// generated by ATG, do not modify unless meow");
// save the actual configuration
g_bindings->WriteBindings(writeLine);
manager->SaveConfiguration(writeLine);
bindMan->WriteBindings(writeLine);
varMan->SaveConfiguration(writeLine);
device->Close(handle);
}
......@@ -154,7 +191,7 @@ void Context::SaveConfigurationIfNeeded(const std::string& path)
{
console::DPrintf("Saving ^1c^2o^3n^4f^5i^6g^7uration to %s...\n", path.c_str());
SaveConfiguration(path, GetVariableManager());
SaveConfiguration(path, GetVariableManager(), GetBindingManager());
wasSavedBefore = true;
......
......@@ -44,7 +44,7 @@ public:
using TWriteLineCB = std::function<void(const std::string& line)>;
public:
BindingManager();
BindingManager(console::Context* context);
void WriteBindings(const TWriteLineCB& writeLineFunction);
......@@ -56,6 +56,8 @@ private:
std::unique_ptr<ConsoleCommand> m_unbindAllCommand;
std::unique_ptr<EventListener<KeyEvent>> m_keyListener;
std::map<KeyCode, std::string> m_bindings;
};
......
......@@ -283,9 +283,9 @@ static std::map<std::string, KeyCode, IgnoreCaseLess> keyNameMap =
{ "Sleep", KeyCode::Sleep }
};
BindingManager::BindingManager()
BindingManager::BindingManager(console::Context* context)
{
m_bindCommand = std::make_unique<ConsoleCommand>("bind", [=] (const std::string& keyName, const std::string& commandString)
m_bindCommand = std::make_unique<ConsoleCommand>(context, "bind", [=] (const std::string& keyName, const std::string& commandString)
{
auto it = keyNameMap.find(keyName);
......@@ -300,7 +300,7 @@ BindingManager::BindingManager()
console::SetVariableModifiedFlags(ConVar_Archive);
});
m_unbindCommand = std::make_unique<ConsoleCommand>("unbind", [=] (const std::string& keyName)
m_unbindCommand = std::make_unique<ConsoleCommand>(context, "unbind", [=] (const std::string& keyName)
{
auto it = keyNameMap.find(keyName);
......@@ -315,10 +315,12 @@ BindingManager::BindingManager()
console::SetVariableModifiedFlags(ConVar_Archive);
});
m_unbindAllCommand = std::make_unique<ConsoleCommand>("unbindall", [=] ()
m_unbindAllCommand = std::make_unique<ConsoleCommand>(context, "unbindall", [=] ()
{
m_bindings.clear();
});
m_keyListener = std::make_unique<EventListener<KeyEvent>>(std::bind(&BindingManager::HandleKeyEvent, this, std::placeholders::_1));
}
void BindingManager::HandleKeyEvent(const KeyEvent* ev)
......@@ -412,7 +414,4 @@ void BindingManager::WriteBindings(const TWriteLineCB& writeLineFunction)
writeLineFunction("bind \"" + reverseKeyMap[binding.first] + "\" \"" + binding.second + "\"");
}
}
static BindingManager g_bindingMgr;
BindingManager* g_bindings = &g_bindingMgr;
}
\ No newline at end of file
......@@ -282,17 +282,43 @@ class MouseEvent : public Event
template <typename T>
struct EventListener
{
struct State
{
std::set<EventListener*> listeners;
EventListener* catcher;
State()
: catcher(nullptr)
{
}
};
template <typename TCallback>
EventListener(const TCallback& callback)
{
m_handler = callback;
ms_listeners.insert(this);
GetState()->listeners.insert(this);
}
~EventListener()
{
ms_listeners.erase(this);
GetState()->listeners.erase(this);
}
void Catch()
{
assert(GetState()->catcher == nullptr);
GetState()->catcher = this;
}
void Release()
{
assert(GetState()->catcher == this);
GetState()->catcher = nullptr;
}
private:
......@@ -300,22 +326,32 @@ struct EventListener
// static members
public:
using TSet = std::set<EventListener*>;
static void Handle(const T* event)
{
for (auto& listener : ms_listeners)
for (auto& listener : GetState()->listeners)
{
// ignore if no catcher is set
if (GetState()->catcher && GetState()->catcher != listener)
{
continue;
}
listener->m_handler(event);
}
}
private:
static TSet ms_listeners;
// this is not a dynamic initializer due to initializer order
static State* GetState()
{
static State state;
return &state;
}
};
#define DECLARE_EVENT_LISTENER(t) \
EventListener<t>::TSet EventListener<t>::ms_listeners;
class EventSystem
{
......
......@@ -38,8 +38,6 @@ public:
void KeyEvent::Handle()
{
g_bindings->HandleKeyEvent(this);
EventListener<KeyEvent>::Handle(this);
}
......
......@@ -57,7 +57,7 @@ private:
};
GfxConsole::GfxConsole()
: m_screenWidth(1920), m_screenHeight(1080), m_active(true), m_keyListener(std::bind(&GfxConsole::HandleKey, this, std::placeholders::_1)), m_charListener(std::bind(&GfxConsole::HandleChar, this, std::placeholders::_1)), m_screenTop(0)
: m_screenWidth(1920), m_screenHeight(1080), m_active(false), m_keyListener(std::bind(&GfxConsole::HandleKey, this, std::placeholders::_1)), m_charListener(std::bind(&GfxConsole::HandleChar, this, std::placeholders::_1)), m_screenTop(0)
{
m_screenBuffer.resize(1);
m_screenSize = 1;
......@@ -79,7 +79,14 @@ void GfxConsole::HandleKey(const KeyEvent* ev)
{
m_active = !m_active;
// TODO: mark a key catcher
if (m_active)
{
m_keyListener.Catch();
}
else
{
m_keyListener.Release();
}
}
// return if not active
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment