Skip to content

Commit

Permalink
Add FrameMotion in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 15, 2024
1 parent a7db7e4 commit b9c4422
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ namespace Skylicht
}
}

void CGUIFitSprite::setFrame(SFrame* frame)
{
m_frame = frame;
}

void CGUIFitSprite::setFrameSource(const char* spritePath, const char* frameName, const char* editorFileRef)
{
CSpriteManager* spriteMgr = CSpriteManager::getInstance();
Expand Down
30 changes: 29 additions & 1 deletion Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIFitSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,35 @@ namespace Skylicht

void setFrameSource(const char* spritePath, const char* frameName, const char* editorFileRef = NULL);

void setFrame(SFrame* frame);
inline void setFrame(SFrame* frame)
{
m_frame = frame;
}

inline SFrame* getFrame()
{
return m_frame;
}

inline const char* getFrameName()
{
return m_frameName.c_str();
}

inline const char* getSpriteName()
{
return m_sprite.c_str();
}

inline const char* getSpriteId()
{
return m_spriteId.c_str();
}

inline const char* getFrameId()
{
return m_guid.c_str();
}

void setAnchor(AnchorType type, float left, float right, float top, float bottom);

Expand Down
12 changes: 3 additions & 9 deletions Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUISprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,19 @@ namespace Skylicht
return r;
}

void CGUISprite::setFrame(SFrame* frame)
{
m_frame = frame;
}

void CGUISprite::setFrameSource(const char* spritePath, const char* frameName, const char* editorRileRef)
void CGUISprite::setFrameSource(const char* spritePath, const char* frameName, const char* editorFileRef)
{
CSpriteManager* spriteMgr = CSpriteManager::getInstance();

// load by id first
CSpriteFrame* sprite = spriteMgr->loadSprite(spritePath);
if (sprite)
{
m_frame = sprite->getFrameByName(frameName);
if (m_frame)
{
m_guid = m_frame->ID;
if (editorRileRef)
m_frameName = editorRileRef;
if (editorFileRef)
m_frameName = editorFileRef;
else
m_frameName = frameName;
m_sprite = spritePath;
Expand Down
34 changes: 31 additions & 3 deletions Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUISprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,37 @@ namespace Skylicht

virtual const core::rectf getNativeRect();

void setFrame(SFrame* frame);

void setFrameSource(const char* spritePath, const char* frameName, const char* editorRileRef = NULL);
inline void setFrame(SFrame* frame)
{
m_frame = frame;
}

inline SFrame* getFrame()
{
return m_frame;
}

inline const char* getFrameName()
{
return m_frameName.c_str();
}

inline const char* getSpriteName()
{
return m_sprite.c_str();
}

inline const char* getSpriteId()
{
return m_spriteId.c_str();
}

inline const char* getFrameId()
{
return m_guid.c_str();
}

void setFrameSource(const char* spritePath, const char* frameName, const char* editorFileRef = NULL);

void setAutoRotate(bool rotate, float rotateAngle, float framePerSec);

Expand Down
125 changes: 125 additions & 0 deletions Projects/Skylicht/UserInterface/Source/Motion/CFrameMotion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
!@
MIT License
Copyright (c) 2024 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CFrameMotion.h"

#include "Graphics2D/GUI/CGUISprite.h"
#include "Graphics2D/GUI/CGUIFitSprite.h"

namespace Skylicht
{
namespace UI
{
CFrameMotion::CFrameMotion(SFrame* frame)
{
m_frames.push_back(frame);
}

CFrameMotion::CFrameMotion(std::vector<SFrame*>& frames)
{
m_frames = frames;
}

CFrameMotion::CFrameMotion()
{
m_toDefault = true;
}

CFrameMotion::~CFrameMotion()
{

}

void CFrameMotion::init(CGUIElement* gui)
{
CMotion::init(gui);

CGUISprite* sprite = dynamic_cast<CGUISprite*>(gui);
CGUIFitSprite* fitSprite = dynamic_cast<CGUIFitSprite*>(gui);

if (sprite)
m_defaultFrame = sprite->getFrame();
else if (fitSprite)
m_defaultFrame = fitSprite->getFrame();
}

void CFrameMotion::setFrame(SFrame* frame)
{
CGUISprite* sprite = dynamic_cast<CGUISprite*>(m_gui);
CGUIFitSprite* fitSprite = dynamic_cast<CGUIFitSprite*>(m_gui);

if (sprite)
sprite->setFrame(frame);
else if (fitSprite)
fitSprite->setFrame(frame);
}

void CFrameMotion::setFrame(float f)
{
if (m_frames.size() == 0)
{
setFrame(m_defaultFrame);
return;
}

std::vector<SFrame*> allFrames;

allFrames.push_back(m_defaultFrame);
for (SFrame* f : m_frames)
allFrames.push_back(f);

int totalFrameId = (int)allFrames.size() - 1;
int frameId = (int)(f / totalFrameId);

setFrame(allFrames[frameId]);
}

void CFrameMotion::start()
{
if (m_toDefault)
m_tween = new CTweenFloat(1.0f, 0.0f, m_duration);
else
{
if (m_inverseMotion)
m_tween = new CTweenFloat(1.0f, 0.0f, m_duration);
else
m_tween = new CTweenFloat(0.0f, 1.0f, m_duration);
}

m_tween->setDelay(m_delay);
m_tween->setEase(m_ease);
m_tween->OnUpdate = [&](CTween* t)
{
setFrame(((CTweenFloat*)t)->getValue());
};
m_tween->OnFinish = [&](CTween* t)
{
m_tween = NULL;
};

CTweenManager::getInstance()->addTween(m_tween);
}
}
}
60 changes: 60 additions & 0 deletions Projects/Skylicht/UserInterface/Source/Motion/CFrameMotion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
!@
MIT License
Copyright (c) 2024 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "CMotion.h"
#include "Graphics2D/SpriteFrame/CSpriteFrame.h"

namespace Skylicht
{
namespace UI
{
class CFrameMotion : public CMotion
{
protected:
SFrame* m_defaultFrame;

std::vector<SFrame*> m_frames;
public:
CFrameMotion(SFrame* frame);

CFrameMotion(std::vector<SFrame*>& frames);

CFrameMotion();

virtual ~CFrameMotion();

virtual void init(CGUIElement* gui);

virtual void start();

void setFrame(SFrame* frame);

protected:

void setFrame(float f);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This file is part of the "Skylicht Engine".
#include "Motion/CPositionMotion.h"
#include "Motion/CScaleMotion.h"
#include "Motion/CRotationMotion.h"
#include "Motion/CFrameMotion.h"

namespace Skylicht
{
Expand Down
8 changes: 8 additions & 0 deletions Samples/GUI/Source/CViewPopupEnterName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ void CViewPopupEnterName::onInit()

m_textBox = new UI::CUITextBox(uiContainer, canvas->getGUIByPath("Canvas/Dialog/InputName"));

CSpriteFrame* spriteFrame = CSpriteManager::getInstance()->loadSprite("SampleGUI/SampleGUI.spritedata");
SFrame* frame = spriteFrame->getFrameByName("input-activate");
if (frame && m_textBox->getBackground())
{
m_textBox->addMotion(UI::EMotionEvent::Focus, m_textBox->getBackground(), new UI::CFrameMotion(frame))->setTime(0.0f, 50.0f);
m_textBox->addMotion(UI::EMotionEvent::UnFocus, m_textBox->getBackground(), new UI::CFrameMotion())->setTime(0.0f, 50.0f);
}

UI::CUIBase* btnClose = new UI::CUIBase(uiContainer, canvas->getGUIByPath("Canvas/Dialog/btnClose"));
btnClose->addMotion(UI::EMotionEvent::PointerHover, new UI::CScaleMotion(1.2f, 1.2f, 1.0f));
btnClose->addMotion(UI::EMotionEvent::PointerOut, new UI::CScaleMotion());
Expand Down

0 comments on commit b9c4422

Please sign in to comment.