LPCGame
A Simple 2d Game Engine
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
objectbutton.h
1 #ifndef OBJECTBUTTON_H
2 #define OBJECTBUTTON_H
3 
4 #include <string>
5 #include "external/json/json.h"
6 #include "entity.h"
7 #include "image.h"
8 #include "text.h"
9 #include "button.h"
10 
12 
16 template<class T>
17 class ObjectButton : public Button {
18 public:
19  ObjectButton()
20  : mObj(nullptr), mObjFunc(nullptr)
21  {
22  mFunc = nullptr;
23  }
24  ~ObjectButton(){
25  }
32  void RegisterCallBack(T *obj, void (T::*func)(std::string), std::string param){
33  mObj = obj;
34  mObjFunc = func;
35  mParam = param;
36  mFunc = nullptr;
37  }
39  void OnClick(){
40  if (mObj != nullptr && mObjFunc != nullptr)
41  ((mObj)->*(mObjFunc))(mParam);
42  else if (mFunc != nullptr)
43  mFunc(mParam);
44  //Attempt to call the script if one is open
45  if (!mScript.Open())
46  return;
47 
48  mScript.CallFunction("OnClick");
49  }
55  Json::Value Save() const {
56  //Unfortunately I can't change the function that's pointed to via loading json
57  //as it's code not data, so instead we just save the param
58  Json::Value val = Entity::Save();
59  val["type"] = "objectbutton";
60  val["text"] = mText.Save();
61  val["param"] = mParam;
62 
63  return val;
64  }
70  void Load(Json::Value val){
71  Entity::Load(val);
72  mParam = val["param"].asString();
73  mText.Load(val["text"]);
74  }
75 
76 private:
77  T *mObj;
78  void (T::*mObjFunc)(std::string);
79  std::string mParam;
80 };
81 
82 #endif