Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void vgui_setup(void *fillrect, void *draw_text, void *mouse_pos, void *mouse_status) {
  2.     g_controllers.fillrect = fillrect;
  3.     g_controllers.draw_text = draw_text;
  4.     g_controllers.get_mouse_pos = mouse_pos;
  5.     g_controllers.get_mouse_button_status = mouse_status;
  6.  
  7.     g_vwindow_firts = NULL;
  8.     g_vwindow_last = NULL;
  9. }
  10.  
  11. /*int vgui_load_bmp_32pp(const char *path, vgui_bmp *vbmp) {
  12.     vgui_bmp_header file_header = {0};
  13.     vgui_bmp_infohdr file_hdrinfo = {0};
  14.     FILE *fp_bitmap = NULL;
  15.  
  16.     if ((fp_bitmap = fopen(path, "rb")) == NULL)
  17.         return VGUI_BMP_ERR_FILE_NO_FOUND;
  18.  
  19.     fread(&file_header, sizeof(file_header), 1, fp_bitmap);
  20.  
  21.     if (file_header.type != VGUI_HEADER_BMP_SIGNATURE)
  22.         return VGUI_BMP_ERR_IS_NOT_BMP;
  23.  
  24.     fread(&file_hdrinfo, sizeof(file_hdrinfo), 1, fp_bitmap);
  25.  
  26.     if (file_hdrinfo.compression != 0 )
  27.         return VGUI_BMP_ERR_IS_NOT_IMPLEMENTED;
  28.  
  29.     fseek(fp_bitmap, file_header.offbitmap, SEEK_SET);
  30.  
  31.     vbmp->bmp_array = malloc(file_hdrinfo.sizeofimage+1);
  32.     memset(vbmp->bmp_array, 0, file_hdrinfo.sizeofimage+1);
  33.  
  34.     if (vbmp->bmp_array == NULL)
  35.         return VGUI_BMP_ERR_IS_NOT_ALLOC_MEMORY;
  36.  
  37.     fread(vbmp->bmp_array, file_hdrinfo.sizeofimage, 1, fp_bitmap);
  38.  
  39.     vbmp->width = file_hdrinfo.width;
  40.     vbmp->height = file_hdrinfo.height;
  41.     vbmp->sizearray = file_hdrinfo.sizeofimage/4;
  42.  
  43.     fclose(fp_bitmap);
  44.     return VGUI_BMP_OK;
  45. }*/
  46.  
  47. void vgui_set_rect(int x, int y, int h, int w, vgui_rect *rect) {
  48.     rect->x = x;
  49.     rect->y = y;
  50.     rect->h = h;
  51.     rect->w = w;
  52. }
  53.  
  54. void vgui_set_rect_relative(vgui_container *container, vgui_rect *relative) {
  55.     if (container->is_relative == true && container->root->have_container == true) {
  56.         relative->x = container->root->rect_container.x+(container->rect.x-container->root->rect.x);
  57.         relative->y = container->root->rect_container.y+(container->rect.y-container->root->rect.y);
  58.     } else {
  59.         relative->x = container->rect.x;
  60.         relative->y = container->rect.y;
  61.     }
  62.  
  63.     relative->h = container->rect.h;
  64.     relative->w = container->rect.w;
  65. }
  66.  
  67. void vgui_callback_close_window(vgui_window *window) {
  68.     vgui_set_visible((hvgui) window, false);
  69. }
  70.  
  71. // printf(0, 0, "%rgbHola, que tal%rgb", 0xFFFFFF, 0xFFFF12);
  72. //void vgui_draw_text(int x, int y, const char *, ...) {
  73. //
  74. //}
  75.  
  76. hvgui vgui_create_button(hvgui hwindow, const char *text, int x, int y, int h, int w, void *fn_callback, bool relative) {
  77.     vgui_window *window = (vgui_window *) hwindow;
  78.     vgui_container *container = malloc(sizeof(vgui_container));
  79.  
  80.     if (container == NULL)
  81.         return 0;
  82.  
  83.     memset(container, 0, sizeof(vgui_container));
  84.     strncpy(container->text, text, sizeof(container->text));
  85.     vgui_set_rect(x+window->rect.x, y+window->rect.y, h, w, &(container->rect));
  86.     container->fn_callback = fn_callback;
  87.     container->type = VGUI_BUTTON;
  88.     container->root = window;
  89.     container->is_relative = relative;
  90.  
  91.     if (window->container_firts == NULL) window->container_firts = window->container_last = container;
  92.     else window->container_last = window->container_last->next = container;
  93.  
  94.     return (hvgui) container;
  95. }
  96.  
  97. hvgui vgui_create_checkbox(hvgui hwindow, int x, int y, bool default_status) {
  98.     vgui_window *window = (vgui_window *) hwindow;
  99.     vgui_container *container = malloc(sizeof(vgui_container));
  100.  
  101.     if (container == NULL)
  102.         return 0;
  103.  
  104.     container->is_press = default_status;
  105.     vgui_set_rect(x+window->rect.x, y+window->rect.y, 20, 20, &(container->rect));
  106.     container->type = VGUI_CHECKBOX;
  107.     container->root = window;
  108.     container->is_relative = true;
  109.  
  110.     if (window->container_firts == NULL) window->container_firts = window->container_last = container;
  111.     else window->container_last = window->container_last->next = container;
  112.  
  113.     return (hvgui) window;
  114. }
  115.  
  116. hvgui vgui_create_window(const char *title, int x, int y, int h, int w, const char withcontainer) {
  117.     vgui_window *window = malloc(sizeof(vgui_window));
  118.  
  119.     if (window == NULL)
  120.         return 0;
  121.  
  122.     memset(window, 0, sizeof(vgui_window));
  123.     strncpy(window->title, title, sizeof(window->title));
  124.     x = (x == VGUI_CENTER_SCREEN_X && w != 0) ? 960/2-w/2 : x;
  125.     y = (y == VGUI_CENTER_SCREEN_X && h != 0) ? 600/2-h/2 : x;
  126.     vgui_set_rect(x, y, h, w, &(window->rect));
  127.     if ((window->have_container = withcontainer) == true)
  128.         vgui_set_rect(window->rect.x+10, window->rect.y+40, window->rect.h-50, window->rect.w-20, &(window->rect_container));
  129.     window->is_visible = false;
  130.     window->is_active = true;
  131.     window->container_firts = window->container_last = NULL;
  132.     window->next = window->last = NULL;
  133.     vgui_create_button((hvgui) window, "X", window->rect.w-30, 8, 20, 20, vgui_callback_close_window, false);
  134.  
  135.     if (!g_vwindow_firts) g_vwindow_firts = g_vwindow_last = window;
  136.     else {
  137.             g_vwindow_last->is_active = false;
  138.             window->last = g_vwindow_last;
  139.             g_vwindow_last = g_vwindow_last->next = window;
  140.     }
  141.  
  142.     return (hvgui) window;
  143. }
  144.  
  145. void vgui_set_visible(hvgui hwindow, bool option) {
  146.     vgui_window *window = (vgui_window *) hwindow;
  147.     window->is_visible = option;
  148. }
  149.  
  150. void vgui_set_active(hvgui hwindow) {
  151.     vgui_window *window = (vgui_window *) hwindow;
  152.  
  153.     if (g_vwindow_last == NULL || window == g_vwindow_last)
  154.         return;
  155.  
  156.     g_vwindow_last->next = window;
  157.     if (window->last != NULL) window->last->next = window->next;
  158.     if (window->next->last != NULL) window->next->last = window->last;
  159.     window->last = g_vwindow_last;
  160.     window->next = NULL;
  161.     g_vwindow_last = window;
  162. }
  163.  
  164. void vgui_draw_box (vgui_rect *rect, uint8_t r, uint8_t g, uint8_t b) {
  165.     //if ( rect->x+rect->w > )
  166.     g_controllers.fillrect(rect->x, rect->y, rect->h, rect->w, r, g, b, 255); /*box*/
  167.     g_controllers.fillrect(rect->x, rect->y-1, 1, rect->w, 0, 0, 0, 255); /*border up*/
  168.     g_controllers.fillrect(rect->x-1, rect->y, rect->h, 1, 0, 0, 0, 255); /*border left*/
  169.     g_controllers.fillrect(rect->x, rect->y+rect->h, 1, rect->w, 0, 0, 0, 255); /*border buttom*/
  170.     g_controllers.fillrect(rect->x+rect->w, rect->y, rect->h, 1, 0, 0, 0, 255); /*border right*/
  171. }
  172.  
  173. void vgui_draw_triangle() {
  174.  
  175. }
  176.  
  177. unsigned int vgui_mouse_in_container(vgui_rect *rect) {
  178.     int x = 0, y = 0;
  179.     static bool last = false;
  180.     bool actual;
  181.  
  182.     actual = g_controllers.get_mouse_button_status(VK_LBUTTON);
  183.     g_controllers.get_mouse_pos(&x, &y);
  184.  
  185.     if (x > rect->x  && x < rect->x+rect->w && y > rect->y && y < rect->y+rect->h) {
  186.             if (last == true && actual == false) return VGUI_MOUSE_TOGGLE;
  187.             return VGUI_MOUSE_HOVER;
  188.     }
  189.  
  190.     return VGUI_MOUSE_NO_AREA;
  191. }
  192.  
  193. void vgui_draw_button(vgui_container *container) {
  194.     unsigned int mouse_event;
  195.     vgui_rect rect_relative = {0};
  196.  
  197.     vgui_set_rect_relative(container, &rect_relative);
  198.     mouse_event = vgui_mouse_in_container(&rect_relative);
  199.  
  200.     if (mouse_event == VGUI_MOUSE_TOGGLE) {
  201.             vgui_draw_box(&rect_relative, 19, 19, 19);
  202.             if ( container->fn_callback != NULL ) container->fn_callback(container->root);
  203.     }
  204.     else if (mouse_event == VGUI_MOUSE_HOVER) vgui_draw_box(&rect_relative, 23, 23, 23);
  205.     else vgui_draw_box(&rect_relative, 19, 19, 19);
  206.  
  207.     g_controllers.draw_text(rect_relative.x, rect_relative.y, container->text);
  208. }
  209.  
  210. void vgui_draw_checkbox(vgui_container *container) {
  211.     unsigned int mouse_event = 0;
  212.     vgui_rect rect_relative = {0};
  213.  
  214.     vgui_set_rect_relative(container, &rect_relative);
  215.     mouse_event = vgui_mouse_in_container(&rect_relative);
  216.  
  217.     if (mouse_event == VGUI_MOUSE_TOGGLE) {
  218.         if (container->is_press == true) container->is_press = false;
  219.         else if (container->is_press == false) container->is_press = true;
  220.     }
  221.  
  222.     if (container->is_press == false) vgui_draw_box(&rect_relative, 30, 30, 30);
  223.     else vgui_draw_box(&rect_relative, 15, 15, 15);
  224. }
  225.  
  226. void vgui_move_window(vgui_window *window) {
  227.     int x = 0, y = 0;
  228.     static int x_last = 0, y_last = 0;
  229.     vgui_rect rect = {0};
  230.  
  231.     vgui_set_rect(window->rect.x, window->rect.y, 20 /*padding*/, window->rect.w, &rect);
  232.     g_controllers.get_mouse_pos(&x, &y);
  233.  
  234.     if (vgui_mouse_in_container(&rect) == VGUI_MOUSE_HOVER) {
  235.         int nx = x - x_last, ny = y - y_last;
  236.  
  237.         window->rect.x += nx;
  238.         window->rect.y += ny;
  239.         window->rect_container.x += nx;
  240.         window->rect_container.y += ny;
  241.     }
  242.  
  243.     x_last = x;
  244.     y_last = y;
  245. }
  246.  
  247. void vgui_draw_window(vgui_window *window) {
  248.     vgui_container *containers = window->container_firts;
  249.  
  250.     vgui_draw_box(&(window->rect), 16, 16, 18);
  251.     vgui_draw_box(&(window->rect_container), 23, 23, 23);
  252.  
  253.     /*vgui_move_window(window);*/
  254.  
  255.     while (containers != NULL) {
  256.         switch (containers->type) {
  257.             case VGUI_BUTTON: vgui_draw_button(containers); break;
  258.             case VGUI_CHECKBOX: vgui_draw_checkbox(containers); break;
  259.         }
  260.  
  261.         containers = containers->next;
  262.     }
  263.  
  264.     g_controllers.draw_text(window->rect.x+10, window->rect.y+10, window->title);
  265.     /*g_draw_tex(window->rect.x, window->rect.y, window->title);*/
  266. }
  267.  
  268. void vgui_paint_all() {
  269.     vgui_window *windows = g_vwindow_firts;
  270.  
  271.     if (windows == NULL)
  272.         return;
  273.  
  274.     while (windows) {
  275.         if (windows->is_visible == true) vgui_draw_window(windows);
  276.         windows = windows->next;
  277.     }
  278.     /* ... */
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement