1 | #include "panel.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | // #include <SDL2/SDL2_gfxPrimitives.h>
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | Panel::Panel(int x, int y, int width, int height, uint32_t color, SDL_Renderer& renderer) :
|
---|
10 | UIElement {x, y, width, height, renderer, nullptr, nullptr, nullptr},
|
---|
11 | color(color) {
|
---|
12 | this->texture = SDL_CreateTexture(&this->renderer, SDL_PIXELFORMAT_RGBA8888,
|
---|
13 | SDL_TEXTUREACCESS_TARGET, this->width, this->height);
|
---|
14 | if (this->texture == nullptr) {
|
---|
15 | cout << "Unable to create texture! SDL Error: " << SDL_GetError() << endl;
|
---|
16 | }
|
---|
17 | }
|
---|
18 |
|
---|
19 | Panel::~Panel() {
|
---|
20 | for (UIElement*& uiElement : this->uiElements) {
|
---|
21 | delete uiElement;
|
---|
22 | }
|
---|
23 |
|
---|
24 | if (this->texture != nullptr) {
|
---|
25 | SDL_DestroyTexture(this->texture);
|
---|
26 | this->texture = nullptr;
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | void Panel::addUIElement(UIElement* element) {
|
---|
31 | this->uiElements.push_back(element);
|
---|
32 | }
|
---|
33 |
|
---|
34 | void Panel::render(int x, int y) {
|
---|
35 | SDL_Texture* renderTarget = SDL_GetRenderTarget(&this->renderer);
|
---|
36 |
|
---|
37 | SDL_SetRenderTarget(&this->renderer, this->texture);
|
---|
38 |
|
---|
39 | // clear the texture
|
---|
40 | SDL_SetRenderDrawBlendMode(&this->renderer, SDL_BLENDMODE_NONE);
|
---|
41 | SDL_SetRenderDrawColor(&this->renderer, 0x00, 0x00, 0x00, 0x00);
|
---|
42 | SDL_RenderClear(&this->renderer);
|
---|
43 |
|
---|
44 | /*
|
---|
45 | roundedBoxRGBA(&this->renderer, 0, 0, this->width, this->height,
|
---|
46 | 8, 0x33, 0x33, 0x33, 0xFF);
|
---|
47 | */
|
---|
48 |
|
---|
49 | int borderThickness = 1;
|
---|
50 |
|
---|
51 | uint8_t colorR = (this->color >> 24) & 0xFF;
|
---|
52 | uint8_t colorG = (this->color >> 16) & 0xFF;
|
---|
53 | uint8_t colorB = (this->color >> 8) & 0xFF;
|
---|
54 | uint8_t colorA = this->color & 0xFF;
|
---|
55 |
|
---|
56 | /*
|
---|
57 | roundedBoxRGBA(&this->renderer, borderThickness, borderThickness,
|
---|
58 | this->width - borderThickness, this->height - borderThickness,
|
---|
59 | 8, colorR, colorG, colorB, colorA);
|
---|
60 | */
|
---|
61 |
|
---|
62 | for (UIElement*& uiElement : this->uiElements) {
|
---|
63 | uiElement->render(7, 7);
|
---|
64 | }
|
---|
65 |
|
---|
66 | SDL_SetRenderTarget(&this->renderer, renderTarget);
|
---|
67 | SDL_SetTextureBlendMode(this->texture, SDL_BLENDMODE_BLEND);
|
---|
68 |
|
---|
69 | SDL_Rect rect = { this->x + x, this->y + y, this->width, this->height };
|
---|
70 |
|
---|
71 | SDL_RenderCopy(&this->renderer, this->texture, nullptr, &rect);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void Panel::handleEvent(UIEvent& e) {
|
---|
75 | for (UIElement*& uiElement : this->uiElements) {
|
---|
76 | uiElement->handleEvent(e);
|
---|
77 | }
|
---|
78 | } |
---|