source: opengl-game/gui/button.cpp@ e1f88a9

feature/imgui-sdl
Last change on this file since e1f88a9 was e1f88a9, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 4 years ago

Create a system to draw and switch between different screens, a Screen class, a MainScreen class that extends it, and some classes for UI elements that can be added to screens.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#include "button.hpp"
2
3#include <SDL2/SDL_ttf.h>
4#include <SDL2/SDL2_gfxPrimitives.h>
5
6#include "../vulkan-game.hpp"
7
8// TODO: Figure out a good way to return errors instead of just printing them
9// Probably throw an exception
10// Make sure to cleanup anythign that was initialized correctly before the error
11// TODO: Support better positioning options (e.g. align to left, right, top, bottom, center,
12// and offsets from those positions)
13Button::Button(string label, int x, int y, int padding, uint32_t color, uint32_t textColor,
14 VulkanGame& gameInfo, SDL_Renderer& renderer,
15 void (*onMouseClick)(VulkanGame& gameInfo),
16 void (*onMouseEnter)(UIElement& element),
17 void (*onMouseLeave)(UIElement& element)) :
18 UIElement(x, y, 0, 0, renderer, onMouseClick, onMouseEnter, onMouseLeave),
19 color(color),
20 focused(false),
21 gameInfo(gameInfo) {
22
23 SDL_Color sdlTextColor {
24 static_cast<Uint8>((textColor >> 24) & 0xFF),
25 static_cast<Uint8>((textColor >> 16) & 0xFF),
26 static_cast<Uint8>((textColor >> 8) & 0xFF),
27 static_cast<Uint8>(textColor & 0xFF)
28 };
29
30 SDL_Surface* labelSurface = TTF_RenderText_Blended(this->gameInfo.proggyFont, label.c_str(),
31 sdlTextColor);
32 if (labelSurface == nullptr) {
33 cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
34 }
35
36 this->labelTexture = SDL_CreateTextureFromSurface(&this->renderer, labelSurface);
37 if (this->labelTexture == nullptr) {
38 cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
39 // SDL_FreeSurface(labelSurface);
40 }
41
42 SDL_FreeSurface(labelSurface);
43
44 TTF_SizeText(this->gameInfo.proggyFont, label.c_str(), &this->labelWidth, &this->labelHeight);
45
46 this->width = this->labelWidth + padding;
47 this->height = this->labelHeight + padding;
48
49 uint32_t rgb = color & 0xFFFFFF00;
50 uint32_t a = color & 0xFF;
51
52 this->focusColor = (int)(rgb * 1.6) | a;
53}
54
55Button::~Button() {
56 if (this->labelTexture != nullptr) {
57 SDL_DestroyTexture(this->labelTexture);
58 this->labelTexture = nullptr;
59 }
60}
61
62void Button::init() {
63 this->focused = false;
64}
65
66void Button::render(int x, int y) {
67 uint32_t cur_color = this->focused ? this->focusColor : this->color;
68
69 uint8_t colorR = (cur_color >> 24) & 0xFF;
70 uint8_t colorG = (cur_color >> 16) & 0xFF;
71 uint8_t colorB = (cur_color >> 8) & 0xFF;
72 uint8_t colorA = cur_color & 0xFF;
73
74 boxRGBA(&this->renderer, this->x + x, this->y + y, this->x + this->width, this->y + this->height,
75 colorR, colorG, colorB, colorA);
76
77 SDL_Rect rect = {
78 this->x + (this->width - this->labelWidth) / 2 + x,
79 this->y + (this->height - this->labelHeight) / 2 + y,
80 this->labelWidth,
81 this->labelHeight
82 };
83
84 SDL_RenderCopy(&this->renderer, this->labelTexture, nullptr, &rect);
85}
86
87void Button::handleEvent(UIEvent& e) {
88 switch(e.type) {
89 case UI_EVENT_MOUSEMOTION:
90 if (this->x < e.mouse.x && e.mouse.x < this->x + this->width &&
91 this->y < e.mouse.y && e.mouse.y < this->y + this->height) {
92 if (!this->focused) {
93 this->focused = true;
94 if (this->onMouseEnter != nullptr) {
95 this->onMouseEnter(*this);
96 }
97 }
98 } else if (this->focused) {
99 this->focused = false;
100 if (this->onMouseLeave != nullptr) {
101 this->onMouseLeave(*this);
102 }
103 }
104 break;
105 case UI_EVENT_MOUSEBUTTONDOWN:
106 break;
107 case UI_EVENT_MOUSEBUTTONUP:
108 if (this->x < e.mouse.x && e.mouse.x < this->x + this->width &&
109 this->y < e.mouse.y && e.mouse.y < this->y + this->height) {
110 if (this->onMouseClick != nullptr) {
111 this->onMouseClick(this->gameInfo);
112 }
113 }
114 break;
115 default:
116 //cout << "Unhandled UI event: " << e.type << endl;
117 break;
118 }
119}
Note: See TracBrowser for help on using the repository browser.