mirror of
https://github.com/FunKey-Project/RetroFE.git
synced 2026-03-19 10:22:42 +01:00
Port from git
This commit is contained in:
282
Source/Graphics/Component/Component.cpp
Normal file
282
Source/Graphics/Component/Component.cpp
Normal file
@@ -0,0 +1,282 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "Component.h"
|
||||
#include "../Animate/Tween.h"
|
||||
#include "../../Graphics/ViewInfo.h"
|
||||
#include "../../Utility/Log.h"
|
||||
|
||||
Component::Component()
|
||||
{
|
||||
OnEnterTweens = NULL;
|
||||
OnExitTweens = NULL;
|
||||
OnIdleTweens = NULL;
|
||||
OnHighlightEnterTweens = NULL;
|
||||
OnHighlightExitTweens = NULL;
|
||||
SelectedItem = NULL;
|
||||
NewItemSelectedSinceEnter = false;
|
||||
FreeGraphicsMemory();
|
||||
|
||||
}
|
||||
|
||||
Component::~Component()
|
||||
{
|
||||
FreeGraphicsMemory();
|
||||
}
|
||||
|
||||
void Component::FreeGraphicsMemory()
|
||||
{
|
||||
CurrentAnimationState = HIDDEN;
|
||||
EnterRequested = false;
|
||||
ExitRequested = false;
|
||||
NewItemSelected = false;
|
||||
HighlightExitComplete = false;
|
||||
CurrentTweens = NULL;
|
||||
CurrentTweenIndex = 0;
|
||||
CurrentTweenComplete = false;
|
||||
ElapsedTweenTime =0;
|
||||
ScrollActive = false;
|
||||
}
|
||||
void Component::AllocateGraphicsMemory()
|
||||
{
|
||||
}
|
||||
|
||||
void Component::TriggerEnterEvent()
|
||||
{
|
||||
EnterRequested = true;
|
||||
}
|
||||
|
||||
void Component::TriggerExitEvent()
|
||||
{
|
||||
ExitRequested = true;
|
||||
}
|
||||
|
||||
void Component::TriggerHighlightEvent(Item *selectedItem)
|
||||
{
|
||||
NewItemSelected = true;
|
||||
this->SelectedItem = selectedItem;
|
||||
}
|
||||
|
||||
|
||||
bool Component::IsIdle()
|
||||
{
|
||||
return (CurrentAnimationState == IDLE);
|
||||
}
|
||||
|
||||
bool Component::IsHidden()
|
||||
{
|
||||
return (CurrentAnimationState == HIDDEN);
|
||||
}
|
||||
bool Component::IsWaiting()
|
||||
{
|
||||
return (CurrentAnimationState == HIGHLIGHT_WAIT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Component::Update(float dt)
|
||||
{
|
||||
ElapsedTweenTime += dt;
|
||||
HighlightExitComplete = false;
|
||||
if(IsHidden() || IsWaiting() || (IsIdle() && ExitRequested))
|
||||
{
|
||||
CurrentTweenComplete = true;
|
||||
}
|
||||
|
||||
if(CurrentTweenComplete)
|
||||
{
|
||||
CurrentTweens = NULL;
|
||||
|
||||
// There was no request to override our state path. Continue on as normal.
|
||||
switch(CurrentAnimationState)
|
||||
{
|
||||
case ENTER:
|
||||
CurrentTweens = OnHighlightEnterTweens;
|
||||
CurrentAnimationState = HIGHLIGHT_ENTER;
|
||||
break;
|
||||
|
||||
case EXIT:
|
||||
CurrentTweens = NULL;
|
||||
CurrentAnimationState = HIDDEN;
|
||||
|
||||
break;
|
||||
|
||||
case HIGHLIGHT_ENTER:
|
||||
CurrentTweens = OnIdleTweens;
|
||||
CurrentAnimationState = IDLE;
|
||||
break;
|
||||
|
||||
case IDLE:
|
||||
// prevent us from automatically jumping to the exit tween upon enter
|
||||
if(EnterRequested)
|
||||
{
|
||||
EnterRequested = false;
|
||||
NewItemSelected = false;
|
||||
}
|
||||
else if(IsScrollActive() || NewItemSelected || ExitRequested)
|
||||
{
|
||||
CurrentTweens = OnHighlightExitTweens;
|
||||
CurrentAnimationState = HIGHLIGHT_EXIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentTweens = OnIdleTweens;
|
||||
CurrentAnimationState = IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case HIGHLIGHT_EXIT:
|
||||
|
||||
// intentionally break down
|
||||
case HIGHLIGHT_WAIT:
|
||||
|
||||
if(ExitRequested && (CurrentAnimationState == HIGHLIGHT_WAIT))
|
||||
{
|
||||
CurrentTweens = OnHighlightExitTweens;
|
||||
CurrentAnimationState = HIGHLIGHT_EXIT;
|
||||
|
||||
}
|
||||
else if(ExitRequested && (CurrentAnimationState == HIGHLIGHT_EXIT))
|
||||
{
|
||||
|
||||
CurrentTweens = OnExitTweens;
|
||||
CurrentAnimationState = EXIT;
|
||||
ExitRequested = false;
|
||||
}
|
||||
else if(IsScrollActive())
|
||||
{
|
||||
CurrentTweens = NULL;
|
||||
CurrentAnimationState = HIGHLIGHT_WAIT;
|
||||
}
|
||||
else if(NewItemSelected)
|
||||
{
|
||||
CurrentTweens = OnHighlightEnterTweens;
|
||||
CurrentAnimationState = HIGHLIGHT_ENTER;
|
||||
HighlightExitComplete = true;
|
||||
NewItemSelected = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentTweens = NULL;
|
||||
CurrentAnimationState = HIGHLIGHT_WAIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case HIDDEN:
|
||||
if(EnterRequested || ExitRequested)
|
||||
{
|
||||
CurrentTweens = OnEnterTweens;
|
||||
CurrentAnimationState = ENTER;
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentTweens = NULL;
|
||||
CurrentAnimationState = HIDDEN;
|
||||
}
|
||||
}
|
||||
|
||||
CurrentTweenIndex = 0;
|
||||
CurrentTweenComplete = false;
|
||||
|
||||
ElapsedTweenTime = 0;
|
||||
}
|
||||
|
||||
CurrentTweenComplete = Animate(IsIdle());
|
||||
}
|
||||
|
||||
|
||||
bool Component::Animate(bool loop)
|
||||
{
|
||||
bool completeDone = false;
|
||||
if(!CurrentTweens || CurrentTweenIndex >= CurrentTweens->size())
|
||||
{
|
||||
completeDone = true;
|
||||
}
|
||||
else if(CurrentTweens)
|
||||
{
|
||||
bool currentDone = true;
|
||||
std::vector<Tween *> *tweenSet = CurrentTweens->at(CurrentTweenIndex);
|
||||
|
||||
for(unsigned int i = 0; i < tweenSet->size(); i++)
|
||||
{
|
||||
Tween *tween = tweenSet->at(i);
|
||||
float elapsedTime = ElapsedTweenTime;
|
||||
|
||||
//todo: too many levels of nesting
|
||||
if(elapsedTime < tween->GetDuration())
|
||||
{
|
||||
currentDone = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
elapsedTime = tween->GetDuration();
|
||||
}
|
||||
|
||||
float value = tween->Animate(elapsedTime);
|
||||
|
||||
switch(tween->GetProperty())
|
||||
{
|
||||
case TWEEN_PROPERTY_X:
|
||||
GetBaseViewInfo()->SetX(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_Y:
|
||||
GetBaseViewInfo()->SetY(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_HEIGHT:
|
||||
GetBaseViewInfo()->SetHeight(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_WIDTH:
|
||||
GetBaseViewInfo()->SetWidth(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_ANGLE:
|
||||
GetBaseViewInfo()->SetAngle(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_TRANSPARENCY:
|
||||
GetBaseViewInfo()->SetTransparency(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_X_ORIGIN:
|
||||
GetBaseViewInfo()->SetXOrigin(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_Y_ORIGIN:
|
||||
GetBaseViewInfo()->SetYOrigin(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_X_OFFSET:
|
||||
GetBaseViewInfo()->SetXOffset(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_Y_OFFSET:
|
||||
GetBaseViewInfo()->SetYOffset(value);
|
||||
break;
|
||||
|
||||
case TWEEN_PROPERTY_FONT_SIZE:
|
||||
GetBaseViewInfo()->SetFontSize(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(currentDone)
|
||||
{
|
||||
CurrentTweenIndex++;
|
||||
ElapsedTweenTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(!CurrentTweens || CurrentTweenIndex >= CurrentTweens->size())
|
||||
{
|
||||
if(loop)
|
||||
{
|
||||
CurrentTweenIndex = 0;
|
||||
}
|
||||
completeDone = true;
|
||||
}
|
||||
|
||||
return completeDone;
|
||||
}
|
||||
122
Source/Graphics/Component/Component.h
Normal file
122
Source/Graphics/Component/Component.h
Normal file
@@ -0,0 +1,122 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "../MenuNotifierInterface.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include "../Animate/Tween.h"
|
||||
#include "../../Collection/Item.h"
|
||||
|
||||
class Component
|
||||
{
|
||||
public:
|
||||
Component();
|
||||
virtual ~Component();
|
||||
virtual void FreeGraphicsMemory();
|
||||
virtual void AllocateGraphicsMemory();
|
||||
virtual void LaunchEnter() {}
|
||||
virtual void LaunchExit() {}
|
||||
void TriggerEnterEvent();
|
||||
void TriggerExitEvent();
|
||||
void TriggerHighlightEvent(Item *selectedItem);
|
||||
bool IsIdle();
|
||||
bool IsHidden();
|
||||
bool IsWaiting();
|
||||
typedef std::vector<std::vector<Tween *> *> TweenSets;
|
||||
|
||||
void SetOnEnterTweens(TweenSets *tweens)
|
||||
{
|
||||
this->OnEnterTweens = tweens;
|
||||
}
|
||||
|
||||
void SetOnExitTweens(TweenSets *tweens)
|
||||
{
|
||||
this->OnExitTweens = tweens;
|
||||
}
|
||||
|
||||
void SetOnIdleTweens(TweenSets *tweens)
|
||||
{
|
||||
this->OnIdleTweens = tweens;
|
||||
}
|
||||
|
||||
void SetOnHighlightEnterTweens(TweenSets *tweens)
|
||||
{
|
||||
this->OnHighlightEnterTweens = tweens;
|
||||
}
|
||||
|
||||
void SetOnHighlightExitTweens(TweenSets *tweens)
|
||||
{
|
||||
this->OnHighlightExitTweens = tweens;
|
||||
}
|
||||
virtual void Update(float dt);
|
||||
|
||||
virtual void Draw() = 0;
|
||||
|
||||
ViewInfo *GetBaseViewInfo()
|
||||
{
|
||||
return &BaseViewInfo;
|
||||
}
|
||||
void UpdateBaseViewInfo(ViewInfo &info)
|
||||
{
|
||||
BaseViewInfo = info;
|
||||
}
|
||||
|
||||
bool IsScrollActive() const
|
||||
{
|
||||
return ScrollActive;
|
||||
}
|
||||
|
||||
void SetScrollActive(bool scrollActive)
|
||||
{
|
||||
ScrollActive = scrollActive;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
Item *GetSelectedItem()
|
||||
{
|
||||
return SelectedItem;
|
||||
}
|
||||
enum AnimationState
|
||||
{
|
||||
IDLE,
|
||||
ENTER,
|
||||
HIGHLIGHT_EXIT,
|
||||
HIGHLIGHT_WAIT,
|
||||
HIGHLIGHT_ENTER,
|
||||
EXIT,
|
||||
HIDDEN
|
||||
};
|
||||
|
||||
AnimationState CurrentAnimationState;
|
||||
bool EnterRequested;
|
||||
bool ExitRequested;
|
||||
bool NewItemSelected;
|
||||
bool HighlightExitComplete;
|
||||
bool NewItemSelectedSinceEnter;
|
||||
private:
|
||||
bool Animate(bool loop);
|
||||
bool IsTweenSequencingComplete();
|
||||
void ResetTweenSequence(std::vector<ViewInfo *> *tweens);
|
||||
|
||||
TweenSets *OnEnterTweens;
|
||||
TweenSets *OnExitTweens;
|
||||
TweenSets *OnIdleTweens;
|
||||
TweenSets *OnHighlightEnterTweens;
|
||||
TweenSets *OnHighlightExitTweens;
|
||||
|
||||
TweenSets *CurrentTweens;
|
||||
unsigned int CurrentTweenIndex;
|
||||
|
||||
bool CurrentTweenComplete;
|
||||
ViewInfo BaseViewInfo;
|
||||
|
||||
float ElapsedTweenTime;
|
||||
Tween *TweenInst;
|
||||
Item *SelectedItem;
|
||||
bool ScrollActive;
|
||||
};
|
||||
75
Source/Graphics/Component/Image.cpp
Normal file
75
Source/Graphics/Component/Image.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "Image.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include "../../SDL.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
Image::Image(std::string file, float scaleX, float scaleY)
|
||||
: Texture(NULL)
|
||||
, File(file)
|
||||
, ScaleX(scaleX)
|
||||
, ScaleY(scaleY)
|
||||
{
|
||||
AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
Image::~Image()
|
||||
{
|
||||
FreeGraphicsMemory();
|
||||
}
|
||||
|
||||
void Image::FreeGraphicsMemory()
|
||||
{
|
||||
Component::FreeGraphicsMemory();
|
||||
|
||||
SDL_LockMutex(SDL::GetMutex());
|
||||
if (Texture != NULL)
|
||||
{
|
||||
SDL_DestroyTexture(Texture);
|
||||
Texture = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(SDL::GetMutex());
|
||||
}
|
||||
|
||||
void Image::AllocateGraphicsMemory()
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
|
||||
Component::AllocateGraphicsMemory();
|
||||
|
||||
if(!Texture)
|
||||
{
|
||||
SDL_LockMutex(SDL::GetMutex());
|
||||
Texture = IMG_LoadTexture(SDL::GetRenderer(), File.c_str());
|
||||
|
||||
if (Texture != NULL)
|
||||
{
|
||||
SDL_SetTextureBlendMode(Texture, SDL_BLENDMODE_BLEND);
|
||||
SDL_QueryTexture(Texture, NULL, NULL, &width, &height);
|
||||
GetBaseViewInfo()->SetImageWidth(width * ScaleX);
|
||||
GetBaseViewInfo()->SetImageHeight(height * ScaleY);
|
||||
}
|
||||
SDL_UnlockMutex(SDL::GetMutex());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Image::Draw()
|
||||
{
|
||||
if(Texture)
|
||||
{
|
||||
ViewInfo *info = GetBaseViewInfo();
|
||||
SDL_Rect rect;
|
||||
|
||||
rect.x = static_cast<int>(info->GetXRelativeToOrigin());
|
||||
rect.y = static_cast<int>(info->GetYRelativeToOrigin());
|
||||
rect.h = static_cast<int>(info->GetHeight());
|
||||
rect.w = static_cast<int>(info->GetWidth());
|
||||
|
||||
SDL::RenderCopy(Texture, static_cast<char>((info->GetTransparency() * 255)), NULL, &rect, info->GetAngle());
|
||||
}
|
||||
}
|
||||
24
Source/Graphics/Component/Image.h
Normal file
24
Source/Graphics/Component/Image.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Component.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
|
||||
class Image : public Component
|
||||
{
|
||||
public:
|
||||
Image(std::string file, float scaleX, float scaleY);
|
||||
virtual ~Image();
|
||||
void FreeGraphicsMemory();
|
||||
void AllocateGraphicsMemory();
|
||||
void Draw();
|
||||
|
||||
protected:
|
||||
SDL_Texture *Texture;
|
||||
std::string File;
|
||||
float ScaleX;
|
||||
float ScaleY;
|
||||
};
|
||||
30
Source/Graphics/Component/ImageBuilder.cpp
Normal file
30
Source/Graphics/Component/ImageBuilder.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "ImageBuilder.h"
|
||||
#include "../../Utility/Utils.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include <fstream>
|
||||
|
||||
Image * ImageBuilder::CreateImage(std::string path, std::string name, float scaleX, float scaleY)
|
||||
{
|
||||
Image *image = NULL;
|
||||
std::vector<std::string> extensions;
|
||||
|
||||
extensions.push_back("png");
|
||||
extensions.push_back("PNG");
|
||||
extensions.push_back("jpg");
|
||||
extensions.push_back("JPG");
|
||||
extensions.push_back("jpeg");
|
||||
extensions.push_back("JPEG");
|
||||
|
||||
std::string prefix = path + "/" + name;
|
||||
std::string file;
|
||||
|
||||
if(Utils::FindMatchingFile(prefix, extensions, file))
|
||||
{
|
||||
image = new Image(file, scaleX, scaleY);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
15
Source/Graphics/Component/ImageBuilder.h
Normal file
15
Source/Graphics/Component/ImageBuilder.h
Normal file
@@ -0,0 +1,15 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Image.h"
|
||||
#include "VideoComponent.h"
|
||||
#include "../../Video/VideoFactory.h"
|
||||
|
||||
//todo: this is more of a factory than a builder
|
||||
class ImageBuilder
|
||||
{
|
||||
public:
|
||||
Image * CreateImage(std::string path, std::string name, float scaleX, float scaleY);
|
||||
};
|
||||
172
Source/Graphics/Component/ReloadableMedia.cpp
Normal file
172
Source/Graphics/Component/ReloadableMedia.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "ReloadableMedia.h"
|
||||
#include "ImageBuilder.h"
|
||||
#include "VideoBuilder.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include "../../Video/VideoFactory.h"
|
||||
#include "../../Database/Configuration.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include "../../Utility/Utils.h"
|
||||
#include "../../SDL.h"
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
ReloadableMedia::ReloadableMedia(std::string imagePath, std::string videoPath, bool isVideo, float scaleX, float scaleY)
|
||||
: LoadedComponent(NULL)
|
||||
, ImagePath(imagePath)
|
||||
, VideoPath(videoPath)
|
||||
, ReloadRequested(false)
|
||||
, FirstLoad(true)
|
||||
, IsVideo(isVideo)
|
||||
, ScaleX(scaleX)
|
||||
, ScaleY(scaleY)
|
||||
{
|
||||
AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
ReloadableMedia::~ReloadableMedia()
|
||||
{
|
||||
if (LoadedComponent != NULL)
|
||||
{
|
||||
delete LoadedComponent;
|
||||
}
|
||||
}
|
||||
|
||||
void ReloadableMedia::Update(float dt)
|
||||
{
|
||||
if(NewItemSelected)
|
||||
{
|
||||
ReloadRequested = true;
|
||||
}
|
||||
// wait for the right moment to reload the image
|
||||
if (ReloadRequested && (HighlightExitComplete || FirstLoad))
|
||||
{
|
||||
ReloadTexture();
|
||||
ReloadRequested = false;
|
||||
FirstLoad = false;
|
||||
}
|
||||
|
||||
if(LoadedComponent)
|
||||
{
|
||||
LoadedComponent->Update(dt);
|
||||
}
|
||||
|
||||
// needs to be ran at the end to prevent the NewItemSelected flag from being detected
|
||||
Component::Update(dt);
|
||||
|
||||
}
|
||||
|
||||
void ReloadableMedia::AllocateGraphicsMemory()
|
||||
{
|
||||
FirstLoad = true;
|
||||
|
||||
if(LoadedComponent)
|
||||
{
|
||||
LoadedComponent->AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
// NOTICE! needs to be done last to prevent flags from being missed
|
||||
Component::AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
void ReloadableMedia::LaunchEnter()
|
||||
{
|
||||
if(LoadedComponent)
|
||||
{
|
||||
LoadedComponent->LaunchEnter();
|
||||
}
|
||||
}
|
||||
|
||||
void ReloadableMedia::LaunchExit()
|
||||
{
|
||||
if(LoadedComponent)
|
||||
{
|
||||
LoadedComponent->LaunchExit();
|
||||
}
|
||||
}
|
||||
|
||||
void ReloadableMedia::FreeGraphicsMemory()
|
||||
{
|
||||
Component::FreeGraphicsMemory();
|
||||
|
||||
if(LoadedComponent)
|
||||
{
|
||||
LoadedComponent->FreeGraphicsMemory();
|
||||
}
|
||||
}
|
||||
void ReloadableMedia::ReloadTexture()
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
if(LoadedComponent)
|
||||
{
|
||||
delete LoadedComponent;
|
||||
LoadedComponent = NULL;
|
||||
}
|
||||
|
||||
Item *selectedItem = GetSelectedItem();
|
||||
|
||||
if (selectedItem != NULL)
|
||||
{
|
||||
if(IsVideo)
|
||||
{
|
||||
std::vector<std::string> names;
|
||||
|
||||
names.push_back(selectedItem->GetName());
|
||||
|
||||
if(selectedItem->GetCloneOf().length() > 0)
|
||||
{
|
||||
names.push_back(selectedItem->GetCloneOf());
|
||||
}
|
||||
|
||||
for(unsigned int n = 0; n < names.size() && !found; ++n)
|
||||
{
|
||||
std::string filePrefix;
|
||||
filePrefix.append(VideoPath);
|
||||
filePrefix.append("/");
|
||||
filePrefix.append(names[n]);
|
||||
|
||||
std::string file;
|
||||
|
||||
VideoBuilder videoBuild;
|
||||
|
||||
LoadedComponent = videoBuild.CreateVideo(VideoPath, names[n], ScaleX, ScaleY);
|
||||
|
||||
if(LoadedComponent)
|
||||
{
|
||||
LoadedComponent->AllocateGraphicsMemory();
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!LoadedComponent)
|
||||
{
|
||||
ImageBuilder imageBuild;
|
||||
LoadedComponent = imageBuild.CreateImage(ImagePath, selectedItem->GetFullTitle(), ScaleX, ScaleY);
|
||||
|
||||
if (LoadedComponent != NULL)
|
||||
{
|
||||
LoadedComponent->AllocateGraphicsMemory();
|
||||
GetBaseViewInfo()->SetImageWidth(LoadedComponent->GetBaseViewInfo()->GetImageWidth());
|
||||
GetBaseViewInfo()->SetImageHeight(LoadedComponent->GetBaseViewInfo()->GetImageHeight());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ReloadableMedia::Draw()
|
||||
{
|
||||
ViewInfo *info = GetBaseViewInfo();
|
||||
|
||||
if(LoadedComponent)
|
||||
{
|
||||
info->SetImageHeight(LoadedComponent->GetBaseViewInfo()->GetImageHeight());
|
||||
info->SetImageWidth(LoadedComponent->GetBaseViewInfo()->GetImageWidth());
|
||||
LoadedComponent->UpdateBaseViewInfo(*info);
|
||||
LoadedComponent->Draw();
|
||||
}
|
||||
}
|
||||
38
Source/Graphics/Component/ReloadableMedia.h
Normal file
38
Source/Graphics/Component/ReloadableMedia.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
#include "Component.h"
|
||||
#include "../../Video/IVideo.h"
|
||||
#include "../../Collection/Item.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
|
||||
class Image;
|
||||
|
||||
//todo: this class should aggregate Image, Text, and Video component classes
|
||||
class ReloadableMedia : public Component
|
||||
{
|
||||
public:
|
||||
ReloadableMedia(std::string imagePath, std::string videoPath, bool isVideo, float scaleX, float scaleY);
|
||||
virtual ~ReloadableMedia();
|
||||
void Update(float dt);
|
||||
void Draw();
|
||||
void FreeGraphicsMemory();
|
||||
void AllocateGraphicsMemory();
|
||||
void LaunchEnter();
|
||||
void LaunchExit();
|
||||
|
||||
private:
|
||||
void ReloadTexture();
|
||||
Component *LoadedComponent;
|
||||
std::string ImagePath;
|
||||
std::string VideoPath;
|
||||
bool ReloadRequested;
|
||||
bool FirstLoad;
|
||||
IVideo *VideoInst;
|
||||
|
||||
bool IsVideo;
|
||||
float ScaleX;
|
||||
float ScaleY;
|
||||
};
|
||||
157
Source/Graphics/Component/ReloadableText.cpp
Normal file
157
Source/Graphics/Component/ReloadableText.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "ReloadableText.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include "../../Database/Configuration.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include "../../SDL.h"
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
ReloadableText::ReloadableText(std::string type, Font *font, SDL_Color color, std::string layoutKey, std::string collection, float scaleX, float scaleY)
|
||||
: ImageInst(NULL)
|
||||
, LayoutKey(layoutKey)
|
||||
, Collection(collection)
|
||||
, ReloadRequested(false)
|
||||
, FirstLoad(true)
|
||||
, FontInst(font)
|
||||
, FontColor(color)
|
||||
, ScaleX(scaleX)
|
||||
, ScaleY(scaleY)
|
||||
{
|
||||
|
||||
Type = TextTypeUnknown;
|
||||
|
||||
if(type == "numberButtons")
|
||||
{
|
||||
Type = TextTypeNumberButtons;
|
||||
}
|
||||
else if(type == "numberPlayers")
|
||||
{
|
||||
Type = TextTypeNumberPlayers;
|
||||
}
|
||||
else if(type == "year")
|
||||
{
|
||||
Type = TextTypeYear;
|
||||
}
|
||||
else if(type == "title")
|
||||
{
|
||||
Type = TextTypeTitle;
|
||||
}
|
||||
else if(type == "manufacturer")
|
||||
{
|
||||
Type = TextTypeManufacturer;
|
||||
}
|
||||
|
||||
AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
|
||||
|
||||
ReloadableText::~ReloadableText()
|
||||
{
|
||||
if (ImageInst != NULL)
|
||||
{
|
||||
delete ImageInst;
|
||||
}
|
||||
}
|
||||
|
||||
void ReloadableText::Update(float dt)
|
||||
{
|
||||
if(NewItemSelected)
|
||||
{
|
||||
ReloadRequested = true;
|
||||
}
|
||||
// wait for the right moment to reload the image
|
||||
if (ReloadRequested && (HighlightExitComplete || FirstLoad))
|
||||
{
|
||||
ReloadTexture();
|
||||
ReloadRequested = false;
|
||||
FirstLoad = false;
|
||||
}
|
||||
|
||||
// needs to be ran at the end to prevent the NewItemSelected flag from being detected
|
||||
Component::Update(dt);
|
||||
|
||||
}
|
||||
|
||||
void ReloadableText::AllocateGraphicsMemory()
|
||||
{
|
||||
FirstLoad = true;
|
||||
|
||||
ReloadTexture();
|
||||
|
||||
// NOTICE! needs to be done last to prevent flags from being missed
|
||||
Component::AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
void ReloadableText::LaunchEnter()
|
||||
{
|
||||
}
|
||||
|
||||
void ReloadableText::LaunchExit()
|
||||
{
|
||||
}
|
||||
|
||||
void ReloadableText::FreeGraphicsMemory()
|
||||
{
|
||||
Component::FreeGraphicsMemory();
|
||||
|
||||
if (ImageInst != NULL)
|
||||
{
|
||||
delete ImageInst;
|
||||
ImageInst = NULL;
|
||||
}
|
||||
}
|
||||
void ReloadableText::ReloadTexture()
|
||||
{
|
||||
if (ImageInst != NULL)
|
||||
{
|
||||
delete ImageInst;
|
||||
ImageInst = NULL;
|
||||
}
|
||||
|
||||
Item *selectedItem = GetSelectedItem();
|
||||
|
||||
if (selectedItem != NULL)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::string text;
|
||||
switch(Type)
|
||||
{
|
||||
case TextTypeNumberButtons:
|
||||
ss << selectedItem->GetNumberButtons();
|
||||
break;
|
||||
case TextTypeNumberPlayers:
|
||||
ss << selectedItem->GetNumberPlayers();
|
||||
break;
|
||||
case TextTypeYear:
|
||||
ss << selectedItem->GetYear();
|
||||
break;
|
||||
case TextTypeTitle:
|
||||
ss << selectedItem->GetTitle();
|
||||
break;
|
||||
case TextTypeManufacturer:
|
||||
ss << selectedItem->GetManufacturer();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
ImageInst = new Text(ss.str(), FontInst, FontColor, ScaleX, ScaleY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ReloadableText::Draw()
|
||||
{
|
||||
ViewInfo *info = GetBaseViewInfo();
|
||||
|
||||
if(ImageInst)
|
||||
{
|
||||
ImageInst->UpdateBaseViewInfo(*info);
|
||||
ImageInst->Draw();
|
||||
}
|
||||
}
|
||||
48
Source/Graphics/Component/ReloadableText.h
Normal file
48
Source/Graphics/Component/ReloadableText.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
#include "Component.h"
|
||||
#include "Text.h"
|
||||
#include "../Font.h"
|
||||
#include "../../Collection/Item.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
|
||||
class ReloadableText : public Component
|
||||
{
|
||||
public:
|
||||
ReloadableText(std::string type, Font *font, SDL_Color color, std::string layoutKey, std::string collectionName, float scaleX, float scaleY);
|
||||
virtual ~ReloadableText();
|
||||
void Update(float dt);
|
||||
void Draw();
|
||||
void FreeGraphicsMemory();
|
||||
void AllocateGraphicsMemory();
|
||||
void LaunchEnter();
|
||||
void LaunchExit();
|
||||
|
||||
private:
|
||||
enum TextType
|
||||
{
|
||||
TextTypeUnknown = 0,
|
||||
TextTypeNumberButtons,
|
||||
TextTypeNumberPlayers,
|
||||
TextTypeYear,
|
||||
TextTypeTitle,
|
||||
TextTypeManufacturer,
|
||||
};
|
||||
|
||||
void ReloadTexture();
|
||||
|
||||
Text *ImageInst;
|
||||
TextType Type;
|
||||
std::string LayoutKey;
|
||||
std::string Collection;
|
||||
bool ReloadRequested;
|
||||
bool FirstLoad;
|
||||
Font *FontInst;
|
||||
SDL_Color FontColor;
|
||||
|
||||
float ScaleX;
|
||||
float ScaleY;
|
||||
};
|
||||
654
Source/Graphics/Component/ScrollingList.cpp
Normal file
654
Source/Graphics/Component/ScrollingList.cpp
Normal file
@@ -0,0 +1,654 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "../Animate/Tween.h"
|
||||
#include "../Animate/TweenTypes.h"
|
||||
#include "../ComponentItemBinding.h"
|
||||
#include "../Font.h"
|
||||
#include "ScrollingList.h"
|
||||
#include "ImageBuilder.h"
|
||||
#include "VideoBuilder.h"
|
||||
#include "VideoComponent.h"
|
||||
#include "ReloadableMedia.h"
|
||||
#include "Text.h"
|
||||
#include "../../Database/Configuration.h" // todo: decouple the GUI from the data
|
||||
#include "../../Collection/Item.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include "../../SDL.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include <math.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
//todo: remove coupling from configuration data (if possible)
|
||||
ScrollingList::ScrollingList(Configuration *c,
|
||||
float scaleX,
|
||||
float scaleY,
|
||||
Font *font,
|
||||
SDL_Color fontColor,
|
||||
std::string layoutKey,
|
||||
std::string collectionName,
|
||||
std::string imageType)
|
||||
: IsScrollChangedStarted(true)
|
||||
, IsScrollChangedSignalled(false)
|
||||
, IsScrollChangedComplete(false)
|
||||
, SpriteList(NULL)
|
||||
, ScrollPoints(NULL)
|
||||
, TweenEnterTime(0)
|
||||
, FirstSpriteIndex(0)
|
||||
, SelectedSpriteListIndex(0)
|
||||
, CurrentAnimateTime(0) // in seconds
|
||||
, ScrollTime(0) // in seconds
|
||||
, CurrentScrollDirection(ScrollDirectionIdle)
|
||||
, RequestedScrollDirection(ScrollDirectionIdle)
|
||||
, CurrentScrollState(ScrollStateIdle)
|
||||
, ScrollAcceleration(6) // todo: make configurable
|
||||
, ScrollVelocity(0)
|
||||
, Config(c)
|
||||
, ScaleX(scaleX)
|
||||
, ScaleY(scaleY)
|
||||
, FontInst(font)
|
||||
, FontColor(fontColor)
|
||||
, LayoutKey(layoutKey)
|
||||
, CollectionName(collectionName)
|
||||
, ImageType(imageType)
|
||||
, MaxLayer(0)
|
||||
{
|
||||
}
|
||||
|
||||
ScrollingList::~ScrollingList()
|
||||
{
|
||||
}
|
||||
|
||||
void ScrollingList::SetItems(std::vector<ComponentItemBinding *> *spriteList)
|
||||
{
|
||||
SpriteList = spriteList;
|
||||
FirstSpriteIndex = 0;
|
||||
|
||||
// loop the scroll points if there are not enough
|
||||
unsigned int originalSize = SpriteList->size();
|
||||
|
||||
while(ScrollPoints && ScrollPoints->size()+4 > SpriteList->size())
|
||||
{
|
||||
for(unsigned int i = 0; i < originalSize; ++i)
|
||||
{
|
||||
Item *newItem = new Item();
|
||||
Item *originalItem = SpriteList->at(i)->GetCollectionItem();
|
||||
|
||||
*newItem = *originalItem;
|
||||
ComponentItemBinding *newSprite = new ComponentItemBinding(newItem);
|
||||
SpriteList->push_back(newSprite);
|
||||
}
|
||||
}
|
||||
for(unsigned int i = 0; SpriteList && ScrollPoints && i < SelectedSpriteListIndex; ++i)
|
||||
{
|
||||
CircularDecrement(FirstSpriteIndex, SpriteList);
|
||||
}
|
||||
|
||||
IsScrollChangedComplete = true;
|
||||
}
|
||||
|
||||
void ScrollingList::SetPoints(std::vector<ViewInfo *> *scrollPoints)
|
||||
{
|
||||
ScrollPoints = scrollPoints;
|
||||
for(unsigned int i = 0; i != scrollPoints->size(); ++i)
|
||||
{
|
||||
ViewInfo *info = scrollPoints->at(i);
|
||||
MaxLayer = (MaxLayer < info->GetLayer()) ? MaxLayer : info->GetLayer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ScrollingList::SetSelectedIndex(int selectedIndex)
|
||||
{
|
||||
SelectedSpriteListIndex = selectedIndex;
|
||||
|
||||
for(unsigned int i = 0; SpriteList && ScrollPoints && i < SelectedSpriteListIndex; ++i)
|
||||
{
|
||||
CircularDecrement(FirstSpriteIndex, SpriteList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ScrollingList::Click()
|
||||
{
|
||||
if(CurrentScrollDirection == ScrollDirectionBack)
|
||||
{
|
||||
CircularDecrement(FirstSpriteIndex, SpriteList);
|
||||
IsScrollChangedComplete = true;
|
||||
}
|
||||
if(CurrentScrollDirection == ScrollDirectionForward)
|
||||
{
|
||||
CircularIncrement(FirstSpriteIndex, SpriteList);
|
||||
IsScrollChangedComplete = true;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int ScrollingList::GetNextTween(unsigned int currentIndex, std::vector<ViewInfo *> *list)
|
||||
{
|
||||
if(CurrentScrollDirection == ScrollDirectionForward)
|
||||
{
|
||||
CircularDecrement(currentIndex, list);
|
||||
}
|
||||
else if(CurrentScrollDirection == ScrollDirectionBack)
|
||||
{
|
||||
CircularIncrement(currentIndex, list);
|
||||
}
|
||||
|
||||
return currentIndex;
|
||||
}
|
||||
|
||||
void ScrollingList::PageUp()
|
||||
{
|
||||
if(ScrollPoints && ScrollPoints->size() > 4)
|
||||
{
|
||||
ScrollVelocity = 0;
|
||||
unsigned int counts = ScrollPoints->size() - 4;
|
||||
|
||||
for(unsigned int i = 0; i < counts; i++)
|
||||
{
|
||||
CircularDecrement(FirstSpriteIndex, SpriteList);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentScrollState = ScrollStatePageChange;
|
||||
IsScrollChangedStarted = true;
|
||||
IsScrollChangedSignalled = false;
|
||||
IsScrollChangedComplete = false;
|
||||
}
|
||||
|
||||
void ScrollingList::PageDown()
|
||||
{
|
||||
if(ScrollPoints && ScrollPoints->size() > 4)
|
||||
{
|
||||
unsigned int counts = ScrollPoints->size() - 4;
|
||||
|
||||
ScrollVelocity = 0;
|
||||
for(unsigned int i = 0; i < counts; i++)
|
||||
{
|
||||
CircularIncrement(FirstSpriteIndex, SpriteList);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentScrollState = ScrollStatePageChange;
|
||||
IsScrollChangedStarted = true;
|
||||
IsScrollChangedSignalled = false;
|
||||
IsScrollChangedComplete = false;
|
||||
}
|
||||
|
||||
void ScrollingList::FreeGraphicsMemory()
|
||||
{
|
||||
Component::FreeGraphicsMemory();
|
||||
TweenEnterTime = 0;
|
||||
CurrentAnimateTime = 0;
|
||||
ScrollTime = 0;
|
||||
CurrentScrollDirection = ScrollDirectionIdle;
|
||||
RequestedScrollDirection = ScrollDirectionIdle;
|
||||
CurrentScrollState = ScrollStateIdle;
|
||||
ScrollAcceleration = 6; // todo: make configurable
|
||||
ScrollVelocity = 0;
|
||||
|
||||
for(unsigned int i = 0; i < SpriteList->size(); i++)
|
||||
{
|
||||
ComponentItemBinding *s = SpriteList->at(i);
|
||||
|
||||
DeallocateTexture(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ScrollingList::Update(float dt)
|
||||
{
|
||||
float scrollPeriod = 0;
|
||||
Component::Update(dt);
|
||||
|
||||
if(!ScrollPoints)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch(CurrentScrollState)
|
||||
{
|
||||
case ScrollStateActive:
|
||||
if(RequestedScrollDirection != CurrentScrollDirection)
|
||||
{
|
||||
CurrentScrollState = ScrollStateStopping;
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ScrollStateIdle:
|
||||
ScrollTime = 0;
|
||||
CurrentAnimateTime = 0;
|
||||
ScrollVelocity = 0;
|
||||
|
||||
if(RequestedScrollDirection != ScrollDirectionIdle)
|
||||
{
|
||||
CurrentScrollState = ScrollStateActive;
|
||||
CurrentScrollDirection = RequestedScrollDirection;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
};
|
||||
|
||||
if(CurrentScrollState != ScrollStatePageChange && CurrentScrollState != ScrollStateIdle)
|
||||
{
|
||||
IsScrollChangedStarted = true;
|
||||
ScrollTime += dt;
|
||||
CurrentAnimateTime += dt;
|
||||
ScrollVelocity = ScrollTime * ScrollAcceleration;
|
||||
|
||||
// clip at 5 items scrolled per second
|
||||
if(ScrollVelocity > 30)
|
||||
{
|
||||
ScrollVelocity = 30;
|
||||
}
|
||||
if(ScrollVelocity > 0)
|
||||
{
|
||||
scrollPeriod = 1 / ScrollVelocity;
|
||||
}
|
||||
|
||||
// have we exceeded the time of when to stop on the next item in the list?
|
||||
if(CurrentScrollState == ScrollStateStopping && CurrentAnimateTime >= scrollPeriod)
|
||||
{
|
||||
Click();
|
||||
CurrentAnimateTime = 0;
|
||||
ScrollVelocity = 0;
|
||||
CurrentScrollState = ScrollStateIdle;
|
||||
}
|
||||
}
|
||||
|
||||
while(CurrentScrollState != ScrollStatePageChange && ScrollVelocity > 0 && CurrentAnimateTime >= scrollPeriod)
|
||||
{
|
||||
Click();
|
||||
CurrentAnimateTime -= scrollPeriod;
|
||||
}
|
||||
|
||||
|
||||
if(ScrollPoints && SpriteList->size() > 0 && FirstSpriteIndex < SpriteList->size())
|
||||
{
|
||||
unsigned int spriteIndex = FirstSpriteIndex;
|
||||
unsigned int numIterations = (ScrollPoints->size() > SpriteList->size()) ? SpriteList->size() : ScrollPoints->size();
|
||||
unsigned int start = (ScrollPoints->size() > SpriteList->size()) ? SelectedSpriteListIndex : 0;
|
||||
|
||||
for(unsigned int i = start; i < start+numIterations; i++)
|
||||
{
|
||||
ComponentItemBinding *s = SpriteList->at(spriteIndex);
|
||||
unsigned int nextI = GetNextTween(i, ScrollPoints);
|
||||
|
||||
ViewInfo *currentViewInfo = ScrollPoints->at(i);
|
||||
ViewInfo *nextViewInfo = ScrollPoints->at(nextI);
|
||||
|
||||
AllocateTexture(s);
|
||||
|
||||
Component *c = s->GetComponent();
|
||||
if(c)
|
||||
{
|
||||
currentViewInfo->SetImageHeight(c->GetBaseViewInfo()->GetImageHeight());
|
||||
currentViewInfo->SetImageWidth(c->GetBaseViewInfo()->GetImageWidth());
|
||||
nextViewInfo->SetImageHeight(c->GetBaseViewInfo()->GetImageHeight());
|
||||
nextViewInfo->SetImageWidth(c->GetBaseViewInfo()->GetImageWidth());
|
||||
|
||||
//todo: 30 is a magic number
|
||||
ViewInfo *spriteViewInfo = c->GetBaseViewInfo();
|
||||
|
||||
spriteViewInfo->SetX(Tween::AnimateSingle(LINEAR, currentViewInfo->GetX(), nextViewInfo->GetX(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetY(Tween::AnimateSingle(LINEAR, currentViewInfo->GetY(), nextViewInfo->GetY(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetXOrigin(Tween::AnimateSingle(LINEAR, currentViewInfo->GetXOrigin(), nextViewInfo->GetXOrigin(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetYOrigin(Tween::AnimateSingle(LINEAR, currentViewInfo->GetYOrigin(), nextViewInfo->GetYOrigin(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetXOffset(Tween::AnimateSingle(LINEAR, currentViewInfo->GetXOffset(), nextViewInfo->GetXOffset(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetYOffset(Tween::AnimateSingle(LINEAR, currentViewInfo->GetYOffset(), nextViewInfo->GetYOffset(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetHeight(Tween::AnimateSingle(LINEAR, currentViewInfo->GetHeight(), nextViewInfo->GetHeight(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetWidth(Tween::AnimateSingle(LINEAR, currentViewInfo->GetWidth(), nextViewInfo->GetWidth(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetTransparency(Tween::AnimateSingle(LINEAR, currentViewInfo->GetTransparency(), nextViewInfo->GetTransparency(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetAngle(Tween::AnimateSingle(LINEAR, currentViewInfo->GetAngle(), nextViewInfo->GetAngle(), scrollPeriod, CurrentAnimateTime));
|
||||
spriteViewInfo->SetFontSize(Tween::AnimateSingle(LINEAR, currentViewInfo->GetFontSize(), nextViewInfo->GetFontSize(), scrollPeriod, CurrentAnimateTime));
|
||||
c->Update(dt);
|
||||
|
||||
}
|
||||
|
||||
CircularIncrement(spriteIndex, SpriteList);
|
||||
}
|
||||
|
||||
// start freeing up memory if the list is too large
|
||||
if(SpriteList->size() + 4 > ScrollPoints->size())
|
||||
{
|
||||
spriteIndex = FirstSpriteIndex;
|
||||
|
||||
CircularDecrement(spriteIndex, SpriteList);
|
||||
DeallocateTexture(SpriteList->at(spriteIndex));
|
||||
|
||||
CircularDecrement(spriteIndex, SpriteList);
|
||||
DeallocateTexture(SpriteList->at(spriteIndex));
|
||||
|
||||
|
||||
// point to the end of the list to start deallocating..
|
||||
// It's not fast, but it's easy to read
|
||||
spriteIndex = FirstSpriteIndex;
|
||||
for(unsigned int i = 0; i < ScrollPoints->size(); i++)
|
||||
{
|
||||
CircularIncrement(spriteIndex, SpriteList);
|
||||
}
|
||||
|
||||
CircularIncrement(spriteIndex, SpriteList);
|
||||
DeallocateTexture(SpriteList->at(spriteIndex));
|
||||
|
||||
CircularIncrement(spriteIndex, SpriteList);
|
||||
DeallocateTexture(SpriteList->at(spriteIndex));
|
||||
}
|
||||
}
|
||||
|
||||
if(IsScrollChangedStarted && !IsScrollChangedSignalled)
|
||||
{
|
||||
IsScrollChangedSignalled = true;
|
||||
ComponentItemBinding *sprite = GetPendingCollectionItemSprite();
|
||||
Item *item = NULL;
|
||||
|
||||
if(sprite)
|
||||
{
|
||||
item = sprite->GetCollectionItem();
|
||||
}
|
||||
|
||||
for(std::vector<MenuNotifierInterface *>::iterator it = NotificationComponents.begin();
|
||||
it != NotificationComponents.end();
|
||||
it++)
|
||||
{
|
||||
MenuNotifierInterface *c = *it;
|
||||
if(c && item)
|
||||
{
|
||||
c->OnNewItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
if(CurrentScrollState == ScrollStatePageChange)
|
||||
{
|
||||
IsScrollChangedComplete = true;
|
||||
CurrentScrollState = ScrollStateIdle;
|
||||
}
|
||||
}
|
||||
|
||||
if(IsScrollChangedStarted && IsScrollChangedSignalled && IsScrollChangedComplete)
|
||||
{
|
||||
IsScrollChangedStarted = false;
|
||||
IsScrollChangedComplete = false;
|
||||
IsScrollChangedSignalled = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ScrollingList::AllocateTexture(ComponentItemBinding *s)
|
||||
{
|
||||
//todo: move this outside of the Draw routine
|
||||
if(s && s->GetComponent() == NULL)
|
||||
{
|
||||
const Item *item = s->GetCollectionItem();
|
||||
//todo: will create a runtime fault if not of the right type
|
||||
//todo: remove coupling from knowing the collection name
|
||||
|
||||
std::string collectionKey ="collections." + CollectionName + ".media." + ImageType;
|
||||
std::string videoKey ="collections." + CollectionName + ".media.video";
|
||||
std::string imagePath;
|
||||
std::string videoPath;
|
||||
|
||||
Component *t = NULL;
|
||||
|
||||
/*
|
||||
// todo: to be supported at a later date
|
||||
if(c->GetProperty(videoKey, videoPath))
|
||||
{
|
||||
t = new VideoComponent(videoPath, item->GetFullTitle(), ScaleX, ScaleY);
|
||||
}
|
||||
*/
|
||||
if(!t && Config->GetPropertyAbsolutePath(collectionKey, imagePath))
|
||||
{
|
||||
ImageBuilder imageBuild;
|
||||
t = imageBuild.CreateImage(imagePath, item->GetName(), ScaleX, ScaleY);
|
||||
|
||||
if(!t && item->GetTitle() != item->GetFullTitle())
|
||||
{
|
||||
t = imageBuild.CreateImage(imagePath, item->GetFullTitle(), ScaleX, ScaleY);
|
||||
}
|
||||
}
|
||||
if (!t)
|
||||
{
|
||||
t = new Text(item->GetTitle(), FontInst, FontColor, ScaleX, ScaleY);
|
||||
}
|
||||
|
||||
if(t)
|
||||
{
|
||||
s->SetComponent(t);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ScrollingList::DeallocateTexture(ComponentItemBinding *s)
|
||||
{
|
||||
if(s && s->GetComponent() != NULL)
|
||||
{
|
||||
delete s->GetComponent();
|
||||
}
|
||||
s->SetComponent(NULL);
|
||||
}
|
||||
|
||||
void ScrollingList::Draw()
|
||||
{
|
||||
//todo: Poor design implementation.
|
||||
// caller should instead call ScrollingList::Draw(unsigned int layer)
|
||||
}
|
||||
|
||||
//todo: this is kind of a hack. Aggregation needs to happen differently
|
||||
void ScrollingList::Draw(unsigned int layer)
|
||||
{
|
||||
if(ScrollPoints && SpriteList && SpriteList->size() > 0 && FirstSpriteIndex < SpriteList->size())
|
||||
{
|
||||
unsigned int spriteIndex = FirstSpriteIndex;
|
||||
|
||||
for(unsigned int i = 0; i < ScrollPoints->size(); i++)
|
||||
{
|
||||
std::vector<ComponentItemBinding *>::iterator it = SpriteList->begin() + spriteIndex;
|
||||
Component *c = (*it)->GetComponent();
|
||||
ViewInfo *currentViewInfo = ScrollPoints->at(i);
|
||||
|
||||
if(currentViewInfo && currentViewInfo->GetLayer() == layer)
|
||||
{
|
||||
c->Draw();
|
||||
}
|
||||
CircularIncrement(spriteIndex, SpriteList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ScrollingList::SetScrollDirection(ScrollDirection direction)
|
||||
{
|
||||
RequestedScrollDirection = direction;
|
||||
}
|
||||
|
||||
void ScrollingList::RemoveSelectedItem()
|
||||
{
|
||||
ComponentItemBinding *sprite = GetSelectedCollectionItemSprite();
|
||||
if(sprite)
|
||||
{
|
||||
Item *item = sprite->GetCollectionItem();
|
||||
DeallocateTexture(sprite);
|
||||
int index = (FirstSpriteIndex + SelectedSpriteListIndex) % SpriteList->size();
|
||||
|
||||
std::vector<ComponentItemBinding *>::iterator it = SpriteList->begin() + index;
|
||||
|
||||
SpriteList->erase(it);
|
||||
delete sprite;
|
||||
|
||||
if(item)
|
||||
{
|
||||
delete item;
|
||||
}
|
||||
|
||||
if(SelectedSpriteListIndex >= SpriteList->size())
|
||||
{
|
||||
SelectedSpriteListIndex = 0;
|
||||
}
|
||||
if(FirstSpriteIndex >= SpriteList->size())
|
||||
{
|
||||
FirstSpriteIndex = 0;
|
||||
}
|
||||
}
|
||||
IsScrollChangedComplete = true;
|
||||
}
|
||||
|
||||
|
||||
std::vector<ComponentItemBinding *> *ScrollingList::GetCollectionItemSprites()
|
||||
{
|
||||
return SpriteList;
|
||||
}
|
||||
|
||||
ComponentItemBinding* ScrollingList::GetSelectedCollectionItemSprite()
|
||||
{
|
||||
ComponentItemBinding *item = NULL;
|
||||
|
||||
if(SpriteList && SpriteList->size() > 0)
|
||||
{
|
||||
int index = (FirstSpriteIndex + SelectedSpriteListIndex) % SpriteList->size();
|
||||
|
||||
item = SpriteList->at(index);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
ComponentItemBinding* ScrollingList::GetPendingCollectionItemSprite()
|
||||
{
|
||||
ComponentItemBinding *item = NULL;
|
||||
unsigned int index = FirstSpriteIndex;
|
||||
if(CurrentScrollState != ScrollStatePageChange)
|
||||
{
|
||||
if(CurrentScrollDirection == ScrollDirectionBack)
|
||||
{
|
||||
CircularDecrement(index, SpriteList);
|
||||
}
|
||||
if(CurrentScrollDirection == ScrollDirectionForward)
|
||||
{
|
||||
CircularIncrement(index, SpriteList);
|
||||
}
|
||||
}
|
||||
if(SpriteList && SpriteList->size() > 0)
|
||||
{
|
||||
index = (index + SelectedSpriteListIndex) % SpriteList->size();
|
||||
|
||||
item = SpriteList->at(index);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
void ScrollingList::AddComponentForNotifications(MenuNotifierInterface *c)
|
||||
{
|
||||
NotificationComponents.push_back(c);
|
||||
}
|
||||
void ScrollingList::RemoveComponentForNotifications(MenuNotifierInterface *c)
|
||||
{
|
||||
for(std::vector<MenuNotifierInterface *>::iterator it = NotificationComponents.begin();
|
||||
it != NotificationComponents.end();
|
||||
it++)
|
||||
{
|
||||
if(c == *it)
|
||||
{
|
||||
NotificationComponents.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ComponentItemBinding* ScrollingList::GetPendingSelectedCollectionItemSprite()
|
||||
{
|
||||
ComponentItemBinding *item = NULL;
|
||||
|
||||
unsigned int index = SelectedSpriteListIndex;
|
||||
|
||||
if(CurrentScrollDirection == ScrollDirectionBack)
|
||||
{
|
||||
CircularDecrement(index, SpriteList);
|
||||
}
|
||||
if(CurrentScrollDirection == ScrollDirectionForward)
|
||||
{
|
||||
CircularIncrement(index, SpriteList);
|
||||
}
|
||||
|
||||
if(SpriteList && SpriteList->size() > 0)
|
||||
{
|
||||
index = (index + SelectedSpriteListIndex) % SpriteList->size();
|
||||
|
||||
item = SpriteList->at(index);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
bool ScrollingList::IsIdle()
|
||||
{
|
||||
return (Component::IsIdle() && CurrentScrollState == ScrollStateIdle);
|
||||
}
|
||||
|
||||
void ScrollingList::CircularIncrement(unsigned int &index, std::vector<ViewInfo*>* list)
|
||||
{
|
||||
index++;
|
||||
|
||||
if(index >= list->size())
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ScrollingList::CircularDecrement(unsigned int &index, std::vector<ViewInfo*>* list)
|
||||
{
|
||||
if(index > 0)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(list->size() > 0)
|
||||
{
|
||||
index = list->size() - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ScrollingList::CircularIncrement(unsigned int &index, std::vector<ComponentItemBinding*> *list)
|
||||
{
|
||||
index++;
|
||||
|
||||
if(index >= list->size())
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
void ScrollingList::CircularDecrement(unsigned int &index, std::vector<ComponentItemBinding*> *list)
|
||||
{
|
||||
if(index > 0)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(list && list->size() > 0)
|
||||
{
|
||||
index = list->size() - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
105
Source/Graphics/Component/ScrollingList.h
Normal file
105
Source/Graphics/Component/ScrollingList.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "Component.h"
|
||||
#include "../Animate/Tween.h"
|
||||
#include "../ComponentItemBinding.h"
|
||||
#include "../MenuNotifierInterface.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include "../../Database/Configuration.h"
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
|
||||
//todo: This scrolling implementation needs to be overhauled
|
||||
// It needs to have a common interface to support different menu types
|
||||
// (It was originally sandbox code that creeped into here)
|
||||
|
||||
class Configuration;
|
||||
class Font;
|
||||
|
||||
class ScrollingList : public Component
|
||||
{
|
||||
public:
|
||||
enum ScrollDirection
|
||||
{
|
||||
ScrollDirectionBack,
|
||||
ScrollDirectionForward,
|
||||
ScrollDirectionIdle,
|
||||
|
||||
};
|
||||
|
||||
ScrollingList(Configuration *c, float scaleX, float scaleY, Font *font, SDL_Color fontColor, std::string layoutKey, std::string CollectionName, std::string imageType);
|
||||
virtual ~ScrollingList();
|
||||
void AllocateTexture(ComponentItemBinding *s);
|
||||
void DeallocateTexture(ComponentItemBinding *s);
|
||||
void SetItems(std::vector<ComponentItemBinding *> *spriteList);
|
||||
void SetPoints(std::vector<ViewInfo *> *scrollPoints);
|
||||
void SetScrollDirection(ScrollDirection direction);
|
||||
void PageUp();
|
||||
void PageDown();
|
||||
bool IsIdle();
|
||||
void SetSelectedIndex(int selectedIndex);
|
||||
ComponentItemBinding *GetSelectedCollectionItemSprite();
|
||||
ComponentItemBinding *GetPendingCollectionItemSprite();
|
||||
ComponentItemBinding *GetPendingSelectedCollectionItemSprite();
|
||||
void AddComponentForNotifications(MenuNotifierInterface *c);
|
||||
void RemoveComponentForNotifications(MenuNotifierInterface *c);
|
||||
std::vector<ComponentItemBinding *> *GetCollectionItemSprites();
|
||||
void RemoveSelectedItem();
|
||||
void FreeGraphicsMemory();
|
||||
void Update(float dt);
|
||||
void Draw();
|
||||
void Draw(unsigned int layer);
|
||||
|
||||
private:
|
||||
void Click();
|
||||
unsigned int GetNextTween(unsigned int currentIndex, std::vector<ViewInfo *> *list);
|
||||
bool IsScrollChangedStarted;
|
||||
bool IsScrollChangedSignalled;
|
||||
bool IsScrollChangedComplete;
|
||||
|
||||
enum ScrollState
|
||||
{
|
||||
ScrollStateActive,
|
||||
ScrollStatePageChange,
|
||||
ScrollStateStopping,
|
||||
ScrollStateIdle
|
||||
};
|
||||
|
||||
std::vector<ComponentItemBinding *> *SpriteList;
|
||||
std::vector<ViewInfo *> *ScrollPoints;
|
||||
std::vector<MenuNotifierInterface *> NotificationComponents;
|
||||
float TweenEnterTime;
|
||||
|
||||
unsigned int FirstSpriteIndex;
|
||||
unsigned int SelectedSpriteListIndex;
|
||||
float CurrentAnimateTime;
|
||||
float ScrollTime;
|
||||
|
||||
ScrollDirection CurrentScrollDirection;
|
||||
ScrollDirection RequestedScrollDirection;
|
||||
ScrollState CurrentScrollState;
|
||||
float ScrollAcceleration;
|
||||
float ScrollVelocity;
|
||||
|
||||
void CircularIncrement(unsigned &index, std::vector<ComponentItemBinding *> *list);
|
||||
void CircularDecrement(unsigned &index, std::vector<ComponentItemBinding *> *list);
|
||||
void CircularIncrement(unsigned &index, std::vector<ViewInfo *> *list);
|
||||
void CircularDecrement(unsigned &index, std::vector<ViewInfo *> *list);
|
||||
void UpdateOffset(float dt);
|
||||
|
||||
std::string Collection;
|
||||
Configuration *Config;
|
||||
float ScaleX;
|
||||
float ScaleY;
|
||||
Font *FontInst;
|
||||
SDL_Color FontColor;
|
||||
std::string LayoutKey;
|
||||
std::string CollectionName;
|
||||
std::string ImageType;
|
||||
unsigned int MaxLayer;
|
||||
};
|
||||
|
||||
103
Source/Graphics/Component/Text.cpp
Normal file
103
Source/Graphics/Component/Text.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "Text.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include "../../SDL.h"
|
||||
#include "../Font.h"
|
||||
#include <sstream>
|
||||
|
||||
Text::Text(std::string text, Font *font, SDL_Color fontColor, float scaleX, float scaleY)
|
||||
: TextData(text)
|
||||
, FontInst(font)
|
||||
, FontColor(fontColor)
|
||||
, ScaleX(scaleX)
|
||||
, ScaleY(scaleY)
|
||||
{
|
||||
AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
Text::~Text()
|
||||
{
|
||||
FreeGraphicsMemory();
|
||||
}
|
||||
|
||||
|
||||
void Text::FreeGraphicsMemory()
|
||||
{
|
||||
Component::FreeGraphicsMemory();
|
||||
}
|
||||
|
||||
void Text::AllocateGraphicsMemory()
|
||||
{
|
||||
//todo: make the font blend color a parameter that is passed in
|
||||
Component::AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
void Text::Draw()
|
||||
{
|
||||
SDL_Texture *t = FontInst->GetTexture();
|
||||
|
||||
ViewInfo *info = GetBaseViewInfo();
|
||||
float imageHeight = 0;
|
||||
float imageWidth = 0;
|
||||
|
||||
// determine image width
|
||||
for(unsigned int i = 0; i < TextData.size(); ++i)
|
||||
{
|
||||
Font::GlyphInfo glyph;
|
||||
if(FontInst->GetRect(TextData[i], glyph))
|
||||
{
|
||||
imageWidth += glyph.Advance;
|
||||
imageHeight = (imageHeight >= glyph.Rect.h) ? imageHeight : glyph.Rect.h;
|
||||
}
|
||||
}
|
||||
|
||||
float scale = (float)info->GetFontSize() / (float)imageHeight;
|
||||
|
||||
|
||||
float width = info->GetRawWidth();
|
||||
float height = info->GetRawHeight();
|
||||
|
||||
info->SetWidth(imageWidth*scale);
|
||||
info->SetHeight(imageHeight*scale);
|
||||
|
||||
float xOrigin = info->GetXRelativeToOrigin();
|
||||
float yOrigin = info->GetYRelativeToOrigin();
|
||||
|
||||
info->SetWidth(width);
|
||||
info->SetHeight(height);
|
||||
|
||||
|
||||
SDL_Rect rect;
|
||||
rect.x = static_cast<int>(xOrigin);
|
||||
|
||||
for(unsigned int i = 0; i < TextData.size(); ++i)
|
||||
{
|
||||
Font::GlyphInfo glyph;
|
||||
|
||||
if(FontInst->GetRect(TextData[i], glyph) && glyph.Rect.h > 0)
|
||||
{
|
||||
SDL_Rect charRect = glyph.Rect;
|
||||
float h = static_cast<float>(charRect.h * scale);
|
||||
float w = static_cast<float>(charRect.w * scale);
|
||||
rect.h = static_cast<int>(h);
|
||||
rect.w = static_cast<int>(w);
|
||||
rect.y = static_cast<int>(yOrigin);
|
||||
/*
|
||||
std::stringstream ss;
|
||||
ss << " cx:" << charRect.x << " cy:" << charRect.y << " cw:" << charRect.w << " ch:" << charRect.h;
|
||||
ss << " x:" << rect.x << " y:" << rect.y << " w:" << rect.w << " h:" << rect.h;
|
||||
Logger::Write(Logger::ZONE_DEBUG, "Text", ss.str());
|
||||
*/
|
||||
|
||||
SDL_LockMutex(SDL::GetMutex());
|
||||
SDL_SetTextureColorMod(t, FontColor.r, FontColor.g, FontColor.b);
|
||||
SDL_UnlockMutex(SDL::GetMutex());
|
||||
|
||||
SDL::RenderCopy(t, static_cast<char>(info->GetTransparency() * 255), &charRect, &rect, info->GetAngle());
|
||||
rect.x += static_cast<int>(glyph.Advance * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
Source/Graphics/Component/Text.h
Normal file
29
Source/Graphics/Component/Text.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Component.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <vector>
|
||||
|
||||
class Font;
|
||||
|
||||
class Text : public Component
|
||||
{
|
||||
public:
|
||||
//todo: should have a Font flass that references fontcache, pass that in as an argument
|
||||
Text(std::string text, Font *font, SDL_Color fontColor, float scaleX, float scaleY);
|
||||
virtual ~Text();
|
||||
void AllocateGraphicsMemory();
|
||||
void FreeGraphicsMemory();
|
||||
void Draw();
|
||||
|
||||
private:
|
||||
std::string TextData;
|
||||
Font *FontInst;
|
||||
SDL_Color FontColor;
|
||||
float ScaleX;
|
||||
float ScaleY;
|
||||
};
|
||||
36
Source/Graphics/Component/VideoBuilder.cpp
Normal file
36
Source/Graphics/Component/VideoBuilder.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "VideoBuilder.h"
|
||||
#include "../../Utility/Utils.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include "../../Video/VideoFactory.h"
|
||||
#include <fstream>
|
||||
|
||||
|
||||
VideoComponent * VideoBuilder::CreateVideo(std::string path, std::string name, float scaleX, float scaleY)
|
||||
{
|
||||
VideoComponent *component = NULL;
|
||||
std::vector<std::string> extensions;
|
||||
|
||||
extensions.push_back("mp4");
|
||||
extensions.push_back("MP4");
|
||||
extensions.push_back("avi");
|
||||
extensions.push_back("AVI");
|
||||
|
||||
std::string prefix = path + "/" + name;
|
||||
std::string file;
|
||||
|
||||
if(Utils::FindMatchingFile(prefix, extensions, file))
|
||||
{
|
||||
IVideo *video = Factory.CreateVideo();
|
||||
|
||||
if(video)
|
||||
{
|
||||
component = new VideoComponent(video, file, scaleX, scaleY);
|
||||
}
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
18
Source/Graphics/Component/VideoBuilder.h
Normal file
18
Source/Graphics/Component/VideoBuilder.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Image.h"
|
||||
#include "VideoComponent.h"
|
||||
#include "../../Video/VideoFactory.h"
|
||||
|
||||
//todo: this is more of a factory than a builder
|
||||
class VideoBuilder
|
||||
{
|
||||
public:
|
||||
VideoComponent * CreateVideo(std::string path, std::string name, float scaleX, float scaleY);
|
||||
|
||||
private:
|
||||
VideoFactory Factory;
|
||||
};
|
||||
84
Source/Graphics/Component/VideoComponent.cpp
Normal file
84
Source/Graphics/Component/VideoComponent.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#include "VideoComponent.h"
|
||||
#include "../ViewInfo.h"
|
||||
#include "../../Database/Configuration.h"
|
||||
#include "../../Utility/Log.h"
|
||||
#include "../../SDL.h"
|
||||
|
||||
VideoComponent::VideoComponent(IVideo *videoInst, std::string videoFile, float scaleX, float scaleY)
|
||||
: VideoTexture(NULL)
|
||||
, VideoFile(videoFile)
|
||||
, VideoInst(videoInst)
|
||||
, ScaleX(scaleX)
|
||||
, ScaleY(scaleY)
|
||||
, IsPlaying(false)
|
||||
{
|
||||
// AllocateGraphicsMemory();
|
||||
}
|
||||
|
||||
VideoComponent::~VideoComponent()
|
||||
{
|
||||
FreeGraphicsMemory();
|
||||
|
||||
if(VideoInst)
|
||||
{
|
||||
VideoInst->Stop();
|
||||
}
|
||||
}
|
||||
|
||||
void VideoComponent::Update(float dt)
|
||||
{
|
||||
if(IsPlaying)
|
||||
{
|
||||
VideoInst->Update(dt);
|
||||
}
|
||||
|
||||
Component::Update(dt);
|
||||
|
||||
}
|
||||
|
||||
void VideoComponent::AllocateGraphicsMemory()
|
||||
{
|
||||
Component::AllocateGraphicsMemory();
|
||||
|
||||
if(!IsPlaying)
|
||||
{
|
||||
IsPlaying = VideoInst->Play(VideoFile);
|
||||
}
|
||||
}
|
||||
|
||||
void VideoComponent::FreeGraphicsMemory()
|
||||
{
|
||||
VideoInst->Stop();
|
||||
IsPlaying = false;
|
||||
|
||||
if (VideoTexture != NULL)
|
||||
{
|
||||
SDL_LockMutex(SDL::GetMutex());
|
||||
SDL_DestroyTexture(VideoTexture);
|
||||
SDL_UnlockMutex(SDL::GetMutex());
|
||||
}
|
||||
|
||||
Component::FreeGraphicsMemory();
|
||||
}
|
||||
|
||||
void VideoComponent::Draw()
|
||||
{
|
||||
ViewInfo *info = GetBaseViewInfo();
|
||||
SDL_Rect rect;
|
||||
|
||||
rect.x = static_cast<int>(info->GetXRelativeToOrigin());
|
||||
rect.y = static_cast<int>(info->GetYRelativeToOrigin());
|
||||
rect.h = static_cast<int>(info->GetHeight());
|
||||
rect.w = static_cast<int>(info->GetWidth());
|
||||
|
||||
VideoInst->Draw();
|
||||
SDL_Texture *texture = VideoInst->GetTexture();
|
||||
|
||||
if(texture)
|
||||
{
|
||||
SDL::RenderCopy(texture, static_cast<int>(info->GetTransparency() * 255), NULL, &rect, info->GetAngle());
|
||||
}
|
||||
}
|
||||
32
Source/Graphics/Component/VideoComponent.h
Normal file
32
Source/Graphics/Component/VideoComponent.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/* This file is subject to the terms and conditions defined in
|
||||
* file 'LICENSE.txt', which is part of this source code package.
|
||||
*/
|
||||
#pragma once
|
||||
#include "Component.h"
|
||||
#include "Image.h"
|
||||
#include "../../Collection/Item.h"
|
||||
#include "../../Video/IVideo.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
|
||||
class VideoComponent : public Component
|
||||
{
|
||||
public:
|
||||
VideoComponent(IVideo *videoInst, std::string videoFile, float scaleX, float scaleY);
|
||||
virtual ~VideoComponent();
|
||||
void Update(float dt);
|
||||
void Draw();
|
||||
void FreeGraphicsMemory();
|
||||
void AllocateGraphicsMemory();
|
||||
void LaunchEnter() {FreeGraphicsMemory(); }
|
||||
void LaunchExit() { AllocateGraphicsMemory(); }
|
||||
|
||||
private:
|
||||
SDL_Texture *VideoTexture;
|
||||
std::string VideoFile;
|
||||
std::string Name;
|
||||
IVideo *VideoInst;
|
||||
float ScaleX;
|
||||
float ScaleY;
|
||||
bool IsPlaying;
|
||||
};
|
||||
Reference in New Issue
Block a user