Code cleanup and code documentation update.

As of this commit, the initial code cleanup and code documentation has been completed for the entire application.
This commit is contained in:
Leon Styhre 2020-06-28 18:39:18 +02:00
parent 5b17edde8b
commit e4fdd1e20d
53 changed files with 3831 additions and 3642 deletions

View file

@ -25,7 +25,7 @@ Some key points:
* Name member variables starting with a small 'm', e.g. mMyMemberVariable * Name member variables starting with a small 'm', e.g. mMyMemberVariable
* Use the same naming convention for functions as for local variables, e.g. someFunction() * Use the same naming convention for functions as for local variables, e.g. someFunction()
* Inline functions makes perfect sense to use, but don't overdo it by using them for functions that won't be called very frequently * Inline functions makes perfect sense to use, but don't overdo it by using them for functions that won't be called very frequently
* Never put more than one statement on a single line, except for lambda expressions * Never put more than one statement on a single line (there are some exceptions though like lambda expressions and some switch statements)
* Avoid overoptimizations, especially if it sacrifices readability, makes the code hard to expand on or is error prone * Avoid overoptimizations, especially if it sacrifices readability, makes the code hard to expand on or is error prone
* For the rest, check the code and have fun! :) * For the rest, check the code and have fun! :)

View file

@ -59,7 +59,7 @@ GuiVideoScreensaverOptions::GuiVideoScreensaverOptions(Window* window, const cha
// Set subtitle position. // Set subtitle position.
auto ss_omx_subs_align = std::make_shared<OptionListComponent<std::string>> auto ss_omx_subs_align = std::make_shared<OptionListComponent<std::string>>
(mWindow, "GAME INFO ALIGNMENT", false); (mWindow, getHelpStyle(), "GAME INFO ALIGNMENT", false);
std::vector<std::string> align_mode; std::vector<std::string> align_mode;
align_mode.push_back("left"); align_mode.push_back("left");
align_mode.push_back("center"); align_mode.push_back("center");
@ -132,7 +132,7 @@ void GuiVideoScreensaverOptions::save()
"never" && Settings::getInstance()->getBool("ScreenSaverOmxPlayer")); "never" && Settings::getInstance()->getBool("ScreenSaverOmxPlayer"));
if (startingStatusNotRisky && endStatusRisky) { if (startingStatusNotRisky && endStatusRisky) {
// If before it wasn't risky but now there's a risk of problems, show warning. // If before it wasn't risky but now there's a risk of problems, show warning.
mWindow->pushGui(new GuiMsgBox(mWindow, mWindow->pushGui(new GuiMsgBox(mWindow, getHelpStyle(),
"Using OMX Player and displaying Game Info may result in the video flickering in " "Using OMX Player and displaying Game Info may result in the video flickering in "
"some TV modes. If that happens, consider:\n\n• Disabling the \"Show Game Info\" " "some TV modes. If that happens, consider:\n\n• Disabling the \"Show Game Info\" "
"option;\n• Disabling \"Overscan\" on the Pi configuration menu might help:\nRetroPie > " "option;\n• Disabling \"Overscan\" on the Pi configuration menu might help:\nRetroPie > "

View file

@ -44,11 +44,11 @@ bool HttpReq::isUrl(const std::string& str)
std::string::npos || str.find("www.") != std::string::npos)); std::string::npos || str.find("www.") != std::string::npos));
} }
HttpReq::HttpReq(const std::string& url) : mStatus(REQ_IN_PROGRESS), mHandle(NULL) HttpReq::HttpReq(const std::string& url) : mStatus(REQ_IN_PROGRESS), mHandle(nullptr)
{ {
mHandle = curl_easy_init(); mHandle = curl_easy_init();
if (mHandle == NULL) { if (mHandle == nullptr) {
mStatus = REQ_IO_ERROR; mStatus = REQ_IO_ERROR;
onError("curl_easy_init failed"); onError("curl_easy_init failed");
return; return;
@ -147,7 +147,7 @@ HttpReq::Status HttpReq::status()
if (msg->msg == CURLMSG_DONE) { if (msg->msg == CURLMSG_DONE) {
HttpReq* req = s_requests[msg->easy_handle]; HttpReq* req = s_requests[msg->easy_handle];
if (req == NULL) { if (req == nullptr) {
LOG(LogError) << "Cannot find easy handle!"; LOG(LogError) << "Cannot find easy handle!";
continue; continue;
} }

View file

@ -69,7 +69,7 @@ void touch(const std::string& filename)
{ {
#ifdef WIN32 #ifdef WIN32
FILE* fp = fopen(filename.c_str(), "ab+"); FILE* fp = fopen(filename.c_str(), "ab+");
if (fp != NULL) if (fp != nullptr)
fclose(fp); fclose(fp);
#else #else
int fd = open(filename.c_str(), O_CREAT|O_WRONLY, 0644); int fd = open(filename.c_str(), O_CREAT|O_WRONLY, 0644);

View file

@ -11,11 +11,12 @@
#include "Log.h" #include "Log.h"
#include "Scripting.h" #include "Scripting.h"
#include "Platform.h" #include "Platform.h"
#include <pugixml.hpp> #include <pugixml.hpp>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
Settings* Settings::sInstance = NULL; Settings* Settings::sInstance = nullptr;
// These values are NOT saved to es_settings.cfg since they're not set via // These values are NOT saved to es_settings.cfg since they're not set via
// the in-program settings menu. Most can be set using command-line arguments, // the in-program settings menu. Most can be set using command-line arguments,
@ -59,7 +60,7 @@ Settings::Settings()
Settings* Settings::getInstance() Settings* Settings::getInstance()
{ {
if (sInstance == NULL) if (sInstance == nullptr)
sInstance = new Settings(); sInstance = new Settings();
return sInstance; return sInstance;

View file

@ -82,7 +82,7 @@ bool NavigationSounds::isPlayingThemeNavigationSound(NavigationSoundsID soundID)
Sound::Sound( Sound::Sound(
const std::string & path) const std::string & path)
: mSampleData(NULL), : mSampleData(nullptr),
mSamplePos(0), mSamplePos(0),
mSampleLength(0), mSampleLength(0),
playing(false) playing(false)
@ -103,7 +103,7 @@ void Sound::loadFile(const std::string & path)
void Sound::init() void Sound::init()
{ {
if(mSampleData != NULL) if(mSampleData != nullptr)
deinit(); deinit();
if(mPath.empty()) if(mPath.empty())

View file

@ -1,10 +1,17 @@
//
// AnimatedImageComponent.cpp
//
// Creates animation from multiple images files.
//
#include "components/AnimatedImageComponent.h" #include "components/AnimatedImageComponent.h"
#include "components/ImageComponent.h" #include "components/ImageComponent.h"
#include "resources/ResourceManager.h" #include "resources/ResourceManager.h"
#include "Log.h" #include "Log.h"
AnimatedImageComponent::AnimatedImageComponent(Window* window) : GuiComponent(window), mEnabled(false) AnimatedImageComponent::AnimatedImageComponent(Window* window)
: GuiComponent(window), mEnabled(false)
{ {
} }
@ -14,11 +21,11 @@ void AnimatedImageComponent::load(const AnimationDef* def)
assert(def->frameCount >= 1); assert(def->frameCount >= 1);
for(size_t i = 0; i < def->frameCount; i++) for (size_t i = 0; i < def->frameCount; i++) {
{ if (def->frames[i].path != nullptr &&
if(def->frames[i].path != NULL && !ResourceManager::getInstance()->fileExists(def->frames[i].path)) !ResourceManager::getInstance()->fileExists(def->frames[i].path)) {
{ LOG(LogError) << "Missing animation frame " << i <<
LOG(LogError) << "Missing animation frame " << i << " (\"" << def->frames[i].path << "\")"; " (\"" << def->frames[i].path << "\")";
continue; continue;
} }
@ -44,8 +51,7 @@ void AnimatedImageComponent::reset()
void AnimatedImageComponent::onSizeChanged() void AnimatedImageComponent::onSizeChanged()
{ {
for(auto it = mFrames.cbegin(); it != mFrames.cend(); it++) for (auto it = mFrames.cbegin(); it != mFrames.cend(); it++) {
{
it->first->setResize(mSize.x(), mSize.y()); it->first->setResize(mSize.x(), mSize.y());
} }
} }
@ -57,18 +63,16 @@ void AnimatedImageComponent::update(int deltaTime)
mFrameAccumulator += deltaTime; mFrameAccumulator += deltaTime;
while(mFrames.at(mCurrentFrame).second <= mFrameAccumulator) while (mFrames.at(mCurrentFrame).second <= mFrameAccumulator) {
{
mCurrentFrame++; mCurrentFrame++;
if(mCurrentFrame == (int)mFrames.size()) if (mCurrentFrame == (int)mFrames.size()) {
{ if (mLoop) {
if(mLoop) // Restart.
{
// restart
mCurrentFrame = 0; mCurrentFrame = 0;
}else{ }
// done, stop at last frame else {
// Done, stop at last frame.
mCurrentFrame--; mCurrentFrame--;
mEnabled = false; mEnabled = false;
break; break;

View file

@ -1,3 +1,9 @@
//
// AnimatedImageComponent.h
//
// Creates animation from multiple images files.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_ANIMATED_IMAGE_COMPONENT_H #ifndef ES_CORE_COMPONENTS_ANIMATED_IMAGE_COMPONENT_H
#define ES_CORE_COMPONENTS_ANIMATED_IMAGE_COMPONENT_H #define ES_CORE_COMPONENTS_ANIMATED_IMAGE_COMPONENT_H
@ -6,14 +12,12 @@
class ImageComponent; class ImageComponent;
struct AnimationFrame struct AnimationFrame {
{
const char* path; const char* path;
int time; int time;
}; };
struct AnimationDef struct AnimationDef {
{
AnimationFrame* frames; AnimationFrame* frames;
size_t frameCount; size_t frameCount;
bool loop; bool loop;
@ -24,9 +28,9 @@ class AnimatedImageComponent : public GuiComponent
public: public:
AnimatedImageComponent(Window* window); AnimatedImageComponent(Window* window);
void load(const AnimationDef* def); // no reference to def is kept after loading is complete void load(const AnimationDef* def); // No reference to def is kept after loading is complete.
void reset(); // set to frame 0 void reset(); // Set to frame 0.
void update(int deltaTime) override; void update(int deltaTime) override;
void render(const Transform4x4f& trans) override; void render(const Transform4x4f& trans) override;

View file

@ -1,16 +1,23 @@
//
// BusyComponent.cpp
//
// Animated busy indicator.
//
#include "BusyComponent.h" #include "BusyComponent.h"
#include "components/AnimatedImageComponent.h" #include "components/AnimatedImageComponent.h"
#include "components/ImageComponent.h" #include "components/ImageComponent.h"
#include "components/TextComponent.h" #include "components/TextComponent.h"
// animation definition // Animation definition.
AnimationFrame BUSY_ANIMATION_FRAMES[] = { AnimationFrame BUSY_ANIMATION_FRAMES[] = {
{":/graphics/busy_0.svg", 300}, {":/graphics/busy_0.svg", 300},
{":/graphics/busy_1.svg", 300}, {":/graphics/busy_1.svg", 300},
{":/graphics/busy_2.svg", 300}, {":/graphics/busy_2.svg", 300},
{":/graphics/busy_3.svg", 300}, {":/graphics/busy_3.svg", 300},
}; };
const AnimationDef BUSY_ANIMATION_DEF = { BUSY_ANIMATION_FRAMES, 4, true }; const AnimationDef BUSY_ANIMATION_DEF = { BUSY_ANIMATION_FRAMES, 4, true };
BusyComponent::BusyComponent(Window* window): GuiComponent(window), BusyComponent::BusyComponent(Window* window): GuiComponent(window),
@ -18,9 +25,10 @@ BusyComponent::BusyComponent(Window* window) : GuiComponent(window),
{ {
mAnimation = std::make_shared<AnimatedImageComponent>(mWindow); mAnimation = std::make_shared<AnimatedImageComponent>(mWindow);
mAnimation->load(&BUSY_ANIMATION_DEF); mAnimation->load(&BUSY_ANIMATION_DEF);
mText = std::make_shared<TextComponent>(mWindow, "WORKING...", Font::get(FONT_SIZE_MEDIUM), 0x777777FF); mText = std::make_shared<TextComponent>(mWindow, "WORKING...",
Font::get(FONT_SIZE_MEDIUM), 0x777777FF);
// col 0 = animation, col 1 = spacer, col 2 = text // Col 0 = animation, col 1 = spacer, col 2 = text.
mGrid.setEntry(mAnimation, Vector2i(1, 1), false, true); mGrid.setEntry(mAnimation, Vector2i(1, 1), false, true);
mGrid.setEntry(mText, Vector2i(3, 1), false, true); mGrid.setEntry(mText, Vector2i(3, 1), false, true);
@ -40,13 +48,14 @@ void BusyComponent::onSizeChanged()
mText->setSize(0, textHeight); mText->setSize(0, textHeight);
const float textWidth = mText->getSize().x() + 4; const float textWidth = mText->getSize().x() + 4;
mGrid.setColWidthPerc(1, textHeight / mSize.x()); // animation is square mGrid.setColWidthPerc(1, textHeight / mSize.x()); // Animation is square.
mGrid.setColWidthPerc(2, middleSpacerWidth / mSize.x()); mGrid.setColWidthPerc(2, middleSpacerWidth / mSize.x());
mGrid.setColWidthPerc(3, textWidth / mSize.x()); mGrid.setColWidthPerc(3, textWidth / mSize.x());
mGrid.setRowHeightPerc(1, textHeight / mSize.y()); mGrid.setRowHeightPerc(1, textHeight / mSize.y());
mBackground.fitTo(Vector2f(mGrid.getColWidth(1) + mGrid.getColWidth(2) + mGrid.getColWidth(3), textHeight + 2), mBackground.fitTo(Vector2f(mGrid.getColWidth(1) +
mGrid.getColWidth(2) + mGrid.getColWidth(3), textHeight + 2),
mAnimation->getPosition(), Vector2f(0, 0)); mAnimation->getPosition(), Vector2f(0, 0));
} }

View file

@ -1,3 +1,9 @@
//
// BusyComponent.h
//
// Animated busy indicator.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_BUSY_COMPONENT_H #ifndef ES_CORE_COMPONENTS_BUSY_COMPONENT_H
#define ES_CORE_COMPONENTS_BUSY_COMPONENT_H #define ES_CORE_COMPONENTS_BUSY_COMPONENT_H
@ -16,7 +22,7 @@ public:
void onSizeChanged() override; void onSizeChanged() override;
void reset(); // reset to frame 0 void reset(); // Reset to frame 0.
private: private:
NinePatchComponent mBackground; NinePatchComponent mBackground;

View file

@ -1,9 +1,19 @@
//
// ButtonComponent.cpp
//
// Basic on/off button.
//
#include "components/ButtonComponent.h" #include "components/ButtonComponent.h"
#include "resources/Font.h" #include "resources/Font.h"
#include "utils/StringUtil.h" #include "utils/StringUtil.h"
ButtonComponent::ButtonComponent(Window* window, const std::string& text, const std::string& helpText, const std::function<void()>& func) : GuiComponent(window), ButtonComponent::ButtonComponent(
Window* window, const std::string& text,
const std::string& helpText,
const std::function<void()>& func)
: GuiComponent(window),
mBox(window, ":/graphics/button.png"), mBox(window, ":/graphics/button.png"),
mFont(Font::get(FONT_SIZE_MEDIUM)), mFont(Font::get(FONT_SIZE_MEDIUM)),
mFocused(false), mFocused(false),
@ -27,8 +37,7 @@ void ButtonComponent::setPressedFunc(std::function<void()> f)
bool ButtonComponent::input(InputConfig* config, Input input) bool ButtonComponent::input(InputConfig* config, Input input)
{ {
if(config->isMappedTo("a", input) && input.value != 0) if (config->isMappedTo("a", input) && input.value != 0) {
{
if (mPressedFunc && mEnabled) if (mPressedFunc && mEnabled)
mPressedFunc(); mPressedFunc();
return true; return true;
@ -70,8 +79,7 @@ void ButtonComponent::setEnabled(bool enabled)
void ButtonComponent::updateImage() void ButtonComponent::updateImage()
{ {
if(!mEnabled || !mPressedFunc) if (!mEnabled || !mPressedFunc) {
{
mBox.setImagePath(":/graphics/button_filled.png"); mBox.setImagePath(":/graphics/button_filled.png");
mBox.setCenterColor(0x770000FF); mBox.setCenterColor(0x770000FF);
mBox.setEdgeColor(0x770000FF); mBox.setEdgeColor(0x770000FF);
@ -91,7 +99,8 @@ void ButtonComponent::render(const Transform4x4f& parentTrans)
if (mTextCache) if (mTextCache)
{ {
Vector3f centerOffset((mSize.x() - mTextCache->metrics.size.x()) / 2, (mSize.y() - mTextCache->metrics.size.y()) / 2, 0); Vector3f centerOffset((mSize.x() - mTextCache->metrics.size.x()) / 2,
(mSize.y() - mTextCache->metrics.size.y()) / 2, 0);
trans = trans.translate(centerOffset); trans = trans.translate(centerOffset);
Renderer::setMatrix(trans); Renderer::setMatrix(trans);

View file

@ -1,3 +1,9 @@
//
// ButtonComponent.h
//
// Basic on/off button.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_BUTTON_COMPONENT_H #ifndef ES_CORE_COMPONENTS_BUTTON_COMPONENT_H
#define ES_CORE_COMPONENTS_BUTTON_COMPONENT_H #define ES_CORE_COMPONENTS_BUTTON_COMPONENT_H
@ -10,10 +16,10 @@ class TextCache;
class ButtonComponent : public GuiComponent class ButtonComponent : public GuiComponent
{ {
public: public:
ButtonComponent(Window* window, const std::string& text = "", const std::string& helpText = "", const std::function<void()>& func = nullptr); ButtonComponent(Window* window, const std::string& text = "",
const std::string& helpText = "", const std::function<void()>& func = nullptr);
void setPressedFunc(std::function<void()> f); void setPressedFunc(std::function<void()> f);
void setEnabled(bool enable); void setEnabled(bool enable);
bool input(InputConfig* config, Input input) override; bool input(InputConfig* config, Input input) override;

View file

@ -100,7 +100,7 @@ void ComponentGrid::setEntry(
{ {
assert(pos.x() >= 0 && pos.x() < mGridSize.x() && pos.y() >= 0 && pos.y() < mGridSize.y()); assert(pos.x() >= 0 && pos.x() < mGridSize.x() && pos.y() >= 0 && pos.y() < mGridSize.y());
assert(comp != nullptr); assert(comp != nullptr);
assert(comp->getParent() == NULL); assert(comp->getParent() == nullptr);
GridEntry entry(pos, size, comp, canFocus, resize, updateType, border); GridEntry entry(pos, size, comp, canFocus, resize, updateType, border);
mCells.push_back(entry); mCells.push_back(entry);
@ -223,7 +223,7 @@ const ComponentGrid::GridEntry* ComponentGrid::getCellAt(int x, int y) const
return &(*it); return &(*it);
} }
return NULL; return nullptr;
} }
bool ComponentGrid::input(InputConfig* config, Input input) bool ComponentGrid::input(InputConfig* config, Input input)
@ -327,7 +327,7 @@ void ComponentGrid::onFocusGained()
bool ComponentGrid::cursorValid() bool ComponentGrid::cursorValid()
{ {
const GridEntry* e = getCellAt(mCursor); const GridEntry* e = getCellAt(mCursor);
return (e != NULL && e->canFocus); return (e != nullptr && e->canFocus);
} }
void ComponentGrid::update(int deltaTime) void ComponentGrid::update(int deltaTime)
@ -359,7 +359,7 @@ void ComponentGrid::render(const Transform4x4f& parentTrans)
void ComponentGrid::textInput(const char* text) void ComponentGrid::textInput(const char* text)
{ {
const GridEntry* selectedEntry = getCellAt(mCursor); const GridEntry* selectedEntry = getCellAt(mCursor);
if (selectedEntry != NULL && selectedEntry->canFocus) if (selectedEntry != nullptr && selectedEntry->canFocus)
selectedEntry->component->textInput(text); selectedEntry->component->textInput(text);
} }

View file

@ -104,7 +104,7 @@ private:
operator bool() const operator bool() const
{ {
return component != NULL; return component != nullptr;
} }
}; };

View file

@ -20,7 +20,7 @@ void ComponentList::addRow(const ComponentListRow& row, bool setCursorHere)
{ {
IList<ComponentListRow, void*>::Entry e; IList<ComponentListRow, void*>::Entry e;
e.name = ""; e.name = "";
e.object = NULL; e.object = nullptr;
e.data = row; e.data = row;
this->add(e); this->add(e);

View file

@ -1,3 +1,9 @@
//
// GridTileComponent.cpp
//
// X*Y grid.
//
#include "GridTileComponent.h" #include "GridTileComponent.h"
#include "animations/LambdaAnimation.h" #include "animations/LambdaAnimation.h"
@ -44,7 +50,7 @@ void GridTileComponent::render(const Transform4x4f& parentTrans)
renderChildren(trans); renderChildren(trans);
} }
// Update all the tile properties to the new status (selected or default) // Update all the tile properties to the new status (selected or default).
void GridTileComponent::update(int deltaTime) void GridTileComponent::update(int deltaTime)
{ {
GuiComponent::update(deltaTime); GuiComponent::update(deltaTime);
@ -62,7 +68,8 @@ void GridTileComponent::update(int deltaTime)
void applyThemeToProperties(const ThemeData::ThemeElement* elem, GridTileProperties* properties) void applyThemeToProperties(const ThemeData::ThemeElement* elem, GridTileProperties* properties)
{ {
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f screen = Vector2f((float)Renderer::getScreenWidth(),
(float)Renderer::getScreenHeight());
if (elem->has("size")) if (elem->has("size"))
properties->mSize = elem->get<Vector2f>("size") * screen; properties->mSize = elem->get<Vector2f>("size") * screen;
@ -79,8 +86,7 @@ void applyThemeToProperties(const ThemeData::ThemeElement* elem, GridTilePropert
if (elem->has("backgroundCornerSize")) if (elem->has("backgroundCornerSize"))
properties->mBackgroundCornerSize = elem->get<Vector2f>("backgroundCornerSize"); properties->mBackgroundCornerSize = elem->get<Vector2f>("backgroundCornerSize");
if (elem->has("backgroundColor")) if (elem->has("backgroundColor")) {
{
properties->mBackgroundCenterColor = elem->get<unsigned int>("backgroundColor"); properties->mBackgroundCenterColor = elem->get<unsigned int>("backgroundColor");
properties->mBackgroundEdgeColor = elem->get<unsigned int>("backgroundColor"); properties->mBackgroundEdgeColor = elem->get<unsigned int>("backgroundColor");
} }
@ -92,18 +98,20 @@ void applyThemeToProperties(const ThemeData::ThemeElement* elem, GridTilePropert
properties->mBackgroundEdgeColor = elem->get<unsigned int>("backgroundEdgeColor"); properties->mBackgroundEdgeColor = elem->get<unsigned int>("backgroundEdgeColor");
} }
void GridTileComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& /*element*/, unsigned int /*properties*/) void GridTileComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
const std::string& view, const std::string& /*element*/, unsigned int /*properties*/)
{ {
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f screen = Vector2f((float)Renderer::getScreenWidth(),
(float)Renderer::getScreenHeight());
// Apply theme to the default gridtile // Apply theme to the default gridtile.
const ThemeData::ThemeElement* elem = theme->getElement(view, "default", "gridtile"); const ThemeData::ThemeElement* elem = theme->getElement(view, "default", "gridtile");
if (elem) if (elem)
applyThemeToProperties(elem, &mDefaultProperties); applyThemeToProperties(elem, &mDefaultProperties);
// Apply theme to the selected gridtile // Apply theme to the selected gridtile. Note that some of the default gridtile
// NOTE that some of the default gridtile properties influence on the selected gridtile properties // properties have influence on the selected gridtile properties.
// See THEMES.md for more informations // See THEMES.md for more informations.
elem = theme->getElement(view, "selected", "gridtile"); elem = theme->getElement(view, "selected", "gridtile");
mSelectedProperties.mSize = getSelectedTileSize(); mSelectedProperties.mSize = getSelectedTileSize();
@ -115,11 +123,12 @@ void GridTileComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, cons
applyThemeToProperties(elem, &mSelectedProperties); applyThemeToProperties(elem, &mSelectedProperties);
} }
// Made this a static function because the ImageGridComponent need to know the default tile size // Made this a static function because the ImageGridComponent needs to know the default tile
// to calculate the grid dimension before it instantiate the GridTileComponents // max size to calculate the grid dimension before it instantiates the GridTileComponents.
Vector2f GridTileComponent::getDefaultTileSize() Vector2f GridTileComponent::getDefaultTileSize()
{ {
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f screen = Vector2f((float)Renderer::getScreenWidth(),
(float)Renderer::getScreenHeight());
return screen * 0.22f; return screen * 0.22f;
} }
@ -143,7 +152,7 @@ void GridTileComponent::setImage(const std::string& path)
{ {
mImage->setImage(path); mImage->setImage(path);
// Resize now to prevent flickering images when scrolling // Resize now to prevent flickering images when scrolling.
resize(); resize();
} }
@ -151,23 +160,20 @@ void GridTileComponent::setImage(const std::shared_ptr<TextureResource>& texture
{ {
mImage->setImage(texture); mImage->setImage(texture);
// Resize now to prevent flickering images when scrolling // Resize now to prevent flickering images when scrolling.
resize(); resize();
} }
void GridTileComponent::setSelected(bool selected, bool allowAnimation, Vector3f* pPosition, bool force) void GridTileComponent::setSelected(bool selected, bool allowAnimation,
Vector3f* pPosition, bool force)
{ {
if (mSelected == selected && !force) if (mSelected == selected && !force)
{
return; return;
}
mSelected = selected; mSelected = selected;
if (selected) if (selected) {
{ if (pPosition == nullptr || !allowAnimation) {
if (pPosition == NULL || !allowAnimation)
{
cancelAnimation(3); cancelAnimation(3);
this->setSelectedZoom(1); this->setSelectedZoom(1);
@ -175,15 +181,12 @@ void GridTileComponent::setSelected(bool selected, bool allowAnimation, Vector3f
resize(); resize();
} }
else else {
{
mAnimPosition = Vector3f(pPosition->x(), pPosition->y(), pPosition->z()); mAnimPosition = Vector3f(pPosition->x(), pPosition->y(), pPosition->z());
auto func = [this](float t) auto func = [this](float t) {
{ t -= 1; // Cubic ease out.
t -= 1; // cubic ease out
float pct = Math::lerp(0, 1, t*t*t + 1); float pct = Math::lerp(0, 1, t*t*t + 1);
this->setSelectedZoom(pct); this->setSelectedZoom(pct);
}; };
@ -194,22 +197,19 @@ void GridTileComponent::setSelected(bool selected, bool allowAnimation, Vector3f
}, false, 3); }, false, 3);
} }
} }
else // if (!selected) // If (!selected).
{ else {
if (!allowAnimation) if (!allowAnimation) {
{
cancelAnimation(3); cancelAnimation(3);
this->setSelectedZoom(0); this->setSelectedZoom(0);
resize(); resize();
} }
else else {
{
this->setSelectedZoom(1); this->setSelectedZoom(1);
auto func = [this](float t) auto func = [this](float t) {
{ t -= 1; // Cubic ease out.
t -= 1; // cubic ease out
float pct = Math::lerp(0, 1, t*t*t + 1); float pct = Math::lerp(0, 1, t*t*t + 1);
this->setSelectedZoom(1.0 - pct); this->setSelectedZoom(1.0 - pct);
}; };
@ -272,34 +272,32 @@ void GridTileComponent::calcCurrentProperties()
float zoomPercentInverse = 1.0 - mSelectedZoomPercent; float zoomPercentInverse = 1.0 - mSelectedZoomPercent;
if (mSelectedZoomPercent != 0.0f && mSelectedZoomPercent != 1.0f) { if (mSelectedZoomPercent != 0.0f && mSelectedZoomPercent != 1.0f) {
if (mDefaultProperties.mSize != mSelectedProperties.mSize) { if (mDefaultProperties.mSize != mSelectedProperties.mSize)
mCurrentProperties.mSize = mDefaultProperties.mSize * zoomPercentInverse + mSelectedProperties.mSize * mSelectedZoomPercent; mCurrentProperties.mSize = mDefaultProperties.mSize * zoomPercentInverse +
} mSelectedProperties.mSize * mSelectedZoomPercent;
if (mDefaultProperties.mPadding != mSelectedProperties.mPadding) if (mDefaultProperties.mPadding != mSelectedProperties.mPadding)
{ mCurrentProperties.mPadding = mDefaultProperties.mPadding * zoomPercentInverse +
mCurrentProperties.mPadding = mDefaultProperties.mPadding * zoomPercentInverse + mSelectedProperties.mPadding * mSelectedZoomPercent; mSelectedProperties.mPadding * mSelectedZoomPercent;
}
if (mDefaultProperties.mImageColor != mSelectedProperties.mImageColor) if (mDefaultProperties.mImageColor != mSelectedProperties.mImageColor)
{ mCurrentProperties.mImageColor = mixColors(mDefaultProperties.mImageColor,
mCurrentProperties.mImageColor = mixColors(mDefaultProperties.mImageColor, mSelectedProperties.mImageColor, mSelectedZoomPercent); mSelectedProperties.mImageColor, mSelectedZoomPercent);
}
if (mDefaultProperties.mBackgroundCornerSize != mSelectedProperties.mBackgroundCornerSize) if (mDefaultProperties.mBackgroundCornerSize != mSelectedProperties.mBackgroundCornerSize)
{ mCurrentProperties.mBackgroundCornerSize = mDefaultProperties.mBackgroundCornerSize *
mCurrentProperties.mBackgroundCornerSize = mDefaultProperties.mBackgroundCornerSize * zoomPercentInverse + mSelectedProperties.mBackgroundCornerSize * mSelectedZoomPercent; zoomPercentInverse + mSelectedProperties.mBackgroundCornerSize *
} mSelectedZoomPercent;
if (mDefaultProperties.mBackgroundCenterColor != mSelectedProperties.mBackgroundCenterColor) if (mDefaultProperties.mBackgroundCenterColor != mSelectedProperties.mBackgroundCenterColor)
{ mCurrentProperties.mBackgroundCenterColor =
mCurrentProperties.mBackgroundCenterColor = mixColors(mDefaultProperties.mBackgroundCenterColor, mSelectedProperties.mBackgroundCenterColor, mSelectedZoomPercent); mixColors(mDefaultProperties.mBackgroundCenterColor,
} mSelectedProperties.mBackgroundCenterColor, mSelectedZoomPercent);
if (mDefaultProperties.mBackgroundEdgeColor != mSelectedProperties.mBackgroundEdgeColor) if (mDefaultProperties.mBackgroundEdgeColor != mSelectedProperties.mBackgroundEdgeColor)
{ mCurrentProperties.mBackgroundEdgeColor =
mCurrentProperties.mBackgroundEdgeColor = mixColors(mDefaultProperties.mBackgroundEdgeColor, mSelectedProperties.mBackgroundEdgeColor, mSelectedZoomPercent); mixColors(mDefaultProperties.mBackgroundEdgeColor,
} mSelectedProperties.mBackgroundEdgeColor, mSelectedZoomPercent);
} }
} }

View file

@ -1,3 +1,9 @@
//
// GridTileComponent.h
//
// X*Y grid.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_GRID_TILE_COMPONENT_H #ifndef ES_CORE_COMPONENTS_GRID_TILE_COMPONENT_H
#define ES_CORE_COMPONENTS_GRID_TILE_COMPONENT_H #define ES_CORE_COMPONENTS_GRID_TILE_COMPONENT_H
@ -5,8 +11,7 @@
#include "NinePatchComponent.h" #include "NinePatchComponent.h"
#include "ImageComponent.h" #include "ImageComponent.h"
struct GridTileProperties struct GridTileProperties {
{
Vector2f mSize; Vector2f mSize;
Vector2f mPadding; Vector2f mPadding;
unsigned int mImageColor; unsigned int mImageColor;
@ -22,10 +27,11 @@ public:
GridTileComponent(Window* window); GridTileComponent(Window* window);
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties) override;
// Made this a static function because the ImageGridComponent need to know the default tile max size // Made this a static function because the ImageGridComponent needs to know the default tile
// to calculate the grid dimension before it instantiate the GridTileComponents // max size to calculate the grid dimension before it instantiates the GridTileComponents.
static Vector2f getDefaultTileSize(); static Vector2f getDefaultTileSize();
Vector2f getSelectedTileSize() const; Vector2f getSelectedTileSize() const;
bool isSelected() const; bool isSelected() const;
@ -34,7 +40,8 @@ public:
void setImage(const std::string& path); void setImage(const std::string& path);
void setImage(const std::shared_ptr<TextureResource>& texture); void setImage(const std::shared_ptr<TextureResource>& texture);
void setSelected(bool selected, bool allowAnimation = true, Vector3f* pPosition = NULL, bool force=false); void setSelected(bool selected, bool allowAnimation = true,
Vector3f* pPosition = nullptr, bool force=false);
void setVisible(bool visible); void setVisible(bool visible);
void forceSize(Vector2f size, float selectedZoom); void forceSize(Vector2f size, float selectedZoom);

View file

@ -1,3 +1,9 @@
//
// HelpComponent.cpp
//
// Help information in icon and text pairs.
//
#include "components/HelpComponent.h" #include "components/HelpComponent.h"
#include "components/ComponentGrid.h" #include "components/ComponentGrid.h"
@ -8,11 +14,11 @@
#include "Log.h" #include "Log.h"
#include "Settings.h" #include "Settings.h"
#define OFFSET_X 12 // move the entire thing right by this amount (px) #define OFFSET_X 12 // Move the entire thing right by this amount (px).
#define OFFSET_Y 12 // move the entire thing up by this amount (px) #define OFFSET_Y 12 // Move the entire thing up by this amount (px).
#define ICON_TEXT_SPACING 8 // space between [icon] and [text] (px) #define ICON_TEXT_SPACING 8 // Space between [icon] and [text] (px).
#define ENTRY_SPACING 16 // space between [text] and next [icon] (px) #define ENTRY_SPACING 16 // Space between [text] and next [icon] (px).
static const std::map<std::string, const char*> ICON_PATH_MAP { static const std::map<std::string, const char*> ICON_PATH_MAP {
{ "up/down", ":/help/dpad_updown.svg" }, { "up/down", ":/help/dpad_updown.svg" },
@ -53,8 +59,7 @@ void HelpComponent::setStyle(const HelpStyle& style)
void HelpComponent::updateGrid() void HelpComponent::updateGrid()
{ {
if(!Settings::getInstance()->getBool("ShowHelpPrompts") || mPrompts.empty()) if (!Settings::getInstance()->getBool("ShowHelpPrompts") || mPrompts.empty()) {
{
mGrid.reset(); mGrid.reset();
return; return;
} }
@ -62,6 +67,7 @@ void HelpComponent::updateGrid()
std::shared_ptr<Font>& font = mStyle.font; std::shared_ptr<Font>& font = mStyle.font;
mGrid = std::make_shared<ComponentGrid>(mWindow, Vector2i((int)mPrompts.size() * 4, 1)); mGrid = std::make_shared<ComponentGrid>(mWindow, Vector2i((int)mPrompts.size() * 4, 1));
// [icon] [spacer1] [text] [spacer2] // [icon] [spacer1] [text] [spacer2]
std::vector< std::shared_ptr<ImageComponent> > icons; std::vector< std::shared_ptr<ImageComponent> > icons;
@ -69,23 +75,24 @@ void HelpComponent::updateGrid()
float width = 0; float width = 0;
const float height = Math::round(font->getLetterHeight() * 1.25f); const float height = Math::round(font->getLetterHeight() * 1.25f);
for(auto it = mPrompts.cbegin(); it != mPrompts.cend(); it++)
{ for (auto it = mPrompts.cbegin(); it != mPrompts.cend(); it++) {
auto icon = std::make_shared<ImageComponent>(mWindow); auto icon = std::make_shared<ImageComponent>(mWindow);
icon->setImage(getIconTexture(it->first.c_str())); icon->setImage(getIconTexture(it->first.c_str()));
icon->setColorShift(mStyle.iconColor); icon->setColorShift(mStyle.iconColor);
icon->setResize(0, height); icon->setResize(0, height);
icons.push_back(icon); icons.push_back(icon);
auto lbl = std::make_shared<TextComponent>(mWindow, Utils::String::toUpper(it->second), font, mStyle.textColor); auto lbl = std::make_shared<TextComponent>(mWindow,
Utils::String::toUpper(it->second), font, mStyle.textColor);
labels.push_back(lbl); labels.push_back(lbl);
width += icon->getSize().x() + lbl->getSize().x() + ICON_TEXT_SPACING + ENTRY_SPACING; width += icon->getSize().x() + lbl->getSize().x() + ICON_TEXT_SPACING + ENTRY_SPACING;
} }
mGrid->setSize(width, height); mGrid->setSize(width, height);
for(unsigned int i = 0; i < icons.size(); i++)
{ for (unsigned int i = 0; i < icons.size(); i++) {
const int col = i*4; const int col = i*4;
mGrid->setColWidthPerc(col, icons.at(i)->getSize().x() / width); mGrid->setColWidthPerc(col, icons.at(i)->getSize().x() / width);
mGrid->setColWidthPerc(col + 1, ICON_TEXT_SPACING / width); mGrid->setColWidthPerc(col + 1, ICON_TEXT_SPACING / width);
@ -107,14 +114,13 @@ std::shared_ptr<TextureResource> HelpComponent::getIconTexture(const char* name)
return it->second; return it->second;
auto pathLookup = ICON_PATH_MAP.find(name); auto pathLookup = ICON_PATH_MAP.find(name);
if(pathLookup == ICON_PATH_MAP.cend()) if (pathLookup == ICON_PATH_MAP.cend()) {
{
LOG(LogError) << "Unknown help icon \"" << name << "\"!"; LOG(LogError) << "Unknown help icon \"" << name << "\"!";
return nullptr; return nullptr;
} }
if(!ResourceManager::getInstance()->fileExists(pathLookup->second)) if (!ResourceManager::getInstance()->fileExists(pathLookup->second)) {
{ LOG(LogError) << "Help icon \"" << name <<
LOG(LogError) << "Help icon \"" << name << "\" - corresponding image file \"" << pathLookup->second << "\" misisng!"; "\" - corresponding image file \"" << pathLookup->second << "\" misisng!";
return nullptr; return nullptr;
} }
@ -128,10 +134,8 @@ void HelpComponent::setOpacity(unsigned char opacity)
GuiComponent::setOpacity(opacity); GuiComponent::setOpacity(opacity);
for (unsigned int i = 0; i < mGrid->getChildCount(); i++) for (unsigned int i = 0; i < mGrid->getChildCount(); i++)
{
mGrid->getChild(i)->setOpacity(opacity); mGrid->getChild(i)->setOpacity(opacity);
} }
}
void HelpComponent::render(const Transform4x4f& parentTrans) void HelpComponent::render(const Transform4x4f& parentTrans)
{ {

View file

@ -1,3 +1,9 @@
//
// HelpComponent.h
//
// Help information in icon and text pairs.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_HELP_COMPONENT_H #ifndef ES_CORE_COMPONENTS_HELP_COMPONENT_H
#define ES_CORE_COMPONENTS_HELP_COMPONENT_H #define ES_CORE_COMPONENTS_HELP_COMPONENT_H

View file

@ -1,3 +1,9 @@
//
// IList.h
//
// Gamelist base class.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_ILIST_H #ifndef ES_CORE_COMPONENTS_ILIST_H
#define ES_CORE_COMPONENTS_ILIST_H #define ES_CORE_COMPONENTS_ILIST_H
@ -6,52 +12,51 @@
#include "resources/Font.h" #include "resources/Font.h"
#include "PowerSaver.h" #include "PowerSaver.h"
enum CursorState enum CursorState {
{
CURSOR_STOPPED, CURSOR_STOPPED,
CURSOR_SCROLLING CURSOR_SCROLLING
}; };
enum ListLoopType enum ListLoopType {
{
LIST_ALWAYS_LOOP, LIST_ALWAYS_LOOP,
LIST_PAUSE_AT_END, LIST_PAUSE_AT_END,
LIST_NEVER_LOOP LIST_NEVER_LOOP
}; };
struct ScrollTier struct ScrollTier {
{ int length; // How long we stay on this tier before going to the next.
int length; // how long we stay on this level before going to the next int scrollDelay; // How long between scrolls.
int scrollDelay; // how long between scrolls
}; };
struct ScrollTierList struct ScrollTierList {
{
const int count; const int count;
const ScrollTier* tiers; const ScrollTier* tiers;
}; };
// default scroll tiers // Default scroll tiers.
const ScrollTier QUICK_SCROLL_TIERS[] = { const ScrollTier QUICK_SCROLL_TIERS[] = {
{500, 500}, {500, 500},
{2000, 114}, {2000, 114},
{4000, 32}, {4000, 32},
{0, 16} {0, 16}
}; };
const ScrollTierList LIST_SCROLL_STYLE_QUICK = { 4, QUICK_SCROLL_TIERS }; const ScrollTierList LIST_SCROLL_STYLE_QUICK = {
4,
QUICK_SCROLL_TIERS
};
const ScrollTier SLOW_SCROLL_TIERS[] = { const ScrollTier SLOW_SCROLL_TIERS[] = {
{500, 500}, {500, 500},
{0, 200} {0, 200}
}; };
const ScrollTierList LIST_SCROLL_STYLE_SLOW = { 2, SLOW_SCROLL_TIERS }; const ScrollTierList LIST_SCROLL_STYLE_SLOW = { 2, SLOW_SCROLL_TIERS };
template <typename EntryData, typename UserData> template <typename EntryData, typename UserData>
class IList : public GuiComponent class IList : public GuiComponent
{ {
public: public:
struct Entry struct Entry {
{
std::string name; std::string name;
UserData object; UserData object;
EntryData data; EntryData data;
@ -77,8 +82,14 @@ protected:
std::vector<Entry> mEntries; std::vector<Entry> mEntries;
public: public:
IList(Window* window, const ScrollTierList& tierList = LIST_SCROLL_STYLE_QUICK, const ListLoopType& loopType = LIST_PAUSE_AT_END) : GuiComponent(window), IList(
mGradient(window), mTierList(tierList), mLoopType(loopType) Window* window,
const ScrollTierList& tierList = LIST_SCROLL_STYLE_QUICK,
const ListLoopType& loopType = LIST_PAUSE_AT_END)
: GuiComponent(window),
mGradient(window),
mTierList(tierList),
mLoopType(loopType)
{ {
mCursor = 0; mCursor = 0;
mScrollTier = 0; mScrollTier = 0;
@ -148,13 +159,11 @@ public:
onCursorChanged(CURSOR_STOPPED); onCursorChanged(CURSOR_STOPPED);
} }
// returns true if successful (select is in our list), false if not // Returns true if successful (select is in our list), false if not.
bool setCursor(const UserData& obj) bool setCursor(const UserData& obj)
{ {
for(auto it = mEntries.cbegin(); it != mEntries.cend(); it++) for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
{ if ((*it).object == obj) {
if((*it).object == obj)
{
mCursor = (int)(it - mEntries.cbegin()); mCursor = (int)(it - mEntries.cbegin());
onCursorChanged(CURSOR_STOPPED); onCursorChanged(CURSOR_STOPPED);
return true; return true;
@ -164,7 +173,7 @@ public:
return false; return false;
} }
// entry management // Entry management.
void add(const Entry& e) void add(const Entry& e)
{ {
mEntries.push_back(e); mEntries.push_back(e);
@ -172,10 +181,8 @@ public:
bool remove(const UserData& obj) bool remove(const UserData& obj)
{ {
for(auto it = mEntries.cbegin(); it != mEntries.cend(); it++) for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
{ if ((*it).object == obj) {
if((*it).object == obj)
{
remove(it); remove(it);
return true; return true;
} }
@ -189,8 +196,7 @@ public:
protected: protected:
void remove(typename std::vector<Entry>::const_iterator& it) void remove(typename std::vector<Entry>::const_iterator& it)
{ {
if(mCursor > 0 && it - mEntries.cbegin() <= mCursor) if (mCursor > 0 && it - mEntries.cbegin() <= mCursor) {
{
mCursor--; mCursor--;
onCursorChanged(CURSOR_STOPPED); onCursorChanged(CURSOR_STOPPED);
} }
@ -214,11 +220,13 @@ protected:
return true; return true;
} }
bool listInput(int velocity) // a velocity of 0 = stop scrolling
bool listInput(int velocity) // A velocity of 0 = stop scrolling.
{ {
PowerSaver::setState(velocity == 0); PowerSaver::setState(velocity == 0);
// generate an onCursorChanged event in the stopped state when the user lets go of the key // Generate an onCursorChanged event in the stopped state when the user
// lets go of the key.
if (velocity == 0 && mScrollVelocity != 0) if (velocity == 0 && mScrollVelocity != 0)
onCursorChanged(CURSOR_STOPPED); onCursorChanged(CURSOR_STOPPED);
@ -234,9 +242,11 @@ protected:
void listUpdate(int deltaTime) void listUpdate(int deltaTime)
{ {
// update the title overlay opacity // Update the title overlay opacity.
const int dir = (mScrollTier >= mTierList.count - 1) ? 1 : -1; // fade in if scroll tier is >= 1, otherwise fade out // Fade in if scroll tier is >= 1, otherwise fade out.
int op = mTitleOverlayOpacity + deltaTime*dir; // we just do a 1-to-1 time -> opacity, no scaling const int dir = (mScrollTier >= mTierList.count - 1) ? 1 : -1;
// We just do a 1-to-1 time -> opacity, no scaling.
int op = mTitleOverlayOpacity + deltaTime*dir;
if (op >= 255) if (op >= 255)
mTitleOverlayOpacity = 255; mTitleOverlayOpacity = 255;
else if (op <= 0) else if (op <= 0)
@ -250,23 +260,23 @@ protected:
mScrollCursorAccumulator += deltaTime; mScrollCursorAccumulator += deltaTime;
mScrollTierAccumulator += deltaTime; mScrollTierAccumulator += deltaTime;
// we delay scrolling until after scroll tier has updated so isScrolling() returns accurately during onCursorChanged callbacks // We delay scrolling until after scroll tier has updated so isScrolling() returns
// we don't just do scroll tier first because it would not catch the scrollDelay == tier length case // accurately during onCursorChanged callbacks. We don't just do scroll tier first
// because it would not catch the scrollDelay == tier length case.
int scrollCount = 0; int scrollCount = 0;
while(mScrollCursorAccumulator >= mTierList.tiers[mScrollTier].scrollDelay) while (mScrollCursorAccumulator >= mTierList.tiers[mScrollTier].scrollDelay) {
{
mScrollCursorAccumulator -= mTierList.tiers[mScrollTier].scrollDelay; mScrollCursorAccumulator -= mTierList.tiers[mScrollTier].scrollDelay;
scrollCount++; scrollCount++;
} }
// are we ready to go even FASTER? // Are we ready to go even FASTER?
while(mScrollTier < mTierList.count - 1 && mScrollTierAccumulator >= mTierList.tiers[mScrollTier].length) while (mScrollTier < mTierList.count - 1 && mScrollTierAccumulator >=
{ mTierList.tiers[mScrollTier].length) {
mScrollTierAccumulator -= mTierList.tiers[mScrollTier].length; mScrollTierAccumulator -= mTierList.tiers[mScrollTier].length;
mScrollTier++; mScrollTier++;
} }
// actually perform the scrolling // Actually perform the scrolling.
for (int i = 0; i < scrollCount; i++) for (int i = 0; i < scrollCount; i++)
scroll(mScrollVelocity); scroll(mScrollVelocity);
} }
@ -276,8 +286,10 @@ protected:
if (size() == 0 || !mTitleOverlayFont || mTitleOverlayOpacity == 0) if (size() == 0 || !mTitleOverlayFont || mTitleOverlayOpacity == 0)
return; return;
// we don't bother caching this because it's only two letters and will change pretty much every frame if we're scrolling // We don't bother caching this because it's only two letters and will change pretty
const std::string text = getSelectedName().size() >= 2 ? getSelectedName().substr(0, 2) : "??"; // much every frame if we're scrolling.
const std::string text = getSelectedName().size() >= 2 ?
getSelectedName().substr(0, 2) : "??";
Vector2f off = mTitleOverlayFont->sizeText(text); Vector2f off = mTitleOverlayFont->sizeText(text);
off[0] = (Renderer::getScreenWidth() - off.x()) * 0.5f; off[0] = (Renderer::getScreenWidth() - off.x()) * 0.5f;
@ -288,8 +300,10 @@ protected:
mGradient.setOpacity(mTitleOverlayOpacity); mGradient.setOpacity(mTitleOverlayOpacity);
mGradient.render(identTrans); mGradient.render(identTrans);
TextCache* cache = mTitleOverlayFont->buildTextCache(text, off.x(), off.y(), 0xFFFFFF00 | mTitleOverlayOpacity); TextCache* cache = mTitleOverlayFont->buildTextCache(text, off.x(), off.y(),
mTitleOverlayFont->renderTextCache(cache); // relies on mGradient's render for Renderer::setMatrix() 0xFFFFFF00 | mTitleOverlayOpacity);
// Relies on mGradient's render for Renderer::setMatrix()
mTitleOverlayFont->renderTextCache(cache);
delete cache; delete cache;
} }
@ -301,24 +315,23 @@ protected:
int cursor = mCursor + amt; int cursor = mCursor + amt;
int absAmt = amt < 0 ? -amt : amt; int absAmt = amt < 0 ? -amt : amt;
// stop at the end if we've been holding down the button for a long time or // Stop at the end if we've been holding down the button for a long time or
// we're scrolling faster than one item at a time (e.g. page up/down) // we're scrolling faster than one item at a time (e.g. page up/down).
// otherwise, loop around // Otherwise, loop around.
if ((mLoopType == LIST_PAUSE_AT_END && (mScrollTier > 0 || absAmt > 1)) || if ((mLoopType == LIST_PAUSE_AT_END && (mScrollTier > 0 || absAmt > 1)) ||
mLoopType == LIST_NEVER_LOOP) mLoopType == LIST_NEVER_LOOP) {
{ if (cursor < 0) {
if(cursor < 0)
{
cursor = 0; cursor = 0;
mScrollVelocity = 0; mScrollVelocity = 0;
mScrollTier = 0; mScrollTier = 0;
}else if(cursor >= size()) }
{ else if (cursor >= size()) {
cursor = size() - 1; cursor = size() - 1;
mScrollVelocity = 0; mScrollVelocity = 0;
mScrollTier = 0; mScrollTier = 0;
} }
}else{ }
else {
while (cursor < 0) while (cursor < 0)
cursor += size(); cursor += size();
while (cursor >= size()) while (cursor >= size())

View file

@ -1,3 +1,9 @@
//
// ImageComponent.cpp
//
// Handles images: loading, resizing, cropping, color shifting etc.
//
#include "components/ImageComponent.h" #include "components/ImageComponent.h"
#include "resources/TextureResource.h" #include "resources/TextureResource.h"
@ -18,10 +24,26 @@ Vector2f ImageComponent::getSize() const
return GuiComponent::getSize() * (mBottomRightCrop - mTopLeftCrop); return GuiComponent::getSize() * (mBottomRightCrop - mTopLeftCrop);
} }
ImageComponent::ImageComponent(Window* window, bool forceLoad, bool dynamic) : GuiComponent(window), ImageComponent::ImageComponent(
mTargetIsMax(false), mTargetIsMin(false), mFlipX(false), mFlipY(false), mTargetSize(0, 0), mColorShift(0xFFFFFFFF), Window* window,
mColorShiftEnd(0xFFFFFFFF), mColorGradientHorizontal(true), mForceLoad(forceLoad), mDynamic(dynamic), bool forceLoad,
mFadeOpacity(0), mFading(false), mRotateByTargetSize(false), mTopLeftCrop(0.0f, 0.0f), mBottomRightCrop(1.0f, 1.0f) bool dynamic)
: GuiComponent(window),
mTargetIsMax(false),
mTargetIsMin(false),
mFlipX(false),
mFlipY(false),
mTargetSize(0, 0),
mColorShift(0xFFFFFFFF),
mColorShiftEnd(0xFFFFFFFF),
mColorGradientHorizontal(true),
mForceLoad(forceLoad),
mDynamic(dynamic),
mFadeOpacity(0),
mFading(false),
mRotateByTargetSize(false),
mTopLeftCrop(0.0f, 0.0f),
mBottomRightCrop(1.0f, 1.0f)
{ {
updateColors(); updateColors();
} }
@ -39,73 +61,75 @@ void ImageComponent::resize()
if (textureSize == Vector2f::Zero()) if (textureSize == Vector2f::Zero())
return; return;
if(mTexture->isTiled()) if (mTexture->isTiled()) {
{
mSize = mTargetSize; mSize = mTargetSize;
}else{
// SVG rasterization is determined by height (see SVGResource.cpp), and rasterization is done in terms of pixels
// if rounding is off enough in the rasterization step (for images with extreme aspect ratios), it can cause cutoff when the aspect ratio breaks
// so, we always make sure the resultant height is an integer to make sure cutoff doesn't happen, and scale width from that
// (you'll see this scattered throughout the function)
// this is probably not the best way, so if you're familiar with this problem and have a better solution, please make a pull request!
if(mTargetIsMax)
{
mSize = textureSize;
Vector2f resizeScale((mTargetSize.x() / mSize.x()), (mTargetSize.y() / mSize.y()));
if(resizeScale.x() < resizeScale.y())
{
mSize[0] *= resizeScale.x(); // this will be mTargetSize.x(). We can't exceed it, nor be lower than it.
// we need to make sure we're not creating an image larger than max size
mSize[1] = Math::min(Math::round(mSize[1] *= resizeScale.x()), mTargetSize.y());
}else{
mSize[1] = Math::round(mSize[1] * resizeScale.y()); // this will be mTargetSize.y(). We can't exceed it.
// for SVG rasterization, always calculate width from rounded height (see comment above)
// we need to make sure we're not creating an image larger than max size
mSize[0] = Math::min((mSize[1] / textureSize.y()) * textureSize.x(), mTargetSize.x());
} }
}else if(mTargetIsMin) else {
{ // SVG rasterization is determined by height and rasterization is done in terms of pixels.
// If rounding is off enough in the rasterization step (for images with extreme aspect
// ratios), it can cause cutoff when the aspect ratio breaks.
// So we always make sure the resultant height is an integer to make sure cutoff doesn't
// happen, and scale width from that (you'll see this scattered throughout the function).
// This is probably not the best way, so if you're familiar with this problem and have a
// better solution, please make a pull request!
if (mTargetIsMax) {
mSize = textureSize; mSize = textureSize;
Vector2f resizeScale((mTargetSize.x() / mSize.x()), (mTargetSize.y() / mSize.y())); Vector2f resizeScale((mTargetSize.x() / mSize.x()), (mTargetSize.y() / mSize.y()));
if(resizeScale.x() > resizeScale.y()) if (resizeScale.x() < resizeScale.y()) {
{ // This will be mTargetSize.x(). We can't exceed it, nor be lower than it.
mSize[0] *= resizeScale.x();
// We need to make sure we're not creating an image larger than max size.
mSize[1] = Math::min(Math::round(mSize[1] *= resizeScale.x()), mTargetSize.y());
}
else {
// This will be mTargetSize.y(). We can't exceed it.
mSize[1] = Math::round(mSize[1] * resizeScale.y());
// For SVG rasterization, always calculate width from rounded height (see comment
// above). We need to make sure we're not creating an image larger than max size.
mSize[0] = Math::min((mSize[1] / textureSize.y()) * textureSize.x(),
mTargetSize.x());
}
}
else if (mTargetIsMin) {
mSize = textureSize;
Vector2f resizeScale((mTargetSize.x() / mSize.x()), (mTargetSize.y() / mSize.y()));
if (resizeScale.x() > resizeScale.y()) {
mSize[0] *= resizeScale.x(); mSize[0] *= resizeScale.x();
mSize[1] *= resizeScale.x(); mSize[1] *= resizeScale.x();
float cropPercent = (mSize.y() - mTargetSize.y()) / (mSize.y() * 2); float cropPercent = (mSize.y() - mTargetSize.y()) / (mSize.y() * 2);
crop(0, cropPercent, 0, cropPercent); crop(0, cropPercent, 0, cropPercent);
}else{ }
else {
mSize[0] *= resizeScale.y(); mSize[0] *= resizeScale.y();
mSize[1] *= resizeScale.y(); mSize[1] *= resizeScale.y();
float cropPercent = (mSize.x() - mTargetSize.x()) / (mSize.x() * 2); float cropPercent = (mSize.x() - mTargetSize.x()) / (mSize.x() * 2);
crop(cropPercent, 0, cropPercent, 0); crop(cropPercent, 0, cropPercent, 0);
} }
// For SVG rasterization, always calculate width from rounded height (see comment
// for SVG rasterization, always calculate width from rounded height (see comment above) // above). We need to make sure we're not creating an image smaller than min size.
// we need to make sure we're not creating an image smaller than min size
mSize[1] = Math::max(Math::round(mSize[1]), mTargetSize.y()); mSize[1] = Math::max(Math::round(mSize[1]), mTargetSize.y());
mSize[0] = Math::max((mSize[1] / textureSize.y()) * textureSize.x(), mTargetSize.x()); mSize[0] = Math::max((mSize[1] / textureSize.y()) * textureSize.x(), mTargetSize.x());
}else{ }
// if both components are set, we just stretch else {
// if no components are set, we don't resize at all // If both components are set, we just stretch.
// If no components are set, we don't resize at all.
mSize = mTargetSize == Vector2f::Zero() ? textureSize : mTargetSize; mSize = mTargetSize == Vector2f::Zero() ? textureSize : mTargetSize;
// if only one component is set, we resize in a way that maintains aspect ratio // If only one component is set, we resize in a way that maintains aspect ratio.
// for SVG rasterization, we always calculate width from rounded height (see comment above) // For SVG rasterization, we always calculate width from rounded height (see
if(!mTargetSize.x() && mTargetSize.y()) // comment above).
{ if (!mTargetSize.x() && mTargetSize.y()) {
mSize[1] = Math::round(mTargetSize.y()); mSize[1] = Math::round(mTargetSize.y());
mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x(); mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x();
}else if(mTargetSize.x() && !mTargetSize.y()) }
{ else if (mTargetSize.x() && !mTargetSize.y()) {
mSize[1] = Math::round((mTargetSize.x() / textureSize.x()) * textureSize.y()); mSize[1] = Math::round((mTargetSize.x() / textureSize.x()) * textureSize.y());
mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x(); mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x();
} }
@ -114,7 +138,7 @@ void ImageComponent::resize()
mSize[0] = Math::round(mSize.x()); mSize[0] = Math::round(mSize.x());
mSize[1] = Math::round(mSize.y()); mSize[1] = Math::round(mSize.y());
// mSize.y() should already be rounded // mSize.y() should already be rounded.
mTexture->rasterizeAt((size_t)mSize.x(), (size_t)mSize.y()); mTexture->rasterizeAt((size_t)mSize.x(), (size_t)mSize.y());
onSizeChanged(); onSizeChanged();
@ -132,13 +156,13 @@ void ImageComponent::setDefaultImage(std::string path)
void ImageComponent::setImage(std::string path, bool tile) void ImageComponent::setImage(std::string path, bool tile)
{ {
if(path.empty() || !ResourceManager::getInstance()->fileExists(path)) if (path.empty() || !ResourceManager::getInstance()->fileExists(path)) {
{
if (mDefaultPath.empty() || !ResourceManager::getInstance()->fileExists(mDefaultPath)) if (mDefaultPath.empty() || !ResourceManager::getInstance()->fileExists(mDefaultPath))
mTexture.reset(); mTexture.reset();
else else
mTexture = TextureResource::get(mDefaultPath, tile, mForceLoad, mDynamic); mTexture = TextureResource::get(mDefaultPath, tile, mForceLoad, mDynamic);
} else { }
else {
mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic); mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic);
} }
@ -274,8 +298,8 @@ void ImageComponent::updateVertices()
if (!mTexture || !mTexture->isInitialized()) if (!mTexture || !mTexture->isInitialized())
return; return;
// we go through this mess to make sure everything is properly rounded // We go through this mess to make sure everything is properly rounded.
// if we just round vertices at the end, edge cases occur near sizes of 0.5 // If we just round vertices at the end, edge cases occur near sizes of 0.5.
const Vector2f topLeft = { mSize * mTopLeftCrop }; const Vector2f topLeft = { mSize * mTopLeftCrop };
const Vector2f bottomRight = { mSize * mBottomRightCrop }; const Vector2f bottomRight = { mSize * mBottomRightCrop };
const float px = mTexture->isTiled() ? mSize.x() / getTextureSize().x() : 1.0f; const float px = mTexture->isTiled() ? mSize.x() / getTextureSize().x() : 1.0f;
@ -288,18 +312,16 @@ void ImageComponent::updateVertices()
updateColors(); updateColors();
// round vertices // Round vertices.
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
mVertices[i].pos.round(); mVertices[i].pos.round();
if(mFlipX) if (mFlipX) {
{
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
mVertices[i].tex[0] = px - mVertices[i].tex[0]; mVertices[i].tex[0] = px - mVertices[i].tex[0];
} }
if(mFlipY) if (mFlipY) {
{
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
mVertices[i].tex[1] = py - mVertices[i].tex[1]; mVertices[i].tex[1] = py - mVertices[i].tex[1];
} }
@ -308,8 +330,10 @@ void ImageComponent::updateVertices()
void ImageComponent::updateColors() void ImageComponent::updateColors()
{ {
const float opacity = (mOpacity * (mFading ? mFadeOpacity / 255.0 : 1.0)) / 255.0; const float opacity = (mOpacity * (mFading ? mFadeOpacity / 255.0 : 1.0)) / 255.0;
const unsigned int color = Renderer::convertColor(mColorShift & 0xFFFFFF00 | (unsigned char)((mColorShift & 0xFF) * opacity)); const unsigned int color = Renderer::convertColor(mColorShift & 0xFFFFFF00 |
const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd & 0xFFFFFF00 | (unsigned char)((mColorShiftEnd & 0xFF) * opacity)); (unsigned char)((mColorShift & 0xFF) * opacity));
const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd & 0xFFFFFF00 |
(unsigned char)((mColorShiftEnd & 0xFF) * opacity));
mVertices[0].col = color; mVertices[0].col = color;
mVertices[1].col = mColorGradientHorizontal ? colorEnd : color; mVertices[1].col = mColorGradientHorizontal ? colorEnd : color;
@ -325,23 +349,23 @@ void ImageComponent::render(const Transform4x4f& parentTrans)
Transform4x4f trans = parentTrans * getTransform(); Transform4x4f trans = parentTrans * getTransform();
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
if(mTexture && mOpacity > 0) if (mTexture && mOpacity > 0) {
{
if (Settings::getInstance()->getBool("DebugImage")) { if (Settings::getInstance()->getBool("DebugImage")) {
Vector2f targetSizePos = (mTargetSize - mSize) * mOrigin * -1; Vector2f targetSizePos = (mTargetSize - mSize) * mOrigin * -1;
Renderer::drawRect(targetSizePos.x(), targetSizePos.y(), mTargetSize.x(), mTargetSize.y(), 0xFF000033, 0xFF000033); Renderer::drawRect(targetSizePos.x(), targetSizePos.y(), mTargetSize.x(),
mTargetSize.y(), 0xFF000033, 0xFF000033);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0x00000033, 0x00000033); Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0x00000033, 0x00000033);
} }
if(mTexture->isInitialized()) if (mTexture->isInitialized()) {
{ // Actually draw the image.
// actually draw the image
// The bind() function returns false if the texture is not currently loaded. A blank // The bind() function returns false if the texture is not currently loaded. A blank
// texture is bound in this case but we want to handle a fade so it doesn't just 'jump' in // texture is bound in this case but we want to handle a fade so it doesn't just
// when it finally loads // 'jump' in when it finally loads.
fadeIn(mTexture->bind()); fadeIn(mTexture->bind());
Renderer::drawTriangleStrips(&mVertices[0], 4); Renderer::drawTriangleStrips(&mVertices[0], 4);
}else{ }
else {
LOG(LogError) << "Image texture is not initialized!"; LOG(LogError) << "Image texture is not initialized!";
mTexture.reset(); mTexture.reset();
} }
@ -352,33 +376,27 @@ void ImageComponent::render(const Transform4x4f& parentTrans)
void ImageComponent::fadeIn(bool textureLoaded) void ImageComponent::fadeIn(bool textureLoaded)
{ {
if (!mForceLoad) if (!mForceLoad) {
{ if (!textureLoaded) {
if (!textureLoaded) // Start the fade if this is the first time we've encountered the unloaded texture.
{ if (!mFading) {
// Start the fade if this is the first time we've encountered the unloaded texture // Start with a zero opacity and flag it as fading.
if (!mFading)
{
// Start with a zero opacity and flag it as fading
mFadeOpacity = 0; mFadeOpacity = 0;
mFading = true; mFading = true;
updateColors(); updateColors();
} }
} }
else if (mFading) else if (mFading) {
{ // The texture is loaded and we need to fade it in. The fade is based on the frame
// The texture is loaded and we need to fade it in. The fade is based on the frame rate // rate and is 1/4 second if running at 60 frames per second although the actual
// and is 1/4 second if running at 60 frames per second although the actual value is not // value is not that important.
// that important
int opacity = mFadeOpacity + 255 / 15; int opacity = mFadeOpacity + 255 / 15;
// See if we've finished fading // See if we've finished fading.
if (opacity >= 255) if (opacity >= 255) {
{
mFadeOpacity = 255; mFadeOpacity = 255;
mFading = false; mFading = false;
} }
else else {
{
mFadeOpacity = (unsigned char)opacity; mFadeOpacity = (unsigned char)opacity;
} }
updateColors(); updateColors();
@ -391,20 +409,22 @@ bool ImageComponent::hasImage()
return (bool)mTexture; return (bool)mTexture;
} }
void ImageComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) void ImageComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties)
{ {
using namespace ThemeFlags; using namespace ThemeFlags;
GuiComponent::applyTheme(theme, view, element, (properties ^ SIZE) | ((properties & (SIZE | POSITION)) ? ORIGIN : 0)); GuiComponent::applyTheme(theme, view, element, (properties ^ SIZE) |
((properties & (SIZE | POSITION)) ? ORIGIN : 0));
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "image"); const ThemeData::ThemeElement* elem = theme->getElement(view, element, "image");
if (!elem) if (!elem)
return; return;
Vector2f scale = getParent() ? getParent()->getSize() : Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f scale = getParent() ? getParent()->getSize() :
Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
if(properties & ThemeFlags::SIZE) if (properties & ThemeFlags::SIZE) {
{
if (elem->has("size")) if (elem->has("size"))
setResize(elem->get<Vector2f>("size") * scale); setResize(elem->get<Vector2f>("size") * scale);
else if (elem->has("maxSize")) else if (elem->has("maxSize"))
@ -416,20 +436,16 @@ void ImageComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const s
if (elem->has("default")) if (elem->has("default"))
setDefaultImage(elem->get<std::string>("default")); setDefaultImage(elem->get<std::string>("default"));
if(properties & PATH && elem->has("path")) if (properties & PATH && elem->has("path")) {
{
bool tile = (elem->has("tile") && elem->get<bool>("tile")); bool tile = (elem->has("tile") && elem->get<bool>("tile"));
setImage(elem->get<std::string>("path"), tile); setImage(elem->get<std::string>("path"), tile);
} }
if(properties & COLOR) if (properties & COLOR) {
{
if (elem->has("color")) if (elem->has("color"))
setColorShift(elem->get<unsigned int>("color")); setColorShift(elem->get<unsigned int>("color"));
if (elem->has("colorEnd")) if (elem->has("colorEnd"))
setColorShiftEnd(elem->get<unsigned int>("colorEnd")); setColorShiftEnd(elem->get<unsigned int>("colorEnd"));
if (elem->has("gradientType")) if (elem->has("gradientType"))
setColorGradientHorizontal(!(elem->get<std::string>("gradientType").compare("horizontal"))); setColorGradientHorizontal(!(elem->get<std::string>("gradientType").compare("horizontal")));
} }

View file

@ -1,3 +1,9 @@
//
// ImageComponent.h
//
// Handles images: loading, resizing, cropping, color shifting etc.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_IMAGE_COMPONENT_H #ifndef ES_CORE_COMPONENTS_IMAGE_COMPONENT_H
#define ES_CORE_COMPONENTS_IMAGE_COMPONENT_H #define ES_CORE_COMPONENTS_IMAGE_COMPONENT_H
@ -16,7 +22,8 @@ public:
void setDefaultImage(std::string path); void setDefaultImage(std::string path);
//Loads the image at the given filepath. Will tile if tile is true (retrieves texture as tiling, creates vertices accordingly). // Loads the image at the given filepath. Will tile if tile is true (retrieves texture
// as tiling, creates vertices accordingly).
void setImage(std::string path, bool tile = false); void setImage(std::string path, bool tile = false);
// Loads an image from memory. // Loads an image from memory.
void setImage(const char* image, size_t length, bool tile = false); void setImage(const char* image, size_t length, bool tile = false);
@ -26,8 +33,9 @@ public:
void onSizeChanged() override; void onSizeChanged() override;
void setOpacity(unsigned char opacity) override; void setOpacity(unsigned char opacity) override;
// Resize the image to fit this size. If one axis is zero, scale that axis to maintain aspect ratio. // Resize the image to fit this size. If one axis is zero, scale that axis to maintain
// If both are non-zero, potentially break the aspect ratio. If both are zero, no resizing. // aspect ratio. If both are non-zero, potentially break the aspect ratio. If both are
// zero, don't do any resizing.
// Can be set before or after an image is loaded. // Can be set before or after an image is loaded.
// setMaxSize() and setResize() are mutually exclusive. // setMaxSize() and setResize() are mutually exclusive.
void setResize(float width, float height); void setResize(float width, float height);
@ -44,7 +52,7 @@ public:
Vector2f getRotationSize() const override; Vector2f getRotationSize() const override;
// Applied AFTER image positioning and sizing // Applied AFTER image positioning and sizing.
// cropTop(0.2) will crop 20% of the top of the image. // cropTop(0.2) will crop 20% of the top of the image.
void cropLeft(float percent); void cropLeft(float percent);
void cropTop(float percent); void cropTop(float percent);
@ -61,9 +69,11 @@ public:
void setFlipX(bool flip); // Mirror on the X axis. void setFlipX(bool flip); // Mirror on the X axis.
void setFlipY(bool flip); // Mirror on the Y axis. void setFlipY(bool flip); // Mirror on the Y axis.
void setRotateByTargetSize(bool rotate); // Flag indicating if rotation should be based on target size vs. actual size. // Flag indicating if rotation should be based on target size vs. actual size.
void setRotateByTargetSize(bool rotate);
// Returns the size of the current texture, or (0, 0) if none is loaded. May be different than drawn size (use getSize() for that). // Returns the size of the current texture, or (0, 0) if none is loaded.
// May be different than drawn size (use getSize() for that).
Vector2i getTextureSize() const; Vector2i getTextureSize() const;
Vector2f getSize() const override; Vector2f getSize() const override;
@ -72,11 +82,13 @@ public:
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties) override;
virtual std::vector<HelpPrompt> getHelpPrompts() override; virtual std::vector<HelpPrompt> getHelpPrompts() override;
std::shared_ptr<TextureResource> getTexture() { return mTexture; }; std::shared_ptr<TextureResource> getTexture() { return mTexture; };
private: private:
Vector2f mTargetSize; Vector2f mTargetSize;

View file

@ -1,3 +1,9 @@
//
// ImageGridComponent.cpp
//
// X*Y image grid, used by GridGameListView.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H #ifndef ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
#define ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H #define ES_CORE_COMPONENTS_IMAGE_GRID_COMPONENT_H
@ -10,21 +16,18 @@
#define EXTRAITEMS 2 #define EXTRAITEMS 2
enum ScrollDirection enum ScrollDirection {
{
SCROLL_VERTICALLY, SCROLL_VERTICALLY,
SCROLL_HORIZONTALLY SCROLL_HORIZONTALLY
}; };
enum ImageSource enum ImageSource {
{
THUMBNAIL, THUMBNAIL,
IMAGE, IMAGE,
MARQUEE MARQUEE
}; };
struct ImageGridData struct ImageGridData {
{
std::string texturePath; std::string texturePath;
}; };
@ -58,10 +61,12 @@ public:
bool input(InputConfig* config, Input input) override; bool input(InputConfig* config, Input input) override;
void update(int deltaTime) override; void update(int deltaTime) override;
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties) override;
void onSizeChanged() override; void onSizeChanged() override;
inline void setCursorChangedCallback(const std::function<void(CursorState state)>& func) { mCursorChangedCallback = func; } inline void setCursorChangedCallback(const std::function<void(CursorState state)>& func)
{ mCursorChangedCallback = func; }
ImageSource getImageSource() { return mImageSource; }; ImageSource getImageSource() { return mImageSource; };
@ -69,23 +74,23 @@ protected:
virtual void onCursorChanged(const CursorState& state) override; virtual void onCursorChanged(const CursorState& state) override;
private: private:
// TILES // Tiles.
void buildTiles(); void buildTiles();
void updateTiles(bool ascending = true, bool allowAnimation = true, bool updateSelectedState = true); void updateTiles(bool ascending = true, bool allowAnimation = true,
bool updateSelectedState = true);
void updateTileAtPos(int tilePos, int imgPos, bool allowAnimation, bool updateSelectedState); void updateTileAtPos(int tilePos, int imgPos, bool allowAnimation, bool updateSelectedState);
void calcGridDimension(); void calcGridDimension();
bool isScrollLoop(); bool isScrollLoop();
bool isVertical() { return mScrollDirection == SCROLL_VERTICALLY; }; bool isVertical() { return mScrollDirection == SCROLL_VERTICALLY; };
// Images and entries.
// IMAGES & ENTRIES
bool mEntriesDirty; bool mEntriesDirty;
int mLastCursor; int mLastCursor;
std::string mDefaultGameTexture; std::string mDefaultGameTexture;
std::string mDefaultFolderTexture; std::string mDefaultFolderTexture;
// TILES // Tiles.
bool mLastRowPartial; bool mLastRowPartial;
Vector2f mAutoLayout; Vector2f mAutoLayout;
float mAutoLayoutZoom; float mAutoLayoutZoom;
@ -102,7 +107,7 @@ private:
float mCamera; float mCamera;
float mCameraDirection; float mCameraDirection;
// MISCELLANEOUS // Miscellaneous.
bool mAnimate; bool mAnimate;
bool mCenterSelection; bool mCenterSelection;
bool mScrollLoop; bool mScrollLoop;
@ -114,7 +119,8 @@ private:
template<typename T> template<typename T>
ImageGridComponent<T>::ImageGridComponent(Window* window) : IList<ImageGridData, T>(window) ImageGridComponent<T>::ImageGridComponent(Window* window) : IList<ImageGridData, T>(window)
{ {
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f screen = Vector2f((float)Renderer::getScreenWidth(),
(float)Renderer::getScreenHeight());
mCamera = 0.0; mCamera = 0.0;
mCameraDirection = 1.0; mCameraDirection = 1.0;
@ -156,8 +162,7 @@ void ImageGridComponent<T>::add(const std::string& name, const std::string& imag
template<typename T> template<typename T>
bool ImageGridComponent<T>::input(InputConfig* config, Input input) bool ImageGridComponent<T>::input(InputConfig* config, Input input)
{ {
if(input.value != 0) if (input.value != 0) {
{
int idx = isVertical() ? 0 : 1; int idx = isVertical() ? 0 : 1;
Vector2i dir = Vector2i::Zero(); Vector2i dir = Vector2i::Zero();
@ -170,19 +175,18 @@ bool ImageGridComponent<T>::input(InputConfig* config, Input input)
else if (config->isMappedLike("right", input)) else if (config->isMappedLike("right", input))
dir[0 ^ idx] = 1; dir[0 ^ idx] = 1;
if(dir != Vector2i::Zero()) if (dir != Vector2i::Zero()) {
{
if (isVertical()) if (isVertical())
listInput(dir.x() + dir.y() * mGridDimension.x()); listInput(dir.x() + dir.y() * mGridDimension.x());
else else
listInput(dir.x() + dir.y() * mGridDimension.y()); listInput(dir.x() + dir.y() * mGridDimension.y());
return true; return true;
} }
}else{
if(config->isMappedLike("up", input) || config->isMappedLike("down", input) || config->isMappedLike("left", input) || config->isMappedLike("right", input))
{
stopScrolling();
} }
else {
if (config->isMappedLike("up", input) || config->isMappedLike("down", input) ||
config->isMappedLike("left", input) || config->isMappedLike("right", input))
stopScrolling();
} }
return GuiComponent::input(config, input); return GuiComponent::input(config, input);
@ -204,33 +208,34 @@ void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
Transform4x4f trans = getTransform() * parentTrans; Transform4x4f trans = getTransform() * parentTrans;
Transform4x4f tileTrans = trans; Transform4x4f tileTrans = trans;
float offsetX = isVertical() ? 0.0f : mCamera * mCameraDirection * (mTileSize.x() + mMargin.x()); float offsetX = isVertical() ? 0.0f : mCamera * mCameraDirection *
float offsetY = isVertical() ? mCamera * mCameraDirection * (mTileSize.y() + mMargin.y()) : 0.0f; (mTileSize.x() + mMargin.x());
float offsetY = isVertical() ? mCamera * mCameraDirection *
(mTileSize.y() + mMargin.y()) : 0.0f;
tileTrans.translate(Vector3f(offsetX, offsetY, 0.0)); tileTrans.translate(Vector3f(offsetX, offsetY, 0.0));
if(mEntriesDirty) if (mEntriesDirty) {
{
updateTiles(); updateTiles();
mEntriesDirty = false; mEntriesDirty = false;
} }
// Create a clipRect to hide tiles used to buffer texture loading // Create a clipRect to hide tiles used to buffer texture loading.
float scaleX = trans.r0().x(); float scaleX = trans.r0().x();
float scaleY = trans.r1().y(); float scaleY = trans.r1().y();
Vector2i pos((int)Math::round(trans.translation()[0]), (int)Math::round(trans.translation()[1])); Vector2i pos((int)Math::round(trans.translation()[0]),
Vector2i size((int)Math::round(mSize.x() * scaleX), (int)Math::round(mSize.y() * scaleY)); (int)Math::round(trans.translation()[1]));
Vector2i size((int)Math::round(mSize.x() * scaleX),
(int)Math::round(mSize.y() * scaleY));
Renderer::pushClipRect(pos, size); Renderer::pushClipRect(pos, size);
// Render all the tiles but the selected one // Render all the tiles but the selected one.
std::shared_ptr<GridTileComponent> selectedTile = NULL; std::shared_ptr<GridTileComponent> selectedTile = nullptr;
for(auto it = mTiles.begin(); it != mTiles.end(); it++) for (auto it = mTiles.begin(); it != mTiles.end(); it++) {
{
std::shared_ptr<GridTileComponent> tile = (*it); std::shared_ptr<GridTileComponent> tile = (*it);
// If it's the selected image, keep it for later, otherwise render it now.
// If it's the selected image, keep it for later, otherwise render it now
if (tile->isSelected()) if (tile->isSelected())
selectedTile = tile; selectedTile = tile;
else else
@ -239,8 +244,8 @@ void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
Renderer::popClipRect(); Renderer::popClipRect();
// Render the selected image on top of the others // Render the selected image on top of the others.
if (selectedTile != NULL) if (selectedTile != nullptr)
selectedTile->render(tileTrans); selectedTile->render(tileTrans);
listRenderTitleOverlay(trans); listRenderTitleOverlay(trans);
@ -249,24 +254,27 @@ void ImageGridComponent<T>::render(const Transform4x4f& parentTrans)
} }
template<typename T> template<typename T>
void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
const std::string& view, const std::string& element, unsigned int properties)
{ {
// Apply theme to GuiComponent but not size property, which will be applied at the end of this function // Apply theme to GuiComponent but not the size property, which will be applied
// at the end of this function.
GuiComponent::applyTheme(theme, view, element, properties ^ ThemeFlags::SIZE); GuiComponent::applyTheme(theme, view, element, properties ^ ThemeFlags::SIZE);
// Keep the theme pointer to apply it on the tiles later on // Keep the theme pointer to apply it on the tiles later on.
mTheme = theme; mTheme = theme;
Vector2f screen = Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f screen = Vector2f((float)Renderer::getScreenWidth(),
(float)Renderer::getScreenHeight());
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "imagegrid"); const ThemeData::ThemeElement* elem = theme->getElement(view, element, "imagegrid");
if (elem) if (elem) {
{
if (elem->has("margin")) if (elem->has("margin"))
mMargin = elem->get<Vector2f>("margin") * screen; mMargin = elem->get<Vector2f>("margin") * screen;
if (elem->has("padding")) if (elem->has("padding"))
mPadding = elem->get<Vector4f>("padding") * Vector4f(screen.x(), screen.y(), screen.x(), screen.y()); mPadding = elem->get<Vector4f>("padding") *
Vector4f(screen.x(), screen.y(), screen.x(), screen.y());
if (elem->has("autoLayout")) if (elem->has("autoLayout"))
mAutoLayout = elem->get<Vector2f>("autoLayout"); mAutoLayout = elem->get<Vector2f>("autoLayout");
@ -274,8 +282,7 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
if (elem->has("autoLayoutSelectedZoom")) if (elem->has("autoLayoutSelectedZoom"))
mAutoLayoutZoom = elem->get<float>("autoLayoutSelectedZoom"); mAutoLayoutZoom = elem->get<float>("autoLayoutSelectedZoom");
if (elem->has("imageSource")) if (elem->has("imageSource")) {
{
auto direction = elem->get<std::string>("imageSource"); auto direction = elem->get<std::string>("imageSource");
if (direction == "image") if (direction == "image")
mImageSource = IMAGE; mImageSource = IMAGE;
@ -284,14 +291,15 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
else else
mImageSource = THUMBNAIL; mImageSource = THUMBNAIL;
} }
else else {
mImageSource = THUMBNAIL; mImageSource = THUMBNAIL;
}
if (elem->has("scrollDirection")) if (elem->has("scrollDirection"))
mScrollDirection = (ScrollDirection)(elem->get<std::string>("scrollDirection") == "horizontal"); mScrollDirection = (ScrollDirection)(elem->
get<std::string>("scrollDirection") == "horizontal");
if (elem->has("centerSelection")) if (elem->has("centerSelection")) {
{
mCenterSelection = (elem->get<bool>("centerSelection")); mCenterSelection = (elem->get<bool>("centerSelection"));
if (elem->has("scrollLoop")) if (elem->has("scrollLoop"))
@ -303,42 +311,38 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
else else
mAnimate = true; mAnimate = true;
if (elem->has("gameImage")) if (elem->has("gameImage")) {
{
std::string path = elem->get<std::string>("gameImage"); std::string path = elem->get<std::string>("gameImage");
if (!ResourceManager::getInstance()->fileExists(path)) if (!ResourceManager::getInstance()->fileExists(path)) {
LOG(LogWarning) << "Could not replace default game image, check path: " << path; LOG(LogWarning) << "Could not replace default game image, check path: " << path;
else }
{ else {
std::string oldDefaultGameTexture = mDefaultGameTexture; std::string oldDefaultGameTexture = mDefaultGameTexture;
mDefaultGameTexture = path; mDefaultGameTexture = path;
// mEntries are already loaded at this point, // mEntries are already loaded at this point, so we need to
// so we need to update them with new game image texture // update them with the new game image texture.
for (auto it = mEntries.begin(); it != mEntries.end(); it++) for (auto it = mEntries.begin(); it != mEntries.end(); it++) {
{
if ((*it).data.texturePath == oldDefaultGameTexture) if ((*it).data.texturePath == oldDefaultGameTexture)
(*it).data.texturePath = mDefaultGameTexture; (*it).data.texturePath = mDefaultGameTexture;
} }
} }
} }
if (elem->has("folderImage")) if (elem->has("folderImage")) {
{
std::string path = elem->get<std::string>("folderImage"); std::string path = elem->get<std::string>("folderImage");
if (!ResourceManager::getInstance()->fileExists(path)) if (!ResourceManager::getInstance()->fileExists(path)) {
LOG(LogWarning) << "Could not replace default folder image, check path: " << path; LOG(LogWarning) << "Could not replace default folder image, check path: " << path;
else }
{ else {
std::string oldDefaultFolderTexture = mDefaultFolderTexture; std::string oldDefaultFolderTexture = mDefaultFolderTexture;
mDefaultFolderTexture = path; mDefaultFolderTexture = path;
// mEntries are already loaded at this point, // mEntries are already loaded at this point, so we need to
// so we need to update them with new folder image texture // update them with new folder image texture.
for (auto it = mEntries.begin(); it != mEntries.end(); it++) for (auto it = mEntries.begin(); it != mEntries.end(); it++) {
{
if ((*it).data.texturePath == oldDefaultFolderTexture) if ((*it).data.texturePath == oldDefaultFolderTexture)
(*it).data.texturePath = mDefaultFolderTexture; (*it).data.texturePath = mDefaultFolderTexture;
} }
@ -346,18 +350,18 @@ void ImageGridComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
} }
} }
// We still need to manually get the grid tile size here, // We still need to manually get the grid tile size here, so we can recalculate
// so we can recalculate the new grid dimension, and THEN (re)build the tiles // the new grid dimension, and THEN (re)build the tiles.
elem = theme->getElement(view, "default", "gridtile"); elem = theme->getElement(view, "default", "gridtile");
mTileSize = elem && elem->has("size") ? mTileSize = elem && elem->has("size") ?
elem->get<Vector2f>("size") * screen : elem->get<Vector2f>("size") * screen :
GridTileComponent::getDefaultTileSize(); GridTileComponent::getDefaultTileSize();
// Apply size property, will trigger a call to onSizeChanged() which will build the tiles // Apply size property which will trigger a call to onSizeChanged() which will build the tiles.
GuiComponent::applyTheme(theme, view, element, ThemeFlags::SIZE); GuiComponent::applyTheme(theme, view, element, ThemeFlags::SIZE);
// Trigger the call manually if the theme have no "imagegrid" element // Trigger the call manually if the theme have no "imagegrid" element.
if (!elem) if (!elem)
buildTiles(); buildTiles();
} }
@ -372,20 +376,16 @@ void ImageGridComponent<T>::onSizeChanged()
template<typename T> template<typename T>
void ImageGridComponent<T>::onCursorChanged(const CursorState& state) void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
{ {
if (mLastCursor == mCursor) if (mLastCursor == mCursor) {
{
if (state == CURSOR_STOPPED && mCursorChangedCallback) if (state == CURSOR_STOPPED && mCursorChangedCallback)
mCursorChangedCallback(state); mCursorChangedCallback(state);
return; return;
} }
bool direction = mCursor >= mLastCursor; bool direction = mCursor >= mLastCursor;
int diff = direction ? mCursor - mLastCursor : mLastCursor - mCursor; int diff = direction ? mCursor - mLastCursor : mLastCursor - mCursor;
if (isScrollLoop() && diff == mEntries.size() - 1) if (isScrollLoop() && diff == mEntries.size() - 1)
{
direction = !direction; direction = !direction;
}
int oldStart = mStartPosition; int oldStart = mStartPosition;
@ -405,15 +405,13 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
float startPos = 0; float startPos = 0;
float endPos = 1; float endPos = 1;
if (((GuiComponent*)this)->isAnimationPlaying(2)) if (((GuiComponent*)this)->isAnimationPlaying(2)) {
{
startPos = 0; startPos = 0;
((GuiComponent*)this)->cancelAnimation(2); ((GuiComponent*)this)->cancelAnimation(2);
updateTiles(direction, false, false); updateTiles(direction, false, false);
} }
if (mAnimate) { if (mAnimate) {
std::shared_ptr<GridTileComponent> oldTile = nullptr; std::shared_ptr<GridTileComponent> oldTile = nullptr;
std::shared_ptr<GridTileComponent> newTile = nullptr; std::shared_ptr<GridTileComponent> newTile = nullptr;
@ -452,19 +450,20 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
int firstVisibleCol = mStartPosition / dimOpposite; int firstVisibleCol = mStartPosition / dimOpposite;
if ((col < centralCol || (col == 0 && col == centralCol)) && !mCenterSelection) if ((col < centralCol || (col == 0 && col == centralCol)) && !mCenterSelection) {
mStartPosition = 0; mStartPosition = 0;
else if ((col - centralCol) > lastScroll && !mCenterSelection && !isScrollLoop()) }
else if ((col - centralCol) > lastScroll && !mCenterSelection && !isScrollLoop()) {
mStartPosition = lastScroll * dimOpposite; mStartPosition = lastScroll * dimOpposite;
else if ((maxCentralCol != centralCol && col == firstVisibleCol + maxCentralCol) || col == firstVisibleCol + centralCol) }
{ else if ((maxCentralCol != centralCol && col == firstVisibleCol + maxCentralCol) ||
col == firstVisibleCol + centralCol) {
if (col == firstVisibleCol + maxCentralCol) if (col == firstVisibleCol + maxCentralCol)
mStartPosition = (col - maxCentralCol) * dimOpposite; mStartPosition = (col - maxCentralCol) * dimOpposite;
else else
mStartPosition = (col - centralCol) * dimOpposite; mStartPosition = (col - centralCol) * dimOpposite;
} }
else else {
{
if (oldCol == firstVisibleCol + maxCentralCol) if (oldCol == firstVisibleCol + maxCentralCol)
mStartPosition = (col - maxCentralCol) * dimOpposite; mStartPosition = (col - maxCentralCol) * dimOpposite;
else else
@ -477,8 +476,7 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
mCameraDirection = direction ? -1.0f : 1.0f; mCameraDirection = direction ? -1.0f : 1.0f;
mCamera = 0; mCamera = 0;
if (lastCursor < 0 || !mAnimate) if (lastCursor < 0 || !mAnimate) {
{
updateTiles(direction, mAnimate && (lastCursor >= 0 || isScrollLoop())); updateTiles(direction, mAnimate && (lastCursor >= 0 || isScrollLoop()));
if (mCursorChangedCallback) if (mCursorChangedCallback)
@ -492,15 +490,13 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
bool moveCamera = (oldStart != mStartPosition); bool moveCamera = (oldStart != mStartPosition);
auto func = [this, startPos, endPos, moveCamera](float t) auto func = [this, startPos, endPos, moveCamera](float t) {
{
if (!moveCamera) if (!moveCamera)
return; return;
t -= 1; // cubic ease out t -= 1; // cubic ease out
float pct = Math::lerp(0, 1, t*t*t + 1); float pct = Math::lerp(0, 1, t*t*t + 1);
t = startPos * (1.0f - pct) + endPos * pct; t = startPos * (1.0f - pct) + endPos * pct;
mCamera = t; mCamera = t;
}; };
@ -511,7 +507,7 @@ void ImageGridComponent<T>::onCursorChanged(const CursorState& state)
} }
// Create and position tiles (mTiles) // Create and position tiles (mTiles).
template<typename T> template<typename T>
void ImageGridComponent<T>::buildTiles() void ImageGridComponent<T>::buildTiles()
{ {
@ -520,45 +516,44 @@ void ImageGridComponent<T>::buildTiles()
calcGridDimension(); calcGridDimension();
if (mCenterSelection) if (mCenterSelection) {
{ int dimScrollable = (isVertical() ? mGridDimension.y() :
int dimScrollable = (isVertical() ? mGridDimension.y() : mGridDimension.x()) - 2 * EXTRAITEMS; mGridDimension.x()) - 2 * EXTRAITEMS;
mStartPosition -= (int) Math::floorf(dimScrollable / 2.0f); mStartPosition -= (int) Math::floorf(dimScrollable / 2.0f);
} }
Vector2f tileDistance = mTileSize + mMargin; Vector2f tileDistance = mTileSize + mMargin;
if (mAutoLayout.x() != 0 && mAutoLayout.y() != 0) if (mAutoLayout.x() != 0 && mAutoLayout.y() != 0) {
{ auto x = (mSize.x() - (mMargin.x() * (mAutoLayout.x() - 1)) - mPadding.x() -
auto x = (mSize.x() - (mMargin.x() * (mAutoLayout.x() - 1)) - mPadding.x() - mPadding.z()) / (int) mAutoLayout.x(); mPadding.z()) / (int) mAutoLayout.x();
auto y = (mSize.y() - (mMargin.y() * (mAutoLayout.y() - 1)) - mPadding.y() - mPadding.w()) / (int) mAutoLayout.y(); auto y = (mSize.y() - (mMargin.y() * (mAutoLayout.y() - 1)) - mPadding.y() -
mPadding.w()) / (int) mAutoLayout.y();
mTileSize = Vector2f(x, y); mTileSize = Vector2f(x, y);
tileDistance = mTileSize + mMargin; tileDistance = mTileSize + mMargin;
} }
bool vert = isVertical(); bool vert = isVertical();
Vector2f startPosition = mTileSize / 2; Vector2f startPosition = mTileSize / 2;
startPosition += mPadding.v2(); startPosition += mPadding.v2();
int X, Y; int X;
int Y;
// Layout tile size and position // Layout tile size and position.
for (int y = 0; y < (vert ? mGridDimension.y() : mGridDimension.x()); y++) for (int y = 0; y < (vert ? mGridDimension.y() : mGridDimension.x()); y++) {
{ for (int x = 0; x < (vert ? mGridDimension.x() : mGridDimension.y()); x++) {
for (int x = 0; x < (vert ? mGridDimension.x() : mGridDimension.y()); x++) // Create tiles.
{
// Create tiles
auto tile = std::make_shared<GridTileComponent>(mWindow); auto tile = std::make_shared<GridTileComponent>(mWindow);
// In Vertical mod, tiles are ordered from left to right, then from top to bottom // In Vertical mod, tiles are ordered from left to right, then from top to bottom.
// In Horizontal mod, tiles are ordered from top to bottom, then from left to right // In Horizontal mod, tiles are ordered from top to bottom, then from left to right.
X = vert ? x : y - EXTRAITEMS; X = vert ? x : y - EXTRAITEMS;
Y = vert ? y - EXTRAITEMS : x; Y = vert ? y - EXTRAITEMS : x;
tile->setPosition(X * tileDistance.x() + startPosition.x(), Y * tileDistance.y() + startPosition.y()); tile->setPosition(X * tileDistance.x() + startPosition.x(), Y *
tileDistance.y() + startPosition.y());
tile->setOrigin(0.5f, 0.5f); tile->setOrigin(0.5f, 0.5f);
tile->setImage(""); tile->setImage("");
@ -574,16 +569,15 @@ void ImageGridComponent<T>::buildTiles()
} }
template<typename T> template<typename T>
void ImageGridComponent<T>::updateTiles(bool ascending, bool allowAnimation, bool updateSelectedState) void ImageGridComponent<T>::updateTiles(bool ascending, bool allowAnimation,
bool updateSelectedState)
{ {
if (!mTiles.size()) if (!mTiles.size())
return; return;
// Stop updating the tiles at highest scroll speed // Stop updating the tiles at highest scroll speed.
if (mScrollTier == 3) if (mScrollTier == 3) {
{ for (int ti = 0; ti < (int)mTiles.size(); ti++) {
for (int ti = 0; ti < (int)mTiles.size(); ti++)
{
std::shared_ptr<GridTileComponent> tile = mTiles.at(ti); std::shared_ptr<GridTileComponent> tile = mTiles.at(ti);
tile->setSelected(false); tile->setSelected(false);
@ -593,16 +587,15 @@ void ImageGridComponent<T>::updateTiles(bool ascending, bool allowAnimation, boo
return; return;
} }
// Temporary store previous texture so they can't be unloaded // Temporary store previous texture so they can't be unloaded.
std::vector<std::shared_ptr<TextureResource>> previousTextures; std::vector<std::shared_ptr<TextureResource>> previousTextures;
for (int ti = 0; ti < (int)mTiles.size(); ti++) for (int ti = 0; ti < (int)mTiles.size(); ti++) {
{
std::shared_ptr<GridTileComponent> tile = mTiles.at(ti); std::shared_ptr<GridTileComponent> tile = mTiles.at(ti);
previousTextures.push_back(tile->getTexture()); previousTextures.push_back(tile->getTexture());
} }
// If going down, update from top to bottom // If going down, update from top to bottom.
// If going up, update from bottom to top // If going up, update from bottom to top.
int scrollDirection = ascending ? 1 : -1; int scrollDirection = ascending ? 1 : -1;
int ti = ascending ? 0 : (int)mTiles.size() - 1; int ti = ascending ? 0 : (int)mTiles.size() - 1;
int end = ascending ? (int)mTiles.size() : -1; int end = ascending ? (int)mTiles.size() : -1;
@ -610,9 +603,8 @@ void ImageGridComponent<T>::updateTiles(bool ascending, bool allowAnimation, boo
img -= EXTRAITEMS * (isVertical() ? mGridDimension.x() : mGridDimension.y()); img -= EXTRAITEMS * (isVertical() ? mGridDimension.x() : mGridDimension.y());
// Update the tiles // Update the tiles.
while (ti != end) while (ti != end) {
{
updateTileAtPos(ti, img, allowAnimation, updateSelectedState); updateTileAtPos(ti, img, allowAnimation, updateSelectedState);
ti += scrollDirection; ti += scrollDirection;
@ -626,29 +618,27 @@ void ImageGridComponent<T>::updateTiles(bool ascending, bool allowAnimation, boo
} }
template<typename T> template<typename T>
void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos, bool allowAnimation, bool updateSelectedState) void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos,
bool allowAnimation, bool updateSelectedState)
{ {
std::shared_ptr<GridTileComponent> tile = mTiles.at(tilePos); std::shared_ptr<GridTileComponent> tile = mTiles.at(tilePos);
if(isScrollLoop()) if (isScrollLoop()) {
{
if (imgPos < 0) if (imgPos < 0)
imgPos += mEntries.size(); imgPos += mEntries.size();
else if (imgPos >= size()) else if (imgPos >= size())
imgPos -= mEntries.size(); imgPos -= mEntries.size();
} }
// If we have more tiles than we have to display images on screen, hide them // If we have more tiles than we have to display images on screen, hide them.
if(imgPos < 0 || imgPos >= size() || tilePos < 0 || tilePos >= (int) mTiles.size()) // Same for tiles out of the buffer // Same for tiles out of the buffer.
{ if (imgPos < 0 || imgPos >= size() || tilePos < 0 || tilePos >= (int) mTiles.size()) {
if (updateSelectedState) if (updateSelectedState)
tile->setSelected(false, allowAnimation); tile->setSelected(false, allowAnimation);
tile->reset(); tile->reset();
tile->setVisible(false); tile->setVisible(false);
} }
else else {
{
tile->setVisible(true); tile->setVisible(true);
std::string imagePath = mEntries.at(imgPos).data.texturePath; std::string imagePath = mEntries.at(imgPos).data.texturePath;
@ -660,10 +650,8 @@ void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos, bool allowA
else else
tile->setImage(mDefaultGameTexture); tile->setImage(mDefaultGameTexture);
if (updateSelectedState) if (updateSelectedState) {
{ if (imgPos == mCursor && mCursor != mLastCursor) {
if (imgPos == mCursor && mCursor != mLastCursor)
{
int dif = mCursor - tilePos; int dif = mCursor - tilePos;
int idx = mLastCursor - dif; int idx = mLastCursor - dif;
@ -673,19 +661,21 @@ void ImageGridComponent<T>::updateTileAtPos(int tilePos, int imgPos, bool allowA
Vector3f pos = mTiles.at(idx)->getBackgroundPosition(); Vector3f pos = mTiles.at(idx)->getBackgroundPosition();
tile->setSelected(true, allowAnimation, &pos); tile->setSelected(true, allowAnimation, &pos);
} }
else else {
tile->setSelected(imgPos == mCursor, allowAnimation); tile->setSelected(imgPos == mCursor, allowAnimation);
} }
}
} }
} }
// Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using a margin of size mMargin // Calculate how much tiles of size mTileSize we can fit in a grid of size mSize using
// a margin of size mMargin.
template<typename T> template<typename T>
void ImageGridComponent<T>::calcGridDimension() void ImageGridComponent<T>::calcGridDimension()
{ {
// GRID_SIZE = COLUMNS * TILE_SIZE + (COLUMNS - 1) * MARGIN // grid_size = columns * tile_size + (columns - 1) * margin
// <=> COLUMNS = (GRID_SIZE + MARGIN) / (TILE_SIZE + MARGIN) // <=> columns = (grid_size + margin) / (tile_size + margin)
Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin); Vector2f gridDimension = (mSize + mMargin) / (mTileSize + mMargin);
if (mAutoLayout.x() != 0 && mAutoLayout.y() != 0) if (mAutoLayout.x() != 0 && mAutoLayout.y() != 0)
@ -693,10 +683,10 @@ void ImageGridComponent<T>::calcGridDimension()
mLastRowPartial = Math::floorf(gridDimension.y()) != gridDimension.y(); mLastRowPartial = Math::floorf(gridDimension.y()) != gridDimension.y();
// Ceil y dim so we can display partial last row // Ceil y dim so we can display partial last row.
mGridDimension = Vector2i(gridDimension.x(), Math::ceilf(gridDimension.y())); mGridDimension = Vector2i(gridDimension.x(), Math::ceilf(gridDimension.y()));
// Grid dimension validation // Grid dimension validation.
if (mGridDimension.x() < 1) { if (mGridDimension.x() < 1) {
LOG(LogError) << "Theme defined grid X dimension below 1"; LOG(LogError) << "Theme defined grid X dimension below 1";
} }
@ -704,7 +694,7 @@ void ImageGridComponent<T>::calcGridDimension()
LOG(LogError) << "Theme defined grid Y dimension below 1"; LOG(LogError) << "Theme defined grid Y dimension below 1";
} }
// Add extra tiles to both sides : Add EXTRAITEMS before, EXTRAITEMS after // Add extra tiles to both sides: Add EXTRAITEMS before, EXTRAITEMS after.
if (isVertical()) if (isVertical())
mGridDimension.y() += 2 * EXTRAITEMS; mGridDimension.y() += 2 * EXTRAITEMS;
else else

View file

@ -1,14 +1,26 @@
//
// NinePatchComponent.cpp
//
// Breaks up an image into 3x3 patches to accomodate resizing without distortions.
//
#include "components/NinePatchComponent.h" #include "components/NinePatchComponent.h"
#include "resources/TextureResource.h" #include "resources/TextureResource.h"
#include "Log.h" #include "Log.h"
#include "ThemeData.h" #include "ThemeData.h"
NinePatchComponent::NinePatchComponent(Window* window, const std::string& path, unsigned int edgeColor, unsigned int centerColor) : GuiComponent(window), NinePatchComponent::NinePatchComponent(
Window* window,
const std::string& path,
unsigned int edgeColor,
unsigned int centerColor)
: GuiComponent(window),
mCornerSize(16, 16), mCornerSize(16, 16),
mEdgeColor(edgeColor), mCenterColor(centerColor), mEdgeColor(edgeColor),
mCenterColor(centerColor),
mPath(path), mPath(path),
mVertices(NULL) mVertices(nullptr)
{ {
if (!mPath.empty()) if (!mPath.empty())
buildVertices(); buildVertices();
@ -16,7 +28,7 @@ NinePatchComponent::NinePatchComponent(Window* window, const std::string& path,
NinePatchComponent::~NinePatchComponent() NinePatchComponent::~NinePatchComponent()
{ {
if (mVertices != NULL) if (mVertices != nullptr)
delete[] mVertices; delete[] mVertices;
} }
@ -34,36 +46,37 @@ void NinePatchComponent::updateColors()
void NinePatchComponent::buildVertices() void NinePatchComponent::buildVertices()
{ {
if(mVertices != NULL) if (mVertices != nullptr)
delete[] mVertices; delete[] mVertices;
mTexture = TextureResource::get(mPath); mTexture = TextureResource::get(mPath);
if(mTexture->getSize() == Vector2i::Zero()) if (mTexture->getSize() == Vector2i::Zero()) {
{ mVertices = nullptr;
mVertices = NULL;
LOG(LogWarning) << "NinePatchComponent missing texture!"; LOG(LogWarning) << "NinePatchComponent missing texture!";
return; return;
} }
mVertices = new Renderer::Vertex[6 * 9]; mVertices = new Renderer::Vertex[6 * 9];
const Vector2f texSize = Vector2f((float)mTexture->getSize().x(), (float)mTexture->getSize().y()); const Vector2f texSize = Vector2f((float)mTexture->getSize().x(),
(float)mTexture->getSize().y());
const float imgSizeX[3] = { mCornerSize.x(), mSize.x() - mCornerSize.x() * 2, mCornerSize.x()}; const float imgSizeX[3] = { mCornerSize.x(), mSize.x() - mCornerSize.x() * 2, mCornerSize.x()};
const float imgSizeY[3] = { mCornerSize.y(), mSize.y() - mCornerSize.y() * 2, mCornerSize.y()}; const float imgSizeY[3] = { mCornerSize.y(), mSize.y() - mCornerSize.y() * 2, mCornerSize.y()};
const float imgPosX[3] = { 0, imgSizeX[0], imgSizeX[0] + imgSizeX[1]}; const float imgPosX[3] = { 0, imgSizeX[0], imgSizeX[0] + imgSizeX[1]};
const float imgPosY[3] = { 0, imgSizeY[0], imgSizeY[0] + imgSizeY[1]}; const float imgPosY[3] = { 0, imgSizeY[0], imgSizeY[0] + imgSizeY[1]};
//the "1 +" in posY and "-" in sizeY is to deal with texture coordinates having a bottom left corner origin vs. verticies having a top left origin // The "1 +" in posY and "-" in sizeY is to deal with texture coordinates having a bottom
// left corner origin vs. verticies having a top left origin.
const float texSizeX[3] = { mCornerSize.x() / texSize.x(), (texSize.x() - mCornerSize.x() * 2) / texSize.x(), mCornerSize.x() / texSize.x() }; const float texSizeX[3] = { mCornerSize.x() / texSize.x(), (texSize.x() - mCornerSize.x() * 2) / texSize.x(), mCornerSize.x() / texSize.x() };
const float texSizeY[3] = { -mCornerSize.y() / texSize.y(), -(texSize.y() - mCornerSize.y() * 2) / texSize.y(), -mCornerSize.y() / texSize.y() }; const float texSizeY[3] = { -mCornerSize.y() / texSize.y(), -(texSize.y() - mCornerSize.y() * 2) / texSize.y(), -mCornerSize.y() / texSize.y() };
const float texPosX[3] = { 0, texSizeX[0], texSizeX[0] + texSizeX[1] }; const float texPosX[3] = { 0, texSizeX[0], texSizeX[0] + texSizeX[1] };
const float texPosY[3] = { 1, 1 + texSizeY[0], 1 + texSizeY[0] + texSizeY[1] }; const float texPosY[3] = { 1, 1 + texSizeY[0], 1 + texSizeY[0] + texSizeY[1] };
int v = 0; int v = 0;
for(int slice = 0; slice < 9; slice++)
{ for (int slice = 0; slice < 9; slice++) {
const int sliceX = slice % 3; const int sliceX = slice % 3;
const int sliceY = slice / 3; const int sliceY = slice / 3;
const Vector2f imgPos = Vector2f(imgPosX[sliceX], imgPosY[sliceY]); const Vector2f imgPos = Vector2f(imgPosX[sliceX], imgPosY[sliceY]);
@ -76,11 +89,11 @@ void NinePatchComponent::buildVertices()
mVertices[v + 3] = { { imgPos.x() + imgSize.x(), imgPos.y() }, { texPos.x() + texSize.x(), texPos.y() }, 0 }; mVertices[v + 3] = { { imgPos.x() + imgSize.x(), imgPos.y() }, { texPos.x() + texSize.x(), texPos.y() }, 0 };
mVertices[v + 4] = { { imgPos.x() + imgSize.x(), imgPos.y() + imgSize.y() }, { texPos.x() + texSize.x(), texPos.y() + texSize.y() }, 0 }; mVertices[v + 4] = { { imgPos.x() + imgSize.x(), imgPos.y() + imgSize.y() }, { texPos.x() + texSize.x(), texPos.y() + texSize.y() }, 0 };
// round vertices // Round vertices.
for (int i = 1; i < 5; ++i) for (int i = 1; i < 5; ++i)
mVertices[v + i].pos.round(); mVertices[v + i].pos.round();
// make duplicates of first and last vertex so this can be rendered as a triangle strip // Make duplicates of first and last vertex so this can be rendered as a triangle strip.
mVertices[v + 0] = mVertices[v + 1]; mVertices[v + 0] = mVertices[v + 1];
mVertices[v + 5] = mVertices[v + 4]; mVertices[v + 5] = mVertices[v + 4];
@ -97,8 +110,7 @@ void NinePatchComponent::render(const Transform4x4f& parentTrans)
Transform4x4f trans = parentTrans * getTransform(); Transform4x4f trans = parentTrans * getTransform();
if(mTexture && mVertices != NULL) if (mTexture && mVertices != nullptr) {
{
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
mTexture->bind(); mTexture->bind();
@ -153,13 +165,14 @@ void NinePatchComponent::setCenterColor(unsigned int centerColor)
updateColors(); updateColors();
} }
void NinePatchComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) void NinePatchComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
const std::string& view, const std::string& element, unsigned int properties)
{ {
GuiComponent::applyTheme(theme, view, element, properties); GuiComponent::applyTheme(theme, view, element, properties);
using namespace ThemeFlags; using namespace ThemeFlags;
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "ninepatch"); const ThemeData::ThemeElement* elem = theme->getElement(view, element, "ninepatch");
if (!elem) if (!elem)
return; return;

View file

@ -1,3 +1,9 @@
//
// NinePatchComponent.h
//
// Breaks up an image into 3x3 patches to accomodate resizing without distortions.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H #ifndef ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H
#define ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H #define ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H
@ -7,13 +13,14 @@
class TextureResource; class TextureResource;
// Display an image in a way so that edges don't get too distorted no matter the final size. Useful for UI elements like backgrounds, buttons, etc. // Display an image in a way so that edges don't get too distorted no matter the final size.
// Useful for UI elements like backgrounds, buttons, etc.
// This is accomplished by splitting an image into 9 pieces: // This is accomplished by splitting an image into 9 pieces:
// ___________ // ___________
// |_1_|_2_|_3_| // |_1_|_2_|_3_|
// |_4_|_5_|_6_| // |_4_|_5_|_6_|
// |_7_|_8_|_9_| // |_7_|_8_|_9_|
//
// Corners (1, 3, 7, 9) will not be stretched at all. // Corners (1, 3, 7, 9) will not be stretched at all.
// Borders (2, 4, 6, 8) will be stretched along one axis (2 and 8 horizontally, 4 and 6 vertically). // Borders (2, 4, 6, 8) will be stretched along one axis (2 and 8 horizontally, 4 and 6 vertically).
// The center (5) will be stretched. // The center (5) will be stretched.
@ -21,20 +28,25 @@ class TextureResource;
class NinePatchComponent : public GuiComponent class NinePatchComponent : public GuiComponent
{ {
public: public:
NinePatchComponent(Window* window, const std::string& path = "", unsigned int edgeColor = 0xFFFFFFFF, unsigned int centerColor = 0xFFFFFFFF); NinePatchComponent(Window* window, const std::string& path = "",
unsigned int edgeColor = 0xFFFFFFFF, unsigned int centerColor = 0xFFFFFFFF);
virtual ~NinePatchComponent(); virtual ~NinePatchComponent();
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
void onSizeChanged() override; void onSizeChanged() override;
void fitTo(Vector2f size, Vector3f position = Vector3f::Zero(), Vector2f padding = Vector2f::Zero()); void fitTo(Vector2f size, Vector3f position = Vector3f::Zero(),
Vector2f padding = Vector2f::Zero());
void setImagePath(const std::string& path); void setImagePath(const std::string& path);
void setEdgeColor(unsigned int edgeColor); // Apply a color shift to the "edge" parts of the ninepatch. // Apply a color shift to the "edge" parts of the ninepatch.
void setCenterColor(unsigned int centerColor); // Apply a color shift to the "center" part of the ninepatch. void setEdgeColor(unsigned int edgeColor);
// Apply a color shift to the "center" part of the ninepatch.
void setCenterColor(unsigned int centerColor);
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties) override;
const Vector2f& getCornerSize() const; const Vector2f& getCornerSize() const;
void setCornerSize(int sizeX, int sizeY); void setCornerSize(int sizeX, int sizeY);

View file

@ -1,14 +1,28 @@
//
// ScrollableContainer.cpp
//
// Area containing scrollable information, for example the game description
// text container in the detailed, video and grid views.
//
#include "components/ScrollableContainer.h" #include "components/ScrollableContainer.h"
#include "math/Vector2i.h" #include "math/Vector2i.h"
#include "renderers/Renderer.h" #include "renderers/Renderer.h"
#define AUTO_SCROLL_RESET_DELAY 3000 // ms to reset to top after we reach the bottom #define AUTO_SCROLL_RESET_DELAY 3000 // ms to reset to top after we reach the bottom.
#define AUTO_SCROLL_DELAY 1000 // ms to wait before we start to scroll #define AUTO_SCROLL_DELAY 1000 // ms to wait before we start to scroll.
#define AUTO_SCROLL_SPEED 50 // ms between scrolls #define AUTO_SCROLL_SPEED 100 // ms between scrolls.
ScrollableContainer::ScrollableContainer(Window* window) : GuiComponent(window), ScrollableContainer::ScrollableContainer(
mAutoScrollDelay(0), mAutoScrollSpeed(0), mAutoScrollAccumulator(0), mScrollPos(0, 0), mScrollDir(0, 0), mAutoScrollResetAccumulator(0) Window* window)
: GuiComponent(window),
mAutoScrollDelay(0),
mAutoScrollSpeed(0),
mAutoScrollAccumulator(0),
mScrollPos(0, 0),
mScrollDir(0, 0),
mAutoScrollResetAccumulator(0)
{ {
} }
@ -22,7 +36,8 @@ void ScrollableContainer::render(const Transform4x4f& parentTrans)
Vector2i clipPos((int)trans.translation().x(), (int)trans.translation().y()); Vector2i clipPos((int)trans.translation().x(), (int)trans.translation().y());
Vector3f dimScaled = trans * Vector3f(mSize.x(), mSize.y(), 0); Vector3f dimScaled = trans * Vector3f(mSize.x(), mSize.y(), 0);
Vector2i clipDim((int)(dimScaled.x() - trans.translation().x()), (int)(dimScaled.y() - trans.translation().y())); Vector2i clipDim((int)(dimScaled.x() - trans.translation().x()),
(int)(dimScaled.y() - trans.translation().y()));
Renderer::pushClipRect(clipPos, clipDim); Renderer::pushClipRect(clipPos, clipDim);
@ -30,19 +45,18 @@ void ScrollableContainer::render(const Transform4x4f& parentTrans)
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
GuiComponent::renderChildren(trans); GuiComponent::renderChildren(trans);
Renderer::popClipRect(); Renderer::popClipRect();
} }
void ScrollableContainer::setAutoScroll(bool autoScroll) void ScrollableContainer::setAutoScroll(bool autoScroll)
{ {
if(autoScroll) if (autoScroll) {
{
mScrollDir = Vector2f(0, 1); mScrollDir = Vector2f(0, 1);
mAutoScrollDelay = AUTO_SCROLL_DELAY; mAutoScrollDelay = AUTO_SCROLL_DELAY;
mAutoScrollSpeed = AUTO_SCROLL_SPEED; mAutoScrollSpeed = AUTO_SCROLL_SPEED;
reset(); reset();
}else{ }
else {
mScrollDir = Vector2f(0, 0); mScrollDir = Vector2f(0, 0);
mAutoScrollDelay = 0; mAutoScrollDelay = 0;
mAutoScrollSpeed = 0; mAutoScrollSpeed = 0;
@ -62,43 +76,38 @@ void ScrollableContainer::setScrollPos(const Vector2f& pos)
void ScrollableContainer::update(int deltaTime) void ScrollableContainer::update(int deltaTime)
{ {
if(mAutoScrollSpeed != 0) if (mAutoScrollSpeed != 0) {
{
mAutoScrollAccumulator += deltaTime; mAutoScrollAccumulator += deltaTime;
//scale speed by our width! more text per line = slower scrolling // Scale speed by our width! more text per line = slower scrolling.
const float widthMod = (680.0f / getSize().x()); const float widthMod = (680.0f / getSize().x());
while(mAutoScrollAccumulator >= mAutoScrollSpeed) while (mAutoScrollAccumulator >= mAutoScrollSpeed) {
{
mScrollPos += mScrollDir; mScrollPos += mScrollDir;
mAutoScrollAccumulator -= mAutoScrollSpeed; mAutoScrollAccumulator -= mAutoScrollSpeed;
} }
} }
//clip scrolling within bounds // Clip scrolling within bounds.
if (mScrollPos.x() < 0) if (mScrollPos.x() < 0)
mScrollPos[0] = 0; mScrollPos[0] = 0;
if (mScrollPos.y() < 0) if (mScrollPos.y() < 0)
mScrollPos[1] = 0; mScrollPos[1] = 0;
const Vector2f contentSize = getContentSize(); const Vector2f contentSize = getContentSize();
if(mScrollPos.x() + getSize().x() > contentSize.x()) if (mScrollPos.x() + getSize().x() > contentSize.x()) {
{
mScrollPos[0] = contentSize.x() - getSize().x(); mScrollPos[0] = contentSize.x() - getSize().x();
mAtEnd = true; mAtEnd = true;
} }
if(contentSize.y() < getSize().y()) if (contentSize.y() < getSize().y()) {
{
mScrollPos[1] = 0; mScrollPos[1] = 0;
}else if(mScrollPos.y() + getSize().y() > contentSize.y()) }
{ else if (mScrollPos.y() + getSize().y() > contentSize.y()) {
mScrollPos[1] = contentSize.y() - getSize().y(); mScrollPos[1] = contentSize.y() - getSize().y();
mAtEnd = true; mAtEnd = true;
} }
if(mAtEnd) if (mAtEnd) {
{
mAutoScrollResetAccumulator += deltaTime; mAutoScrollResetAccumulator += deltaTime;
if (mAutoScrollResetAccumulator >= AUTO_SCROLL_RESET_DELAY) if (mAutoScrollResetAccumulator >= AUTO_SCROLL_RESET_DELAY)
reset(); reset();
@ -107,12 +116,11 @@ void ScrollableContainer::update(int deltaTime)
GuiComponent::update(deltaTime); GuiComponent::update(deltaTime);
} }
//this should probably return a box to allow for when controls don't start at 0,0 // This should probably return a box to allow for when controls don't start at 0,0.
Vector2f ScrollableContainer::getContentSize() Vector2f ScrollableContainer::getContentSize()
{ {
Vector2f max(0, 0); Vector2f max(0, 0);
for(unsigned int i = 0; i < mChildren.size(); i++) for (unsigned int i = 0; i < mChildren.size(); i++) {
{
Vector2f pos(mChildren.at(i)->getPosition()[0], mChildren.at(i)->getPosition()[1]); Vector2f pos(mChildren.at(i)->getPosition()[0], mChildren.at(i)->getPosition()[1]);
Vector2f bottomRight = mChildren.at(i)->getSize() + pos; Vector2f bottomRight = mChildren.at(i)->getSize() + pos;
if (bottomRight.x() > max.x()) if (bottomRight.x() > max.x())

View file

@ -1,3 +1,10 @@
//
// ScrollableContainer.h
//
// Area containing scrollable information, for example the game description
// text container in the detailed, video and grid views.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H #ifndef ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H
#define ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H #define ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H
@ -22,8 +29,8 @@ private:
Vector2f mScrollPos; Vector2f mScrollPos;
Vector2f mScrollDir; Vector2f mScrollDir;
int mAutoScrollDelay; // ms to wait before starting to autoscroll int mAutoScrollDelay; // ms to wait before starting to autoscroll.
int mAutoScrollSpeed; // ms to wait before scrolling down by mScrollDir int mAutoScrollSpeed; // ms to wait before scrolling down by mScrollDir.
int mAutoScrollAccumulator; int mAutoScrollAccumulator;
bool mAtEnd; bool mAtEnd;
int mAutoScrollResetAccumulator; int mAutoScrollResetAccumulator;

View file

@ -1,3 +1,9 @@
//
// SliderComponent.cpp
//
// Slider to set value in a predefined range.
//
#include "components/SliderComponent.h" #include "components/SliderComponent.h"
#include "resources/Font.h" #include "resources/Font.h"
@ -5,12 +11,23 @@
#define MOVE_REPEAT_DELAY 500 #define MOVE_REPEAT_DELAY 500
#define MOVE_REPEAT_RATE 40 #define MOVE_REPEAT_RATE 40
SliderComponent::SliderComponent(Window* window, float min, float max, float increment, const std::string& suffix) : GuiComponent(window), SliderComponent::SliderComponent(
mMin(min), mMax(max), mSingleIncrement(increment), mMoveRate(0), mKnob(window), mSuffix(suffix) Window* window,
float min,
float max,
float increment,
const std::string& suffix)
: GuiComponent(window),
mMin(min),
mMax(max),
mSingleIncrement(increment),
mMoveRate(0),
mKnob(window),
mSuffix(suffix)
{ {
assert((min - max) != 0); assert((min - max) != 0);
// some sane default value // Some sane default value.
mValue = (max + min) / 2; mValue = (max + min) / 2;
mKnob.setOrigin(0.5f, 0.5f); mKnob.setOrigin(0.5f, 0.5f);
@ -21,8 +38,7 @@ SliderComponent::SliderComponent(Window* window, float min, float max, float inc
bool SliderComponent::input(InputConfig* config, Input input) bool SliderComponent::input(InputConfig* config, Input input)
{ {
if(config->isMappedLike("left", input)) if (config->isMappedLike("left", input)) {
{
if (input.value) if (input.value)
setValue(mValue - mSingleIncrement); setValue(mValue - mSingleIncrement);
@ -30,8 +46,7 @@ bool SliderComponent::input(InputConfig* config, Input input)
mMoveAccumulator = -MOVE_REPEAT_DELAY; mMoveAccumulator = -MOVE_REPEAT_DELAY;
return true; return true;
} }
if(config->isMappedLike("right", input)) if (config->isMappedLike("right", input)) {
{
if (input.value) if (input.value)
setValue(mValue + mSingleIncrement); setValue(mValue + mSingleIncrement);
@ -45,11 +60,9 @@ bool SliderComponent::input(InputConfig* config, Input input)
void SliderComponent::update(int deltaTime) void SliderComponent::update(int deltaTime)
{ {
if(mMoveRate != 0) if (mMoveRate != 0) {
{
mMoveAccumulator += deltaTime; mMoveAccumulator += deltaTime;
while(mMoveAccumulator >= MOVE_REPEAT_RATE) while (mMoveAccumulator >= MOVE_REPEAT_RATE) {
{
setValue(mValue + mMoveRate); setValue(mValue + mMoveRate);
mMoveAccumulator -= MOVE_REPEAT_RATE; mMoveAccumulator -= MOVE_REPEAT_RATE;
} }
@ -63,17 +76,19 @@ void SliderComponent::render(const Transform4x4f& parentTrans)
Transform4x4f trans = parentTrans * getTransform(); Transform4x4f trans = parentTrans * getTransform();
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
// render suffix // Render suffix.
if (mValueCache) if (mValueCache)
mFont->renderTextCache(mValueCache.get()); mFont->renderTextCache(mValueCache.get());
float width = mSize.x() - mKnob.getSize().x() - (mValueCache ? mValueCache->metrics.size.x() + 4 : 0); float width = mSize.x() - mKnob.getSize().x() -
(mValueCache ? mValueCache->metrics.size.x() + 4 : 0);
//render line // Render line.
const float lineWidth = 2; const float lineWidth = 2;
Renderer::drawRect(mKnob.getSize().x() / 2, mSize.y() / 2 - lineWidth / 2, width, lineWidth, 0x777777FF, 0x777777FF); Renderer::drawRect(mKnob.getSize().x() / 2, mSize.y() / 2 -
lineWidth / 2, width, lineWidth, 0x777777FF, 0x777777FF);
//render knob // Render knob.
mKnob.render(trans); mKnob.render(trans);
GuiComponent::renderChildren(trans); GuiComponent::renderChildren(trans);
@ -105,9 +120,8 @@ void SliderComponent::onSizeChanged()
void SliderComponent::onValueChanged() void SliderComponent::onValueChanged()
{ {
// update suffix textcache // Update suffix textcache.
if(mFont) if (mFont) {
{
std::stringstream ss; std::stringstream ss;
ss << std::fixed; ss << std::fixed;
ss.precision(0); ss.precision(0);
@ -124,14 +138,17 @@ void SliderComponent::onValueChanged()
const std::string max = ss.str(); const std::string max = ss.str();
Vector2f textSize = mFont->sizeText(max); Vector2f textSize = mFont->sizeText(max);
mValueCache = std::shared_ptr<TextCache>(mFont->buildTextCache(val, mSize.x() - textSize.x(), (mSize.y() - textSize.y()) / 2, 0x777777FF)); mValueCache = std::shared_ptr<TextCache>(mFont->buildTextCache(val, mSize.x() -
mValueCache->metrics.size[0] = textSize.x(); // fudge the width textSize.x(), (mSize.y() - textSize.y()) / 2, 0x777777FF));
mValueCache->metrics.size[0] = textSize.x(); // Fudge the width.
} }
// update knob position/size // Update knob position/size.
mKnob.setResize(0, mSize.y() * 0.7f); mKnob.setResize(0, mSize.y() * 0.7f);
float lineLength = mSize.x() - mKnob.getSize().x() - (mValueCache ? mValueCache->metrics.size.x() + 4 : 0); float lineLength = mSize.x() - mKnob.getSize().x() -
mKnob.setPosition(((mValue + mMin) / mMax) * lineLength + mKnob.getSize().x()/2, mSize.y() / 2); (mValueCache ? mValueCache->metrics.size.x() + 4 : 0);
mKnob.setPosition(((mValue + mMin) / mMax) * lineLength +
mKnob.getSize().x()/2, mSize.y() / 2);
} }
std::vector<HelpPrompt> SliderComponent::getHelpPrompts() std::vector<HelpPrompt> SliderComponent::getHelpPrompts()

View file

@ -1,3 +1,9 @@
//
// SliderComponent.h
//
// Slider to set value in a predefined range.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_SLIDER_COMPONENT_H #ifndef ES_CORE_COMPONENTS_SLIDER_COMPONENT_H
#define ES_CORE_COMPONENTS_SLIDER_COMPONENT_H #define ES_CORE_COMPONENTS_SLIDER_COMPONENT_H
@ -12,8 +18,14 @@ class TextCache;
class SliderComponent : public GuiComponent class SliderComponent : public GuiComponent
{ {
public: public:
//Minimum value (far left of the slider), maximum value (far right of the slider), increment size (how much just pressing L/R moves by), unit to display (optional). // Minimum value (far left of the slider), maximum value (far right of the slider),
SliderComponent(Window* window, float min, float max, float increment, const std::string& suffix = ""); // increment size (how much just pressing L/R moves by), unit to display (optional).
SliderComponent(
Window* window,
float min,
float max,
float increment,
const std::string& suffix = "");
void setValue(float val); void setValue(float val);
float getValue(); float getValue();

View file

@ -1,20 +1,48 @@
//
// TextComponent.cpp
//
// Displays text.
//
#include "components/TextComponent.h" #include "components/TextComponent.h"
#include "utils/StringUtil.h" #include "utils/StringUtil.h"
#include "Log.h" #include "Log.h"
#include "Settings.h" #include "Settings.h"
TextComponent::TextComponent(Window* window) : GuiComponent(window), TextComponent::TextComponent(
mFont(Font::get(FONT_SIZE_MEDIUM)), mUppercase(false), mColor(0x000000FF), mAutoCalcExtent(true, true), Window* window)
mHorizontalAlignment(ALIGN_LEFT), mVerticalAlignment(ALIGN_CENTER), mLineSpacing(1.5f), mBgColor(0), : GuiComponent(window),
mFont(Font::get(FONT_SIZE_MEDIUM)),
mUppercase(false),
mColor(0x000000FF),
mAutoCalcExtent(true, true),
mHorizontalAlignment(ALIGN_LEFT),
mVerticalAlignment(ALIGN_CENTER),
mLineSpacing(1.5f),
mBgColor(0),
mRenderBackground(false) mRenderBackground(false)
{ {
} }
TextComponent::TextComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color, Alignment align, TextComponent::TextComponent(
Vector3f pos, Vector2f size, unsigned int bgcolor) : GuiComponent(window), Window* window,
mFont(NULL), mUppercase(false), mColor(0x000000FF), mAutoCalcExtent(true, true), const std::string& text,
mHorizontalAlignment(align), mVerticalAlignment(ALIGN_CENTER), mLineSpacing(1.5f), mBgColor(0), const std::shared_ptr<Font>& font,
unsigned int color,
Alignment align,
Vector3f pos,
Vector2f size,
unsigned int bgcolor)
: GuiComponent(window),
mFont(nullptr),
mUppercase(false),
mColor(0x000000FF),
mAutoCalcExtent(true, true),
mHorizontalAlignment(align),
mVerticalAlignment(ALIGN_CENTER),
mLineSpacing(1.5f),
mBgColor(0),
mRenderBackground(false) mRenderBackground(false)
{ {
setFont(font); setFont(font);
@ -37,7 +65,7 @@ void TextComponent::setFont(const std::shared_ptr<Font>& font)
onTextChanged(); onTextChanged();
} }
// Set the color of the font/text // Set the color of the font/text.
void TextComponent::setColor(unsigned int color) void TextComponent::setColor(unsigned int color)
{ {
mColor = color; mColor = color;
@ -45,7 +73,7 @@ void TextComponent::setColor(unsigned int color)
onColorChanged(); onColorChanged();
} }
// Set the color of the background box // Set the color of the background box.
void TextComponent::setBackgroundColor(unsigned int color) void TextComponent::setBackgroundColor(unsigned int color)
{ {
mBgColor = color; mBgColor = color;
@ -57,7 +85,7 @@ void TextComponent::setRenderBackground(bool render)
mRenderBackground = render; mRenderBackground = render;
} }
// Scale the opacity // Scale the opacity.
void TextComponent::setOpacity(unsigned char opacity) void TextComponent::setOpacity(unsigned char opacity)
{ {
// This method is mostly called to do fading in-out of the Text component element. // This method is mostly called to do fading in-out of the Text component element.
@ -71,7 +99,6 @@ void TextComponent::setOpacity(unsigned char opacity)
mBgColor = (mBgColor & 0xFFFFFF00) | (unsigned char)bgo; mBgColor = (mBgColor & 0xFFFFFF00) | (unsigned char)bgo;
onColorChanged(); onColorChanged();
GuiComponent::setOpacity(opacity); GuiComponent::setOpacity(opacity);
} }
@ -99,18 +126,15 @@ void TextComponent::render(const Transform4x4f& parentTrans)
Transform4x4f trans = parentTrans * getTransform(); Transform4x4f trans = parentTrans * getTransform();
if (mRenderBackground) if (mRenderBackground) {
{
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), mBgColor, mBgColor); Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), mBgColor, mBgColor);
} }
if(mTextCache) if (mTextCache) {
{
const Vector2f& textSize = mTextCache->metrics.size; const Vector2f& textSize = mTextCache->metrics.size;
float yOff = 0; float yOff = 0;
switch(mVerticalAlignment) switch (mVerticalAlignment) {
{
case ALIGN_TOP: case ALIGN_TOP:
yOff = 0; yOff = 0;
break; break;
@ -125,9 +149,8 @@ void TextComponent::render(const Transform4x4f& parentTrans)
} }
Vector3f off(0, yOff, 0); Vector3f off(0, yOff, 0);
if(Settings::getInstance()->getBool("DebugText")) if (Settings::getInstance()->getBool("DebugText")) {
{ // Draw the "textbox" area, what we are aligned within.
// draw the "textbox" area, what we are aligned within
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0xFF000033, 0xFF000033); Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0xFF000033, 0xFF000033);
} }
@ -135,19 +158,22 @@ void TextComponent::render(const Transform4x4f& parentTrans)
trans.translate(off); trans.translate(off);
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
// draw the text area, where the text actually is going // Draw the text area, where the text actually is located.
if(Settings::getInstance()->getBool("DebugText")) if (Settings::getInstance()->getBool("DebugText")) {
{ switch (mHorizontalAlignment) {
switch(mHorizontalAlignment)
{
case ALIGN_LEFT: case ALIGN_LEFT:
Renderer::drawRect(0.0f, 0.0f, mTextCache->metrics.size.x(), mTextCache->metrics.size.y(), 0x00000033, 0x00000033); Renderer::drawRect(0.0f, 0.0f, mTextCache->metrics.size.x(),
mTextCache->metrics.size.y(), 0x00000033, 0x00000033);
break; break;
case ALIGN_CENTER: case ALIGN_CENTER:
Renderer::drawRect((mSize.x() - mTextCache->metrics.size.x()) / 2.0f, 0.0f, mTextCache->metrics.size.x(), mTextCache->metrics.size.y(), 0x00000033, 0x00000033); Renderer::drawRect((mSize.x() - mTextCache->metrics.size.x()) / 2.0f, 0.0f,
mTextCache->metrics.size.x(), mTextCache->metrics.size.y(),
0x00000033, 0x00000033);
break; break;
case ALIGN_RIGHT: case ALIGN_RIGHT:
Renderer::drawRect(mSize.x() - mTextCache->metrics.size.x(), 0.0f, mTextCache->metrics.size.x(), mTextCache->metrics.size.y(), 0x00000033, 0x00000033); Renderer::drawRect(mSize.x() - mTextCache->metrics.size.x(), 0.0f,
mTextCache->metrics.size.x(), mTextCache->metrics.size.y(),
0x00000033, 0x00000033);
break; break;
default: default:
break; break;
@ -159,14 +185,13 @@ void TextComponent::render(const Transform4x4f& parentTrans)
void TextComponent::calculateExtent() void TextComponent::calculateExtent()
{ {
if(mAutoCalcExtent.x()) if (mAutoCalcExtent.x()) {
{
mSize = mFont->sizeText(mUppercase ? Utils::String::toUpper(mText) : mText, mLineSpacing); mSize = mFont->sizeText(mUppercase ? Utils::String::toUpper(mText) : mText, mLineSpacing);
}else{
if(mAutoCalcExtent.y())
{
mSize[1] = mFont->sizeWrappedText(mUppercase ? Utils::String::toUpper(mText) : mText, getSize().x(), mLineSpacing).y();
} }
else {
if (mAutoCalcExtent.y())
mSize[1] = mFont->sizeWrappedText(mUppercase ? Utils::String::toUpper(mText)
: mText, getSize().x(), mLineSpacing).y();
} }
} }
@ -174,8 +199,7 @@ void TextComponent::onTextChanged()
{ {
calculateExtent(); calculateExtent();
if(!mFont || mText.empty()) if (!mFont || mText.empty()) {
{
mTextCache.reset(); mTextCache.reset();
return; return;
} }
@ -186,22 +210,20 @@ void TextComponent::onTextChanged()
const bool isMultiline = (mSize.y() == 0 || mSize.y() > f->getHeight()*1.2f); const bool isMultiline = (mSize.y() == 0 || mSize.y() > f->getHeight()*1.2f);
bool addAbbrev = false; bool addAbbrev = false;
if(!isMultiline) if (!isMultiline) {
{
size_t newline = text.find('\n'); size_t newline = text.find('\n');
text = text.substr(0, newline); // single line of text - stop at the first newline since it'll mess everything up // Single line of text - stop at the first newline since it'll mess everything up.
text = text.substr(0, newline);
addAbbrev = newline != std::string::npos; addAbbrev = newline != std::string::npos;
} }
Vector2f size = f->sizeText(text); Vector2f size = f->sizeText(text);
if(!isMultiline && mSize.x() && text.size() && (size.x() > mSize.x() || addAbbrev)) if (!isMultiline && mSize.x() && text.size() && (size.x() > mSize.x() || addAbbrev)) {
{ // Abbreviate text.
// abbreviate text
const std::string abbrev = "..."; const std::string abbrev = "...";
Vector2f abbrevSize = f->sizeText(abbrev); Vector2f abbrevSize = f->sizeText(abbrev);
while(text.size() && size.x() + abbrevSize.x() > mSize.x()) while (text.size() && size.x() + abbrevSize.x() > mSize.x()) {
{
size_t newSize = Utils::String::prevCursor(text, text.size()); size_t newSize = Utils::String::prevCursor(text, text.size());
text.erase(newSize, text.size() - newSize); text.erase(newSize, text.size() - newSize);
size = f->sizeText(text); size = f->sizeText(text);
@ -209,19 +231,21 @@ void TextComponent::onTextChanged()
text.append(abbrev); text.append(abbrev);
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(text, Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(), mHorizontalAlignment, mLineSpacing)); mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(text, Vector2f(0, 0),
}else{ (mColor >> 8 << 8) | mOpacity, mSize.x(), mHorizontalAlignment, mLineSpacing));
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(f->wrapText(text, mSize.x()), Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(), mHorizontalAlignment, mLineSpacing)); }
else {
mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(f->wrapText(
text, mSize.x()), Vector2f(0, 0), (mColor >> 8 << 8) | mOpacity, mSize.x(),
mHorizontalAlignment, mLineSpacing));
} }
} }
void TextComponent::onColorChanged() void TextComponent::onColorChanged()
{ {
if (mTextCache) if (mTextCache)
{
mTextCache->setColor(mColor); mTextCache->setColor(mColor);
} }
}
void TextComponent::setHorizontalAlignment(Alignment align) void TextComponent::setHorizontalAlignment(Alignment align)
{ {
@ -250,7 +274,8 @@ std::string TextComponent::getValue() const
return mText; return mText;
} }
void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties)
{ {
GuiComponent::applyTheme(theme, view, element, properties); GuiComponent::applyTheme(theme, view, element, properties);
@ -269,8 +294,7 @@ void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const st
setRenderBackground(true); setRenderBackground(true);
} }
if(properties & ALIGNMENT && elem->has("alignment")) if (properties & ALIGNMENT && elem->has("alignment")) {
{
std::string str = elem->get<std::string>("alignment"); std::string str = elem->get<std::string>("alignment");
if (str == "left") if (str == "left")
setHorizontalAlignment(ALIGN_LEFT); setHorizontalAlignment(ALIGN_LEFT);

View file

@ -1,3 +1,9 @@
//
// TextComponent.h
//
// Displays text.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_TEXT_COMPONENT_H #ifndef ES_CORE_COMPONENTS_TEXT_COMPONENT_H
#define ES_CORE_COMPONENTS_TEXT_COMPONENT_H #define ES_CORE_COMPONENTS_TEXT_COMPONENT_H
@ -9,15 +15,24 @@ class ThemeData;
// Used to display text. // Used to display text.
// TextComponent::setSize(x, y) works a little differently than most components: // TextComponent::setSize(x, y) works a little differently than most components:
// * (0, 0) - will automatically calculate a size that fits the text on one line (expand horizontally) // * (0, 0) - Will automatically calculate a size that fits
// * (x != 0, 0) - wrap text so that it does not reach beyond x. Will automatically calculate a vertical size (expand vertically). // the text on one line (expand horizontally).
// * (x != 0, y <= fontHeight) - will truncate text so it fits within this box. // * (x != 0, 0) - Wrap text so that it does not reach beyond x. Will
// automatically calculate a vertical size (expand vertically).
// * (x != 0, y <= fontHeight) - Will truncate text so it fits within this box.
class TextComponent : public GuiComponent class TextComponent : public GuiComponent
{ {
public: public:
TextComponent(Window* window); TextComponent(Window* window);
TextComponent(Window* window, const std::string& text, const std::shared_ptr<Font>& font, unsigned int color = 0x000000FF, Alignment align = ALIGN_LEFT, TextComponent(
Vector3f pos = Vector3f::Zero(), Vector2f size = Vector2f::Zero(), unsigned int bgcolor = 0x00000000); Window* window,
const std::string& text,
const std::shared_ptr<Font>& font,
unsigned int color = 0x000000FF,
Alignment align = ALIGN_LEFT,
Vector3f pos = Vector3f::Zero(),
Vector2f size = Vector2f::Zero(),
unsigned int bgcolor = 0x00000000);
void setFont(const std::shared_ptr<Font>& font); void setFont(const std::shared_ptr<Font>& font);
void setUppercase(bool uppercase); void setUppercase(bool uppercase);
@ -41,7 +56,8 @@ public:
inline std::shared_ptr<Font> getFont() const { return mFont; } inline std::shared_ptr<Font> getFont() const { return mFont; }
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties) override;
protected: protected:
virtual void onTextChanged(); virtual void onTextChanged();

View file

@ -1,3 +1,9 @@
//
// VideoComponent.cpp
//
// Base class for playing videos.
//
#include "components/VideoComponent.h" #include "components/VideoComponent.h"
#include "resources/ResourceManager.h" #include "resources/ResourceManager.h"
@ -28,26 +34,24 @@ void writeSubtitle(const char* gameName, const char* systemName, bool always)
{ {
FILE* file = fopen(getTitlePath().c_str(), "w"); FILE* file = fopen(getTitlePath().c_str(), "w");
int end = (int)(Settings::getInstance()->getInt("ScreenSaverSwapVideoTimeout") / (1000)); int end = (int)(Settings::getInstance()->getInt("ScreenSaverSwapVideoTimeout") / (1000));
if (always) {
if (always)
fprintf(file, "1\n00:00:01,000 --> 00:00:%d,000\n", end); fprintf(file, "1\n00:00:01,000 --> 00:00:%d,000\n", end);
}
else else
{
fprintf(file, "1\n00:00:01,000 --> 00:00:08,000\n"); fprintf(file, "1\n00:00:01,000 --> 00:00:08,000\n");
}
fprintf(file, "%s\n", gameName); fprintf(file, "%s\n", gameName);
fprintf(file, "<i>%s</i>\n\n", systemName); fprintf(file, "<i>%s</i>\n\n", systemName);
if (!always) { if (!always) {
if (end > 12) if (end > 12)
{ fprintf(file, "2\n00:00:%d,000 --> 00:00:%d,000\n%s\n<i>%s</i>\n",
fprintf(file, "2\n00:00:%d,000 --> 00:00:%d,000\n%s\n<i>%s</i>\n", end-4, end, gameName, systemName); end-4, end, gameName, systemName);
}
} }
fflush(file); fflush(file);
fclose(file); fclose(file);
file = NULL; file = nullptr;
} }
void VideoComponent::setScreensaverMode(bool isScreensaver) void VideoComponent::setScreensaverMode(bool isScreensaver)
@ -55,8 +59,9 @@ void VideoComponent::setScreensaverMode(bool isScreensaver)
mScreensaverMode = isScreensaver; mScreensaverMode = isScreensaver;
} }
VideoComponent::VideoComponent(Window* window) : VideoComponent::VideoComponent(
GuiComponent(window), Window* window)
: GuiComponent(window),
mStaticImage(window), mStaticImage(window),
mVideoHeight(0), mVideoHeight(0),
mVideoWidth(0), mVideoWidth(0),
@ -69,69 +74,71 @@ VideoComponent::VideoComponent(Window* window) :
mTargetIsMax(false), mTargetIsMax(false),
mTargetSize(0, 0) mTargetSize(0, 0)
{ {
// Setup the default configuration // Setup the default configuration.
mConfig.showSnapshotDelay = false; mConfig.showSnapshotDelay = false;
mConfig.showSnapshotNoVideo = false; mConfig.showSnapshotNoVideo = false;
mConfig.startDelay = 0; mConfig.startDelay = 0;
if (mWindow->getGuiStackSize() > 1) {
if (mWindow->getGuiStackSize() > 1)
topWindow(false); topWindow(false);
}
std::string path = getTitleFolder(); std::string path = getTitleFolder();
if (!Utils::FileSystem::exists(path)) if (!Utils::FileSystem::exists(path))
Utils::FileSystem::createDirectory(path); Utils::FileSystem::createDirectory(path);
} }
VideoComponent::~VideoComponent() VideoComponent::~VideoComponent()
{ {
// Stop any currently running video // Stop any currently running video.
stopVideo(); stopVideo();
// Delete subtitle file, if existing // Delete subtitle file, if existing.
remove(getTitlePath().c_str()); remove(getTitlePath().c_str());
} }
void VideoComponent::onOriginChanged() void VideoComponent::onOriginChanged()
{ {
// Update the embeded static image // Update the embeded static image.
mStaticImage.setOrigin(mOrigin); mStaticImage.setOrigin(mOrigin);
} }
void VideoComponent::onPositionChanged() void VideoComponent::onPositionChanged()
{ {
// Update the embeded static image // Update the embeded static image.
mStaticImage.setPosition(mPosition); mStaticImage.setPosition(mPosition);
} }
void VideoComponent::onSizeChanged() void VideoComponent::onSizeChanged()
{ {
// Update the embeded static image // Update the embeded static image.
mStaticImage.onSizeChanged(); mStaticImage.onSizeChanged();
} }
bool VideoComponent::setVideo(std::string path) bool VideoComponent::setVideo(std::string path)
{ {
// Convert the path into a generic format // Convert the path into a generic format.
std::string fullPath = Utils::FileSystem::getCanonicalPath(path); std::string fullPath = Utils::FileSystem::getCanonicalPath(path);
// Check that it's changed // Check that it's changed.
if (fullPath == mVideoPath) if (fullPath == mVideoPath)
return !path.empty(); return !path.empty();
// Store the path // Store the path.
mVideoPath = fullPath; mVideoPath = fullPath;
// If the file exists then set the new video // If the file exists then set the new video.
if (!fullPath.empty() && ResourceManager::getInstance()->fileExists(fullPath)) if (!fullPath.empty() && ResourceManager::getInstance()->fileExists(fullPath)) {
{ // Return true to show that we are going to attempt to play a video.
// Return true to show that we are going to attempt to play a video
return true; return true;
} }
// Return false to show that no video will be displayed
// Return false to show that no video will be displayed.
return false; return false;
} }
void VideoComponent::setImage(std::string path) void VideoComponent::setImage(std::string path)
{ {
// Check that the image has changed // Check that the image has changed.
if (path == mStaticImagePath) if (path == mStaticImagePath)
return; return;
@ -148,7 +155,7 @@ void VideoComponent::setDefaultVideo()
void VideoComponent::setOpacity(unsigned char opacity) void VideoComponent::setOpacity(unsigned char opacity)
{ {
mOpacity = opacity; mOpacity = opacity;
// Update the embeded static image // Update the embeded static image.
mStaticImage.setOpacity(opacity); mStaticImage.setOpacity(opacity);
} }
@ -162,39 +169,42 @@ void VideoComponent::render(const Transform4x4f& parentTrans)
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
// Handle the case where the video is delayed // Handle the case where the video is delayed.
handleStartDelay(); handleStartDelay();
// Handle looping of the video // Handle looping of the video.
handleLooping(); handleLooping();
} }
void VideoComponent::renderSnapshot(const Transform4x4f& parentTrans) void VideoComponent::renderSnapshot(const Transform4x4f& parentTrans)
{ {
// This is the case where the video is not currently being displayed. Work out // This is the case where the video is not currently being displayed. Work out
// if we need to display a static image // if we need to display a static image.
if ((mConfig.showSnapshotNoVideo && mVideoPath.empty()) || (mStartDelayed && mConfig.showSnapshotDelay)) if ((mConfig.showSnapshotNoVideo && mVideoPath.empty()) ||
{ (mStartDelayed && mConfig.showSnapshotDelay)) {
// Display the static image instead // Display the static image instead.
mStaticImage.setOpacity((unsigned char)(mFadeIn * 255.0f)); mStaticImage.setOpacity((unsigned char)(mFadeIn * 255.0f));
mStaticImage.render(parentTrans); mStaticImage.render(parentTrans);
} }
} }
void VideoComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) void VideoComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties)
{ {
using namespace ThemeFlags; using namespace ThemeFlags;
GuiComponent::applyTheme(theme, view, element, (properties ^ SIZE) | ((properties & (SIZE | POSITION)) ? ORIGIN : 0)); GuiComponent::applyTheme(theme, view, element, (properties ^ SIZE) |
((properties & (SIZE | POSITION)) ? ORIGIN : 0));
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "video"); const ThemeData::ThemeElement* elem = theme->getElement(view, element, "video");
if (!elem) if (!elem)
return; return;
Vector2f scale = getParent() ? getParent()->getSize() : Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight()); Vector2f scale = getParent() ? getParent()->getSize()
: Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
if(properties & ThemeFlags::SIZE) if (properties & ThemeFlags::SIZE) {
{
if (elem->has("size")) if (elem->has("size"))
setResize(elem->get<Vector2f>("size") * scale); setResize(elem->get<Vector2f>("size") * scale);
else if (elem->has("maxSize")) else if (elem->has("maxSize"))
@ -223,17 +233,15 @@ std::vector<HelpPrompt> VideoComponent::getHelpPrompts()
void VideoComponent::handleStartDelay() void VideoComponent::handleStartDelay()
{ {
// Only play if any delay has timed out // Only play if any delay has timed out.
if (mStartDelayed) if (mStartDelayed) {
{ if (mStartTime > SDL_GetTicks()) {
if (mStartTime > SDL_GetTicks()) // Timeout not yet completed.
{
// Timeout not yet completed
return; return;
} }
// Completed // Completed.
mStartDelayed = false; mStartDelayed = false;
// Clear the playing flag so startVideo works // Clear the playing flag so startVideo works.
mIsPlaying = false; mIsPlaying = false;
startVideo(); startVideo();
} }
@ -245,21 +253,18 @@ void VideoComponent::handleLooping()
void VideoComponent::startVideoWithDelay() void VideoComponent::startVideoWithDelay()
{ {
// If not playing then either start the video or initiate the delay // If not playing then either start the video or initiate the delay.
if (!mIsPlaying) if (!mIsPlaying) {
{ // Set the video that we are going to be playing so we don't attempt to restart it.
// Set the video that we are going to be playing so we don't attempt to restart it
mPlayingVideoPath = mVideoPath; mPlayingVideoPath = mVideoPath;
if (mConfig.startDelay == 0 || PowerSaver::getMode() == PowerSaver::INSTANT) if (mConfig.startDelay == 0 || PowerSaver::getMode() == PowerSaver::INSTANT) {
{ // No delay. Just start the video.
// No delay. Just start the video
mStartDelayed = false; mStartDelayed = false;
startVideo(); startVideo();
} }
else else {
{ // Configure the start delay.
// Configure the start delay
mStartDelayed = true; mStartDelayed = true;
mFadeIn = 0.0f; mFadeIn = 0.0f;
mStartTime = SDL_GetTicks() + mConfig.startDelay; mStartTime = SDL_GetTicks() + mConfig.startDelay;
@ -272,24 +277,20 @@ void VideoComponent::update(int deltaTime)
{ {
manageState(); manageState();
// If the video start is delayed and there is less than the fade time then set the image fade // If the video start is delayed and there is less than the fade time, then set
// accordingly // the image fade accordingly.
if (mStartDelayed) if (mStartDelayed) {
{
Uint32 ticks = SDL_GetTicks(); Uint32 ticks = SDL_GetTicks();
if (mStartTime > ticks) if (mStartTime > ticks) {
{
Uint32 diff = mStartTime - ticks; Uint32 diff = mStartTime - ticks;
if (diff < FADE_TIME_MS) if (diff < FADE_TIME_MS) {
{
mFadeIn = (float)diff / (float)FADE_TIME_MS; mFadeIn = (float)diff / (float)FADE_TIME_MS;
return; return;
} }
} }
} }
// If the fade in is less than 1 then increment it // If the fade in is less than 1 then increment it.
if (mFadeIn < 1.0f) if (mFadeIn < 1.0f) {
{
mFadeIn += deltaTime / (float)FADE_TIME_MS; mFadeIn += deltaTime / (float)FADE_TIME_MS;
if (mFadeIn > 1.0f) if (mFadeIn > 1.0f)
mFadeIn = 1.0f; mFadeIn = 1.0f;
@ -300,37 +301,30 @@ void VideoComponent::update(int deltaTime)
void VideoComponent::manageState() void VideoComponent::manageState()
{ {
// We will only show if the component is on display and the screensaver // We will only show if the component is on display and the screensaver
// is not active // is not active.
bool show = mShowing && !mScreensaverActive && !mDisable; bool show = mShowing && !mScreensaverActive && !mDisable;
// See if we're already playing // See if we're already playing.
if (mIsPlaying) if (mIsPlaying) {
{ // If we are not on display then stop the video from playing.
// If we are not on display then stop the video from playing if (!show) {
if (!show)
{
stopVideo(); stopVideo();
} }
else else {
{ if (mVideoPath != mPlayingVideoPath) {
if (mVideoPath != mPlayingVideoPath)
{
// Path changed. Stop the video. We will start it again below because // Path changed. Stop the video. We will start it again below because
// mIsPlaying will be modified by stopVideo to be false // mIsPlaying will be modified by stopVideo to be false.
stopVideo(); stopVideo();
} }
} }
} }
// Need to recheck variable rather than 'else' because it may be modified above // Need to recheck variable rather than 'else' because it may be modified above.
if (!mIsPlaying) if (!mIsPlaying) {
{ // If we are on display then see if we should start the video.
// If we are on display then see if we should start the video
if (show && !mVideoPath.empty()) if (show && !mVideoPath.empty())
{
startVideoWithDelay(); startVideoWithDelay();
} }
} }
}
void VideoComponent::onShow() void VideoComponent::onShow()
{ {

View file

@ -1,9 +1,16 @@
//
// VideoComponent.h
//
// Base class for playing videos.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_VIDEO_COMPONENT_H #ifndef ES_CORE_COMPONENTS_VIDEO_COMPONENT_H
#define ES_CORE_COMPONENTS_VIDEO_COMPONENT_H #define ES_CORE_COMPONENTS_VIDEO_COMPONENT_H
#include "components/ImageComponent.h" #include "components/ImageComponent.h"
#include "GuiComponent.h" #include "GuiComponent.h"
#include <string> #include <string>
class TextureResource; class TextureResource;
@ -14,9 +21,8 @@ void writeSubtitle(const char* gameName, const char* systemName, bool always);
class VideoComponent : public GuiComponent class VideoComponent : public GuiComponent
{ {
// Structure that groups together the configuration of the video component // Structure that groups together the configuration of the video component.
struct Configuration struct Configuration {
{
unsigned startDelay; unsigned startDelay;
bool showSnapshotNoVideo; bool showSnapshotNoVideo;
bool showSnapshotDelay; bool showSnapshotDelay;
@ -27,15 +33,15 @@ public:
VideoComponent(Window* window); VideoComponent(Window* window);
virtual ~VideoComponent(); virtual ~VideoComponent();
// Loads the video at the given filepath // Loads the video at the given filepath.
bool setVideo(std::string path); bool setVideo(std::string path);
// Loads a static image that is displayed if the video cannot be played // Loads a static image that is displayed if the video cannot be played.
void setImage(std::string path); void setImage(std::string path);
// Configures the component to show the default video // Configures the component to show the default video.
void setDefaultVideo(); void setDefaultVideo();
// sets whether it's going to render in screensaver mode // Sets whether it's going to render in screensaver mode.
void setScreensaverMode(bool isScreensaver); void setScreensaverMode(bool isScreensaver);
virtual void onShow() override; virtual void onShow() override;
@ -52,40 +58,41 @@ public:
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
void renderSnapshot(const Transform4x4f& parentTrans); void renderSnapshot(const Transform4x4f& parentTrans);
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view,
const std::string& element, unsigned int properties) override;
virtual std::vector<HelpPrompt> getHelpPrompts() override; virtual std::vector<HelpPrompt> getHelpPrompts() override;
virtual void update(int deltaTime) override; virtual void update(int deltaTime) override;
// Resize the video to fit this size. If one axis is zero, scale that axis to maintain aspect ratio. // Resize the video to fit this size. If one axis is zero, scale that axis to maintain
// If both are non-zero, potentially break the aspect ratio. If both are zero, no resizing. // aspect ratio. If both are non-zero, potentially break the aspect ratio. If both are
// Can be set before or after a video is loaded. // zero, no resizing. This can be set before or after a video is loaded.
// setMaxSize() and setResize() are mutually exclusive. // setMaxSize() and setResize() are mutually exclusive.
virtual void setResize(float width, float height) = 0; virtual void setResize(float width, float height) = 0;
inline void setResize(const Vector2f& size) { setResize(size.x(), size.y()); } inline void setResize(const Vector2f& size) { setResize(size.x(), size.y()); }
// Resize the video to be as large as possible but fit within a box of this size. // Resize the video to be as large as possible but fit within a box of this size.
// Can be set before or after a video is loaded. // This can be set before or after a video is loaded.
// Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive. // Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive.
virtual void setMaxSize(float width, float height) = 0; virtual void setMaxSize(float width, float height) = 0;
inline void setMaxSize(const Vector2f& size) { setMaxSize(size.x(), size.y()); } inline void setMaxSize(const Vector2f& size) { setMaxSize(size.x(), size.y()); }
private: private:
// Start the video Immediately // Start the video immediately.
virtual void startVideo() = 0; virtual void startVideo() = 0;
// Stop the video // Stop the video.
virtual void stopVideo() { }; virtual void stopVideo() { };
// Handle looping the video. Must be called periodically // Handle looping the video. Must be called periodically.
virtual void handleLooping(); virtual void handleLooping();
// Start the video after any configured delay // Start the video after any configured delay.
void startVideoWithDelay(); void startVideoWithDelay();
// Handle any delay to the start of playing the video clip. Must be called periodically // Handle any delay to the start of playing the video clip. Must be called periodically.
void handleStartDelay(); void handleStartDelay();
// Manage the playing state of the component // Manage the playing state of the component.
void manageState(); void manageState();
protected: protected:

View file

@ -1,3 +1,9 @@
//
// VideoPlayerComponent.cpp
//
// OMXPlayer video playing for Raspberry Pi.
//
#ifdef _RPI_ #ifdef _RPI_
#include "components/VideoPlayerComponent.h" #include "components/VideoPlayerComponent.h"
@ -5,6 +11,7 @@
#include "utils/StringUtil.h" #include "utils/StringUtil.h"
#include "AudioManager.h" #include "AudioManager.h"
#include "Settings.h" #include "Settings.h"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <wait.h> #include <wait.h>
@ -59,55 +66,46 @@ void VideoPlayerComponent::setMaxSize(float width, float height)
void VideoPlayerComponent::startVideo() void VideoPlayerComponent::startVideo()
{ {
if (!mIsPlaying) if (!mIsPlaying) {
{
mVideoWidth = 0; mVideoWidth = 0;
mVideoHeight = 0; mVideoHeight = 0;
std::string path(mVideoPath.c_str()); std::string path(mVideoPath.c_str());
// Make sure we have a video path // Make sure we have a video path.
if ((path.size() > 0) && (mPlayerPid == -1)) if ((path.size() > 0) && (mPlayerPid == -1)) {
{ // Set the video that we are going to be playing so we don't attempt to restart it.
// Set the video that we are going to be playing so we don't attempt to restart it
mPlayingVideoPath = mVideoPath; mPlayingVideoPath = mVideoPath;
// Disable AudioManager so video can play, in case we're requesting ALSA // Disable AudioManager so video can play, in case we're requesting ALSA.
if (Utils::String::startsWith(Settings::getInstance()->getString("OMXAudioDev").c_str(), "alsa")) if (Utils::String::startsWith(Settings::getInstance()->
{ getString("OMXAudioDev").c_str(), "alsa"))
AudioManager::getInstance()->deinit(); AudioManager::getInstance()->deinit();
}
// Start the player process // Start the player process.
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) if (pid == -1) {
{ // Failed.
// Failed
mPlayingVideoPath = ""; mPlayingVideoPath = "";
} }
else if (pid > 0) else if (pid > 0) {
{
mPlayerPid = pid; mPlayerPid = pid;
// Update the playing state // Update the playing state.
signal(SIGCHLD, catch_child); signal(SIGCHLD, catch_child);
mIsPlaying = true; mIsPlaying = true;
mFadeIn = 0.0f; mFadeIn = 0.0f;
} }
else else {
{
// Find out the pixel position of the video view and build a command line for // Find out the pixel position of the video view and build a command line for
// omxplayer to position it in the right place // OMXPlayer to position it in the right place.
char buf1[32]; char buf1[32];
char buf2[32]; char buf2[32];
float x = mPosition.x() - (mOrigin.x() * mSize.x()); float x = mPosition.x() - (mOrigin.x() * mSize.x());
float y = mPosition.y() - (mOrigin.y() * mSize.y()); float y = mPosition.y() - (mOrigin.y() * mSize.y());
// fix x and y // Fix x and y.
switch(Renderer::getScreenRotate()) switch (Renderer::getScreenRotate()) {
{ case 0: {
case 0:
{
const int x1 = (int)(Renderer::getScreenOffsetX() + x); const int x1 = (int)(Renderer::getScreenOffsetX() + x);
const int y1 = (int)(Renderer::getScreenOffsetY() + y); const int y1 = (int)(Renderer::getScreenOffsetY() + y);
const int x2 = (int)(x1 + mSize.x()); const int x2 = (int)(x1 + mSize.x());
@ -116,9 +114,9 @@ void VideoPlayerComponent::startVideo()
} }
break; break;
case 1: case 1: {
{ const int x1 = (int)(Renderer::getWindowWidth() -
const int x1 = (int)(Renderer::getWindowWidth() - Renderer::getScreenOffsetY() - y - mSize.y()); Renderer::getScreenOffsetY() - y - mSize.y());
const int y1 = (int)(Renderer::getScreenOffsetX() + x); const int y1 = (int)(Renderer::getScreenOffsetX() + x);
const int x2 = (int)(x1 + mSize.y()); const int x2 = (int)(x1 + mSize.y());
const int y2 = (int)(y1 + mSize.x()); const int y2 = (int)(y1 + mSize.x());
@ -126,20 +124,21 @@ void VideoPlayerComponent::startVideo()
} }
break; break;
case 2: case 2: {
{ const int x1 = (int)(Renderer::getWindowWidth() -
const int x1 = (int)(Renderer::getWindowWidth() - Renderer::getScreenOffsetX() - x - mSize.x()); Renderer::getScreenOffsetX() - x - mSize.x());
const int y1 = (int)(Renderer::getWindowHeight() - Renderer::getScreenOffsetY() - y - mSize.y()); const int y1 = (int)(Renderer::getWindowHeight() -
Renderer::getScreenOffsetY() - y - mSize.y());
const int x2 = (int)(x1 + mSize.x()); const int x2 = (int)(x1 + mSize.x());
const int y2 = (int)(y1 + mSize.y()); const int y2 = (int)(y1 + mSize.y());
sprintf(buf1, "%d,%d,%d,%d", x1, y1, x2, y2); sprintf(buf1, "%d,%d,%d,%d", x1, y1, x2, y2);
} }
break; break;
case 3: case 3: {
{
const int x1 = (int)(Renderer::getScreenOffsetY() + y); const int x1 = (int)(Renderer::getScreenOffsetY() + y);
const int y1 = (int)(Renderer::getWindowHeight() - Renderer::getScreenOffsetX() - x - mSize.x()); const int y1 = (int)(Renderer::getWindowHeight() -
Renderer::getScreenOffsetX() - x - mSize.x());
const int x2 = (int)(x1 + mSize.y()); const int x2 = (int)(x1 + mSize.y());
const int y2 = (int)(y1 + mSize.x()); const int y2 = (int)(y1 + mSize.x());
sprintf(buf1, "%d,%d,%d,%d", x1, y1, x2, y2); sprintf(buf1, "%d,%d,%d,%d", x1, y1, x2, y2);
@ -147,72 +146,67 @@ void VideoPlayerComponent::startVideo()
break; break;
} }
// rotate the video // Rotate the video.
switch(Renderer::getScreenRotate()) switch (Renderer::getScreenRotate()) {
{
case 0: { sprintf(buf2, "%d", (int) 0); } break; case 0: { sprintf(buf2, "%d", (int) 0); } break;
case 1: { sprintf(buf2, "%d", (int) 90); } break; case 1: { sprintf(buf2, "%d", (int) 90); } break;
case 2: { sprintf(buf2, "%d", (int)180); } break; case 2: { sprintf(buf2, "%d", (int)180); } break;
case 3: { sprintf(buf2, "%d", (int)270); } break; case 3: { sprintf(buf2, "%d", (int)270); } break;
} }
// We need to specify the layer of 10000 or above to ensure the video is displayed on top // We need to specify the layer of 10000 or above to ensure the video is
// of our SDL display // displayed on top of our SDL display.
const char* argv[] = { "", "--layer", "10010", "--loop", "--no-osd",
"--aspect-mode", "letterbox", "--vol", "0", "-o", "both",
"--win", buf1, "--orientation", buf2, "", "", "", "", "", "",
"", "", "", "", "", NULL };
const char* argv[] = { "", "--layer", "10010", "--loop", "--no-osd", "--aspect-mode", "letterbox", "--vol", "0", "-o", "both","--win", buf1, "--orientation", buf2, "", "", "", "", "", "", "", "", "", "", "", NULL }; // Check if we want to mute the audio.
if ((!Settings::getInstance()->getBool("VideoAudio") ||
// check if we want to mute the audio (float)VolumeControl::getInstance()->getVolume() == 0) ||
if ((!Settings::getInstance()->getBool("VideoAudio") || (float)VolumeControl::getInstance()->getVolume() == 0) || (Settings::getInstance()->getBool("ScreenSaverVideoMute") &&
(Settings::getInstance()->getBool("ScreenSaverVideoMute") && mScreensaverMode)) mScreensaverMode)) {
{
argv[8] = "-1000000"; argv[8] = "-1000000";
} }
else else {
{
float percentVolume = (float)VolumeControl::getInstance()->getVolume(); float percentVolume = (float)VolumeControl::getInstance()->getVolume();
int OMXVolume = (int)((percentVolume-98)*105); int OMXVolume = (int)((percentVolume-98)*105);
argv[8] = std::to_string(OMXVolume).c_str(); argv[8] = std::to_string(OMXVolume).c_str();
} }
// test if there's a path for possible subtitles, meaning we're a screensaver video // Test if there's a path for possible subtitles, meaning we're a screensaver video.
if (!subtitlePath.empty()) if (!subtitlePath.empty()) {
{ // If we are rendering a screensaver.
// if we are rendering a screensaver // Check if we want to stretch the image.
// check if we want to stretch the image
if (Settings::getInstance()->getBool("StretchVideoOnScreenSaver")) if (Settings::getInstance()->getBool("StretchVideoOnScreenSaver"))
{
argv[6] = "stretch"; argv[6] = "stretch";
}
if (Settings::getInstance()->getString("ScreenSaverGameInfo") != "never") if (Settings::getInstance()->getString("ScreenSaverGameInfo") != "never") {
{ // If we have chosen to render subtitles.
// if we have chosen to render subtitles
argv[15] = "--subtitles"; argv[15] = "--subtitles";
argv[16] = subtitlePath.c_str(); argv[16] = subtitlePath.c_str();
argv[17] = mPlayingVideoPath.c_str(); argv[17] = mPlayingVideoPath.c_str();
argv[18] = "--font"; argv[18] = "--font";
argv[19] = Settings::getInstance()->getString("SubtitleFont").c_str(); argv[19] = Settings::getInstance()->getString("SubtitleFont").c_str();
argv[20] = "--italic-font"; argv[20] = "--italic-font";
argv[21] = Settings::getInstance()->getString("SubtitleItalicFont").c_str(); argv[21] = Settings::getInstance()->
getString("SubtitleItalicFont").c_str();
argv[22] = "--font-size"; argv[22] = "--font-size";
argv[23] = std::to_string(Settings::getInstance()->getInt("SubtitleSize")).c_str(); argv[23] = std::to_string(Settings::getInstance()->
getInt("SubtitleSize")).c_str();
argv[24] = "--align"; argv[24] = "--align";
argv[25] = Settings::getInstance()->getString("SubtitleAlignment").c_str(); argv[25] = Settings::getInstance()->
getString("SubtitleAlignment").c_str();
} }
else else {
{ // If we have chosen NOT to render subtitles in the screensaver.
// if we have chosen NOT to render subtitles in the screensaver
argv[15] = mPlayingVideoPath.c_str(); argv[15] = mPlayingVideoPath.c_str();
} }
} }
else else {
{ // If we are rendering a video gamelist.
// if we are rendering a video gamelist
if (!mTargetIsMax) if (!mTargetIsMax)
{
argv[6] = "stretch"; argv[6] = "stretch";
}
argv[15] = mPlayingVideoPath.c_str(); argv[15] = mPlayingVideoPath.c_str();
} }
@ -221,12 +215,12 @@ void VideoPlayerComponent::startVideo()
//const char* argv[] = args; //const char* argv[] = args;
const char* env[] = { "LD_LIBRARY_PATH=/opt/vc/libs:/usr/lib/omxplayer", NULL }; const char* env[] = { "LD_LIBRARY_PATH=/opt/vc/libs:/usr/lib/omxplayer", NULL };
// Redirect stdout // Redirect stdout.
int fdin = open("/dev/null", O_RDONLY); int fdin = open("/dev/null", O_RDONLY);
int fdout = open("/dev/null", O_WRONLY); int fdout = open("/dev/null", O_WRONLY);
dup2(fdin, 0); dup2(fdin, 0);
dup2(fdout, 1); dup2(fdout, 1);
// Run the omxplayer binary // Run the OMXPlayer binary.
execve("/usr/bin/omxplayer.bin", (char**)argv, (char**)env); execve("/usr/bin/omxplayer.bin", (char**)argv, (char**)env);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
@ -237,7 +231,7 @@ void VideoPlayerComponent::startVideo()
void catch_child(int sig_num) void catch_child(int sig_num)
{ {
/* when we get here, we know there's a zombie child waiting */ // When we get here, we know there's a zombie child waiting.
int child_status; int child_status;
wait(&child_status); wait(&child_status);
} }
@ -247,9 +241,8 @@ void VideoPlayerComponent::stopVideo()
mIsPlaying = false; mIsPlaying = false;
mStartDelayed = false; mStartDelayed = false;
// Stop the player process // Stop the player process.
if (mPlayerPid != -1) if (mPlayerPid != -1) {
{
int status; int status;
kill(mPlayerPid, SIGKILL); kill(mPlayerPid, SIGKILL);
waitpid(mPlayerPid, &status, WNOHANG); waitpid(mPlayerPid, &status, WNOHANG);
@ -258,4 +251,3 @@ void VideoPlayerComponent::stopVideo()
} }
#endif #endif

View file

@ -1,3 +1,9 @@
//
// VideoPlayerComponent.h
//
// OMXPlayer video playing for Raspberry Pi.
//
#ifdef _RPI_ #ifdef _RPI_
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_VIDEO_PLAYER_COMPONENT_H #ifndef ES_CORE_COMPONENTS_VIDEO_PLAYER_COMPONENT_H
@ -15,22 +21,22 @@ public:
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
// Resize the video to fit this size. If one axis is zero, scale that axis to maintain aspect ratio. // Resize the video to fit this size. If one axis is zero, scale that axis to maintain
// If both are non-zero, potentially break the aspect ratio. If both are zero, no resizing. // aspect ratio. If both are non-zero, potentially break the aspect ratio. If both are
// Can be set before or after a video is loaded. // zero, no resizing. This can be set before or after a video is loaded.
// setMaxSize() and setResize() are mutually exclusive. // setMaxSize() and setResize() are mutually exclusive.
void setResize(float width, float height); void setResize(float width, float height) override;
// Resize the video to be as large as possible but fit within a box of this size. // Resize the video to be as large as possible but fit within a box of this size.
// Can be set before or after a video is loaded. // This can be set before or after a video is loaded.
// Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive. // Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive.
void setMaxSize(float width, float height); void setMaxSize(float width, float height) override;
private: private:
// Start the video Immediately // Start the video Immediately.
virtual void startVideo(); virtual void startVideo() override;
// Stop the video // Stop the video.
virtual void stopVideo(); virtual void stopVideo() override;
private: private:
pid_t mPlayerPid; pid_t mPlayerPid;

View file

@ -1,3 +1,9 @@
//
// VideoVlcComponent.cpp
//
// Video playing using libVLC.
//
#include "components/VideoVlcComponent.h" #include "components/VideoVlcComponent.h"
#include "renderers/Renderer.h" #include "renderers/Renderer.h"
@ -20,7 +26,7 @@
#include <codecvt> #include <codecvt>
#endif #endif
libvlc_instance_t* VideoVlcComponent::mVLC = NULL; libvlc_instance_t* VideoVlcComponent::mVLC = nullptr;
// VLC prepares to render a video frame. // VLC prepares to render a video frame.
static void* lock(void* data, void** p_pixels) { static void* lock(void* data, void** p_pixels) {
@ -28,7 +34,7 @@ static void *lock(void *data, void **p_pixels) {
SDL_LockMutex(c->mutex); SDL_LockMutex(c->mutex);
SDL_LockSurface(c->surface); SDL_LockSurface(c->surface);
*p_pixels = c->surface->pixels; *p_pixels = c->surface->pixels;
return NULL; // Picture identifier, not needed here. return nullptr; // Picture identifier, not needed here.
} }
// VLC just rendered a video frame. // VLC just rendered a video frame.
@ -40,19 +46,18 @@ static void unlock(void *data, void* /*id*/, void *const* /*p_pixels*/) {
// VLC wants to display a video frame. // VLC wants to display a video frame.
static void display(void* /*data*/, void* /*id*/) { static void display(void* /*data*/, void* /*id*/) {
//Data to be displayed // Data to be displayed.
} }
VideoVlcComponent::VideoVlcComponent(Window* window, std::string subtitles) : VideoVlcComponent::VideoVlcComponent(Window* window, std::string subtitles)
VideoComponent(window), : VideoComponent(window), mMediaPlayer(nullptr)
mMediaPlayer(nullptr)
{ {
memset(&mContext, 0, sizeof(mContext)); memset(&mContext, 0, sizeof(mContext));
// Get an empty texture for rendering the video // Get an empty texture for rendering the video.
mTexture = TextureResource::get(""); mTexture = TextureResource::get("");
// Make sure VLC has been initialised // Make sure VLC has been initialized.
setupVLC(subtitles); setupVLC(subtitles);
} }
@ -87,51 +92,50 @@ void VideoVlcComponent::resize()
if (textureSize == Vector2f::Zero()) if (textureSize == Vector2f::Zero())
return; return;
// SVG rasterization is determined by height (see SVGResource.cpp), and rasterization is done in terms of pixels // SVG rasterization is determined by height and rasterization is done in terms of pixels.
// if rounding is off enough in the rasterization step (for images with extreme aspect ratios), it can cause cutoff when the aspect ratio breaks // If rounding is off enough in the rasterization step (for images with extreme aspect
// so, we always make sure the resultant height is an integer to make sure cutoff doesn't happen, and scale width from that // ratios), it can cause cutoff when the aspect ratio breaks.
// (you'll see this scattered throughout the function) // So we always make sure the resultant height is an integer to make sure cutoff doesn't
// this is probably not the best way, so if you're familiar with this problem and have a better solution, please make a pull request! // happen, and scale width from that (you'll see this scattered throughout the function).
// This is probably not the best way, so if you're familiar with this problem and have a
if(mTargetIsMax) // better solution, please make a pull request!
{ if (mTargetIsMax) {
mSize = textureSize; mSize = textureSize;
Vector2f resizeScale((mTargetSize.x() / mSize.x()), (mTargetSize.y() / mSize.y())); Vector2f resizeScale((mTargetSize.x() / mSize.x()), (mTargetSize.y() / mSize.y()));
if(resizeScale.x() < resizeScale.y()) if (resizeScale.x() < resizeScale.y()) {
{
mSize[0] *= resizeScale.x(); mSize[0] *= resizeScale.x();
mSize[1] *= resizeScale.x(); mSize[1] *= resizeScale.x();
}else{ }
else {
mSize[0] *= resizeScale.y(); mSize[0] *= resizeScale.y();
mSize[1] *= resizeScale.y(); mSize[1] *= resizeScale.y();
} }
// for SVG rasterization, always calculate width from rounded height (see comment above) // For SVG rasterization, always calculate width from rounded height (see comment above).
mSize[1] = Math::round(mSize[1]); mSize[1] = Math::round(mSize[1]);
mSize[0] = (mSize[1] / textureSize.y()) * textureSize.x(); mSize[0] = (mSize[1] / textureSize.y()) * textureSize.x();
}else{ }
// if both components are set, we just stretch else {
// if no components are set, we don't resize at all // If both components are set, we just stretch.
// If no components are set, we don't resize at all.
mSize = mTargetSize == Vector2f::Zero() ? textureSize : mTargetSize; mSize = mTargetSize == Vector2f::Zero() ? textureSize : mTargetSize;
// if only one component is set, we resize in a way that maintains aspect ratio // If only one component is set, we resize in a way that maintains aspect ratio.
// for SVG rasterization, we always calculate width from rounded height (see comment above) // For SVG rasterization, we always calculate width from rounded height (see comment above).
if(!mTargetSize.x() && mTargetSize.y()) if (!mTargetSize.x() && mTargetSize.y()) {
{
mSize[1] = Math::round(mTargetSize.y()); mSize[1] = Math::round(mTargetSize.y());
mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x(); mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x();
}else if(mTargetSize.x() && !mTargetSize.y()) }
{ else if (mTargetSize.x() && !mTargetSize.y()) {
mSize[1] = Math::round((mTargetSize.x() / textureSize.x()) * textureSize.y()); mSize[1] = Math::round((mTargetSize.x() / textureSize.x()) * textureSize.y());
mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x(); mSize[0] = (mSize.y() / textureSize.y()) * textureSize.x();
} }
} }
// mSize.y() should already be rounded // mSize.y() should already be rounded.
mTexture->rasterizeAt((size_t)Math::round(mSize.x()), (size_t)Math::round(mSize.y())); mTexture->rasterizeAt((size_t)Math::round(mSize.x()), (size_t)Math::round(mSize.y()));
onSizeChanged(); onSizeChanged();
@ -147,10 +151,10 @@ void VideoVlcComponent::render(const Transform4x4f& parentTrans)
GuiComponent::renderChildren(trans); GuiComponent::renderChildren(trans);
Renderer::setMatrix(trans); Renderer::setMatrix(trans);
if (mIsPlaying && mContext.valid) if (mIsPlaying && mContext.valid) {
{
const unsigned int fadeIn = (unsigned int)(Math::clamp(0.0f, mFadeIn, 1.0f) * 255.0f); const unsigned int fadeIn = (unsigned int)(Math::clamp(0.0f, mFadeIn, 1.0f) * 255.0f);
const unsigned int color = Renderer::convertColor((fadeIn << 24) | (fadeIn << 16) | (fadeIn << 8) | 255); const unsigned int color =
Renderer::convertColor((fadeIn << 24) | (fadeIn << 16) | (fadeIn << 8) | 255);
Renderer::Vertex vertices[4]; Renderer::Vertex vertices[4];
vertices[0] = { { 0.0f , 0.0f }, { 0.0f, 0.0f }, color }; vertices[0] = { { 0.0f , 0.0f }, { 0.0f, 0.0f }, color };
@ -158,29 +162,29 @@ void VideoVlcComponent::render(const Transform4x4f& parentTrans)
vertices[2] = { { mSize.x(), 0.0f }, { 1.0f, 0.0f }, color }; vertices[2] = { { mSize.x(), 0.0f }, { 1.0f, 0.0f }, color };
vertices[3] = { { mSize.x(), mSize.y() }, { 1.0f, 1.0f }, color }; vertices[3] = { { mSize.x(), mSize.y() }, { 1.0f, 1.0f }, color };
// round vertices // Round vertices.
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
vertices[i].pos.round(); vertices[i].pos.round();
// Build a texture for the video frame // Build a texture for the video frame.
mTexture->initFromPixels((unsigned char*)mContext.surface->pixels, mContext.surface->w, mContext.surface->h); mTexture->initFromPixels((unsigned char*)mContext.surface->pixels,
mContext.surface->w, mContext.surface->h);
mTexture->bind(); mTexture->bind();
// Render it // Render it.
Renderer::drawTriangleStrips(&vertices[0], 4); Renderer::drawTriangleStrips(&vertices[0], 4);
} }
else else {
{
VideoComponent::renderSnapshot(parentTrans); VideoComponent::renderSnapshot(parentTrans);
} }
} }
void VideoVlcComponent::setupContext() void VideoVlcComponent::setupContext()
{ {
if (!mContext.valid) if (!mContext.valid) {
{ // Create an RGBA surface to render the video into.
// Create an RGBA surface to render the video into mContext.surface = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)mVideoWidth,
mContext.surface = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)mVideoWidth, (int)mVideoHeight, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); (int)mVideoHeight, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
mContext.mutex = SDL_CreateMutex(); mContext.mutex = SDL_CreateMutex();
mContext.valid = true; mContext.valid = true;
resize(); resize();
@ -189,8 +193,7 @@ void VideoVlcComponent::setupContext()
void VideoVlcComponent::freeContext() void VideoVlcComponent::freeContext()
{ {
if (mContext.valid) if (mContext.valid) {
{
SDL_FreeSurface(mContext.surface); SDL_FreeSurface(mContext.surface);
SDL_DestroyMutex(mContext.mutex); SDL_DestroyMutex(mContext.mutex);
mContext.valid = false; mContext.valid = false;
@ -199,21 +202,18 @@ void VideoVlcComponent::freeContext()
void VideoVlcComponent::setupVLC(std::string subtitles) void VideoVlcComponent::setupVLC(std::string subtitles)
{ {
// If VLC hasn't been initialised yet then do it now // If VLC hasn't been initialised yet then do it now.
if (!mVLC) if (!mVLC) {
{
const char** args; const char** args;
const char* newargs[] = { "--quiet", "--sub-file", subtitles.c_str() }; const char* newargs[] = { "--quiet", "--sub-file", subtitles.c_str() };
const char* singleargs[] = { "--quiet" }; const char* singleargs[] = { "--quiet" };
int argslen = 0; int argslen = 0;
if (!subtitles.empty()) if (!subtitles.empty()) {
{
argslen = sizeof(newargs) / sizeof(newargs[0]); argslen = sizeof(newargs) / sizeof(newargs[0]);
args = newargs; args = newargs;
} }
else else {
{
argslen = sizeof(singleargs) / sizeof(singleargs[0]); argslen = sizeof(singleargs) / sizeof(singleargs[0]);
args = singleargs; args = singleargs;
} }
@ -223,16 +223,13 @@ void VideoVlcComponent::setupVLC(std::string subtitles)
void VideoVlcComponent::handleLooping() void VideoVlcComponent::handleLooping()
{ {
if (mIsPlaying && mMediaPlayer) if (mIsPlaying && mMediaPlayer) {
{
libvlc_state_t state = libvlc_media_player_get_state(mMediaPlayer); libvlc_state_t state = libvlc_media_player_get_state(mMediaPlayer);
if (state == libvlc_Ended) if (state == libvlc_Ended) {
{
if (!Settings::getInstance()->getBool("VideoAudio") || if (!Settings::getInstance()->getBool("VideoAudio") ||
(Settings::getInstance()->getBool("ScreenSaverVideoMute") && mScreensaverMode)) (Settings::getInstance()->getBool("ScreenSaverVideoMute") && mScreensaverMode))
{
libvlc_audio_set_mute(mMediaPlayer, 1); libvlc_audio_set_mute(mMediaPlayer, 1);
}
//libvlc_media_player_set_position(mMediaPlayer, 0.0f); //libvlc_media_player_set_position(mMediaPlayer, 0.0f);
libvlc_media_player_set_media(mMediaPlayer, mMedia); libvlc_media_player_set_media(mMediaPlayer, mMedia);
libvlc_media_player_play(mMediaPlayer); libvlc_media_player_play(mMediaPlayer);
@ -251,30 +248,27 @@ void VideoVlcComponent::startVideo()
#else #else
std::string path(mVideoPath); std::string path(mVideoPath);
#endif #endif
// Make sure we have a video path // Make sure we have a video path.
if (mVLC && (path.size() > 0)) if (mVLC && (path.size() > 0)) {
{ // Set the video that we are going to be playing so we don't attempt to restart it.
// Set the video that we are going to be playing so we don't attempt to restart it
mPlayingVideoPath = mVideoPath; mPlayingVideoPath = mVideoPath;
// Open the media // Open the media.
mMedia = libvlc_media_new_path(mVLC, path.c_str()); mMedia = libvlc_media_new_path(mVLC, path.c_str());
if (mMedia) if (mMedia) {
{
unsigned track_count; unsigned track_count;
int parseResult; int parseResult;
libvlc_event_t vlcEvent; libvlc_event_t vlcEvent;
// Asynchronous media parsing // Asynchronous media parsing.
libvlc_event_attach(libvlc_media_event_manager(mMedia), libvlc_MediaParsedChanged, VlcMediaParseCallback, 0); libvlc_event_attach(libvlc_media_event_manager(
mMedia), libvlc_MediaParsedChanged, VlcMediaParseCallback, 0);
parseResult = libvlc_media_parse_with_options(mMedia, libvlc_media_parse_local, -1); parseResult = libvlc_media_parse_with_options(mMedia, libvlc_media_parse_local, -1);
if (!parseResult) if (!parseResult) {
{ // Wait for a maximum of 1 second for the media parsing.
// Wait for a maximum of 1 second for the media parsing for (int i = 0; i < 200; i++) {
for(int i=0; i<200; i++) if (libvlc_media_get_parsed_status(mMedia))
{
if ((libvlc_media_get_parsed_status(mMedia)))
break; break;
SDL_Delay(5); SDL_Delay(5);
}; };
@ -282,10 +276,8 @@ void VideoVlcComponent::startVideo()
libvlc_media_track_t** tracks; libvlc_media_track_t** tracks;
track_count = libvlc_media_tracks_get(mMedia, &tracks); track_count = libvlc_media_tracks_get(mMedia, &tracks);
for (unsigned track = 0; track < track_count; ++track) for (unsigned track = 0; track < track_count; ++track) {
{ if (tracks[track]->i_type == libvlc_track_video) {
if (tracks[track]->i_type == libvlc_track_video)
{
mVideoWidth = tracks[track]->video->i_width; mVideoWidth = tracks[track]->video->i_width;
mVideoHeight = tracks[track]->video->i_height; mVideoHeight = tracks[track]->video->i_height;
break; break;
@ -293,21 +285,20 @@ void VideoVlcComponent::startVideo()
} }
libvlc_media_tracks_release(tracks, track_count); libvlc_media_tracks_release(tracks, track_count);
// Make sure we found a valid video track // Make sure we found a valid video track.
if ((mVideoWidth > 0) && (mVideoHeight > 0)) if ((mVideoWidth > 0) && (mVideoHeight > 0)) {
{
#ifndef _RPI_ #ifndef _RPI_
if (mScreensaverMode) if (mScreensaverMode) {
{
if (!Settings::getInstance()->getBool("CaptionsCompatibility")) { if (!Settings::getInstance()->getBool("CaptionsCompatibility")) {
Vector2f resizeScale((Renderer::getScreenWidth() / (float)mVideoWidth), (Renderer::getScreenHeight() / (float)mVideoHeight)); Vector2f resizeScale((Renderer::getScreenWidth() / (float)mVideoWidth),
(Renderer::getScreenHeight() / (float)mVideoHeight));
if(resizeScale.x() < resizeScale.y()) if (resizeScale.x() < resizeScale.y()) {
{
mVideoWidth = (unsigned int) (mVideoWidth * resizeScale.x()); mVideoWidth = (unsigned int) (mVideoWidth * resizeScale.x());
mVideoHeight = (unsigned int) (mVideoHeight * resizeScale.x()); mVideoHeight = (unsigned int) (mVideoHeight * resizeScale.x());
}else{ }
else {
mVideoWidth = (unsigned int) (mVideoWidth * resizeScale.y()); mVideoWidth = (unsigned int) (mVideoWidth * resizeScale.y());
mVideoHeight = (unsigned int) (mVideoHeight * resizeScale.y()); mVideoHeight = (unsigned int) (mVideoHeight * resizeScale.y());
} }
@ -317,20 +308,21 @@ void VideoVlcComponent::startVideo()
PowerSaver::pause(); PowerSaver::pause();
setupContext(); setupContext();
// Setup the media player // Setup the media player.
mMediaPlayer = libvlc_media_player_new_from_media(mMedia); mMediaPlayer = libvlc_media_player_new_from_media(mMedia);
if (!Settings::getInstance()->getBool("VideoAudio") || if (!Settings::getInstance()->getBool("VideoAudio") ||
(Settings::getInstance()->getBool("ScreenSaverVideoMute") && mScreensaverMode)) (Settings::getInstance()->getBool("ScreenSaverVideoMute") &&
{ mScreensaverMode))
libvlc_audio_set_mute(mMediaPlayer, 1); libvlc_audio_set_mute(mMediaPlayer, 1);
}
libvlc_media_player_play(mMediaPlayer); libvlc_media_player_play(mMediaPlayer);
libvlc_video_set_callbacks(mMediaPlayer, lock, unlock, display, (void*)&mContext); libvlc_video_set_callbacks(mMediaPlayer, lock, unlock, display,
libvlc_video_set_format(mMediaPlayer, "RGBA", (int)mVideoWidth, (int)mVideoHeight, (int)mVideoWidth * 4); (void*)&mContext);
libvlc_video_set_format(mMediaPlayer, "RGBA", (int)mVideoWidth,
(int)mVideoHeight, (int)mVideoWidth * 4);
// Update the playing state // Update the playing state.
mIsPlaying = true; mIsPlaying = true;
mFadeIn = 0.0f; mFadeIn = 0.0f;
} }
@ -343,13 +335,12 @@ void VideoVlcComponent::stopVideo()
{ {
mIsPlaying = false; mIsPlaying = false;
mStartDelayed = false; mStartDelayed = false;
// Release the media player so it stops calling back to us // Release the media player so it stops calling back to us.
if (mMediaPlayer) if (mMediaPlayer) {
{
libvlc_media_player_stop(mMediaPlayer); libvlc_media_player_stop(mMediaPlayer);
libvlc_media_player_release(mMediaPlayer); libvlc_media_player_release(mMediaPlayer);
libvlc_media_release(mMedia); libvlc_media_release(mMedia);
mMediaPlayer = NULL; mMediaPlayer = nullptr;
freeContext(); freeContext();
PowerSaver::resume(); PowerSaver::resume();
} }

View file

@ -1,8 +1,15 @@
//
// VideoVlcComponent.h
//
// Video playing using libVLC.
//
#pragma once #pragma once
#ifndef ES_CORE_COMPONENTS_VIDEO_VLC_COMPONENT_H #ifndef ES_CORE_COMPONENTS_VIDEO_VLC_COMPONENT_H
#define ES_CORE_COMPONENTS_VIDEO_VLC_COMPONENT_H #define ES_CORE_COMPONENTS_VIDEO_VLC_COMPONENT_H
#include "VideoComponent.h" #include "VideoComponent.h"
#include <vlc/vlc.h> #include <vlc/vlc.h>
struct SDL_mutex; struct SDL_mutex;
@ -19,9 +26,8 @@ struct VideoContext {
class VideoVlcComponent : public VideoComponent class VideoVlcComponent : public VideoComponent
{ {
// Structure that groups together the configuration of the video component // Structure that groups together the configuration of the video component.
struct Configuration struct Configuration {
{
unsigned startDelay; unsigned startDelay;
bool showSnapshotNoVideo; bool showSnapshotNoVideo;
bool showSnapshotDelay; bool showSnapshotDelay;
@ -36,15 +42,14 @@ public:
void render(const Transform4x4f& parentTrans) override; void render(const Transform4x4f& parentTrans) override;
// Resize the video to fit this size. If one axis is zero, scale that axis to maintain
// Resize the video to fit this size. If one axis is zero, scale that axis to maintain aspect ratio. // aspect ratio. If both are non-zero, potentially break the aspect ratio. If both are
// If both are non-zero, potentially break the aspect ratio. If both are zero, no resizing. // zero, no resizing. This can be set before or after a video is loaded.
// Can be set before or after a video is loaded.
// setMaxSize() and setResize() are mutually exclusive. // setMaxSize() and setResize() are mutually exclusive.
void setResize(float width, float height) override; void setResize(float width, float height) override;
// Resize the video to be as large as possible but fit within a box of this size. // Resize the video to be as large as possible but fit within a box of this size.
// Can be set before or after a video is loaded. // This can be set before or after a video is loaded.
// Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive. // Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive.
void setMaxSize(float width, float height) override; void setMaxSize(float width, float height) override;
@ -52,11 +57,11 @@ private:
// Calculates the correct mSize from our resizing information (set by setResize/setMaxSize). // Calculates the correct mSize from our resizing information (set by setResize/setMaxSize).
// Used internally whenever the resizing parameters or texture change. // Used internally whenever the resizing parameters or texture change.
void resize(); void resize();
// Start the video Immediately // Start the video immediately.
virtual void startVideo() override; virtual void startVideo() override;
// Stop the video // Stop the video.
virtual void stopVideo() override; virtual void stopVideo() override;
// Handle looping the video. Must be called periodically // Handle looping the video. Must be called periodically.
virtual void handleLooping() override; virtual void handleLooping() override;
void setupContext(); void setupContext();

View file

@ -1,7 +1,7 @@
// //
// Renderer.cpp // Renderer.cpp
// //
// Rendering functions. // General rendering functions.
// //
#include "renderers/Renderer.h" #include "renderers/Renderer.h"

View file

@ -1,7 +1,7 @@
// //
// Renderer.h // Renderer.h
// //
// Rendering functions. // General rendering functions.
// //
#pragma once #pragma once

View file

@ -60,7 +60,7 @@ const ResourceData ResourceManager::getFileData(const std::string& path) const
} }
// If the file doesn't exist, return an "empty" ResourceData. // If the file doesn't exist, return an "empty" ResourceData.
ResourceData data = {NULL, 0}; ResourceData data = {nullptr, 0};
return data; return data;
} }

View file

@ -54,7 +54,7 @@ bool TextureData::initSVGFromMemory(const unsigned char* fileData, size_t length
// nsvgParse excepts a modifiable, null-terminated string. // nsvgParse excepts a modifiable, null-terminated string.
char* copy = (char*)malloc(length + 1); char* copy = (char*)malloc(length + 1);
assert(copy != NULL); assert(copy != nullptr);
memcpy(copy, fileData, length); memcpy(copy, fileData, length);
copy[length] = '\0'; copy[length] = '\0';