Skip to content

Commit

Permalink
Widgets::Scene: inherit Scene, fix docs and improve naming
Browse files Browse the repository at this point in the history
This commit improves readability and maintainability of
the project by unifying the interface via inheritance.

Also, local functions were made static.
  • Loading branch information
congard committed Sep 29, 2023
1 parent 806f76d commit ac7ddd6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
34 changes: 19 additions & 15 deletions include/common/algine/core/widgets/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algine/core/painter/Painter.h>
#include <algine/core/math/Size.h>
#include <algine/core/unified/UnifiedEventHandler.h>
#include <algine/core/scene/Scene.h>

namespace algine {
class Widget;
Expand All @@ -20,37 +21,28 @@ class Layer;
* <ol>
* <li>Inherit your custom scene from <code>algine::Widgets::Scene</code></li>
* <li>Load layers (make them as children of the scene)</li>
* <li>Add listeners to <code>XEventHandler</code>: call <code>listen(handler)</code>
* <li>Add listeners to <code>UnifiedEventHandler</code>: call <code>listen(handler)</code>
* <li>Call <code>setSize(width, height)</code></li>
* <li>Show your start layer: <code>scene->showLayer(scene->myStartLayer)</code></li>
* <li>In the render loop draw UI: <code>scene.draw()</code></li>
* <li>In the render loop draw UI: <code>scene.render()</code></li>
* </ol>
*/
class AL_EXPORT Scene: public Object {
class AL_EXPORT Scene: public algine::Scene {
public:
explicit Scene(Object *parent = defaultParent());
Scene(int width, int height, Object *parent = defaultParent());
~Scene() override = default;

/**
* Draws layers from level <code>begin</code> to <code>end</code>
* <br>If <code>end</code> is less than zero, it will be set to
* layers count
* @param begin
* @param end
*/
void draw(int begin = 0, int end = -1);

void showLayer(Layer *layer, int level = -1);
void showLayerInsteadOf(Layer *replacement, Layer *src);
void replaceLayer(Layer *src, Layer *replacement);
void hideLayer(Layer *layer);
void hideLevel(int level);
void hideLevelAt(int level);

Layer* layerAt(int level) const;
Layer* getTopLayer() const;

int getLevel(const Layer *layer) const;
int getLayersCount() const;
int getLayerCount() const;

/**
* @note ownership of the object will not be transferred to the scene
Expand Down Expand Up @@ -82,7 +74,19 @@ class AL_EXPORT Scene: public Object {

Painter* getPainter() const;

protected:
void onRender() override;

private:
/**
* Draws layers from level <code>begin</code> to <code>end</code>
* <br>If <code>end</code> is less than zero, it will be set to
* layers count
* @param begin
* @param end
*/
void draw(int begin = 0, int end = -1);

bool handlePointerEvent(const Event &event);
bool handleKeyboardEvent(const Event &event);

Expand Down
20 changes: 12 additions & 8 deletions src/common/core/widgets/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace tulz;

namespace algine::Widgets {
Scene::Scene(Object *parent)
: Object(parent),
: algine::Scene(parent),
m_painter(new Painter(this))
{
m_options.painter = m_painter;
Expand All @@ -28,9 +28,9 @@ Scene::Scene(int width, int height, Object *parent)
* @param insert must be set to false for non-opaque layers
* @complexity <code>O(NlogN)</code>
* @return <code>true</code> if <code>rect</code> is visible
* and was added to the heap, otherwise <code>false</code>
* (and was added to the heap, if specified), otherwise <code>false</code>
*/
bool placeRect(std::vector<RectI> &heap, const RectI &rect, bool insert) {
static bool placeRect(std::vector<RectI> &heap, const RectI &rect, bool insert) {
if (heap.empty()) {
heap.emplace_back(rect);
return true;
Expand Down Expand Up @@ -131,7 +131,7 @@ void Scene::showLayer(Layer *layer, int level) {
layer->onShow();
}

void Scene::showLayerInsteadOf(Layer *replacement, Layer *src) {
void Scene::replaceLayer(Layer *src, Layer *replacement) {
assert(src->getParent() == this);
assert(replacement->getParent() == this);

Expand All @@ -154,10 +154,10 @@ void Scene::hideLayer(Layer *layer) {
if (level == -1)
throw std::runtime_error("Layer was not found at this Scene");

hideLevel(level);
hideLevelAt(level);
}

void Scene::hideLevel(int level) {
void Scene::hideLevelAt(int level) {
m_layers[level]->onHide();
m_layers.erase(m_layers.begin() + level);
}
Expand All @@ -175,7 +175,7 @@ int Scene::getLevel(const Layer *layer) const {
return it == m_layers.end() ? -1 : static_cast<int>(std::distance(m_layers.begin(), it));
}

int Scene::getLayersCount() const {
int Scene::getLayerCount() const {
return static_cast<int>(m_layers.size());
}

Expand Down Expand Up @@ -254,13 +254,17 @@ Painter* Scene::getPainter() const {
return m_painter;
}

void Scene::onRender() {
draw();
}

bool Scene::handlePointerEvent(const Event &event) {
auto &info = event.getPointerInfo();
PointI globalPoint {(int) info.getX(), (int) info.getY()};

// returns a widget on the scene at globalPoint
auto findWidget = [this, &globalPoint]() -> Widget* {
for (int i = getLayersCount() - 1; i >= 0; --i) {
for (int i = getLayerCount() - 1; i >= 0; --i) {
auto container = layerAt(i)->getContainer();
PointI container_lp = container->mapFromGlobal(globalPoint);

Expand Down

0 comments on commit ac7ddd6

Please sign in to comment.