Simple GUI Library
component.h
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file component.h
4 * @date 2021-11-05
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#pragma once
10
11#include "sml/sml_graphics_wrapper.h"
12#include "../events/gui_event_dispatching.h"
13#include "../paint/shadow.h"
14
15namespace Sgl
16{
17 class Parent;
18 class Scene;
19
20 enum class Orientation
21 {
22 HORIZONTAL,
23 VERTICAL
24 };
25
26 class Component : public Sml::EventTarget
27 {
28 public:
29 /**
30 * @brief Used to determine if Component should be rendered and targeted by events.
31 *
32 * @note The least significant bit specifies interactability, and the following one
33 * specifies visibility.
34 */
35 enum class Visibility
36 {
37 INVISIBLE_DISABLED = 0b00,
38 INVISIBLE_INTERACTABLE = 0b01,
39 VISIBLE_DISABLED = 0b10,
40 VISIBLE_INTERACTABLE = 0b11
41 };
42
43 public:
44 static const int32_t USE_COMPUTED_SIZE = INT32_MIN;
45 static const int32_t UNLIMITED_SIZE = INT32_MAX;
46
47 public:
48 friend Scene;
49
50 Component() = default;
51 virtual ~Component() = default;
52
53 virtual Component* getHitComponent(int32_t x, int32_t y);
54 virtual void render(const Sml::Rectangle<int32_t>& targetRegion) = 0;
55 virtual void layout();
56 virtual void prerender();
57 virtual bool containsComponent(Component* component);
58
59 virtual Sml::EventDispatchChain* buildEventDispatchChain(Sml::EventDispatchChain* chain) override;
60
61 bool isVisible() const;
62 void setVisible(bool visible);
63
64 bool isInteractable() const;
65 void setInteractable(bool interactable);
66
67 Visibility getVisibility() const;
68 void setVisibility(Visibility visibility);
69
70 bool isFocused() const;
71 bool isHovered() const;
72
73 virtual bool isResizable() const = 0;
74
75 void requestFocus();
76 void requestDrag();
77
78 const Shadow& getShadow() const;
79 void setShadow(const ShadowSpecification* specification);
80
81 GuiEventDispatcher* getEventDispatcher();
82
83 Scene* getScene();
84 void setScene(Scene* scene);
85
86 const Parent* getParent() const;
87 Parent* getModifiableParent();
88 void setParent(Parent* parent);
89
90 Sml::Vec2i computeLocalToScenePos(const Sml::Vec2i& localPos) const;
91 Sml::Vec2i computeSceneToLocalPos(const Sml::Vec2i& scenePos) const;
92
93 Sml::Rectangle<int32_t> getOriginBounds() const;
94 const Sml::Rectangle<int32_t>& getLayoutBounds() const;
95 const Sml::Vec2i& getLayoutPos() const;
96 int32_t getLayoutX() const;
97 int32_t getLayoutY() const;
98 int32_t getLayoutWidth() const;
99 int32_t getLayoutHeight() const;
100
101 void setLayoutX(int32_t x);
102 void setLayoutY(int32_t y);
103 void setLayoutWidth(int32_t width);
104 void setLayoutHeight(int32_t height);
105
106 Sml::Vec2i computeScenePos();
107
108 virtual int32_t computePrefWidth(int32_t height = -1) const = 0;
109 virtual int32_t computePrefHeight(int32_t width = -1) const = 0;
110
111 virtual int32_t computeMinWidth(int32_t height = -1) const = 0;
112 virtual int32_t computeMinHeight(int32_t width = -1) const = 0;
113
114 virtual int32_t computeMaxWidth(int32_t height = -1) const = 0;
115 virtual int32_t computeMaxHeight(int32_t width = -1) const = 0;
116
117 protected:
118 Visibility m_Visibility = Visibility::VISIBLE_INTERACTABLE;
119 bool m_Focused = false;
120 bool m_Hovered = false;
121 Shadow m_Shadow;
122
123 GuiEventDispatcher m_Dispatcher;
124
125 Scene* m_Scene = nullptr;
126 Parent* m_Parent = nullptr;
127 Sml::Rectangle<int32_t> m_LayoutBounds = {{0, 0}, 0, 0};
128
129 void updateShadow();
130 void renderShadow(const Sml::Rectangle<int32_t>& targetRegion);
131 virtual void setSceneInSceneTree(Scene* scene);
132 };
133}
Visibility
Used to determine if Component should be rendered and targeted by events.
Definition: component.h:36