2014-06-20 01:30:09 +00:00
|
|
|
#include "guis/GuiDetectDevice.h"
|
|
|
|
#include "Window.h"
|
|
|
|
#include "Renderer.h"
|
|
|
|
#include "resources/Font.h"
|
|
|
|
#include "guis/GuiInputConfig.h"
|
|
|
|
#include "components/TextComponent.h"
|
2013-04-09 18:13:47 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Util.h"
|
2015-09-28 17:25:15 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/locale.hpp>
|
2013-04-09 18:13:47 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
#define HOLD_TIME 1000
|
2013-04-09 18:13:47 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
using namespace Eigen;
|
2013-04-09 18:13:47 +00:00
|
|
|
|
2015-09-28 17:25:15 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
GuiDetectDevice::GuiDetectDevice(Window* window, bool firstRun, const std::function<void()>& doneCallback) : GuiComponent(window), mFirstRun(firstRun),
|
2014-03-23 00:48:48 +00:00
|
|
|
mBackground(window, ":/frame.png"), mGrid(window, Vector2i(1, 5))
|
2013-04-09 18:13:47 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
mHoldingConfig = NULL;
|
|
|
|
mHoldTime = 0;
|
2014-06-20 01:30:09 +00:00
|
|
|
mDoneCallback = doneCallback;
|
2013-04-09 18:13:47 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
2014-06-20 01:30:09 +00:00
|
|
|
|
2014-03-23 00:48:48 +00:00
|
|
|
// title
|
|
|
|
mTitle = std::make_shared<TextComponent>(mWindow, firstRun ? "WELCOME" : "CONFIGURE INPUT",
|
2014-05-01 19:47:33 +00:00
|
|
|
Font::get(FONT_SIZE_LARGE), 0x555555FF, ALIGN_CENTER);
|
2014-03-23 00:48:48 +00:00
|
|
|
mGrid.setEntry(mTitle, Vector2i(0, 0), false, true, Vector2i(1, 1), GridFlags::BORDER_BOTTOM);
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2014-03-23 00:48:48 +00:00
|
|
|
// device info
|
2014-03-22 01:12:57 +00:00
|
|
|
std::stringstream deviceInfo;
|
2014-04-18 18:07:32 +00:00
|
|
|
int numDevices = InputManager::getInstance()->getNumJoysticks();
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
if(numDevices > 0)
|
2014-03-23 00:48:48 +00:00
|
|
|
deviceInfo << numDevices << " GAMEPAD" << (numDevices > 1 ? "S" : "") << " DETECTED";
|
2014-03-22 01:12:57 +00:00
|
|
|
else
|
2014-03-23 00:48:48 +00:00
|
|
|
deviceInfo << "NO GAMEPADS DETECTED";
|
2014-05-01 19:47:33 +00:00
|
|
|
mDeviceInfo = std::make_shared<TextComponent>(mWindow, deviceInfo.str(), Font::get(FONT_SIZE_SMALL), 0x999999FF, ALIGN_CENTER);
|
2014-03-23 00:48:48 +00:00
|
|
|
mGrid.setEntry(mDeviceInfo, Vector2i(0, 1), false, true);
|
|
|
|
|
|
|
|
// message
|
2014-05-01 19:47:33 +00:00
|
|
|
mMsg1 = std::make_shared<TextComponent>(mWindow, "HOLD A BUTTON ON YOUR DEVICE TO CONFIGURE IT.", Font::get(FONT_SIZE_SMALL), 0x777777FF, ALIGN_CENTER);
|
2014-03-23 00:48:48 +00:00
|
|
|
mGrid.setEntry(mMsg1, Vector2i(0, 2), false, true);
|
2014-05-17 00:14:22 +00:00
|
|
|
|
|
|
|
const char* msg2str = firstRun ? "PRESS F4 TO QUIT AT ANY TIME." : "PRESS ESC TO CANCEL.";
|
|
|
|
mMsg2 = std::make_shared<TextComponent>(mWindow, msg2str, Font::get(FONT_SIZE_SMALL), 0x777777FF, ALIGN_CENTER);
|
2014-03-23 00:48:48 +00:00
|
|
|
mGrid.setEntry(mMsg2, Vector2i(0, 3), false, true);
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2014-03-23 00:48:48 +00:00
|
|
|
// currently held device
|
2014-05-01 19:47:33 +00:00
|
|
|
mDeviceHeld = std::make_shared<TextComponent>(mWindow, "", Font::get(FONT_SIZE_MEDIUM), 0xFFFFFFFF, ALIGN_CENTER);
|
2014-03-23 00:48:48 +00:00
|
|
|
mGrid.setEntry(mDeviceHeld, Vector2i(0, 4), false, true);
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
setSize(Renderer::getScreenWidth() * 0.6f, Renderer::getScreenHeight() * 0.5f);
|
|
|
|
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2, (Renderer::getScreenHeight() - mSize.y()) / 2);
|
2013-04-09 18:13:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
void GuiDetectDevice::onSizeChanged()
|
2013-04-09 18:13:47 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
mBackground.fitTo(mSize, Vector3f::Zero(), Vector2f(-32, -32));
|
|
|
|
|
|
|
|
// grid
|
|
|
|
mGrid.setSize(mSize);
|
|
|
|
mGrid.setRowHeightPerc(0, mTitle->getFont()->getHeight() / mSize.y());
|
2014-03-23 00:48:48 +00:00
|
|
|
//mGrid.setRowHeightPerc(1, mDeviceInfo->getFont()->getHeight() / mSize.y());
|
|
|
|
mGrid.setRowHeightPerc(2, mMsg1->getFont()->getHeight() / mSize.y());
|
|
|
|
mGrid.setRowHeightPerc(3, mMsg2->getFont()->getHeight() / mSize.y());
|
|
|
|
//mGrid.setRowHeightPerc(4, mDeviceHeld->getFont()->getHeight() / mSize.y());
|
2013-04-09 18:13:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
bool GuiDetectDevice::input(InputConfig* config, Input input)
|
2013-04-09 18:13:47 +00:00
|
|
|
{
|
2014-05-17 00:14:22 +00:00
|
|
|
if(!mFirstRun && input.device == DEVICE_KEYBOARD && input.type == TYPE_KEY && input.value && input.id == SDLK_ESCAPE)
|
|
|
|
{
|
|
|
|
// cancel configuring
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
if(input.type == TYPE_BUTTON || input.type == TYPE_KEY)
|
2013-04-09 18:13:47 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
if(input.value && mHoldingConfig == NULL)
|
|
|
|
{
|
|
|
|
// started holding
|
|
|
|
mHoldingConfig = config;
|
|
|
|
mHoldTime = HOLD_TIME;
|
2014-03-22 22:37:40 +00:00
|
|
|
mDeviceHeld->setText(strToUpper(config->getDeviceName()));
|
2014-03-22 01:12:57 +00:00
|
|
|
}else if(!input.value && mHoldingConfig == config)
|
|
|
|
{
|
|
|
|
// cancel
|
|
|
|
mHoldingConfig = NULL;
|
|
|
|
mDeviceHeld->setText("");
|
|
|
|
}
|
2013-04-09 18:13:47 +00:00
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
return true;
|
2013-04-09 18:13:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
void GuiDetectDevice::update(int deltaTime)
|
2013-04-09 18:13:47 +00:00
|
|
|
{
|
2014-03-22 01:12:57 +00:00
|
|
|
if(mHoldingConfig)
|
2013-04-09 18:13:47 +00:00
|
|
|
{
|
2015-09-28 17:25:15 +00:00
|
|
|
// If ES starts and if a known device is connected after startup skip controller configuration
|
|
|
|
if(mFirstRun && fs::exists(InputManager::getConfigPath()) && InputManager::getInstance()->getNumConfiguredDevices() > 0)
|
|
|
|
{
|
|
|
|
InputManager::getInstance()->doOnFinish(); // execute possible onFinish commands
|
|
|
|
if(mDoneCallback)
|
|
|
|
mDoneCallback();
|
|
|
|
delete this; // delete GUI element
|
|
|
|
}
|
|
|
|
else
|
2014-03-22 01:12:57 +00:00
|
|
|
{
|
2015-09-28 17:25:15 +00:00
|
|
|
mHoldTime -= deltaTime;
|
|
|
|
const float t = (float)mHoldTime / HOLD_TIME;
|
|
|
|
unsigned int c = (unsigned char)(t * 255);
|
|
|
|
mDeviceHeld->setColor((c << 24) | (c << 16) | (c << 8) | 0xFF);
|
|
|
|
if(mHoldTime <= 0)
|
|
|
|
{
|
|
|
|
// picked one!
|
|
|
|
mWindow->pushGui(new GuiInputConfig(mWindow, mHoldingConfig, true, mDoneCallback));
|
|
|
|
delete this;
|
|
|
|
}
|
2014-03-22 01:12:57 +00:00
|
|
|
}
|
2013-04-09 18:13:47 +00:00
|
|
|
}
|
|
|
|
}
|