fork download
  1. #include <includes.h>
  2.  
  3. //AARRGGBB
  4. struct ButtonColor
  5. {
  6. DWORD button;
  7. DWORD border;
  8. DWORD text;
  9. ButtonColor(){}
  10. ~ButtonColor(){}
  11. ButtonColor(DWORD color_button,DWORD color_border,DWORD color_text)
  12. : button(color_button),border(color_border),text(color_text)
  13. {}
  14. };
  15.  
  16. class Button
  17. {
  18. public:
  19. typedef std::function<void(Button*)> Callback;
  20. std::string text_;
  21. ButtonColor appearance_;
  22. ButtonColor highlighted_;
  23. ButtonColor mousedown_;
  24. bool clickable_;
  25. bool highlight_;
  26. unsigned short vkkey_;
  27. int fontid_;
  28. Callback * OnButtonClicked_;
  29. Button()
  30. {
  31. OnButtonClicked_ = NULL;
  32. }
  33. ~Button()
  34. {
  35. if(OnButtonClicked_)
  36. delete OnButtonClicked_;
  37. }
  38. Button(std::string button_text,ButtonColor &appearance, ButtonColor &highlighted, ButtonColor &mousedown, bool clickable, bool highlight, int fontid = 1, unsigned short vkkey = VK_LBUTTON)
  39. : text_(button_text), appearance_(appearance), highlighted_(highlighted), clickable_(clickable), highlight_(highlight), mousedown_(mousedown), vkkey_(vkkey), fontid_(fontid)
  40. {
  41. OnButtonClicked_ = NULL;
  42. }
  43. void SetInfo(std::string button_text,ButtonColor &appearance, ButtonColor &highlighted, ButtonColor &mousedown, bool clickable, bool highlight, int fontid = 1, unsigned short vkkey = VK_LBUTTON)
  44. {
  45. text_.assign(button_text);
  46. appearance_ = appearance;
  47. highlighted_ = highlighted;
  48. clickable_ = clickable;
  49. highlight_ = highlight;
  50. mousedown_ = mousedown;
  51. vkkey_ = vkkey;
  52. fontid_ = fontid;
  53. }
  54. void SetCallback(std::function<void(Button*)> &callback)
  55. {
  56. if(OnButtonClicked_)
  57. delete OnButtonClicked_;
  58. OnButtonClicked_ = NULL;
  59. OnButtonClicked_ = new Callback(callback);
  60. }
  61. float Lenght()
  62. {
  63. return DirectXFont::Access(fontid_)->DrawLength(text_.c_str());
  64. }
  65. float Height()
  66. {
  67. return DirectXFont::Access(fontid_)->DrawHeight();
  68. }
  69. bool IsCursorOnThis(float btn_x, float btn_y, float btn_w, float btn_h)
  70. {
  71. return IsPointInArea(cursorPos.x,cursorPos.y,btn_x,btn_y,btn_w,btn_h);
  72. }
  73. void Display(float btn_x, float btn_y, float btn_w, float btn_h)
  74. {
  75. if(!IsPointInArea(cursorPos.x,cursorPos.y,btn_x,btn_y,btn_w,btn_h))
  76. {
  77. render->D3DBoxBorder(btn_x,btn_y,btn_w,btn_h,appearance_.border,appearance_.button);
  78. DirectXFont::Access(fontid_)->Print(btn_x+((btn_w-Lenght())/2.0f),btn_y+((btn_h-Height())/2.0f),appearance_.text,text_.c_str(),true);
  79. return;
  80. }
  81. if(!clickable_)
  82. {
  83. render->D3DBoxBorder(btn_x,btn_y,btn_w,btn_h,(highlight_)? highlighted_.border : appearance_.border,(highlight_)? highlighted_.button : appearance_.button);
  84. DirectXFont::Access(fontid_)->Print(btn_x+((btn_w-Lenght())/2.0f),btn_y+((btn_h-Height())/2.0f),(highlight_)? highlighted_.text : appearance_.text,text_.c_str(),true);
  85. return;
  86. }
  87. if(!OldKeys(vkkey_).Down)
  88. {
  89. render->D3DBoxBorder(btn_x,btn_y,btn_w,btn_h,(highlight_)? highlighted_.border : appearance_.border,(highlight_)? highlighted_.button : appearance_.button);
  90. DirectXFont::Access(fontid_)->Print(btn_x+((btn_w-Lenght())/2.0f),btn_y+((btn_h-Height())/2.0f),(highlight_)? highlighted_.text : appearance_.text,text_.c_str(),true);
  91. return;
  92. }
  93. if(!Keys(vkkey_).Released)
  94. {
  95. render->D3DBoxBorder(btn_x,btn_y,btn_w,btn_h, mousedown_.border,mousedown_.button);
  96. DirectXFont::Access(fontid_)->Print(btn_x+((btn_w-Lenght())/2.0f),btn_y+((btn_h-Height())/2.0f), mousedown_.text,text_.c_str(),true);
  97. return;
  98. }
  99. if(OnButtonClicked_)
  100. (*OnButtonClicked_)(this);
  101. return;
  102. }
  103. };
  104.  
  105. class Menu
  106. {
  107. public:
  108. std::vector<Button*> Buttons_;
  109. float pos_top_;
  110. float pos_left_;
  111. float spacing_;
  112. float width_;
  113. float height_;
  114. //int maxcolumns_;
  115. DWORD BoxColor;
  116. DWORD BorderColor;
  117.  
  118. std::string name_;
  119. Menu()
  120. {
  121. pos_top_ = 0.0f;
  122. pos_left_ = 0.0f;
  123. spacing_ = 5.0f;
  124. width_ = 0.0f;
  125. height_ = 0.0f;
  126. //maxcolumns_ = 1;
  127. BoxColor = 0xFF330088;
  128. BorderColor = 0xFF338800;
  129. }
  130. ~Menu()
  131. {
  132. for(unsigned int i = 0; i < Buttons_.size(); ++i)
  133. delete Buttons_[i];
  134. Buttons_.clear();
  135. }
  136. void SetupWidth()
  137. {
  138. width_ = 0.0f;
  139. height_ = 0.0f;
  140. for(unsigned int i = 0; i < Buttons_.size(); ++i)
  141. {
  142. if(width_ < Buttons_[i]->Lenght())
  143. {
  144. width_ = Buttons_[i]->Lenght();
  145. }
  146. if(height_ < Buttons_[i]->Height())
  147. {
  148. height_ = Buttons_[i]->Height();
  149. }
  150. }
  151. }
  152. void AddButton(Button* button)
  153. {
  154. Buttons_.push_back(button);
  155. SetupWidth();
  156. }
  157. void RemoveButton(Button* button)
  158. {
  159. for(unsigned int i = 0; i < Buttons_.size(); ++i)
  160. {
  161. if(Buttons_[i] == button)
  162. {
  163. delete Buttons_[i];
  164. Buttons_.erase(Buttons_.begin()+i);
  165. }
  166. }
  167. SetupWidth();
  168. }
  169. float Width(bool horizontal = false)
  170. {
  171. if(!horizontal)
  172. return (width_ + (spacing_ * 4.0f));
  173. if(Buttons_.empty())
  174. return spacing_;
  175. return spacing_ + ((width_ + (3.0f * spacing_)) * ((float)Buttons_.size()));
  176. }
  177. float Height(bool horizontal = false)
  178. {
  179. if(horizontal)
  180. return (3.0f * spacing_) + height_;
  181. if(Buttons_.empty())
  182. return spacing_;
  183. return spacing_ + (((float)Buttons_.size())*spacing_)+((spacing_ + height_)*((float)Buttons_.size()));
  184. }
  185. void Process(bool horizontal = false, bool central = true)
  186. {
  187. if(Buttons_.empty())
  188. return;
  189. if(central)
  190. {
  191. Pos((((float)ScreenX)/2.0f)-(Width(horizontal)/2.0f),(((float)ScreenY)/2.0f)-(Height(horizontal)/2.0f));
  192. }
  193. if(!horizontal)
  194. {
  195. render->D3DBoxBorder(pos_left_,pos_top_,(width_ + (spacing_ * 4.0f)),spacing_ + (((float)Buttons_.size())*spacing_)+((spacing_ + height_)*((float)Buttons_.size())), BorderColor,BoxColor);
  196. for(unsigned int i = 0; i < Buttons_.size(); ++i)
  197. {
  198. Buttons_[i]->Display(pos_left_ + spacing_,pos_top_ + spacing_ + spacing_*((float)i) + ((spacing_ + height_)*(float)i),(width_ + (2.0f * spacing_)),height_+spacing_);
  199. }
  200. }
  201. else
  202. {
  203. render->D3DBoxBorder(pos_left_,pos_top_,spacing_ + ((width_ + (3.0f * spacing_)) * ((float)Buttons_.size())),(3.0f * spacing_) + height_, BorderColor,BoxColor);
  204. for(unsigned int i = 0; i < Buttons_.size(); ++i)
  205. {
  206. Buttons_[i]->Display(pos_left_ + spacing_ + ((width_ + (3.0f * spacing_)) * ((float)i)),pos_top_ + spacing_ ,(width_ + (2.0f * spacing_)),height_+spacing_);
  207. }
  208. }
  209. }
  210. void SetCallback(std::function<void(Button*)> &callback)
  211. {
  212. for(unsigned int i = 0; i < Buttons_.size(); ++i)
  213. {
  214. Buttons_[i]->SetCallback(callback);
  215. }
  216. }
  217. void Pos(float x, float y)
  218. {
  219. pos_left_ = x;
  220. pos_top_ = y;
  221. }
  222. void Colors(DWORD border,DWORD background)
  223. {
  224. BorderColor = border;
  225. BoxColor = background;
  226. }
  227. };
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty