Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "engine/plugin.hpp"
- #include "engine/hook.hpp"
- #include "ws/ui/draw.hpp"
- #include "ws/gui/GBaseSimple.hpp"
- #include <functional>
- namespace {
- using namespace ws;
- using namespace engine;
- using namespace gui;
- class GTest: public GBase {
- protected:
- bool pointDraw;
- point pointPos;
- bool dragging;
- point dragStart;
- public:
- GTest(): GBase(){
- dragging = false;
- pointDraw = false;
- }
- void OnDraw(){
- draw::SetColor(50, 100, 50);
- draw::Rect(Pos.x, Pos.y, Size.x, Size.y);
- draw::SetColor(255,255,255);
- draw::RectOutline(Pos.x, Pos.y, Size.x, Size.y);
- GBase::OnDraw();
- }
- bool OnMouseFocus(bool focus){
- if(GBase::OnMouseFocus(focus))
- return false;
- pointDraw = focus;
- if(!focus)
- dragging = false;
- return false;
- }
- bool OnMouseButton(MOUSE_BUTTON button, bool pressed, double x, double y){
- if(GBase::OnMouseButton(button, pressed, x, y))
- return false;
- if(button == MOUSE_BUTTON_LEFT){
- dragging = pressed;
- if(dragging)
- dragStart = point(x, y);
- }
- return false;
- }
- bool OnMouseMove(double x, double y){
- if(GBase::OnMouseMove(x, y))
- return false;
- if(dragging){
- SetPos(Pos.x + (x - dragStart.x), Pos.y + (y - dragStart.y));
- dragStart = point(x, y);
- }
- if(pointDraw)
- pointPos = point(x, y);
- return false;
- }
- };
- class GButtonTest: public GBaseSimple {
- protected:
- bool active;
- std::function<void(GBase&)> onClick;
- public:
- GButtonTest(): active(false) {}
- void OnDraw(){
- if(active)
- draw::SetColor(50, 200, 50);
- else
- draw::SetColor(150, 150, 150);
- draw::Rect(Pos.x, Pos.y, Size.x, Size.y);
- draw::SetColor(25,25,25);
- draw::RectOutline(Pos.x, Pos.y, Size.x, Size.y);
- }
- bool OnMouseButton(MOUSE_BUTTON button, bool pressed, double x, double y){
- GBase::OnMouseButton(button, pressed, x, y);
- if(button == MOUSE_BUTTON_LEFT){
- active = pressed;
- if(!pressed)
- onClick(*this);
- return true;
- }
- return false;
- }
- void SetFunction(std::function<void(GBase&)> f){
- onClick = f;
- }
- };
- struct plugin_t: public plugin {
- static void Register(){
- print("registering");
- gui::Register<GTest>("guiTestWindow");
- gui::Register<GButtonTest>("guiTestButton");
- }
- static void Spawn(GBase& base){
- print("spawning");
- GBase& g = base.Add("guiTestWindow");
- g.SetPos(100, 100);
- g.SetSize(200, 150);
- GBase& b = g.Add("guiTestButton");
- b.SetLocalPos(5, 5);
- b.SetSize(190, 10);
- static_cast<GButtonTest&>(b).SetFunction([](GBase&){});
- GBase& c = g.Add("guiTestButton");
- c.SetLocalPos(g.Size.x - 10, g.Size.y - 10);
- c.SetSize(5, 5);
- static_cast<GButtonTest&>(c).SetFunction([](GBase& button){
- if(button.Parent)
- button.Parent->Close();
- });
- }
- void Load(){
- hook<>::Add("GUI_Register", "TEST_GUI_Init", Register);
- hook<GBase&>::Add("GUI_Spawn", "TEST_GUI_Spawn", Spawn);
- }
- void Unload(){
- hook<>::Remove("GUI_Register", "TEST_GUI_Init");
- hook<GBase&>::Remove("GUI_Spawn", "TEST_GUI_Spawn");
- }
- } plugin;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement