openspades/Sources/Gui/MainScreen.cpp

363 lines
9.7 KiB
C++
Raw Normal View History

2013-11-22 22:50:14 +09:00
/*
Copyright (c) 2013 yvt
2013-11-22 22:50:14 +09:00
This file is part of OpenSpades.
2013-11-22 22:50:14 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-11-22 22:50:14 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-11-22 22:50:14 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
2013-11-22 22:50:14 +09:00
#include "MainScreen.h"
2013-11-23 22:13:04 +09:00
#include "MainScreenHelper.h"
2013-11-24 02:27:08 +09:00
#include <Client/Client.h>
2014-02-08 22:37:46 +09:00
#include <Client/Fonts.h>
#include <Core/Exception.h>
2014-02-14 16:24:10 +09:00
#include <Core/Strings.h>
#include <ScriptBindings/Config.h>
#include <ScriptBindings/ScriptFunction.h>
2013-11-24 02:27:08 +09:00
2013-11-22 22:50:14 +09:00
namespace spades {
namespace gui {
MainScreen::MainScreen(client::IRenderer *r, client::IAudioDevice *a,
client::FontManager *fontManager)
: renderer(r), audioDevice(a), fontManager(fontManager) {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
if (r == NULL)
2013-11-22 22:50:14 +09:00
SPInvalidArgument("r");
if (a == NULL)
2013-11-22 22:50:14 +09:00
SPInvalidArgument("a");
2013-11-23 22:13:04 +09:00
helper = new MainScreenHelper(this);
2013-11-23 22:13:04 +09:00
// first call to RunFrame tends to have larger dt value.
// so this value is set in the first call.
timeToStartInitialization = 1000.f;
2013-11-22 22:50:14 +09:00
}
MainScreen::~MainScreen() {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
helper->MainScreenDestroyed();
2013-11-22 22:50:14 +09:00
}
2013-11-24 02:27:08 +09:00
// Restores renderer's state (game map, fog color)
// after returning from the game client.
void MainScreen::RestoreRenderer() {
ScopedPrivilegeEscalation privilege;
2013-11-24 02:27:08 +09:00
static ScriptFunction func("MainScreenUI", "void SetupRenderer()");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c.ExecuteChecked();
}
2014-02-01 21:55:30 +09:00
bool MainScreen::NeedsAbsoluteMouseCoordinate() {
SPADES_MARK_FUNCTION();
if (subview) {
2014-02-01 21:55:30 +09:00
return subview->NeedsAbsoluteMouseCoordinate();
}
return true;
}
2013-11-22 22:50:14 +09:00
void MainScreen::MouseEvent(float x, float y) {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
if (subview) {
2013-11-22 22:50:14 +09:00
subview->MouseEvent(x, y);
return;
}
if (!ui) {
2013-11-23 22:13:04 +09:00
return;
}
ScopedPrivilegeEscalation privilege;
2013-11-23 22:13:04 +09:00
static ScriptFunction func("MainScreenUI", "void MouseEvent(float, float)");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c->SetArgFloat(0, x);
c->SetArgFloat(1, y);
c.ExecuteChecked();
2013-11-22 22:50:14 +09:00
}
2013-12-09 17:36:05 +09:00
void MainScreen::WheelEvent(float x, float y) {
SPADES_MARK_FUNCTION();
if (subview) {
2013-12-09 17:36:05 +09:00
subview->WheelEvent(x, y);
return;
}
if (!ui) {
2013-12-09 17:36:05 +09:00
return;
}
ScopedPrivilegeEscalation privilege;
2013-12-09 17:36:05 +09:00
static ScriptFunction func("MainScreenUI", "void WheelEvent(float, float)");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c->SetArgFloat(0, x);
c->SetArgFloat(1, y);
c.ExecuteChecked();
}
void MainScreen::KeyEvent(const std::string &key, bool down) {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
if (subview) {
2013-11-22 22:50:14 +09:00
subview->KeyEvent(key, down);
return;
}
if (!ui) {
2013-11-23 22:13:04 +09:00
return;
2013-11-22 22:50:14 +09:00
}
ScopedPrivilegeEscalation privilege;
2013-11-23 22:13:04 +09:00
static ScriptFunction func("MainScreenUI", "void KeyEvent(string, bool)");
ScriptContextHandle c = func.Prepare();
std::string k = key;
c->SetObject(&*ui);
c->SetArgObject(0, reinterpret_cast<void *>(&k));
2013-11-23 22:13:04 +09:00
c->SetArgByte(1, down ? 1 : 0);
c.ExecuteChecked();
2013-11-22 22:50:14 +09:00
}
2013-12-09 17:36:05 +09:00
void MainScreen::TextInputEvent(const std::string &ch) {
SPADES_MARK_FUNCTION();
if (subview) {
2013-12-09 17:36:05 +09:00
subview->TextInputEvent(ch);
return;
}
if (!ui) {
2013-12-09 17:36:05 +09:00
return;
}
ScopedPrivilegeEscalation privilege;
2013-12-09 17:36:05 +09:00
static ScriptFunction func("MainScreenUI", "void TextInputEvent(string)");
ScriptContextHandle c = func.Prepare();
std::string k = ch;
c->SetObject(&*ui);
c->SetArgObject(0, reinterpret_cast<void *>(&k));
2013-12-09 17:36:05 +09:00
c.ExecuteChecked();
}
void MainScreen::TextEditingEvent(const std::string &ch, int start, int len) {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
if (subview) {
2013-12-09 17:36:05 +09:00
subview->TextEditingEvent(ch, start, len);
2013-11-22 22:50:14 +09:00
return;
}
if (!ui) {
2013-11-23 22:13:04 +09:00
return;
}
ScopedPrivilegeEscalation privilege;
2013-12-09 17:36:05 +09:00
static ScriptFunction func("MainScreenUI", "void TextEditingEvent(string, int, int)");
2013-11-23 22:13:04 +09:00
ScriptContextHandle c = func.Prepare();
std::string k = ch;
c->SetObject(&*ui);
c->SetArgObject(0, reinterpret_cast<void *>(&k));
2013-12-09 17:36:05 +09:00
c->SetArgDWord(1, static_cast<asDWORD>(start));
c->SetArgDWord(2, static_cast<asDWORD>(len));
c.ExecuteChecked();
}
2013-12-09 17:36:05 +09:00
bool MainScreen::AcceptsTextInput() {
SPADES_MARK_FUNCTION();
if (subview) {
2013-12-09 17:36:05 +09:00
return subview->AcceptsTextInput();
}
if (!ui) {
2013-12-09 17:36:05 +09:00
return false;
}
ScopedPrivilegeEscalation privilege;
2013-12-09 17:36:05 +09:00
static ScriptFunction func("MainScreenUI", "bool AcceptsTextInput()");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c.ExecuteChecked();
return c->GetReturnByte() != 0;
}
2013-12-09 17:36:05 +09:00
AABB2 MainScreen::GetTextInputRect() {
SPADES_MARK_FUNCTION();
if (subview) {
2013-12-09 17:36:05 +09:00
return subview->GetTextInputRect();
}
if (!ui) {
2013-12-09 17:36:05 +09:00
return AABB2();
}
ScopedPrivilegeEscalation privilege;
2013-12-09 17:36:05 +09:00
static ScriptFunction func("MainScreenUI", "AABB2 GetTextInputRect()");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
2013-11-23 22:13:04 +09:00
c.ExecuteChecked();
return *reinterpret_cast<AABB2 *>(c->GetReturnObject());
2013-11-23 22:13:04 +09:00
}
2013-11-23 22:13:04 +09:00
bool MainScreen::WantsToBeClosed() {
SPADES_MARK_FUNCTION();
if (!ui) {
2013-11-23 22:13:04 +09:00
return false;
}
ScopedPrivilegeEscalation privilege;
2013-11-23 22:13:04 +09:00
static ScriptFunction func("MainScreenUI", "bool WantsToBeClosed()");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c.ExecuteChecked();
return c->GetReturnByte() != 0;
}
2013-11-23 22:13:04 +09:00
void MainScreen::DrawStartupScreen() {
SPADES_MARK_FUNCTION();
Handle<client::IImage> img;
Vector2 scrSize = {renderer->ScreenWidth(), renderer->ScreenHeight()};
renderer->SetColorAlphaPremultiplied(MakeVector4(0, 0, 0, 1.));
2013-11-23 22:13:04 +09:00
img = renderer->RegisterImage("Gfx/White.tga");
renderer->DrawImage(img, AABB2(0, 0, scrSize.x, scrSize.y));
2014-02-14 16:20:42 +09:00
std::string str = _Tr("MainScreen", "NOW LOADING");
client::IFont *font = fontManager->GetGuiFont();
2013-11-23 22:13:04 +09:00
Vector2 size = font->Measure(str);
Vector2 pos = MakeVector2(scrSize.x - 16.f, scrSize.y - 16.f);
pos -= size;
font->DrawShadow(str, pos, 1.f, MakeVector4(1, 1, 1, 1), MakeVector4(0, 0, 0, 0.5));
2013-11-23 22:13:04 +09:00
renderer->FrameDone();
renderer->Flip();
2013-11-22 22:50:14 +09:00
}
2013-11-22 22:50:14 +09:00
void MainScreen::RunFrame(float dt) {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
if (subview) {
try {
2013-11-24 02:27:08 +09:00
subview->RunFrame(dt);
return;
} catch (const std::exception &ex) {
SPLog("[!] Error while running a game client: %s", ex.what());
2013-11-24 02:27:08 +09:00
subview->Closing();
subview = NULL;
RestoreRenderer();
helper->errorMessage = ex.what();
}
2013-11-22 22:50:14 +09:00
}
if (timeToStartInitialization > 100.f) {
2013-11-23 22:13:04 +09:00
timeToStartInitialization = 0.2f;
}
if (timeToStartInitialization > 0.f) {
2013-11-23 22:13:04 +09:00
DrawStartupScreen();
timeToStartInitialization -= dt;
if (timeToStartInitialization <= 0.f) {
2013-11-23 22:13:04 +09:00
// do init
DoInit();
}
return;
}
ScopedPrivilegeEscalation privilege;
2013-11-23 22:13:04 +09:00
static ScriptFunction func("MainScreenUI", "void RunFrame(float)");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c->SetArgFloat(0, dt);
c.ExecuteChecked();
}
void MainScreen::RunFrameLate(float dt) {
SPADES_MARK_FUNCTION();
if (subview) {
try {
subview->RunFrameLate(dt);
if (subview->WantsToBeClosed()) {
subview->Closing();
subview = NULL;
RestoreRenderer();
} else {
return;
}
} catch (const std::exception &ex) {
SPLog("[!] Error while running a game client: %s", ex.what());
subview->Closing();
subview = NULL;
RestoreRenderer();
helper->errorMessage = ex.what();
}
}
if (!ui) {
return;
}
ScopedPrivilegeEscalation privilege;
static ScriptFunction func("MainScreenUI", "void RunFrameLate(float)");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c->SetArgFloat(0, dt);
c.ExecuteChecked();
}
2013-11-23 22:13:04 +09:00
void MainScreen::DoInit() {
SPADES_MARK_FUNCTION();
renderer->Init();
ScopedPrivilegeEscalation privilege;
static ScriptFunction uiFactory("MainScreenUI@ CreateMainScreenUI(Renderer@, "
"AudioDevice@, FontManager@, MainScreenHelper@)");
2013-11-23 22:13:04 +09:00
{
ScriptContextHandle ctx = uiFactory.Prepare();
ctx->SetArgObject(0, renderer);
ctx->SetArgObject(1, audioDevice);
ctx->SetArgObject(2, fontManager);
2013-11-23 22:13:04 +09:00
ctx->SetArgObject(3, &*helper);
2013-11-23 22:13:04 +09:00
ctx.ExecuteChecked();
ui = reinterpret_cast<asIScriptObject *>(ctx->GetReturnObject());
}
2013-11-22 22:50:14 +09:00
}
2013-11-22 22:50:14 +09:00
void MainScreen::Closing() {
2013-11-23 22:13:04 +09:00
SPADES_MARK_FUNCTION();
if (subview) {
2013-11-22 22:50:14 +09:00
subview->Closing();
subview = NULL;
}
if (!ui) {
2013-11-23 22:13:04 +09:00
return;
}
ScopedPrivilegeEscalation privilege;
2013-11-23 22:13:04 +09:00
static ScriptFunction func("MainScreenUI", "void Closing()");
ScriptContextHandle c = func.Prepare();
c->SetObject(&*ui);
c.ExecuteChecked();
2013-11-22 22:50:14 +09:00
}
bool MainScreen::ExecCommand(const Handle<ConsoleCommand> &cmd) {
SPADES_MARK_FUNCTION();
if (subview) {
return subview->ExecCommand(cmd);
}
return View::ExecCommand(cmd);
}
Handle<ConsoleCommandCandidateIterator>
MainScreen::AutocompleteCommandName(const std::string &name) {
SPADES_MARK_FUNCTION();
if (subview) {
return subview->AutocompleteCommandName(name);
}
return View::AutocompleteCommandName(name);
}
2017-01-05 03:03:39 +09:00
std::string MainScreen::Connect(const ServerAddress &host) {
2013-11-24 02:27:08 +09:00
try {
subview.Set(new client::Client(&*renderer, &*audioDevice, host, fontManager),
false);
} catch (const std::exception &ex) {
SPLog("[!] Error while initializing a game client: %s", ex.what());
2013-11-24 02:27:08 +09:00
return ex.what();
}
return "";
}
} // namespace gui
} // namespace spades