Advertisement
WeltEnSTurm

Untitled

Dec 5th, 2011
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1.  
  2. #include "engine/plugin.hpp"
  3. #include "engine/hook.hpp"
  4.  
  5. #include "ws/ui/draw.hpp"
  6.  
  7. #include "ws/gui/GBaseSimple.hpp"
  8.  
  9. #include <functional>
  10.  
  11. namespace {
  12.     using namespace ws;
  13.     using namespace engine;
  14.     using namespace gui;
  15.  
  16.     class GTest: public GBase {
  17.         protected:
  18.             bool pointDraw;
  19.             point pointPos;
  20.  
  21.             bool dragging;
  22.             point dragStart;
  23.         public:
  24.  
  25.             GTest(): GBase(){
  26.                 dragging = false;
  27.                 pointDraw = false;
  28.             }
  29.  
  30.             void OnDraw(){
  31.                 draw::SetColor(50, 100, 50);
  32.                 draw::Rect(Pos.x, Pos.y, Size.x, Size.y);
  33.                 draw::SetColor(255,255,255);
  34.                 draw::RectOutline(Pos.x, Pos.y, Size.x, Size.y);
  35.  
  36.                 GBase::OnDraw();
  37.             }
  38.  
  39.             bool OnMouseFocus(bool focus){
  40.                 if(GBase::OnMouseFocus(focus))
  41.                     return false;
  42.  
  43.                 pointDraw = focus;
  44.                 if(!focus)
  45.                     dragging = false;
  46.  
  47.                 return false;
  48.             }
  49.  
  50.             bool OnMouseButton(MOUSE_BUTTON button, bool pressed, double x, double y){
  51.                 if(GBase::OnMouseButton(button, pressed, x, y))
  52.                     return false;
  53.  
  54.                 if(button == MOUSE_BUTTON_LEFT){
  55.                     dragging = pressed;
  56.                     if(dragging)
  57.                         dragStart = point(x, y);
  58.                 }
  59.  
  60.                 return false;
  61.             }
  62.  
  63.             bool OnMouseMove(double x, double y){
  64.                 if(GBase::OnMouseMove(x, y))
  65.                     return false;
  66.  
  67.                 if(dragging){
  68.                     SetPos(Pos.x + (x - dragStart.x), Pos.y + (y - dragStart.y));
  69.                     dragStart = point(x, y);
  70.                 }
  71.  
  72.                 if(pointDraw)
  73.                     pointPos = point(x, y);
  74.  
  75.                 return false;
  76.             }
  77.     };
  78.  
  79.  
  80.     class GButtonTest: public GBaseSimple {
  81.         protected:
  82.             bool active;
  83.             std::function<void(GBase&)> onClick;
  84.         public:
  85.             GButtonTest(): active(false) {}
  86.  
  87.             void OnDraw(){
  88.                 if(active)
  89.                     draw::SetColor(50, 200, 50);
  90.                 else
  91.                     draw::SetColor(150, 150, 150);
  92.                 draw::Rect(Pos.x, Pos.y, Size.x, Size.y);
  93.                 draw::SetColor(25,25,25);
  94.                 draw::RectOutline(Pos.x, Pos.y, Size.x, Size.y);
  95.             }
  96.  
  97.             bool OnMouseButton(MOUSE_BUTTON button, bool pressed, double x, double y){
  98.                 GBase::OnMouseButton(button, pressed, x, y);
  99.                 if(button == MOUSE_BUTTON_LEFT){
  100.                     active = pressed;
  101.                     if(!pressed)
  102.                         onClick(*this);
  103.                     return true;
  104.                 }
  105.                 return false;
  106.             }
  107.  
  108.             void SetFunction(std::function<void(GBase&)> f){
  109.                 onClick = f;
  110.             }
  111.     };
  112.  
  113.     struct plugin_t: public plugin {
  114.  
  115.         static void Register(){
  116.             print("registering");
  117.             gui::Register<GTest>("guiTestWindow");
  118.             gui::Register<GButtonTest>("guiTestButton");
  119.         }
  120.  
  121.         static void Spawn(GBase& base){
  122.             print("spawning");
  123.  
  124.             GBase& g = base.Add("guiTestWindow");
  125.             g.SetPos(100, 100);
  126.             g.SetSize(200, 150);
  127.  
  128.             GBase& b = g.Add("guiTestButton");
  129.             b.SetLocalPos(5, 5);
  130.             b.SetSize(190, 10);
  131.             static_cast<GButtonTest&>(b).SetFunction([](GBase&){});
  132.  
  133.             GBase& c = g.Add("guiTestButton");
  134.             c.SetLocalPos(g.Size.x - 10, g.Size.y - 10);
  135.             c.SetSize(5, 5);
  136.             static_cast<GButtonTest&>(c).SetFunction([](GBase& button){
  137.                 if(button.Parent)
  138.                     button.Parent->Close();
  139.             });
  140.         }
  141.  
  142.         void Load(){
  143.             hook<>::Add("GUI_Register", "TEST_GUI_Init", Register);
  144.             hook<GBase&>::Add("GUI_Spawn", "TEST_GUI_Spawn", Spawn);
  145.         }
  146.  
  147.         void Unload(){
  148.             hook<>::Remove("GUI_Register", "TEST_GUI_Init");
  149.             hook<GBase&>::Remove("GUI_Spawn", "TEST_GUI_Spawn");
  150.         }
  151.  
  152.     } plugin;
  153. }
  154.  
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement