Advertisement
Masterchoc

Untitled

Jan 28th, 2018
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include "window.h"
  2. #include <iostream>
  3.  
  4. Window::Window(const std::string& text, glm::vec2& position, glm::vec2& size, const std::string& font) : Widget(position, size)
  5. {
  6.     m_header.text = text;
  7.     m_header.font = font;
  8.     m_header.fontSize = 20.0f;
  9.     m_header.blur = 0.0f;
  10.     m_header.align = NVG_ALIGN_CENTER;
  11.     m_header.color = glm::vec4(255.0f, 255.0f, 255.0f, 225.0f);
  12.     m_header.rect = new Rect(glm::vec2(position.x + 1, position.y + 1), glm::vec2(size.x - 2, 30));
  13.  
  14.     m_background = glm::vec4(28.0f, 30.0f, 34.0f, 192.0f);
  15.     m_borderRadius = 3.0f;
  16. }
  17.  
  18. void Window::draw(NVGcontext* ctx)
  19. {
  20.     if (m_dragged)
  21.     {
  22.         glm::vec2 pos = this->getPosition();
  23.         glm::vec2 size = this->getSize();
  24.         glm::vec2 diff = m_mouse - pos;
  25.         glm::vec2 delta = diff + pos;
  26.         this->setPosition(delta);
  27.     }
  28.  
  29.     m_background = m_hovered
  30.         ? glm::vec4(28.0f, 30.0f, 34.0f, 255.0f)
  31.         : glm::vec4(28.0f, 30.0f, 34.0f, 192.0f);
  32.  
  33.     nvgSave(ctx);
  34.  
  35.     // Window
  36.     nvgBeginPath(ctx);
  37.     nvgRoundedRect(ctx,
  38.         this->getPosition().x,
  39.         this->getPosition().y,
  40.         this->getSize().x,
  41.         this->getSize().y,
  42.         m_borderRadius
  43.     );
  44.  
  45.     nvgFillColor(ctx, nvgRGBA(
  46.         (unsigned char)m_background.r,
  47.         (unsigned char)m_background.g,
  48.         (unsigned char)m_background.b,
  49.         (unsigned char)m_background.a)
  50.     );
  51.     nvgFill(ctx);
  52.  
  53.     // Shadow
  54.     m_shadowPaint = nvgBoxGradient(
  55.         ctx,
  56.         this->getPosition().x,
  57.         this->getPosition().y + 2,
  58.         this->getSize().x,
  59.         this->getSize().y,
  60.         m_borderRadius * 2,
  61.         10,
  62.         nvgRGBA(0, 0, 0, 255),
  63.         nvgRGBA(0, 0, 0, 0)
  64.     );
  65.  
  66.     nvgBeginPath(ctx);
  67.     nvgRect(ctx,
  68.         this->getPosition().x - 10,
  69.         this->getPosition().y - 10,
  70.         this->getSize().x + 20,
  71.         this->getSize().y + 30
  72.     );
  73.  
  74.     nvgRoundedRect(ctx,
  75.         this->getPosition().x,
  76.         this->getPosition().y,
  77.         this->getSize().x,
  78.         this->getSize().y,
  79.         m_borderRadius
  80.     );
  81.  
  82.     nvgPathWinding(ctx, NVG_HOLE);
  83.     nvgFillPaint(ctx, m_shadowPaint);
  84.     nvgFill(ctx);
  85.  
  86.     m_header.rect->setPosition(glm::vec2(
  87.         this->getPosition().x + 1,
  88.         this->getPosition().y + 1)
  89.     );
  90.     m_header.rect->setSize(glm::vec2(this->getSize().x - 2, 30));
  91.  
  92.     // Header
  93.     m_headerPaint = nvgLinearGradient(ctx,
  94.         this->getPosition().x,
  95.         this->getPosition().y,
  96.         this->getPosition().x,
  97.         this->getPosition().y + 15,
  98.         nvgRGBA(255, 255, 255, 16),
  99.         nvgRGBA(0, 0, 0, 16)
  100.     );
  101.  
  102.     nvgBeginPath(ctx);
  103.     nvgRoundedRect(ctx,
  104.         m_header.rect->getPosition().x,
  105.         m_header.rect->getPosition().y,
  106.         m_header.rect->getSize().x,
  107.         m_header.rect->getSize().y,
  108.         m_borderRadius - 1
  109.     );
  110.     nvgFillPaint(ctx, m_headerPaint);
  111.     nvgFill(ctx);
  112.     nvgBeginPath(ctx);
  113.     nvgMoveTo(ctx,
  114.         this->getPosition().x + 0.5f,
  115.         this->getPosition().y + 0.5f + 30
  116.     );
  117.     nvgLineTo(ctx,
  118.         this->getPosition().x + 0.5f + this->getSize().x - 1,
  119.         this->getPosition().y + 0.5f + 30
  120.     );
  121.     nvgStrokeColor(ctx, nvgRGBA(0, 0, 0, 32));
  122.     nvgStroke(ctx);
  123.  
  124.     // Header text
  125.     nvgFontSize(ctx, m_header.fontSize);
  126.     nvgFontFace(ctx, m_header.font.c_str());
  127.     nvgTextAlign(ctx, m_header.align);
  128.     nvgFontBlur(ctx, m_header.blur);
  129.     nvgFillColor(ctx, nvgRGBA(
  130.         (unsigned char)m_header.color.r,
  131.         (unsigned char)m_header.color.g,
  132.         (unsigned char)m_header.color.b,
  133.         (unsigned char)m_header.color.a)
  134.     );
  135.  
  136.     nvgText(ctx,
  137.         this->getPosition().x + this->getSize().x * 0.5f,
  138.         this->getPosition().y + 16 + 1,
  139.         m_header.text.c_str(),
  140.         NULL
  141.     );
  142.  
  143.     nvgRestore(ctx);
  144. }
  145.  
  146. void Window::onKeyDown(const SDL_Keycode& keycode)
  147. {
  148.  
  149. }
  150.  
  151. void Window::onKeyUp(const SDL_Keycode& keycode)
  152. {
  153.  
  154. }
  155.  
  156. void Window::onMouseMove(const glm::vec2& mouse)
  157. {
  158.     m_mouse = mouse;
  159.     m_hovered = this->getRect()->intersects(mouse) ? true : false;
  160. }
  161.  
  162. void Window::onMouseDown(Uint8 button)
  163. {
  164.     m_hovered = this->getRect()->intersects(m_mouse) ? true : false;
  165.  
  166.     if (m_header.rect->intersects(m_mouse))
  167.         m_dragged = button == SDL_BUTTON_LEFT ? true : false;
  168. }
  169.  
  170. void Window::onMouseUp(Uint8 button)
  171. {
  172.     m_dragged = false;
  173.     m_hovered = this->getRect()->intersects(m_mouse) ? true : false;
  174. }
  175.  
  176. Window::~Window()
  177. {
  178.     delete m_header.rect;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement