Move all Lua initialization out of PiGui.cpp

- Now LuaPiGui handles it, decoupling the PiGui implementation from lua
- Made all externally-facing symbols in LuaPiGui part of the PiGUI namespace
This will be renamed to PiGui and the class of the same name will become part of the namespace.
master
Webster Sheets 2020-03-30 23:32:52 -04:00
parent 7d70c7f75f
commit a1745867ff
8 changed files with 114 additions and 114 deletions

View File

@ -28,6 +28,7 @@
#include "lua/Lua.h"
#include "lua/LuaConsole.h"
#include "lua/LuaEvent.h"
#include "lua/LuaPiGui.h"
#include "lua/LuaTimer.h"
#include "profiler/Profiler.h"
#include "sound/AmbientSounds.h"
@ -493,10 +494,11 @@ void LoadStep::Start()
// and then load all the lua-related state once Lua's registered and online...
Pi::pigui.Reset(new PiGui);
Pi::pigui->Init(Pi::renderer->GetSDLWindow());
LuaObject<PiGui>::RegisterClass();
// Don't render the first frame, just make sure all of our fonts are loaded
Pi::pigui->NewFrame(Pi::renderer->GetSDLWindow());
Pi::pigui->RunHandler(0.01, "INIT");
PiGUI::RunHandler(0.01, "INIT");
Pi::pigui->EndFrame();
AddStep("UI::AddContext", []() {
@ -609,7 +611,7 @@ void LoadStep::Update(float deltaTime)
loader.name.c_str(), timer.milliseconds());
Pi::pigui->NewFrame(Pi::renderer->GetSDLWindow());
Pi::pigui->RunHandler(progress, "INIT");
PiGUI::RunHandler(progress, "INIT");
Pi::pigui->Render();
} else {
@ -679,7 +681,7 @@ void MainMenu::Update(float deltaTime)
Pi::intro->Draw(deltaTime);
Pi::pigui->NewFrame(Pi::renderer->GetSDLWindow());
Pi::pigui->RunHandler(deltaTime, "MAINMENU");
PiGUI::RunHandler(deltaTime, "MAINMENU");
Pi::pigui->Render();
@ -1137,7 +1139,7 @@ void GameLoop::Update(float deltaTime)
Pi::game->GetWorldView()->BeginCameraFrame();
// FIXME: major hack to work around the fact that the console is in newUI and not pigui
if (!Pi::IsConsoleActive())
Pi::pigui->RunHandler(deltaTime, "GAME");
PiGUI::RunHandler(deltaTime, "GAME");
Pi::game->GetWorldView()->EndCameraFrame();
}

View File

@ -107,7 +107,6 @@ namespace Lua {
GameUI::Lua::Init();
SceneGraph::Lua::Init();
LuaObject<PiGui>::RegisterClass();
PiGUI::Lua::Init();
// XXX load everything. for now, just modules

View File

@ -24,11 +24,12 @@
#include "Ship.h"
#include "SpaceStation.h"
#include "Star.h"
#include "HyperspaceCloud.h"
// Defined in LuaPiGui.h
extern bool first_body_is_more_important_than(Body*, Body*);
extern int pushOnScreenPositionDirection(lua_State *l, vector3d position);
namespace PiGUI {
// Defined in LuaPiGui.h
extern bool first_body_is_more_important_than(Body *, Body *);
extern int pushOnScreenPositionDirection(lua_State *l, vector3d position);
} // namespace PiGUI
/*
* Class: Body
@ -246,7 +247,7 @@ static int l_body_is_more_important_than(lua_State *l)
LuaPush<bool>(l, false);
return 1;
}
LuaPush<bool>(l, first_body_is_more_important_than(body, other));
LuaPush<bool>(l, PiGUI::first_body_is_more_important_than(body, other));
return 1;
}
/*
@ -653,7 +654,7 @@ static int l_body_get_projected_screen_position(lua_State *l)
Body *b = LuaObject<Body>::CheckFromLua(1);
WorldView *wv = Pi::game->GetWorldView();
vector3d p = wv->WorldSpaceToScreenSpace(b);
return pushOnScreenPositionDirection(l, p);
return PiGUI::pushOnScreenPositionDirection(l, p);
}
static int l_body_get_atmospheric_state(lua_State *l)
@ -685,7 +686,7 @@ static int l_body_get_target_indicator_screen_position(lua_State *l)
Body *b = LuaObject<Body>::CheckFromLua(1);
WorldView *wv = Pi::game->GetWorldView();
vector3d p = wv->GetTargetIndicatorScreenPosition(b);
return pushOnScreenPositionDirection(l, p);
return PiGUI::pushOnScreenPositionDirection(l, p);
}
static bool push_body_to_lua(Body *body)

View File

@ -785,7 +785,7 @@ static int l_engine_world_space_to_screen_space(lua_State *l)
{
vector3d pos = LuaPull<vector3d>(l, 1);
TScreenSpace res = lua_world_space_to_screen_space(pos); // defined in LuaPiGui.cpp
PiGUI::TScreenSpace res = PiGUI::lua_world_space_to_screen_space(pos); // defined in LuaPiGui.cpp
LuaPush<bool>(l, res._onScreen);
LuaPush<vector2d>(l, res._screenPosition);
@ -1077,7 +1077,7 @@ static int l_engine_get_sector_map_factions(lua_State *l)
lua_setfield(l, -2, "faction");
lua_pushboolean(l, hidden.count(f) == 0);
lua_setfield(l, -2, "visible"); // inner table
lua_settable(l, -3); // outer table
lua_settable(l, -3); // outer table
}
return 1;
}

View File

@ -29,6 +29,33 @@
// undef it, to avoid including yet another header that undefs it
#undef RegisterClass
LuaRef m_handlers;
LuaRef PiGUI::GetHandlers() { return m_handlers; }
LuaRef m_keys;
LuaRef PiGUI::GetKeys() { return m_keys; }
static std::vector<std::pair<std::string, int>> m_keycodes = {
{ "left", SDLK_LEFT },
{ "right", SDLK_RIGHT },
{ "up", SDLK_UP },
{ "down", SDLK_DOWN },
{ "escape", SDLK_ESCAPE },
{ "f1", SDLK_F1 },
{ "f2", SDLK_F2 },
{ "f3", SDLK_F3 },
{ "f4", SDLK_F4 },
{ "f5", SDLK_F5 },
{ "f6", SDLK_F6 },
{ "f7", SDLK_F7 },
{ "f8", SDLK_F8 },
{ "f9", SDLK_F9 },
{ "f10", SDLK_F10 },
{ "f11", SDLK_F11 },
{ "f12", SDLK_F12 },
{ "tab", SDLK_TAB },
};
template <typename Type>
static Type parse_imgui_flags(lua_State *l, int index, LuaFlags<Type> &lookupTable)
{
@ -77,7 +104,7 @@ void pi_lua_generic_pull(lua_State *l, int index, ImVec2 &vec)
vec = ImVec2(tr.x, tr.y);
}
int pushOnScreenPositionDirection(lua_State *l, vector3d position)
int PiGUI::pushOnScreenPositionDirection(lua_State *l, vector3d position)
{
PROFILE_SCOPED()
const int width = Graphics::GetScreenWidth();
@ -1560,7 +1587,7 @@ static int l_pigui_get_mouse_clicked_pos(lua_State *l)
return 1;
}
TScreenSpace lua_world_space_to_screen_space(const vector3d &pos)
PiGUI::TScreenSpace PiGUI::lua_world_space_to_screen_space(const vector3d &pos)
{
PROFILE_SCOPED()
const WorldView *wv = Pi::game->GetWorldView();
@ -1569,13 +1596,13 @@ TScreenSpace lua_world_space_to_screen_space(const vector3d &pos)
const int height = Graphics::GetScreenHeight();
const vector3d direction = (p - vector3d(width / 2, height / 2, 0)).Normalized();
if (vector3d(0, 0, 0) == p || p.x < 0 || p.y < 0 || p.x > width || p.y > height || p.z > 0) {
return TScreenSpace(false, vector2d(0, 0), direction * (p.z > 0 ? -1 : 1));
return PiGUI::TScreenSpace(false, vector2d(0, 0), direction * (p.z > 0 ? -1 : 1));
} else {
return TScreenSpace(true, vector2d(p.x, p.y), direction);
return PiGUI::TScreenSpace(true, vector2d(p.x, p.y), direction);
}
}
TScreenSpace lua_world_space_to_screen_space(const Body *body)
PiGUI::TScreenSpace lua_world_space_to_screen_space(const Body *body)
{
PROFILE_SCOPED()
const WorldView *wv = Pi::game->GetWorldView();
@ -1584,13 +1611,13 @@ TScreenSpace lua_world_space_to_screen_space(const Body *body)
const int height = Graphics::GetScreenHeight();
const vector3d direction = (p - vector3d(width / 2, height / 2, 0)).Normalized();
if (vector3d(0, 0, 0) == p || p.x < 0 || p.y < 0 || p.x > width || p.y > height || p.z > 0) {
return TScreenSpace(false, vector2d(0, 0), direction * (p.z > 0 ? -1 : 1));
return PiGUI::TScreenSpace(false, vector2d(0, 0), direction * (p.z > 0 ? -1 : 1));
} else {
return TScreenSpace(true, vector2d(p.x, p.y), direction);
return PiGUI::TScreenSpace(true, vector2d(p.x, p.y), direction);
}
}
bool first_body_is_more_important_than(Body *body, Body *other)
bool PiGUI::first_body_is_more_important_than(Body *body, Body *other)
{
Object::Type a = body->GetType();
@ -1720,7 +1747,7 @@ static int l_pigui_get_projected_bodies_grouped(lua_State *l)
const double cluster_size = LuaPull<double>(l, 1);
const double ship_max_distance = LuaPull<double>(l, 2);
TSS_vector filtered;
PiGUI::TSS_vector filtered;
filtered.reserve(Pi::game->GetSpace()->GetNumBodies());
for (Body *body : Pi::game->GetSpace()->GetBodies()) {
@ -1728,7 +1755,7 @@ static int l_pigui_get_projected_bodies_grouped(lua_State *l)
if (body->GetType() == Object::PROJECTILE) continue;
if (body->GetType() == Object::SHIP &&
body->GetPositionRelTo(Pi::player).Length() > ship_max_distance) continue;
const TScreenSpace res = lua_world_space_to_screen_space(body); // defined in LuaPiGui.cpp
const PiGUI::TScreenSpace res = lua_world_space_to_screen_space(body); // defined in LuaPiGui.cpp
if (!res._onScreen) continue;
filtered.emplace_back(res);
filtered.back()._body = body;
@ -1757,7 +1784,7 @@ static int l_pigui_get_projected_bodies_grouped(lua_State *l)
const Body *combat_target = Pi::game->GetPlayer()->GetCombatTarget();
const Body *setspeed_target = Pi::game->GetPlayer()->GetSetSpeedTarget();
for (TScreenSpace &obj : filtered) {
for (PiGUI::TScreenSpace &obj : filtered) {
bool inserted = false;
// never collapse combat target
@ -1773,7 +1800,7 @@ static int l_pigui_get_projected_bodies_grouped(lua_State *l)
group.m_hasNavTarget = true;
group.m_mainBody = obj._body;
group.m_screenCoords = obj._screenPosition;
} else if (!group.m_hasNavTarget && first_body_is_more_important_than(obj._body, group.m_mainBody)) {
} else if (!group.m_hasNavTarget && PiGUI::first_body_is_more_important_than(obj._body, group.m_mainBody)) {
group.m_mainBody = obj._body;
group.m_screenCoords = obj._screenPosition;
}
@ -1797,7 +1824,7 @@ static int l_pigui_get_projected_bodies_grouped(lua_State *l)
for (GroupInfo &group : groups) {
std::sort(begin(group.m_bodies), end(group.m_bodies),
[](Body *a, Body *b) {
return first_body_is_more_important_than(a, b);
return PiGUI::first_body_is_more_important_than(a, b);
});
}
@ -1826,19 +1853,19 @@ static int l_pigui_get_projected_bodies_grouped(lua_State *l)
static int l_pigui_get_projected_bodies(lua_State *l)
{
PROFILE_SCOPED()
TSS_vector filtered;
PiGUI::TSS_vector filtered;
filtered.reserve(Pi::game->GetSpace()->GetNumBodies());
for (Body *body : Pi::game->GetSpace()->GetBodies()) {
if (body == Pi::game->GetPlayer()) continue;
if (body->GetType() == Object::PROJECTILE) continue;
const TScreenSpace res = lua_world_space_to_screen_space(body); // defined in LuaPiGui.cpp
const PiGUI::TScreenSpace res = lua_world_space_to_screen_space(body); // defined in LuaPiGui.cpp
if (!res._onScreen) continue;
filtered.emplace_back(res);
filtered.back()._body = body;
}
LuaTable result(l, 0, filtered.size());
for (TScreenSpace &res : filtered) {
for (PiGUI::TScreenSpace &res : filtered) {
LuaTable object(l, 0, 3);
object.Set("onscreen", res._onScreen);
@ -1942,7 +1969,7 @@ static int l_attr_handlers(lua_State *l)
{
PROFILE_SCOPED()
PiGui *pigui = LuaObject<PiGui>::CheckFromLua(1);
pigui->GetHandlers().PushCopyToStack();
PiGUI::GetHandlers().PushCopyToStack();
return 1;
}
@ -1950,7 +1977,7 @@ static int l_attr_keys(lua_State *l)
{
PROFILE_SCOPED()
PiGui *pigui = LuaObject<PiGui>::CheckFromLua(1);
pigui->GetKeys().PushCopyToStack();
PiGUI::GetKeys().PushCopyToStack();
return 1;
}
@ -2467,6 +2494,16 @@ static int l_pigui_push_text_wrap_pos(lua_State *l)
return 0;
}
void PiGUI::RunHandler(double delta, std::string handler)
{
PROFILE_SCOPED()
ScopedTable t(m_handlers);
if (t.Get<bool>(handler)) {
t.Call<bool>(handler, delta);
Pi::renderer->CheckRenderErrors(__FUNCTION__, __LINE__);
}
}
template <>
const char *LuaObject<PiGui>::s_type = "PiGui";
@ -2629,4 +2666,14 @@ void LuaObject<PiGui>::RegisterClass()
imguiStyleVarTable.Register(l, "ImGuiStyleVar");
imguiWindowFlagsTable.Register(l, "ImGuiWindowFlags");
imguiHoveredFlagsTable.Register(l, "ImGuiHoveredFlags");
lua_newtable(l);
m_handlers = LuaRef(l, -1);
lua_newtable(l);
m_keys = LuaRef(l, -1);
LuaTable keys(l, -1);
for (auto p : m_keycodes) {
keys.Set(p.first, p.second);
}
}

View File

@ -3,6 +3,7 @@
#ifndef _LUAPIGUI_H
#define _LUAPIGUI_H
#include "LuaObject.h"
#include "LuaPushPull.h"
@ -11,19 +12,30 @@
class Body;
bool first_body_is_more_important_than(Body* body, Body* other);
namespace PiGUI {
bool first_body_is_more_important_than(Body *body, Body *other);
struct TScreenSpace
{
TScreenSpace(const bool onScreen, const vector2d &screenPos, const vector3d &direction) : _onScreen(onScreen), _screenPosition(screenPos), _direction(direction) {}
bool _onScreen;
vector2d _screenPosition;
vector3d _direction;
Body *_body;
};
struct TScreenSpace {
TScreenSpace(const bool onScreen, const vector2d &screenPos, const vector3d &direction) :
_onScreen(onScreen), _screenPosition(screenPos), _direction(direction) {}
bool _onScreen;
vector2d _screenPosition;
vector3d _direction;
Body *_body;
};
typedef std::vector<TScreenSpace> TSS_vector;
typedef std::vector<TScreenSpace> TSS_vector;
int pushOnScreenPositionDirection(lua_State *l, vector3d position);
TScreenSpace lua_world_space_to_screen_space(const vector3d &pos);
// Get registered PiGui handlers.
LuaRef GetHandlers();
// Get a table of key name to SDL-keycode mappings
LuaRef GetKeys();
// Run a lua PiGui handler.
void RunHandler(double delta, std::string handler = "GAME");
} // namespace PiGUI
int pushOnScreenPositionDirection(lua_State *l, vector3d position);
TScreenSpace lua_world_space_to_screen_space(const vector3d &pos);
#endif

View File

@ -26,35 +26,6 @@
std::vector<Graphics::Texture *> PiGui::m_svg_textures;
static int to_keycode(int key)
{
/*if(key & SDLK_SCANCODE_MASK) {
return (key & ~SDLK_SCANCODE_MASK) | 0x100;
}*/
return key;
}
static std::vector<std::pair<std::string, int>> keycodes = {
{ "left", to_keycode(SDLK_LEFT) },
{ "right", to_keycode(SDLK_RIGHT) },
{ "up", to_keycode(SDLK_UP) },
{ "down", to_keycode(SDLK_DOWN) },
{ "escape", to_keycode(SDLK_ESCAPE) },
{ "f1", to_keycode(SDLK_F1) },
{ "f2", to_keycode(SDLK_F2) },
{ "f3", to_keycode(SDLK_F3) },
{ "f4", to_keycode(SDLK_F4) },
{ "f5", to_keycode(SDLK_F5) },
{ "f6", to_keycode(SDLK_F6) },
{ "f7", to_keycode(SDLK_F7) },
{ "f8", to_keycode(SDLK_F8) },
{ "f9", to_keycode(SDLK_F9) },
{ "f10", to_keycode(SDLK_F10) },
{ "f11", to_keycode(SDLK_F11) },
{ "f12", to_keycode(SDLK_F12) },
{ "tab", to_keycode(SDLK_TAB) },
};
ImTextureID PiGui::RenderSVG(std::string svgFilename, int width, int height)
{
PROFILE_SCOPED()
@ -192,18 +163,6 @@ void PiDefaultStyle(ImGuiStyle &style)
void PiGui::Init(SDL_Window *window)
{
PROFILE_SCOPED()
m_handlers.Unref();
lua_State *l = Lua::manager->GetLuaState();
lua_newtable(l);
m_handlers = LuaRef(l, -1);
lua_newtable(l);
m_keys = LuaRef(l, -1);
LuaTable keys(l, -1);
for (auto p : keycodes) {
keys.Set(p.first, p.second);
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
@ -402,16 +361,6 @@ void PiGui::NewFrame(SDL_Window *window)
ImGui::SetMouseCursor(ImGuiMouseCursor_Arrow);
}
void PiGui::RunHandler(double delta, std::string handler)
{
PROFILE_SCOPED()
ScopedTable t(m_handlers);
if (t.Get<bool>(handler)) {
t.Call<bool>(handler, delta);
Pi::renderer->CheckRenderErrors(__FUNCTION__, __LINE__);
}
}
void PiGui::EndFrame()
{
PROFILE_SCOPED()
@ -719,7 +668,7 @@ bool PiGui::ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, co
return pressed;
}
void PiGui::Cleanup()
void PiGui::Uninit()
{
PROFILE_SCOPED()
for (auto tex : m_svg_textures) {

View File

@ -1,13 +1,15 @@
// Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#pragma once
#include "FileSystem.h"
#include "RefCounted.h"
#include "graphics/opengl/RendererGL.h"
#include "imgui/imgui.h"
#include "lua/Lua.h"
#include "lua/LuaRef.h"
#include "lua/LuaTable.h"
#include "utils.h"
#include <unordered_set>
class PiFace {
@ -78,12 +80,6 @@ class PiGui : public RefCounted {
public:
PiGui();
LuaRef GetHandlers() const { return m_handlers; }
LuaRef GetKeys() const { return m_keys; }
void RunHandler(double delta, std::string handler = "GAME");
// Call at the start of every frame. Calls ImGui::NewFrame() internally.
void NewFrame(SDL_Window *window);
@ -97,12 +93,8 @@ public:
ImFont *GetFont(const std::string &name, int size);
void Uninit()
{
Cleanup();
m_handlers.Unref();
m_keys.Unref();
}
void Uninit();
ImFont *AddFont(const std::string &name, int size);
void AddGlyph(ImFont *font, unsigned short glyph);
@ -124,17 +116,15 @@ public:
{
return ImGui::GetIO().WantCaptureKeyboard;
}
static int RadialPopupSelectMenu(const ImVec2 &center, std::string popup_id, int mouse_button, std::vector<ImTextureID> tex_ids, std::vector<std::pair<ImVec2, ImVec2>> uvs, unsigned int size, std::vector<std::string> tooltips);
static bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max);
void Cleanup();
static bool LowThrustButton(const char *label, const ImVec2 &size_arg, int thrust_level, const ImVec4 &bg_col, int frame_padding, ImColor gauge_fg, ImColor gauge_bg);
static bool ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &imgSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col);
static void ThrustIndicator(const std::string &id_string, const ImVec2 &size, const ImVec4 &thrust, const ImVec4 &velocity, const ImVec4 &bg_col, int frame_padding, ImColor vel_fg, ImColor vel_bg, ImColor thrust_fg, ImColor thrust_bg);
private:
LuaRef m_handlers;
LuaRef m_keys;
static std::vector<Graphics::Texture *> m_svg_textures;
};