Advertisement
Guest User

GUI_Engine

a guest
Sep 17th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 KB | None | 0 0
  1. /*
  2.     Class:      GUI_Engine
  3.     Purpose:    -To provide a singleton class that will see over the entire GUI system.
  4.                 -To handle input and invoke the event-checking functions of the frames/elements
  5.                 -To load/unload GUI skins and to load more than one.
  6.                 -To
  7.  
  8. */
  9.  
  10. #ifndef GUI_ENGINE_H
  11. #define GUI_ENGINE_H
  12.  
  13. #include "globalvar.h"
  14. #include "SpritePool.h"
  15.  
  16. #include "GUI_Window.h"
  17. #include "GUI_Button.h"
  18. #include "GUI_Textbox.h"
  19.  
  20. #include <sol.hpp>
  21.  
  22. class GUI_Engine
  23. {
  24.     public:
  25.         GUI_Engine();
  26.         virtual ~GUI_Engine();
  27.  
  28.         void Init();
  29.         void Cleanup();
  30.  
  31.         //Load GUI skin
  32.         void load_gui_skin(std::string dirname);
  33.  
  34.         void clear_window_list();
  35.         void push_window(GUI_Window* window);
  36.  
  37.         void add_window(SpritePool gui_skin, int x, int y, int w, int h, bool visible, std::string str);
  38.         void add_button(SpritePool gui_skin, int x, int y, int w, int h, bool visible, std::string str, std::string parent_name);
  39.         void add_textbox(SpritePool gui_skin, int x, int y, int w, int h, bool visible, std::string str, std::string parent_name, std::string def_text);
  40.  
  41.         GUI_Window* get_window(std::string name);
  42.  
  43.         SpritePool get_gui_skin() {return gui_skin;}
  44.  
  45.         void render_gui();
  46.         void update_gui();
  47.         void check_input(Input &input);
  48.  
  49.     private:
  50.         std::vector<GUI_Window*> window_list;
  51.  
  52.         //current_script describes the current *.lua file that is being used for the GUI
  53.         std::string current_script;
  54.  
  55.         sol::state lua;
  56.  
  57.         SpritePool gui_skin;
  58.  
  59. };
  60.  
  61. #endif // GUI_ENGINE_H
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. #include "GUI_Engine.h"
  70.  
  71. GUI_Engine::GUI_Engine()
  72. {
  73.  
  74. }
  75.  
  76. GUI_Engine::~GUI_Engine()
  77. {
  78.     //dtor
  79. }
  80.  
  81. void GUI_Engine::Init()
  82. {
  83.     load_gui_skin("default");
  84.  
  85.     lua.set_function("load_gui_skin", &GUI_Engine::load_gui_skin, this);
  86.     lua.set_function("clear_window_list", &GUI_Engine::clear_window_list, this);
  87.     lua.set_function("push_window", &GUI_Engine::push_window, this);
  88.    // lua.set_function("add_window", &GUI_Engine::add_window, this);
  89.    // lua.set_function("add_button", &GUI_Engine::add_button, this);
  90.    // lua.set_function("add_textbox", &GUI_Engine::add_textbox, this);
  91.     lua.set_function("get_window", &GUI_Engine::get_window, this);
  92.     lua.set_function("get_gui_skin", &GUI_Engine::get_gui_skin, this);
  93.  
  94. }
  95.  
  96. void GUI_Engine::Cleanup()
  97. {
  98.     gui_skin.unload_all_texture();
  99. }
  100.  
  101. void GUI_Engine::load_gui_skin(std::string dir)
  102. {
  103.     std::string path = "resources/graphics/gui/skins/";
  104.     path = path + dir + "/";
  105.  
  106.     gui_skin.load_texture("for some reason the first two textures don't load");
  107.     gui_skin.load_texture("hi some reason the first two textures don't load");
  108.     gui_skin.load_texture(path + "window_left.png");
  109.     gui_skin.load_texture(path + "window_right.png");
  110.     gui_skin.load_texture(path + "window_topleft.png");
  111.     gui_skin.load_texture(path + "window_topright.png");
  112.     gui_skin.load_texture(path + "window_bottomleft.png");
  113.     gui_skin.load_texture(path + "window_bottomright.png");
  114.     gui_skin.load_texture(path + "window_minimize.png");
  115.     gui_skin.load_texture(path + "window_pin.png");
  116.     gui_skin.load_texture(path + "window_exit.png");
  117.     gui_skin.load_texture(path + "gui_bg.png");
  118.     gui_skin.load_texture(path + "button_rectnormal.png");
  119.     gui_skin.load_texture(path + "button_recthot.png");
  120.     gui_skin.load_texture(path + "button_rectactivated.png");
  121.     gui_skin.load_texture(path + "button_rectlongnormal.png");
  122.     gui_skin.load_texture(path + "button_rectlonghot.png");
  123.     gui_skin.load_texture(path + "button_rectlongactivated.png");
  124.     gui_skin.load_texture(path + "button_squarenormal.png");
  125.     gui_skin.load_texture(path + "button_squarehot.png");
  126.     gui_skin.load_texture(path + "button_squareactivated.png");
  127.     gui_skin.load_texture(path + "checkbox_normal.png");
  128.     gui_skin.load_texture(path + "checkbox_selected.png");
  129.     gui_skin.load_texture(path + "menu_dropdown.png");
  130.     gui_skin.load_texture(path + "menu_member.png");
  131.     gui_skin.load_texture(path + "window_top.png");
  132.     gui_skin.load_texture(path + "window_bottom.png");
  133. }
  134. void GUI_Engine::clear_window_list()
  135. {
  136.     for (unsigned int i = 0; i < window_list.size(); i++)
  137.     {
  138.         window_list.erase(window_list.begin() + (i));
  139.     }
  140. }
  141.  
  142. void GUI_Engine::push_window(GUI_Window* window)
  143. {
  144.     if (window != NULL)
  145.     {
  146.         window_list.push_back(window);
  147.     }
  148. }
  149.  
  150. void GUI_Engine::add_window(SpritePool gui_skin, int x, int y, int w, int h, bool visible, std::string name)
  151. {
  152.     GUI_Window* window = new GUI_Window(gui_skin, x, y, w, h, visible, name);
  153.     this->push_window(window);
  154. }
  155.  
  156. void GUI_Engine::add_button(SpritePool gui_skin, int x, int y, int w, int h, bool visible, std::string str, std::string parent_name)
  157. {
  158.     GUI_Window* parent_window = get_window(parent_name);
  159.     GUI_Element* parent = parent_window;
  160.  
  161.     GUI_Button* button = new GUI_Button(gui_skin, x, y, w, h, visible, str, parent);
  162.  
  163.     parent_window->add_element(button);
  164. }
  165.  
  166. void GUI_Engine::add_textbox(SpritePool gui_skin, int x, int y, int w, int h, bool visible, std::string str, std::string parent_name, std::string def_text)
  167. {
  168.     GUI_Window* parent_window = get_window(parent_name);
  169.     GUI_Element* parent = parent_window;
  170.  
  171.     GUI_Textbox* textbox = new GUI_Textbox(gui_skin, x, y, w, h, visible, str, parent, def_text);
  172.  
  173.     parent_window->add_element(textbox);
  174. }
  175.  
  176. GUI_Window* GUI_Engine::get_window(std::string name)
  177. {
  178.     GUI_Window* window_ptr = NULL;
  179.     for (unsigned int i = 0; i < window_list.size(); i++)
  180.     {
  181.        if (window_list[i]->get_name() == name)
  182.             window_ptr = window_list[i];
  183.     }
  184.  
  185.     return window_ptr;
  186. }
  187.  
  188. void GUI_Engine::render_gui()
  189. {
  190.     for (unsigned int i = 0; i < window_list.size(); i++)
  191.     {
  192.         window_list[i]->render();
  193.     }
  194. }
  195.  
  196. void GUI_Engine::update_gui()
  197. {
  198.     for (unsigned int i = 0; i < window_list.size(); i++)
  199.     {
  200.         window_list[i]->update();
  201.     }
  202. }
  203.  
  204. void GUI_Engine::check_input(Input &input)
  205. {
  206.     for (unsigned int i = 0; i < window_list.size(); i++)
  207.     {
  208.         window_list[i]->check(input);
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement