Advertisement
Guest User

uLib.h

a guest
Sep 16th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.69 KB | None | 0 0
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <tuple>
  5. #include <unordered_map>
  6. #include <variant>
  7. #include <optional>
  8. #include <any>
  9. #include <memory>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <algorithm>
  13. #include <random>
  14. #include <utility>
  15. #include <chrono>
  16. #include <thread>
  17. #include <locale>
  18. #include <codecvt>
  19. #include <experimental/filesystem>
  20. #include <Windows.h>
  21. #include <ObjIdl.h>
  22. #include <gdiplus.h>
  23. #include <TlHelp32.h>
  24. #include <psapi.h>
  25. #pragma comment(lib, "Gdiplus.lib")
  26. #pragma comment(lib, "psapi")
  27. #undef BACKGROUND_INTENSITY
  28.  
  29.  
  30. // ULIB
  31. namespace ulib
  32. {
  33.     using namespace std::experimental::filesystem;
  34.     using namespace std::chrono;
  35.     using string = std::wstring;
  36.     using outfile = std::wofstream;
  37.     using infile = std::wifstream;
  38.     using file = std::wfstream;
  39.     using dir = directory_entry;
  40.     using string_content = std::vector<string>;
  41.     using range = std::pair<int, int>;
  42.     using position = std::pair<int, int>;
  43.     using uposition = std::pair<size_t, size_t>;
  44.     using precise_position = std::pair<float, float>;
  45.     using time_ms = int;
  46.     using time_s = double;
  47.     using time_m = double;
  48.     using time_point = system_clock::time_point;
  49.     using point = std::pair<int, int>;
  50.  
  51.  
  52.     enum key
  53.     {
  54.         key_null = -1,
  55.         key_0 = 0x30, key_5 = 0x31,
  56.         key_1 = 0x32, key_6 = 0x33,
  57.         key_2 = 0x34, key_7 = 0x35,
  58.         key_3 = 0x36, key_8 = 0x37,
  59.         key_4 = 0x38, key_9 = 0x39,
  60.         key_enter = 0x0D, key_backspace = 0x08,
  61.         key_shift = 0x10, key_control = 0x11,
  62.         key_alt = 0x12, key_tab = 0x09,
  63.         key_escape = 0x1B, key_pause = 0x13,
  64.         key_caps = 0x14, key_space = 0x20,
  65.         key_screenshot = 0x2C, key_print = 0x2A,
  66.         key_insert = 0x2D, key_up = 0x26,
  67.         key_down = 0x28, key_left = 0x25,
  68.         key_right = 0x27, key_cancel = 0x03,
  69.         key_multiply = 0x6A, key_add = 0x6B,
  70.         key_subtract = 0x6C, key_divide = 0x6F,
  71.         key_decimal = 0x6E, key_left_click = 0x01,
  72.         key_right_click = 0x02, key_middle_click = 0x04,
  73.  
  74.         key_A = 0x41, key_B = 0x42, key_C = 0x43,
  75.         key_D = 0x44, key_E = 0x45, key_F = 0x46,
  76.         key_G = 0x47, key_H = 0x48, key_I = 0x49,
  77.         key_J = 0x4A, key_K = 0x4B, key_L = 0x4C,
  78.         key_M = 0x4D, key_N = 0x4E, key_O = 0x4F,
  79.         key_P = 0x50, key_Q = 0x51, key_R = 0x52,
  80.         key_S = 0x53, key_T = 0x54, key_U = 0x55,
  81.         key_V = 0x56, key_W = 0x57, key_X = 0x58,
  82.         key_Y = 0x59, key_Z = 0x5A,
  83.  
  84.         click_RIGHT,
  85.         click_LEFT,
  86.         click_MIDDLE,
  87.     };
  88.  
  89.  
  90.     std::unordered_map<ulib::string, key> keys
  91.     {
  92.         {L"0", key_0},
  93.         {L"1", key_1},
  94.         {L"2", key_2},
  95.         {L"3", key_3},
  96.         {L"4", key_4},
  97.         {L"5", key_5},
  98.         {L"6", key_6},
  99.         {L"7", key_7},
  100.         {L"8", key_8},
  101.         {L"9", key_9},
  102.         {L"enter", key_enter},
  103.         {L"backspace", key_backspace},
  104.         {L"shift", key_shift},
  105.         {L"control", key_control},
  106.         {L"alt", key_alt},
  107.         {L"tab", key_tab},
  108.         {L"escape", key_escape},
  109.     };
  110.  
  111.  
  112.     enum color
  113.     {
  114.         null_color = -1,
  115.         black = 0,
  116.         blue = 1 | 8,
  117.         green = 2 | 8,
  118.         aqua = 3 | 8,
  119.         red = 4 | 8,
  120.         purple = 5 | 8,
  121.         yellow = 6 | 8,
  122.         white = 7 | 8,
  123.         grey = 8 | 8,
  124.         blue_dark = 1,
  125.         green_dark = 2,
  126.         aqua_dark = 3,
  127.         red_dark = 4,
  128.         purple_dark = 5,
  129.         yellow_dark = 6,
  130.         white_dark = 7,
  131.         grey_dark = 8,
  132.  
  133.         blue_background = 0x0010 | 0x0080,
  134.         blue_dark_background = 0x0010,
  135.         green_background = 0x0020 | 0x0080,
  136.         green_dark_background = 0x0020,
  137.         red_background = 0x0040 | 0x0080,
  138.         red_dark_background = 0x0040,
  139.         teal_background = 0x0010 | 0x0020 | 0x0080,
  140.         teal_dark_background = 0x0010 | 0x0020,
  141.         pink_background = 0x0010 | 0x0040 | 0x0080,
  142.         pink_dark_background = 0x0010 | 0x0040,
  143.         grey_background = 0x0080,
  144.     };
  145.  
  146.  
  147.     std::vector<color> colors
  148.     {
  149.         black,
  150.         blue,
  151.         green,
  152.         aqua,
  153.         red,
  154.         purple,
  155.         yellow,
  156.         white,
  157.         grey,
  158.         blue_dark,
  159.         green_dark,
  160.         aqua_dark,
  161.         red_dark,
  162.         purple_dark,
  163.         yellow_dark,
  164.         white_dark,
  165.         grey_dark
  166.     };
  167.  
  168.  
  169.     std::vector<color> background_colors
  170.     {
  171.         blue_background,
  172.         blue_dark_background,
  173.         green_background,
  174.         green_dark_background,
  175.         red_background,
  176.         red_dark_background,
  177.         teal_background,
  178.         teal_dark_background,
  179.         pink_background,
  180.         pink_dark_background,
  181.         grey_background
  182.     };
  183.  
  184.  
  185.     // Wait
  186.     void Wait(time_ms wait_time)
  187.     {
  188.         std::chrono::system_clock::time_point epoch = std::chrono::system_clock::now();
  189.         std::chrono::duration<double, std::milli> work_time = epoch - epoch;
  190.  
  191.         while (work_time.count() < wait_time)
  192.             work_time = std::chrono::system_clock::now() - epoch;
  193.     }
  194.  
  195.  
  196.     // RandomInt
  197.     int RandomInt(range range = { 0, 100 })
  198.     {
  199.         std::random_device device;
  200.         std::mt19937 engine(device());
  201.         std::uniform_int_distribution<> distribution(range.first, range.second);
  202.  
  203.         return distribution(engine);
  204.     }
  205.  
  206. }
  207.  
  208.  
  209.  
  210.  
  211. // FILES
  212. namespace ulib::files
  213. {
  214. #undef CreateDirectory
  215. #undef CreateFile
  216. #undef DeleteFile
  217.  
  218.  
  219.     struct File
  220.     {
  221.         ulib::path path;
  222.         ulib::string_content content;
  223.     };
  224.  
  225.  
  226.     // CheckDirectory
  227.     bool DirectoryCheck(ulib::path path)
  228.     {
  229.         if (ulib::exists(path) && ulib::is_directory(path))
  230.             return true;
  231.         return false;
  232.     }
  233.  
  234.  
  235.     // CheckFile
  236.     bool FileCheck(ulib::path path)
  237.     {
  238.         if (ulib::exists(path) && !is_directory(path))
  239.         {
  240.             ulib::infile file(path);
  241.  
  242.             if (!file.is_open())
  243.                 return false;
  244.  
  245.             file.close();
  246.             return true;
  247.         }
  248.  
  249.         return false;
  250.     }
  251.  
  252.  
  253.     // CreateDirectory
  254.     bool DirectoryCreate(ulib::path path)
  255.     {
  256.         if (DirectoryCheck(path))
  257.             return false;
  258.  
  259.         ulib::create_directories(path);
  260.         return true;
  261.     }
  262.  
  263.  
  264.     // CreateFile
  265.     bool FileCreate(ulib::path path)
  266.     {
  267.         if (!path.has_extension())
  268.             return false;
  269.         if (!DirectoryCheck(path.parent_path()))
  270.             DirectoryCreate(path.parent_path());
  271.  
  272.         outfile file(path);
  273.  
  274.         if (!file.is_open())
  275.             return false;
  276.  
  277.         file.close();
  278.         return true;
  279.     }
  280.  
  281.  
  282.     // DeleteFile
  283.     bool FileDelete(ulib::path path)
  284.     {
  285.         if (!FileCheck(path))
  286.             return false;
  287.  
  288.         std::remove(path.string().c_str());
  289.  
  290.         if (FileCheck(path))
  291.             return false;
  292.  
  293.         return true;
  294.     }
  295.  
  296.  
  297.     // ReadFile
  298.     string_content FileRead(const ulib::path& path)
  299.     {
  300.         if (!FileCheck(path))
  301.             return { L"\0" };
  302.  
  303.         ulib::string_content content;
  304.         ulib::infile file(path);
  305.         ulib::string line;
  306.  
  307.         while (std::getline(file, line))
  308.             content.emplace_back(line);
  309.  
  310.         file.close();
  311.         return content;
  312.     }
  313.  
  314.  
  315.     // WriteFile
  316.     bool FileWrite(ulib::path path, ulib::string_content content)
  317.     {
  318.  
  319.         if (!FileCheck(path))
  320.             return false;
  321.  
  322.         outfile file(path);
  323.  
  324.         for (auto& line : content)
  325.             file << line << L"\n";
  326.  
  327.         file.close();
  328.         return true;
  329.     }
  330.  
  331.  
  332.     // IterateDirectory
  333.     std::vector<path> DirectoryRead(path path)
  334.     {
  335.         if (!DirectoryCheck(path))
  336.             return {};
  337.  
  338.         std::vector<ulib::path> dirs;
  339.  
  340.         for (auto& dir : ulib::directory_iterator(path))
  341.         {
  342.             if (DirectoryCheck(dir))
  343.                 for (auto& file : directory_iterator(dir))
  344.                     dirs.emplace_back(file);
  345.             else
  346.                 dirs.emplace_back(dir);
  347.         }
  348.  
  349.         return dirs;
  350.     }
  351. }
  352.  
  353.  
  354.  
  355.  
  356. // IO
  357. namespace ulib::io
  358. {
  359. #undef PlaySound
  360.  
  361.  
  362.     // Print
  363.     void Print(ulib::string msg)
  364.     {
  365.         std::wcout << msg << L"\n";
  366.     }
  367.  
  368.  
  369.     // GetCursorPosition
  370.     point GetCursorPosition()
  371.     {
  372.         POINT cursor_pos;
  373.         GetCursorPos(&cursor_pos);
  374.  
  375.         return point(cursor_pos.x, cursor_pos.y);
  376.     }
  377.  
  378.  
  379.     // SetCursorPosition
  380.     void SetCursorPosition(point pos)
  381.     {
  382.         SetCursorPos(pos.first, pos.second);
  383.     }
  384.  
  385.  
  386.     // IsKeyDown
  387.     bool IsKeyDown(ulib::key key, ulib::key modifier)
  388.     {
  389.         if (modifier != ulib::key_null && GetKeyState(key) & 0x8000 && GetAsyncKeyState(modifier) & 0x8000)
  390.         {
  391.             FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  392.             return true;
  393.         }
  394.         else if (modifier == ulib::key_null && GetKeyState(key) & 0x8000)
  395.         {
  396.             FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  397.             return true;
  398.         }
  399.  
  400.         return false;
  401.     }
  402.  
  403.  
  404.     // IsKeyUp
  405.     bool IsKeyUp(ulib::key key)
  406.     {
  407.         if (GetKeyState(key) == 0)
  408.             return true;
  409.  
  410.         return false;
  411.     }
  412.  
  413.  
  414.     // KeyPress
  415.     void KeyPress(ulib::key key, HWND window = NULL, time_ms time = 20)
  416.     {
  417.         if (window != NULL)
  418.             switch (key)
  419.             {
  420.             case click_LEFT:
  421.                 PostMessage(window, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(GetCursorPosition().first, GetCursorPosition().second));
  422.                 Wait((DWORD)time);
  423.                 PostMessage(window, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(GetCursorPosition().first, GetCursorPosition().second));
  424.                 break;
  425.             case click_RIGHT:
  426.                 PostMessage(window, WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM(GetCursorPosition().first, GetCursorPosition().second));
  427.                 Wait((DWORD)time);
  428.                 PostMessage(window, WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM(GetCursorPosition().first, GetCursorPosition().second));
  429.                 break;
  430.             case click_MIDDLE:
  431.                 PostMessage(window, WM_MBUTTONDOWN, MK_MBUTTON, MAKELPARAM(GetCursorPosition().first, GetCursorPosition().second));
  432.                 Wait((DWORD)time);
  433.                 PostMessage(window, WM_MBUTTONUP, MK_MBUTTON, MAKELPARAM(GetCursorPosition().first, GetCursorPosition().second));
  434.                 break;
  435.             default:
  436.                 PostMessage(window, WM_KEYDOWN, key, 0);
  437.                 Sleep((DWORD)time);
  438.                 PostMessage(window, WM_KEYUP, key, 0);
  439.                 break;
  440.             }
  441.         else
  442.             switch (key)
  443.             {
  444.             case click_LEFT:
  445.                 mouse_event(MOUSEEVENTF_LEFTDOWN, GetCursorPosition().first, GetCursorPosition().second, 0, 0);
  446.                 Sleep((DWORD)time);
  447.                 mouse_event(MOUSEEVENTF_LEFTUP, GetCursorPosition().first, GetCursorPosition().second, 0, 0);
  448.                 break;
  449.             case click_RIGHT:
  450.                 mouse_event(MOUSEEVENTF_RIGHTDOWN, GetCursorPosition().first, GetCursorPosition().second, 0, 0);
  451.                 Sleep((DWORD)time);
  452.                 mouse_event(MOUSEEVENTF_RIGHTUP, GetCursorPosition().first, GetCursorPosition().second, 0, 0);
  453.                 break;
  454.             case click_MIDDLE:
  455.                 mouse_event(MOUSEEVENTF_MIDDLEDOWN, GetCursorPosition().first, GetCursorPosition().second, 0, 0);
  456.                 Sleep((DWORD)time);
  457.                 mouse_event(MOUSEEVENTF_MIDDLEUP, GetCursorPosition().first, GetCursorPosition().second, 0, 0);
  458.                 break;
  459.             default:
  460.                 INPUT ip;
  461.  
  462.                 ip.type = INPUT_KEYBOARD;
  463.                 ip.ki.wScan = 0;
  464.                 ip.ki.time = 0;
  465.                 ip.ki.dwExtraInfo = 0;
  466.                 ip.ki.wVk = key;
  467.                 ip.ki.dwFlags = 0;
  468.                 SendInput(1, &ip, sizeof(INPUT));
  469.                 Sleep(20);
  470.                 ip.ki.dwFlags = KEYEVENTF_KEYUP;
  471.                 SendInput(1, &ip, sizeof(INPUT));
  472.                 break;
  473.             }
  474.     }
  475.  
  476.  
  477.     // KeyDown
  478.  
  479.     // KeyUp
  480.  
  481.     // MouseClick
  482.  
  483.     // MouseDown
  484.  
  485.     // MouseUp
  486.  
  487.  
  488.  
  489. }
  490.  
  491.  
  492.  
  493.  
  494. // PARSING
  495. namespace ulib::parsing
  496. {
  497.  
  498.     enum parse_flag
  499.     {
  500.         PREFIX,
  501.         POSTFIX,
  502.         INFIX,
  503.     };
  504.  
  505.  
  506.     // ParseIndentation
  507.     int ParseIndentation(string line)
  508.     {
  509.         int count = 0;
  510.  
  511.         for (auto& char_in : line)
  512.             if (char_in == L' ')
  513.                 count++;
  514.  
  515.         return count;
  516.     }
  517.  
  518.  
  519.     // Parse
  520.     string Parse(const string& token, const string& line, parse_flag flag = PREFIX)
  521.     {
  522.         switch (flag)
  523.         {
  524.         case PREFIX:
  525.             if (line.find(token) != line.npos)
  526.                 return line.substr(line.find(token) + token.length());
  527.             break;
  528.         case POSTFIX:
  529.             if (line.find(token) != line.npos)
  530.                 return line.substr(0, line.find(token));
  531.             break;
  532.         case INFIX:
  533.             if (line.find(token) != line.npos && line.substr(line.find(token) + 1).find(token) != line.npos)
  534.                 return line.substr(line.find(token) + token.length(), line.substr(line.find(token) + token.length()).find(token));
  535.             break;
  536.         }
  537.  
  538.         return L"\0";
  539.     }
  540.  
  541.     // Parse - Overload
  542.     string Parse(const string& token, const string_content& lines)
  543.     {
  544.         for (auto& line : lines)
  545.             if (line.find(token) != line.npos)
  546.                 return line.substr(line.find(token) + token.length());
  547.  
  548.         return L"\0";
  549.     }
  550.  
  551.     // Parse - Overload
  552.     string_content ParseAll(const string& token, const string_content& lines)
  553.     {
  554.         string_content parsed{};
  555.  
  556.         for (auto& line : lines)
  557.             if (line.find(token) != line.npos)
  558.                 parsed.emplace_back(line.substr(line.find(token) + token.length()));
  559.  
  560.         return parsed;
  561.     }
  562.  
  563.  
  564.     // AsInt
  565.     int AsInt(string text)
  566.     {
  567.         return std::stoi(text);
  568.     }
  569.  
  570.  
  571.     // AsFloat
  572.     float AsFloat(string text)
  573.     {
  574.         return std::stof(text);
  575.     }
  576. }
  577.  
  578.  
  579.  
  580.  
  581. // CONSOLE
  582. namespace ulib::console
  583. {
  584.  
  585.     class Console
  586.     {
  587.     private:
  588.         HANDLE write_handle, read_handle, input_handle;
  589.         SMALL_RECT window_size, write_area;
  590.         COORD char_buffer_size, char_position;
  591.         std::vector<CHAR_INFO> buffer;
  592.  
  593.     public:
  594.         ulib::color foreground_color, background_color;
  595.         COORD buffer_size;
  596.         int line;
  597.  
  598.         Console(std::string name = "Application", ulib::position size = { 25, 25 }, ulib::color foreground_color = ulib::color::white, ulib::color background_color = ulib::color::black)
  599.             : write_handle(GetStdHandle(STD_OUTPUT_HANDLE))
  600.             , read_handle(GetStdHandle(STD_OUTPUT_HANDLE))
  601.             , input_handle(GetStdHandle(STD_INPUT_HANDLE))
  602.             , window_size({ 0, 0, (SHORT)size.first - 1, (SHORT)size.second - 1 })
  603.             , buffer_size({ (SHORT)size.first, (SHORT)size.second })
  604.             , char_buffer_size({ (SHORT)size.first, (SHORT)size.second })
  605.             , char_position({ 0, 0 })
  606.             , buffer(size.first * size.second)
  607.             , write_area({ 0, 0, (SHORT)size.first - 1, (SHORT)size.second - 1 })
  608.             , foreground_color(foreground_color)
  609.             , background_color(background_color)
  610.             , line(0)
  611.         {
  612.             //HWND console = GetConsoleWindow();
  613.             //ShowWindow(console, 10);
  614.             SetConsoleOutputCP(CP_UTF8);
  615.             SetConsoleCP(CP_UTF8);
  616.             SetConsoleTitle(name.c_str());
  617.             show_cursor(false);
  618.             SetConsoleWindowInfo(write_handle, TRUE, &window_size);
  619.             SetConsoleScreenBufferSize(write_handle, buffer_size);
  620.             clear();
  621.         }
  622.  
  623.  
  624.         // Clear
  625.         void clear()
  626.         {
  627.             line = 0;
  628.             for (int y = 0; y < buffer_size.Y; ++y)
  629.                 for (int x = 0; x < buffer_size.X; ++x)
  630.                 {
  631.                     buffer[x + buffer_size.X * y].Char.UnicodeChar = L' ';
  632.                     buffer[x + buffer_size.X * y].Char.AsciiChar = ' ';
  633.                     buffer[x + buffer_size.X * y].Attributes = background_color;
  634.                 }
  635.         }
  636.  
  637.  
  638.         // Display
  639.         void display()
  640.         {
  641.             WriteConsoleOutputW(write_handle, buffer.data(), char_buffer_size, char_position, &write_area);
  642.         }
  643.  
  644.  
  645.         // Draw
  646.         void draw(wchar_t character, ulib::position pos = { 0, 0 }, ulib::color foreground = ulib::color::null_color, ulib::color background = ulib::color::null_color)
  647.         {
  648.             if (foreground == ulib::color::null_color)
  649.                 foreground = foreground_color;
  650.             if (background == ulib::color::null_color)
  651.                 background = background_color;
  652.  
  653.             buffer[pos.first + buffer_size.X * pos.second].Char.UnicodeChar = character;
  654.             buffer[pos.first + buffer_size.X * pos.second].Attributes = foreground | background;
  655.             pos.first++;
  656.         }
  657.  
  658.  
  659.         // Draw - Overload
  660.         void draw(const ulib::string& string, ulib::position pos = { 0, 0 }, ulib::color foreground = ulib::color::null_color, ulib::color background = ulib::color::null_color)
  661.         {
  662.             if (foreground == ulib::color::null_color)
  663.                 foreground = foreground_color;
  664.             if (background == ulib::color::null_color)
  665.                 background = background_color;
  666.  
  667.             for (const auto& chr : string)
  668.             {
  669.                 buffer[pos.first + buffer_size.X * pos.second].Char.UnicodeChar = chr;
  670.                 buffer[pos.first + buffer_size.X * pos.second].Attributes = foreground | background;
  671.                 pos.first++;
  672.             }
  673.         }
  674.  
  675.         // Draw - Overload
  676.         void draw(const ulib::string_content& strings, ulib::position pos = { 0, 0 }, unsigned short attributes = 0)
  677.         {
  678.             if (attributes == 0)
  679.                 attributes = background_color;
  680.  
  681.             int default_posx = pos.first;
  682.  
  683.             for (const auto& str : strings)
  684.             {
  685.                 for (const auto chr : str)
  686.                 {
  687.                     buffer[pos.first + buffer_size.X * pos.second].Char.UnicodeChar = chr;
  688.                     buffer[pos.first + buffer_size.X * pos.second].Attributes = attributes;
  689.                     pos.first++;
  690.                 }
  691.                 pos.first = default_posx;
  692.                 pos.second++;
  693.             }
  694.         }
  695.  
  696.         // Print - Overload
  697.         void print(const ulib::string& string = L"", ulib::color foreground = ulib::color::null_color, ulib::color background = ulib::color::null_color, ulib::position pos = { 0, 0 })
  698.         {
  699.             if (foreground == ulib::color::null_color)
  700.                 foreground = foreground_color;
  701.             if (background == ulib::color::null_color)
  702.                 background = background_color;
  703.  
  704.             if (pos.second == 0)
  705.                 pos.second = line;
  706.             else
  707.                 line = pos.second;
  708.  
  709.             for (auto& chr : string)
  710.             {
  711.                 buffer[pos.first + buffer_size.X * pos.second].Char.UnicodeChar = chr;
  712.                 buffer[pos.first + buffer_size.X * pos.second].Attributes = foreground | background;
  713.                 pos.first++;
  714.             }
  715.  
  716.             line++;
  717.             display();
  718.         }
  719.  
  720.         // Print - Overload
  721.         void print(std::string string, ulib::color foreground = ulib::color::null_color, ulib::color background = ulib::color::null_color, ulib::position pos = { 0, 0 })
  722.         {
  723.             if (foreground == ulib::color::null_color)
  724.                 foreground = foreground_color;
  725.             if (background == ulib::color::null_color)
  726.                 background = background_color;
  727.  
  728.             if (pos.second == 0)
  729.                 pos.second = line;
  730.             else
  731.                 line = pos.second;
  732.  
  733.             for (auto& chr : string)
  734.             {
  735.                 buffer[pos.first + buffer_size.X * pos.second].Char.AsciiChar = chr;
  736.                 buffer[pos.first + buffer_size.X * pos.second].Attributes = foreground | background;
  737.                 pos.first++;
  738.             }
  739.  
  740.             line++;
  741.             display();
  742.         }
  743.  
  744.  
  745.         // SetCursorPos
  746.         void set_cursor(ulib::position pos)
  747.         {
  748.             SetConsoleCursorPosition(write_handle, { (SHORT)pos.first, (SHORT)pos.second });
  749.         }
  750.  
  751.  
  752.         // ShowCursorPos
  753.         void show_cursor(bool to_show)
  754.         {
  755.             CONSOLE_CURSOR_INFO cursorInfo;
  756.  
  757.             GetConsoleCursorInfo(write_handle, &cursorInfo);
  758.             cursorInfo.bVisible = to_show;
  759.             SetConsoleCursorInfo(write_handle, &cursorInfo);
  760.         }
  761.  
  762.     };
  763.  
  764. }
  765.  
  766.  
  767.  
  768.  
  769. // SCREEN
  770. namespace ulib::screen
  771. {
  772.  
  773.     struct Color
  774.     {
  775.         int r, g, b;
  776.  
  777.         const bool operator==(const Color& other) const
  778.         {
  779.             if (r == other.r && g == other.g && b == other.b)
  780.                 return true;
  781.  
  782.             return false;
  783.         }
  784.  
  785.         bool operator!=(const Color& other) const
  786.         {
  787.             if (r != other.r || g != other.g || b == other.b)
  788.                 return true;
  789.  
  790.             return false;
  791.         }
  792.  
  793.     };
  794.  
  795.  
  796.     struct Pixel
  797.     {
  798.         point pos;
  799.         Color color;
  800.  
  801.         bool operator==(const Pixel& pixel) const
  802.         {
  803.             if (color == pixel.color && pos == pixel.pos)
  804.                 return true;
  805.  
  806.             return false;
  807.         }
  808.  
  809.         bool operator!=(const Pixel& pixel) const
  810.         {
  811.             if (color != pixel.color || pos != pixel.pos)
  812.                 return true;
  813.  
  814.             return false;
  815.         }
  816.     };
  817.  
  818.  
  819.     class Screen
  820.     {
  821.     private:
  822.         int width, height;
  823.         HDC screen, screen_mem;
  824.         HBITMAP bitmap_handle;
  825.         HGDIOBJ oldbitmap_handle;
  826.         BITMAPINFO bi;
  827.  
  828.     public:
  829.         Screen(int width = GetSystemMetrics(SM_CXSCREEN), int height = GetSystemMetrics(SM_CYSCREEN))
  830.             : width(width)
  831.             , height(height)
  832.             , screen(GetDC(0))
  833.             , screen_mem(CreateCompatibleDC(screen))
  834.             , bitmap_handle(CreateCompatibleBitmap(screen, width, height))
  835.             , oldbitmap_handle(SelectObject(screen_mem, bitmap_handle))
  836.             , bi({ 0 })
  837.         {
  838.             BitBlt(screen_mem, 0, 0, width, height, screen, 0, 0, SRCCOPY | CAPTUREBLT);
  839.             SelectObject(screen_mem, oldbitmap_handle);
  840.             DeleteDC(screen_mem);
  841.  
  842.             bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
  843.             bi.bmiHeader.biWidth = width;
  844.             bi.bmiHeader.biHeight = -height;
  845.             bi.bmiHeader.biBitCount = 32;
  846.             bi.bmiHeader.biPlanes = 1;
  847.             bi.bmiHeader.biCompression = BI_RGB;
  848.         }
  849.  
  850.         ~Screen()
  851.         {
  852.             DeleteObject(bitmap_handle);
  853.             ReleaseDC(NULL, screen);
  854.         }
  855.  
  856.  
  857.         void refresh()
  858.         {
  859.             DeleteObject(bitmap_handle);
  860.             ReleaseDC(NULL, screen);
  861.  
  862.             screen = GetDC(0);
  863.             screen_mem = CreateCompatibleDC(screen);
  864.             bitmap_handle = CreateCompatibleBitmap(screen, width, height);
  865.             oldbitmap_handle = SelectObject(screen_mem, bitmap_handle);
  866.             BitBlt(screen_mem, 0, 0, width, height, screen, 0, 0, SRCCOPY | CAPTUREBLT);
  867.             SelectObject(screen_mem, oldbitmap_handle);
  868.             DeleteDC(screen_mem);
  869.         }
  870.  
  871.  
  872.         void set_pixel(point pos, Color color)
  873.         {
  874.             SetPixel(screen, pos.first, pos.second, RGB(color.r, color.g, color.b));
  875.         }
  876.  
  877.  
  878.         Pixel get_pixel(point pos) const
  879.         {
  880.             std::vector<BYTE> colormap(height * width * 4);
  881.             BITMAPINFO info = bi;
  882.  
  883.             if (0 != GetDIBits(screen, bitmap_handle, 0, height, colormap.data(), &info, DIB_RGB_COLORS))
  884.             {
  885.                 int index = (pos.second * width + pos.first) * 4;
  886.                 Pixel pixel{ pos, Color{std::stoi(std::to_string(colormap.data()[index + 2])), std::stoi(std::to_string(colormap.data()[index + 1])), std::stoi(std::to_string(colormap.data()[index + 0]))} };
  887.                 return pixel;
  888.             }
  889.  
  890.             return Pixel{};
  891.         }
  892.  
  893.  
  894.         bool match_pixel(const Pixel& pixel) const
  895.         {
  896.             if (pixel == get_pixel(pixel.pos))
  897.                 return true;
  898.  
  899.             return false;
  900.         }
  901.     };
  902.  
  903. }
  904.  
  905.  
  906.  
  907.  
  908. // WINDOW
  909. namespace ulib::window
  910. {
  911.  
  912.     class Overlay
  913.     {
  914.     private:
  915.         std::string class_name;
  916.         HWND hwnd;
  917.         WNDCLASSEX wc;
  918.         MSG Msg;
  919.         Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  920.         ULONG_PTR gdiplusToken;
  921.  
  922.     public:
  923.         Overlay(std::string name)
  924.             : class_name(name)
  925.         {
  926.             GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  927.  
  928.             wc.cbSize = sizeof(WNDCLASSEX);
  929.             wc.style = CS_HREDRAW | CS_VREDRAW;
  930.             wc.lpfnWndProc = WndProc;
  931.             wc.cbClsExtra = 0;
  932.             wc.cbWndExtra = 0;
  933.             wc.hInstance = GetModuleHandle(0);
  934.             wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  935.             wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  936.             wc.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(1, 0, 0)));//(COLOR_WINDOW + 1);
  937.             wc.lpszMenuName = NULL;
  938.             wc.lpszClassName = class_name.c_str();
  939.             wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  940.  
  941.             if (!RegisterClassEx(&wc))
  942.                 MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  943.  
  944.             hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST, class_name.c_str(), class_name.c_str(), NULL, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, GetModuleHandle(NULL), NULL);
  945.             SetLayeredWindowAttributes(hwnd, RGB(1, 0, 0), 255, LWA_COLORKEY | LWA_ALPHA);
  946.  
  947.             if (hwnd == NULL)
  948.                 MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  949.  
  950.             ShowWindow(hwnd, 10);
  951.             UpdateWindow(hwnd);
  952.  
  953.             while (GetMessage(&Msg, NULL, 0, 0) > 0)
  954.             {
  955.                 TranslateMessage(&Msg);
  956.                 DispatchMessage(&Msg);
  957.             }
  958.         }
  959.  
  960.  
  961.         ~Overlay()
  962.         {
  963.             Gdiplus::GdiplusShutdown(gdiplusToken);
  964.         }
  965.  
  966.  
  967.         static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  968.         {
  969.             HDC hdc;
  970.             PAINTSTRUCT ps;
  971.             BOOL draw = false;
  972.             POINT prev{ 0, 0 };
  973.  
  974.             switch (msg)
  975.             {
  976.             case WM_LBUTTONDOWN:
  977.                 draw = true;
  978.                 prev.x = LOWORD(lParam);
  979.                 prev.y = HIWORD(lParam);
  980.                 return 0L;
  981.                 break;
  982.             case WM_LBUTTONUP:
  983.                 if (draw)
  984.                 {
  985.                     draw = true;
  986.                     prev.x = LOWORD(lParam);
  987.                     prev.y = HIWORD(lParam);
  988.                 }
  989.                 draw = false;
  990.                 return 0L;
  991.                 break;
  992.             case WM_MOUSEMOVE:
  993.                 if (draw)
  994.                 {
  995.                     hdc = GetDC(hwnd);
  996.                     HPEN pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  997.                     HPEN oldPen = (HPEN)SelectObject(hdc, pen);
  998.                     MoveToEx(hdc, prev.x, prev.y, NULL);
  999.                     LineTo(hdc, prev.x = LOWORD(lParam), prev.y = HIWORD(lParam));
  1000.                 }
  1001.                 return 0L;
  1002.             case WM_PAINT:
  1003.                 hdc = BeginPaint(hwnd, &ps);
  1004.                 OnPaint(hdc);
  1005.                 EndPaint(hwnd, &ps);
  1006.                 ReleaseDC(hwnd, hdc);
  1007.                 break;
  1008.             case WM_CLOSE:
  1009.                 DestroyWindow(hwnd);
  1010.                 break;
  1011.             case WM_DESTROY:
  1012.                 PostQuitMessage(0);
  1013.                 break;
  1014.             default:
  1015.                 return DefWindowProc(hwnd, msg, wParam, lParam);
  1016.             }
  1017.             return 0;
  1018.         }
  1019.  
  1020.  
  1021.         static VOID OnPaint(HDC hdc)
  1022.         {
  1023.             Gdiplus::Graphics graphics(hdc);
  1024.             Gdiplus::Pen blackPen(Gdiplus::Color(255, 0, 0, 0), 5);
  1025.  
  1026.             Gdiplus::FontFamily fontFamily(L"Times New Roman");
  1027.             Gdiplus::SolidBrush blue_brush(Gdiplus::Color(255, 0, 0, 255));
  1028.             Gdiplus::SolidBrush green_brush(Gdiplus::Color(255, 0, 255, 0));
  1029.             Gdiplus::SolidBrush black_brush(Gdiplus::Color(255, 0, 0, 0));
  1030.             Gdiplus::Font header_font(&fontFamily, 20, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
  1031.             Gdiplus::Font text_font(&fontFamily, 15, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
  1032.  
  1033.             graphics.DrawRectangle(&blackPen, 2, 2, 100, 400);
  1034.             graphics.DrawString(L"Action:", -1, &header_font, Gdiplus::PointF{ 05.0f, 05.0f }, &blue_brush);
  1035.             graphics.DrawString(L"-", -1, &header_font, Gdiplus::PointF{ 05.0f, 20.0f }, &black_brush);
  1036.             graphics.DrawString(L"Mode:", -1, &header_font, Gdiplus::PointF{ 05.0f, 55.0f }, &blue_brush);
  1037.  
  1038.         }
  1039.     };
  1040.  
  1041. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement