generated from sigonasr2/CPlusPlusProjectTemplate
Included angle normalizing example
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
2830f5b550
commit
722d293fb8
Binary file not shown.
222
main.cpp
222
main.cpp
@ -1,188 +1,98 @@
|
|||||||
#define OLC_PGE_APPLICATION
|
#define OLC_PGE_APPLICATION
|
||||||
#include "pixelGameEngine.h"
|
#include "pixelGameEngine.h"
|
||||||
|
|
||||||
// PGEX Require the presence of olc::PixelGameEngine
|
#if defined(__EMSCRIPTEN__)
|
||||||
#define OLC_PGEX_QUICKGUI
|
#include <emscripten.h>
|
||||||
#include "quickgui.h"
|
#define FILE_RESOLVE(url, file) emscripten_wget(url, file); emscripten_sleep(0)
|
||||||
|
#else
|
||||||
|
#define FILE_RESOLVE(url, file)
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace olc::QuickGUI{
|
using namespace olc;
|
||||||
class PictureBox : public olc::QuickGUI::BaseControl{
|
|
||||||
private:
|
float angle_difference(float angle_1, float angle_2)
|
||||||
olc::vi2d clickPos;
|
{
|
||||||
|
angle_1=(angle_1<0)?angle_1+360:fmod(angle_1,360);
|
||||||
|
angle_2=(angle_2<0)?angle_2+360:fmod(angle_2,360);
|
||||||
|
float angle_diff = angle_1-angle_2;
|
||||||
|
if (abs(angle_diff)>180) {
|
||||||
|
if (angle_1>180) {
|
||||||
|
angle_1-=360;
|
||||||
|
}
|
||||||
|
if (angle_2>180) {
|
||||||
|
angle_2-=360;
|
||||||
|
}
|
||||||
|
angle_diff = angle_1-angle_2;
|
||||||
|
}
|
||||||
|
return -angle_diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Points an angle from point1 to point2, returning an angle between 0-2*PI.
|
||||||
|
// (atan2 returns -PI to PI)
|
||||||
|
float atan2fmod(vd2d point1,vd2d point2){
|
||||||
|
float angle = atan2f(point2.y-point1.y,point2.x-point1.x);
|
||||||
|
if (angle<0) {
|
||||||
|
angle+=2*3.14159f;
|
||||||
|
}
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Object{
|
||||||
public:
|
public:
|
||||||
PictureBox(olc::QuickGUI::Manager& manager, // Associate with a Manager
|
float facing_dir;
|
||||||
const olc::vf2d& pos,
|
float rotation_spd;
|
||||||
const olc::vf2d& size,
|
vd2d pos;
|
||||||
const olc::vf2d& offset,
|
vd2d size;
|
||||||
olc::Renderable& image);
|
Object(vd2d pos, vd2d size, float rotation_spd=100*(3.14159f/180.f), float facing_dir=0)
|
||||||
|
:pos(pos),size(size),rotation_spd(rotation_spd),facing_dir(facing_dir) {}
|
||||||
public:
|
void draw(PixelGameEngine*pge) {
|
||||||
// Minium value
|
pge->DrawCircle(pos,size.x);
|
||||||
olc::vf2d offset;
|
pge->DrawLine(pos,{(int)(pos.x+4*cos(facing_dir)),(int)(pos.y+4*sin(facing_dir))});
|
||||||
olc::vf2d pos;
|
}
|
||||||
olc::vf2d size;
|
|
||||||
olc::Renderableℑ
|
|
||||||
|
|
||||||
public: // BaseControl overrides
|
|
||||||
void Update(olc::PixelGameEngine* pge) override;
|
|
||||||
void Draw(olc::PixelGameEngine* pge) override;
|
|
||||||
void DrawDecal(olc::PixelGameEngine* pge) override;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
PictureBox::PictureBox(olc::QuickGUI::Manager& manager, const olc::vf2d& pos, const olc::vf2d& size, const olc::vf2d& offset,olc::Renderable& image)
|
// Override base class with your custom functionality
|
||||||
: BaseControl(manager),pos(pos),size(size),offset(offset),image(image){}
|
class Example : public olc::PixelGameEngine
|
||||||
|
|
||||||
void PictureBox::Update(olc::PixelGameEngine* pge)
|
|
||||||
{
|
|
||||||
bPressed = false;
|
|
||||||
bReleased = false;
|
|
||||||
float fElapsedTime = pge->GetElapsedTime();
|
|
||||||
|
|
||||||
olc::vf2d vMouse = pge->GetMousePos();
|
|
||||||
if (m_state != State::Click)
|
|
||||||
{
|
|
||||||
if (vMouse.x >= pos.x && vMouse.x < pos.x + size.x &&
|
|
||||||
vMouse.y >= pos.y && vMouse.y < pos.y + size.y)
|
|
||||||
{
|
|
||||||
m_fTransition += fElapsedTime * m_manager.fHoverSpeedOn;
|
|
||||||
m_state = State::Hover;
|
|
||||||
|
|
||||||
bPressed = pge->GetMouse(olc::Mouse::LEFT).bPressed;
|
|
||||||
if (bPressed)
|
|
||||||
{
|
|
||||||
m_state = State::Click;
|
|
||||||
clickPos = vMouse;
|
|
||||||
}
|
|
||||||
|
|
||||||
bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_fTransition -= fElapsedTime * m_manager.fHoverSpeedOff;
|
|
||||||
m_state = State::Normal;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
bHeld = pge->GetMouse(olc::Mouse::LEFT).bHeld;
|
|
||||||
bReleased = pge->GetMouse(olc::Mouse::LEFT).bReleased;
|
|
||||||
if (bReleased) m_state = State::Normal;
|
|
||||||
offset+=clickPos-vMouse;
|
|
||||||
offset.x=std::clamp((int)offset.x,0,image.Sprite()->width-(int)size.x);
|
|
||||||
offset.y=std::clamp((int)offset.y,0,image.Sprite()->height-(int)size.y);
|
|
||||||
clickPos=vMouse;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PictureBox::Draw(olc::PixelGameEngine* pge)
|
|
||||||
{
|
|
||||||
if (!bVisible)
|
|
||||||
return;
|
|
||||||
switch (m_state)
|
|
||||||
{
|
|
||||||
case State::Disabled: {
|
|
||||||
pge->FillRect(pos,size,m_manager.colDisable);
|
|
||||||
}break;
|
|
||||||
case State::Normal:
|
|
||||||
case State::Hover: {
|
|
||||||
pge->FillRect(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition));
|
|
||||||
}break;
|
|
||||||
case State::Click: {
|
|
||||||
pge->FillRect(pos,size,m_manager.colClick);
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
pge->DrawPartialSprite({(int)pos.x+1,(int)pos.y+1},image.Sprite(),offset,{(int)size.x-2,(int)size.y-2});
|
|
||||||
pge->DrawRect(pos,size,m_manager.colBorder);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PictureBox::DrawDecal(olc::PixelGameEngine* pge)
|
|
||||||
{
|
|
||||||
if (!bVisible)
|
|
||||||
return;
|
|
||||||
|
|
||||||
switch (m_state)
|
|
||||||
{
|
|
||||||
case State::Disabled: {
|
|
||||||
pge->FillRectDecal(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition));
|
|
||||||
}break;
|
|
||||||
case State::Normal:
|
|
||||||
case State::Hover: {
|
|
||||||
pge->FillRectDecal(pos,size,olc::PixelLerp(m_manager.colNormal, m_manager.colHover, m_fTransition));
|
|
||||||
}break;
|
|
||||||
case State::Click: {
|
|
||||||
pge->FillRectDecal(pos,size,m_manager.colClick);
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
pge->DrawPartialDecal({pos.x+1,pos.y+1},image.Decal(),offset,{size.x-2,size.y-2});
|
|
||||||
pge->SetDecalMode(olc::DecalMode::WIREFRAME);
|
|
||||||
pge->DrawRectDecal(pos,size,m_manager.colBorder);
|
|
||||||
pge->SetDecalMode(olc::DecalMode::NORMAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class olcDemo_QuickGUI : public olc::PixelGameEngine
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
olcDemo_QuickGUI()
|
Example()
|
||||||
{
|
{
|
||||||
sAppName = "olcDemo_QuickGUI";
|
// Name your application
|
||||||
|
sAppName = "Rotation";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
|
||||||
olc::QuickGUI::Manager guiManager;
|
|
||||||
|
|
||||||
olc::QuickGUI::PictureBox* pictureBox = nullptr;
|
|
||||||
olc::QuickGUI::Slider* pictureBoxSliderX = nullptr;
|
|
||||||
olc::QuickGUI::Slider* pictureBoxSliderY = nullptr;
|
|
||||||
olc::Renderable dirtimg;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Object*testRotationObj;
|
||||||
bool OnUserCreate() override
|
bool OnUserCreate() override
|
||||||
{
|
{
|
||||||
dirtimg.Load("dirtblock.png");
|
testRotationObj = new Object({(double)ScreenWidth()/2,(double)ScreenHeight()/2},{8,8});
|
||||||
|
|
||||||
pictureBox = new olc::QuickGUI::PictureBox(guiManager,
|
|
||||||
{ 10.0f, 10.0f }, { 128.0f, 128.0f }, {0,0}, dirtimg);
|
|
||||||
pictureBoxSliderX = new olc::QuickGUI::Slider(guiManager,{pictureBox->pos.x,pictureBox->pos.y+pictureBox->size.y+16},{pictureBox->pos.x+pictureBox->size.x,pictureBox->pos.y+pictureBox->size.y+16},
|
|
||||||
0,pictureBox->image.Sprite()->width-(int)pictureBox->size.x,pictureBox->offset.x);
|
|
||||||
pictureBoxSliderY = new olc::QuickGUI::Slider(guiManager,{pictureBox->pos.x+pictureBox->size.x+16,pictureBox->pos.y},{pictureBox->pos.x+pictureBox->size.x+16,pictureBox->pos.y+pictureBox->size.y},
|
|
||||||
0,pictureBox->image.Sprite()->height-(int)pictureBox->size.y,pictureBox->offset.y);
|
|
||||||
|
|
||||||
guiManager.AddControl(pictureBox);
|
|
||||||
guiManager.AddControl(pictureBoxSliderX);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnUserUpdate(float fElapsedTime) override
|
bool OnUserUpdate(float fElapsedTime) override
|
||||||
{
|
{
|
||||||
guiManager.Update(this);
|
Clear(BLACK);
|
||||||
|
float diffAng = atan2fmod(testRotationObj->pos,GetMousePos());
|
||||||
if (pictureBoxSliderX->bHeld) {
|
float rotAngle=angle_difference((180.f/3.14159f)*testRotationObj->facing_dir,(180.f/3.14159f)*diffAng);
|
||||||
pictureBox->offset.x=pictureBoxSliderX->fValue;
|
//std::cout<<diffAng<<std::endl;
|
||||||
|
//std::cout<<" "<<rotAngle<<std::endl;
|
||||||
|
if (abs(rotAngle)>testRotationObj->rotation_spd*fElapsedTime*(180.f/3.14159f)) {
|
||||||
|
if (rotAngle>0) {
|
||||||
|
testRotationObj->facing_dir+=testRotationObj->rotation_spd*fElapsedTime;
|
||||||
} else {
|
} else {
|
||||||
pictureBoxSliderX->fValue=pictureBox->offset.x;
|
testRotationObj->facing_dir-=testRotationObj->rotation_spd*fElapsedTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pictureBoxSliderY->bHeld) {
|
|
||||||
pictureBox->offset.y=pictureBoxSliderY->fValue;
|
|
||||||
} else {
|
|
||||||
pictureBoxSliderY->fValue=pictureBox->offset.y;
|
|
||||||
}
|
}
|
||||||
|
testRotationObj->draw(this);
|
||||||
// Draw Stuff!
|
|
||||||
Clear(olc::BLACK);
|
|
||||||
|
|
||||||
guiManager.DrawDecal(this);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
olcDemo_QuickGUI demo;
|
Example demo;
|
||||||
if (demo.Construct(256, 240, 4, 4))
|
if (demo.Construct(256, 240, 2, 2))
|
||||||
demo.Start();
|
demo.Start();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user