Skip to content

Commit 0acade8

Browse files
committed
Add DefaultWinProc to simplify window procedure creation.
1 parent ed8f359 commit 0acade8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Spore ModAPI/Spore/UTFWin/IWinProc.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace UTFWin
3535
///
3636
/// This class is a window procedure, also known as an event/message listener. IWinProc objects are added to windows;
3737
/// when a message (aka event) is received or generated on the window, the window procedures receive it and handle it.
38+
/// If you are making a standalone window procedure, it is recommended to inherit from DefaultWinProc instead
39+
/// to get the memory management functions.
3840
///
3941
class IWinProc : public Object
4042
{
@@ -99,4 +101,46 @@ namespace UTFWin
99101
protected:
100102
eastl::vector<MessageType> mTypes;
101103
};
104+
105+
/// A helper class to simplify the creation of window procedures (IWinProc).
106+
/// Provides implementations for memory management and GetEventFlags(), so the only method
107+
/// you need to implement is HandleUIMessage().
108+
/// The template parameter is a combination of EventFlags flags, which determine which kind of
109+
/// events it reacts to. By default, it reacts to basic keyboard/mouse input, and to advanced events.
110+
/// Example usage:
111+
/// ```cpp
112+
/// class MyProcedure : public DefaultWinProc<>
113+
/// {
114+
/// public:
115+
/// bool HandleUIMessage(IWindow* window, const Message& message) override;
116+
/// };
117+
///
118+
/// bool MyProcedure::HandleUIMessage(IWindow* window, const Message& message) {
119+
/// if (message.IsType(kMsgButtonClick)) {
120+
/// App::ConsolePrintF("Button clicked!");
121+
/// }
122+
/// return false;
123+
/// }
124+
/// ```
125+
template <int eventFlags = kEventFlagBasicInput | kEventFlagAdvanced>
126+
class DefaultWinProc
127+
: public UTFWin::IWinProc
128+
, public DefaultRefCounted
129+
{
130+
public:
131+
virtual int AddRef() override {
132+
return DefaultRefCounted::AddRef();
133+
}
134+
virtual int Release() override {
135+
return DefaultRefCounted::Release();
136+
}
137+
virtual void* Cast(uint32_t type) const override {
138+
CLASS_CAST(UTFWin::IWinProc);
139+
CLASS_CAST(Object);
140+
return nullptr;
141+
}
142+
virtual int GetEventFlags() const override {
143+
return eventFlags;
144+
}
145+
};
102146
};

0 commit comments

Comments
 (0)