Skip to content

Commit

Permalink
Merge pull request ddnet#7308 from Marmare314/add-editor-object
Browse files Browse the repository at this point in the history
Add `CEditorObject` class
  • Loading branch information
Robyt3 authored Oct 7, 2023
2 parents d6aa18e + 719b2bd commit 093edb7
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 142 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,8 @@ if(CLIENT)
component.h
editor.cpp
editor.h
editor_object.cpp
editor_object.h
explanations.cpp
map_grid.cpp
map_grid.h
Expand Down
73 changes: 0 additions & 73 deletions src/game/editor/component.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
#include "component.h"

#include "editor.h"

void CEditorComponent::Init(CEditor *pEditor)
{
m_pEditor = pEditor;
OnReset();
}

void CEditorComponent::OnUpdate(CUIRect View)
{
OnRender(View);
if(IsActive())
OnActive();
else if(IsHot())
OnHot();
}

bool CEditorComponent::OnInput(const IInput::CEvent &Event)
{
for(CEditorComponent &Component : m_vSubComponents)
Expand All @@ -27,48 +10,6 @@ bool CEditorComponent::OnInput(const IInput::CEvent &Event)
return false;
}

void CEditorComponent::OnRender(CUIRect View) {}

void CEditorComponent::OnReset() {}
void CEditorComponent::OnMapLoad() {}

bool CEditorComponent::IsHot()
{
return UI()->HotItem() == this;
}

void CEditorComponent::SetHot()
{
UI()->SetHotItem(this);
}

void CEditorComponent::UnsetHot()
{
if(IsHot())
UI()->SetHotItem(nullptr);
}

void CEditorComponent::OnHot() {}

bool CEditorComponent::IsActive()
{
return UI()->CheckActiveItem(this);
}

void CEditorComponent::SetActive()
{
SetHot();
UI()->SetActiveItem(this);
}

void CEditorComponent::SetInactive()
{
if(IsActive())
UI()->SetActiveItem(nullptr);
}

void CEditorComponent::OnActive() {}

