I'm done with the basics.
This is a class in a header file. It moves containers around. No friction. No elasticity. Just drag and drop. Also, I haven't tested this feature yet, but it should work for multiple containers. Finally, it doesn't have any restraints yet, but it does have the option for vertical or horizontal scrolling.
#ifndef SIMPLE_SCROLLER
#define SIMPLE_SCROLLER
int nOldX;
int nOldY;
int nNewX;
int nNewY;
int OnTouch(int id, int event, int x, int y)
{
if (event == 1)
{
nOldX = x;
nOldY = y;
nNewX = x;
nNewY = y;
}
if (event == 2)
{
nNewX = x;
nNewY = y;
}
if (event == 3)
{
nOldX = 0;
nOldY = 0;
nNewX = 0;
nNewY = 0;
}
return id;
}
class SimpleScroller
{
private:
int m_nContainer;
int m_nTouch;
int m_nX;
int m_nY;
bool m_bHorizontalScroll;
bool m_bVerticalScroll;
public:
SimpleScroller()
{
}
void Init(int& container, int x, int y, int width, int height)
{
m_nContainer = container;
m_nTouch = TouchAdd(container, x, y, width, height, OnTouch, 0);
m_nX = ContainerGetx(container);
m_nY = ContainerGety(container);
m_bHorizontalScroll = true;
m_bVerticalScroll = true;
}
void Update()
{
if (nOldX != nNewX && m_bHorizontalScroll)
ContainerSetx(m_nContainer, m_nX+nNewX-nOldX);
else if (nOldY != nNewY && m_bVerticalScroll)
ContainerSety(m_nContainer, m_nY+nNewY-nOldY);
else
{
m_nX = ContainerGetx(m_nContainer);
m_nY = ContainerGety(m_nContainer);
}
}
void SetHorizontalScroll(bool flag)
{
m_bHorizontalScroll = flag;
}
void SetVerticalScroll(bool flag)
{
m_bVerticalScroll = flag;
}
};
#endif
This is how you would use it. To initiate a scroll component, you need a container to be scrolled and some other variables for the touch component. The last two variables are width and height, which are important to establish the boundaries of your scroller.
I think the code is pretty self explanatory. You could use this as a basis for menus, sliders, etc.
//====================================================
// App.cpp
//====================================================
#include "DragonFireSDK.h"
#include "SimpleScroller.h"
int nContainer;
SimpleScroller simpleScroller;
void AppMain()
{
nContainer = ContainerAdd(0, 0, 0);
ViewAdd(nContainer, "Images/Background.png", 0, 0);
simpleScroller.Init(nContainer, 0, 0, 320, 960);
simpleScroller.SetHorizontalScroll(false);
}
void AppExit()
{
}
void OnTimer()
{
simpleScroller.Update();
}