I spent a bit of time playing around with widgets. Here's a code dump in case any one wants the very preliminary basics. It's inspired by the qt and Swing toolkits.
Assume you have simple classes WPoint (for position), WDim (for size), and WRect (for bounds). I can provide them if necessary.
Then, define a widget class like this:
- Code:
class Widget
{
public:
static
void
dispatchEventStylusDoubleClick(
const WPoint& kScreenPosition);
static
void
dispatchEventStylusDown(
const WPoint& kScreenPosition);
static
void
dispatchEventStylusMove(
const WPoint& kScreenPosition);
static
void
dispatchEventStylusUp(
const WPoint& kScreenPosition);
static
CGapiSurface&
getBackBuffer()
{
return *m_pBackBuffer;
}
static
void
setBackBuffer(
CGapiSurface& backBuffer)
{
m_pBackBuffer = &backBuffer;
}
static
void
repaintTopLevelWidgets()
{
for (int n = 0; n != m_cpTopLevelWidget.size(); ++n)
{
m_cpTopLevelWidget[n]->repaint();
}
}
public:
Widget() :
m_pParent(0)
{
addTopLevelWidget(*this);
};
Widget(
Widget& parent) :
m_pParent(&parent)
{
parent.addChild(*this);
};
virtual
void
eventStylusDoubleClick(
const WPoint& kPosition) {};
virtual
void
eventStylusDown(
const WPoint& kPosition) {};
virtual
void
eventStylusMove(
const WPoint& kPosition) {};
virtual
void
eventStylusUp(
const WPoint& kPosition) {};
WRect
getBounds() const
{
return m_bounds;
};
Widget&
getChild(
const int knIndex) const
{
return *m_cpChild[knIndex];
};
Widget*
getChild(
const WPoint& kPosition);
int
getHeight() const
{
return m_bounds.getHeight();
};
int
getNumberOfChildren() const
{
return m_cpChild.size();
};
Widget&
getParent() const
{
return *m_pParent;
};
WPoint
getPosition() const
{
return m_bounds.getPosition();
};
WDim
getSize() const
{
return m_bounds.getSize();
};
int
getWidth() const
{
return m_bounds.getWidth();
};
int
getX() const
{
return m_bounds.getX();
};
int
getY() const
{
return m_bounds.getY();
};
bool
hasChildren() const
{
return !m_cpChild.empty();
};
bool
hasParent() const
{
return m_pParent != 0;
};
bool
isEnabled() const
{
return m_bEnabled;
};
virtual
void
repaint() {};
void
setBounds(
const WRect& kBounds)
{
m_bounds = kBounds;
};
void
setEnabled(
const bool kbEnabled)
{
m_bEnabled = kbEnabled;
};
void
setHeight(
const int knHeight)
{
m_bounds.setHeight(knHeight);
};
void
setPosition(
const WPoint& kPosition)
{
m_bounds.setPosition(kPosition);
};
void
setSize(
const WDim& kSize)
{
m_bounds.setSize(kSize);
};
void
setWidth(
const int knWidth)
{
m_bounds.setWidth(knWidth);
};
void
setX(
const int knX)
{
m_bounds.setX(knX);
};
void
setY(
const int knY)
{
m_bounds.setY(knY);
};
WPoint
translateFromGlobal(
const WPoint& kPosition) const;
WPoint
translateFromParent(
const WPoint& kPosition) const
{
return WPoint(kPosition.getX() - getX(), kPosition.getY() - getY());
}
private:
static
void
addTopLevelWidget(
Widget& widget)
{
m_cpTopLevelWidget.push_back(&widget);
};
private:
static CGapiSurface* m_pBackBuffer;
static std::vector<Widget*> m_cpTopLevelWidget;
private:
void
addChild(
Widget& child)
{
m_cpChild.push_back(&child);
}
private:
Widget* m_pParent;
std::vector<Widget*> m_cpChild;
WRect m_bounds;
bool m_bEnabled;
};
Now, in the .cpp file you have to define the static members and a few functions. First are the child walking functions:
- Code:
Widget*
Widget::getChild(
const WPoint& kPosition)
{
if (!WRect(WPoint(), getSize()).contains(kPosition))
{
return 0;
}
for (int nIndex = 0; nIndex != getNumberOfChildren(); ++nIndex)
{
Widget& child = getChild(nIndex);
if (child.getChild(child.translateFromParent(kPosition)))
{
return &child;
}
}
return this;
}
WPoint
Widget::translateFromGlobal(
const WPoint& kPosition) const
{
WPoint position(kPosition);
const Widget* pkWidget = this;
while (pkWidget->hasParent())
{
position = pkWidget->translateFromParent(position);
pkWidget = &pkWidget->getParent();
}
return pkWidget->translateFromParent(position);
}
Next are the static event dispatch functions (do all four like this):
- Code:
void
Widget::dispatchEventStylusMove(
const WPoint& kScreenPosition)
{
for (int n = 0; n != m_cpTopLevelWidget.size(); ++n)
{
if (Widget* pWidget =
m_cpTopLevelWidget[n]->getChild(
m_cpTopLevelWidget[n]->translateFromParent(kScreenPosition)))
{
pWidget->eventStylusMove(
pWidget->translateFromGlobal(kScreenPosition));
}
}
}
Now, hook it up to GapiDraw by calling the static event dispatch functions from the corresponding GapiDraw callbacks. So CGapiApplication::StylusMove calls Widget::dispatchEventStylusMove, etc. Make CGapiApplication::ProcessFrame call Widget::repaintTopLevelWidgets. Set the back buffer during initialization by calling Widget::setBackBuffer.
Subclass Widget to make widget classes. Override the event handlers (e.g. Widget::eventStylusMove) to handle events, and override Widget::repaint to draw.
Create a widget object by calling its constructor with no parent if it is top level, or with its parent if it is a child. (But don't make a child statically, or it will race condition with the top level widget vector, that is a known issue.)
Children live within the coordinate system of their parents. When events occur, they are dispatched to the deepest child over which they occur, in the coordinate system of that child. See the translateFromParent and translateFromGlobal coordinate system transforms. All of this assumes children are within their parents and don't overlap.
What would make this a functioning widget toolkit, besides implementing some actual widget classes? Hook up key events to a widget that has the key focus. Add timer events, enter/leave events, etc.
Make repaint also walk its children. Maybe add support for invalidation (delayed repainting), although that's not as necessary for a game that redraws many times per second.
On the bounds front, perhaps add layout managers. Since widget use on PDA games is on a small scale, we can afford to waste a few cycles here, on layout and on coordinate transforms. If not, forego a layout manager and set bounds manually; maybe add caching of global coordinates for everything. But I think widget use on PDAs is limited to games like cards which can afford it, or just a couple of buttons on a busy screen which won't use much resources anyways.
Add all the widget properties such as enabled, visible, etc. And finally add action support so things can happen when you manipulate widgets (like signals/slots in qt or ActionListeners in Swing).
If anyone does anything with this, post here. I'm only doing this because I need a better way to organize my event handling and drawing in my game for a few things like menus and toolbars, primarily for my in-game editor.