void CEditorComponent::InitSubComponents()
{
for(CEditorComponent &Component : m_vSubComponents)
Expand All @@ -81,17 +22,3 @@ void CEditorComponent::RegisterSubComponent(CEditorComponent &Component)
{
m_vSubComponents.emplace_back(Component);
}

CEditor *CEditorComponent::Editor() { return m_pEditor; }
const CEditor *CEditorComponent::Editor() const { return m_pEditor; }
IInput *CEditorComponent::Input() { return m_pEditor->Input(); }
IClient *CEditorComponent::Client() { return m_pEditor->Client(); }
CConfig *CEditorComponent::Config() { return m_pEditor->Config(); }
IConsole *CEditorComponent::Console() { return m_pEditor->Console(); }
IEngine *CEditorComponent::Engine() { return m_pEditor->Engine(); }
IGraphics *CEditorComponent::Graphics() { return m_pEditor->Graphics(); }
ISound *CEditorComponent::Sound() { return m_pEditor->Sound(); }
ITextRender *CEditorComponent::TextRender() { return m_pEditor->TextRender(); }
IStorage *CEditorComponent::Storage() { return m_pEditor->Storage(); }
CUI *CEditorComponent::UI() { return m_pEditor->UI(); }
CRenderTools *CEditorComponent::RenderTools() { return m_pEditor->RenderTools(); }
77 changes: 8 additions & 69 deletions src/game/editor/component.h
Original file line number Diff line number Diff line change
@@ -1,91 +1,30 @@
#ifndef GAME_EDITOR_COMPONENT_H
#define GAME_EDITOR_COMPONENT_H

#include <functional>
#include <game/client/ui.h>
#include "editor_object.h"

class CEditor;
class IInput;
class IClient;
class CConfig;
class IConsole;
class IEngine;
class IGraphics;
class ISound;
class ITextRender;
class IStorage;
class CRenderTools;
#include <vector>

class CEditorComponent
class CEditorComponent : public CEditorObject
{
public:
virtual ~CEditorComponent() = default;

/**
* Initialise the component and interface pointers.
* Needs to be the first function that is called.
*/
virtual void Init(CEditor *pEditor);

/**
* Calls `OnRender` and then maybe `OnHot` or `OnActive`.
*/
void OnUpdate(CUIRect View);

/**
* Gets called before `OnRender`. Should return true
* if the event was consumed.
*/
virtual bool OnInput(const IInput::CEvent &Event);

virtual void OnRender(CUIRect View);

/**
* Gets called after `OnRender` when the component is hot but not active.
* I
* if the event was consumed. By default the events
* are forwarded to the subcomponents.
*/
virtual void OnHot();

/**
* Gets called after `OnRender` when the component is active.
*/
virtual void OnActive();

virtual void OnReset();
virtual void OnMapLoad();

bool IsHot();
void SetHot();
void UnsetHot();

bool IsActive();
void SetActive();
void SetInactive();
virtual bool OnInput(const IInput::CEvent &Event) override;

/**
* Initialise all registered subcomponents.
* Needs to be called after the interfaces have been initialised.
*/
void InitSubComponents();
void RegisterSubComponent(CEditorComponent &Component);

CEditor *Editor();
const CEditor *Editor() const;
IInput *Input();
IClient *Client();
CConfig *Config();
IConsole *Console();
IEngine *Engine();
IGraphics *Graphics();
ISound *Sound();
ITextRender *TextRender();
IStorage *Storage();
CUI *UI();
CRenderTools *RenderTools();
// Register a new subcomponent.
void RegisterSubComponent(CEditorComponent &Component);

private:
CEditor *m_pEditor;

std::vector<std::reference_wrapper<CEditorComponent>> m_vSubComponents = {};
};

Expand Down
79 changes: 79 additions & 0 deletions src/game/editor/editor_object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "editor_object.h"

#include "editor.h"

void CEditorObject::Init(CEditor *pEditor)
{
m_pEditor = pEditor;
OnReset();
}

void CEditorObject::OnUpdate(CUIRect View)
{
OnRender(View);
if(IsActive())
OnActive();
else if(IsHot())
OnHot();
}

bool CEditorObject::OnInput(const IInput::CEvent &Event)
{
return false;
}

void CEditorObject::OnRender(CUIRect View) {}

void CEditorObject::OnReset() {}
void CEditorObject::OnMapLoad() {}

bool CEditorObject::IsHot()
{
return UI()->HotItem() == this;
}

void CEditorObject::SetHot()
{
UI()->SetHotItem(this);
}

void CEditorObject::UnsetHot()
{
if(IsHot())
UI()->SetHotItem(nullptr);
}

void CEditorObject::OnHot() {}

bool CEditorObject::IsActive()
{
return UI()->CheckActiveItem(this);
}

void CEditorObject::SetActive()
{
SetHot();
UI()->SetActiveItem(this);
}

void CEditorObject::SetInactive()
{
if(IsActive())
UI()->SetActiveItem(nullptr);
}

void CEditorObject::OnActive() {}

CEditor *CEditorObject::Editor() { return m_pEditor; }
const CEditor *CEditorObject::Editor() const { return m_pEditor; }
IInput *CEditorObject::Input() { return m_pEditor->Input(); }
IClient *CEditorObject::Client() { return m_pEditor->Client(); }
CConfig *CEditorObject::Config() { return m_pEditor->Config(); }
IConsole *CEditorObject::Console() { return m_pEditor->Console(); }
IEngine *CEditorObject::Engine() { return m_pEditor->Engine(); }
IGraphics *CEditorObject::Graphics() { return m_pEditor->Graphics(); }
ISound *CEditorObject::Sound() { return m_pEditor->Sound(); }
ITextRender *CEditorObject::TextRender() { return m_pEditor->TextRender(); }
IStorage *CEditorObject::Storage() { return m_pEditor->Storage(); }
CUI *CEditorObject::UI() { return m_pEditor->UI(); }
CRenderTools *CEditorObject::RenderTools() { return m_pEditor->RenderTools(); }
86 changes: 86 additions & 0 deletions src/game/editor/editor_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef GAME_EDITOR_EDITOR_OBJECT_H
#define GAME_EDITOR_EDITOR_OBJECT_H

#include <functional>

#include <engine/input.h>
#include <game/client/ui_rect.h>

class CUI;
class CEditor;
class IClient;
class CConfig;
class IConsole;
class IEngine;
class IGraphics;
class ISound;
class ITextRender;
class IStorage;
class CRenderTools;

class CEditorObject
{
public:
virtual ~CEditorObject() = default;

/**
* Initialise the component and interface pointers.
* Needs to be the first function that is called.
* The default implentation also resets the component.
*/
virtual void Init(CEditor *pEditor);

/**
* Calls `OnRender` and then maybe `OnHot` or `OnActive`.
*/
void OnUpdate(CUIRect View);

/**
* Gets called before `OnRender`. Should return true
* if the event was consumed.
*/
virtual bool OnInput(const IInput::CEvent &Event);

virtual void OnRender(CUIRect View);

/**
* Gets called after `OnRender` when the component is hot but not active.
* I
*/
virtual void OnHot();

/**
* Gets called after `OnRender` when the component is active.
*/
virtual void OnActive();

virtual void OnReset();
virtual void OnMapLoad();

bool IsHot();
void SetHot();
void UnsetHot();

bool IsActive();
void SetActive();
void SetInactive();

CEditor *Editor();
const CEditor *Editor() const;
IInput *Input();
IClient *Client();
CConfig *Config();
IConsole *Console();
IEngine *Engine();
IGraphics *Graphics();
ISound *Sound();
ITextRender *TextRender();
IStorage *Storage();
CUI *UI();
CRenderTools *RenderTools();

private:
CEditor *m_pEditor;
};

#endif

0 comments on commit 093edb7

Please sign in to comment.