Skip to content

Commit

Permalink
Edit multiple tiles from Inspector.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianensis committed Sep 24, 2020
1 parent ef1e6ae commit 1ad03c2
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 72 deletions.
6 changes: 4 additions & 2 deletions code/include/Graphics/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Renderer: public Component {
Matrix4 mRenderereModelMatrix;
bool mPositionOffsetDirty;

bool mForceRecalculateVertices;

Array<Vector2>* mVertices;

Vector2 mRegionPosition;
Expand Down Expand Up @@ -171,9 +173,9 @@ class Renderer: public Component {

void renderCollider();

const Matrix4& getRendererModelMatrix();
const Array<Vector2>* getVertices(bool force = false);

const Array<Vector2>* getVertices();
void forceRecalculateVertices();
};

} /* namespace DE */
Expand Down
8 changes: 7 additions & 1 deletion code/include/Scene/Transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Transform: public Component {

bool mIsAffectedByProjection;

bool mForceModelMatrixCalculation;

public:

static const Vector3 smRight;
Expand Down Expand Up @@ -91,7 +93,7 @@ class Transform: public Component {
const Matrix4& getRotationMatrix();
const Matrix4& getScaleMatrix();

const Matrix4& getModelMatrix();
const Matrix4& getModelMatrix(bool force = false);

bool isDirtyTranslation() const;
void setDirtyTranslation(bool dirty);
Expand All @@ -106,6 +108,10 @@ class Transform: public Component {
mIsAffectedByProjection = affectedByProjection;
}

void forceModelMatrixCalculation() {
mForceModelMatrixCalculation = true;
}

// ---------------------------------------------------------------------------

};
Expand Down
10 changes: 8 additions & 2 deletions code/include/UI/UIElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class UIGroup;

class UIElement: public GameObject {

private:
protected:

FunctorVoid mOnPressedFunctor;
FunctorVoid mOnReleasedFunctor;

FunctorVoid mOnTextChangedFunctor;
FunctorVoid mOnFocusLostFunctor;

Collider* mCollider;
Expand Down Expand Up @@ -54,7 +55,7 @@ class UIElement: public GameObject {
void onPressed();
void onReleased();

void onFocusLost();
virtual void onFocusLost();
void onFocus();

void setOnPressedCallback(std::function<void()> callback) {
Expand All @@ -66,6 +67,11 @@ class UIElement: public GameObject {
}

void setOnTextChangedCallback(std::function<void()> callback) {
//mOnFocusLostFunctor.setCallback(callback);
mOnTextChangedFunctor.setCallback(callback);
}

void setOnFocusLostCallback(std::function<void()> callback) {
mOnFocusLostFunctor.setCallback(callback);
}

Expand Down
1 change: 1 addition & 0 deletions code/include/UI/UITextEditable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class UITextEditable: public UIText {
DE_CLASS(UITextEditable, UIText);

virtual void init();
virtual void onFocusLost();

};

Expand Down
2 changes: 1 addition & 1 deletion code/source/Core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void Engine::run() {
}

Time::getInstance()->endFrame();
std::cout << " " << 1.0f/Time::getInstance()->getDeltaTimeSeconds() << std::endl;
//std::cout << " " << 1.0f/Time::getInstance()->getDeltaTimeSeconds() << std::endl;


}
Expand Down
2 changes: 1 addition & 1 deletion code/source/Graphics/Batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Batch::Batch() : DE_Class() {
mVBOPosition = 0;
mEBO = 0;
mVBOTexture = 0;
//mVBOColor = 0;
mVBOColor = 0;
mVBONormal = 0;
mVAO = 0;
mMesh = nullptr;
Expand Down
24 changes: 13 additions & 11 deletions code/source/Graphics/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Renderer::Renderer() : Component() {

mChunk = nullptr;
mIsAlreadyInBatch = false;

mForceRecalculateVertices = false;
}

Renderer::~Renderer() {
Expand Down Expand Up @@ -85,6 +87,8 @@ void Renderer::init() {
mVertices->set(2, Vector2(0, 0)); // RIGHT BOTTOM
mVertices->set(3, Vector2(0, 0)); // RIGHT TOP

mForceRecalculateVertices = false;

}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -195,37 +199,35 @@ void Renderer::renderCollider() {
}
}

// ---------------------------------------------------------------------------
const Array<Vector2>* Renderer::getVertices(bool force /*= false*/) {

const Matrix4& Renderer::getRendererModelMatrix() {
if (mPositionOffsetDirty || !isStatic()) {
if(mPositionOffsetDirty || !isStatic() || force || mForceRecalculateVertices ){

mRenderereModelMatrix.translation(mPositionOffset);

mRenderereModelMatrix.mul(getGameObject()->getTransform()->getModelMatrix());
mPositionOffsetDirty = false;
}

return mRenderereModelMatrix;
};

const Array<Vector2>* Renderer::getVertices() {

if(mPositionOffsetDirty || !isStatic()){
FOR_ARRAY(i, mVertices) {
Vector3 vertexPosition(
mMesh->getVertices()->get(i*3 + 0),
mMesh->getVertices()->get(i*3 + 1),
mMesh->getVertices()->get(i*3 + 2));

vertexPosition = getRendererModelMatrix().mulVector(Vector4(vertexPosition,1));
vertexPosition = mRenderereModelMatrix.mulVector(Vector4(vertexPosition,1));

mVertices->set(i, vertexPosition);
}

mForceRecalculateVertices = false;
}

return mVertices;
}

void Renderer::forceRecalculateVertices() {
getGameObject()->getTransform()->forceModelMatrixCalculation();
mForceRecalculateVertices = true;
}

} /* namespace DE */
3 changes: 2 additions & 1 deletion code/source/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void Scene::init() {
ortho(-50.0, 50.0, -50.0 / aspect, 50.0 / aspect, 1.0, -1.0);
*/
// f32 aspect = RenderContext::getAspectRatio();
cameraComponent->setOrtho(-720, 720, -720, 720, 1, -1);
f32 size = RenderContext::getWindowSize().y;
cameraComponent->setOrtho(-size, size, -size, size, 1, -1);

setCameraGameObject(cameraGameObject);

Expand Down
7 changes: 5 additions & 2 deletions code/source/Scene/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Transform::Transform() : Component() {

mIsAffectedByProjection = true;
mModelMatrixGenerated = false;
mForceModelMatrixCalculation = false;
}

Transform::~Transform() {
Expand All @@ -40,6 +41,7 @@ void Transform::init() {
mScale = Vector3(1.0f, 1.0f, 1.0f);

mModelMatrixGenerated = false;
mForceModelMatrixCalculation = false;
}

bool Transform::isDirtyTranslation() const {
Expand Down Expand Up @@ -163,8 +165,8 @@ const Matrix4& Transform::getScaleMatrix() {
return mScaleMatrix;
}

const Matrix4& Transform::getModelMatrix() {
if (!isStatic() || (isStatic() && !mModelMatrixGenerated)) {
const Matrix4& Transform::getModelMatrix(bool force /*= false*/) {
if (!isStatic() || (isStatic() && !mModelMatrixGenerated) || force || mForceModelMatrixCalculation) {
mModelMatrix.init(getTranslationMatrix());
Matrix4 rotationMatrix(getRotationMatrix());
Matrix4 scaleMatrix(getScaleMatrix());
Expand All @@ -173,6 +175,7 @@ const Matrix4& Transform::getModelMatrix() {
mModelMatrix.mul(scaleMatrix);

mModelMatrixGenerated = true;
mForceModelMatrixCalculation = false;
}

return mModelMatrix;
Expand Down
34 changes: 17 additions & 17 deletions code/source/UI/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ void UIElement::init() {

void UIElement::onDestroy() {
GameObject::onDestroy();
//DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyPressed);
//DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyReleased);
DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyPressed);
DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyReleased);
DE_UNSUBSCRIBE_TO_EVENT(InputEventMouseButtonPressed);
//DE_UNSUBSCRIBE_TO_EVENT(InputEventMouseButtonReleased);
//DE_UNSUBSCRIBE_TO_EVENT(InputEventChar);
//DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyEnter);
//DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyEsc);
DE_UNSUBSCRIBE_TO_EVENT(InputEventMouseButtonReleased);
DE_UNSUBSCRIBE_TO_EVENT(InputEventChar);
DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyEnter);
DE_UNSUBSCRIBE_TO_EVENT(InputEventKeyEsc);
}

// ---------------------------------------------------------------------------

void UIElement::subscribeToKeyEvents() {
/*DE_SUBSCRIBE_TO_EVENT(InputEventKeyPressed, [this](const Event* event){
DE_SUBSCRIBE_TO_EVENT(InputEventKeyPressed, [this](const Event* event){
if(isActive()){

}
Expand All @@ -61,16 +61,16 @@ void UIElement::subscribeToKeyEvents() {
if(isActive()){

}
});*/
});
}

void UIElement::subscribeToCharEvents() {
/*DE_SUBSCRIBE_TO_EVENT(InputEventChar, [this](const Event* event){
DE_SUBSCRIBE_TO_EVENT(InputEventChar, [this](const Event* event){
if(isActive()){
// TODO : boolean to enable or disable : can receive char input?
onChar(((const InputEventChar*)event)->mChar);
}
});*/
});
}

void UIElement::subscribeToMouseButtonEvents() {
Expand All @@ -85,27 +85,27 @@ void UIElement::subscribeToMouseButtonEvents() {
}
});

/*DE_SUBSCRIBE_TO_EVENT(InputEventMouseButtonReleased, [this](const Event* event){
DE_SUBSCRIBE_TO_EVENT(InputEventMouseButtonReleased, [this](const Event* event){
if(isActive()){

}
});*/
});
}

void UIElement::subscribeToEnterEvent() {
/*DE_SUBSCRIBE_TO_EVENT(InputEventKeyEnter, [this](const Event* event){
DE_SUBSCRIBE_TO_EVENT(InputEventKeyEnter, [this](const Event* event){
if(isActive()){
onFocusLost(); // TODO : call something more generic
}
});*/
});
}

void UIElement::subscribeToEscEvent() {
/*DE_SUBSCRIBE_TO_EVENT(InputEventKeyEsc, [this](const Event* event){
DE_SUBSCRIBE_TO_EVENT(InputEventKeyEsc, [this](const Event* event){
if(isActive()){
onFocusLost(); // TODO : call something more generic
}
});*/
});
}

// ---------------------------------------------------------------------------
Expand All @@ -124,7 +124,7 @@ void UIElement::onChar(c8 character) {
void UIElement::onFocusLost() {
if(hasFocus()){
mOnFocusLostFunctor.execute();
mInputString.clear();
UI::getInstance()->setFocusedElement(nullptr);
}
}

Expand Down
11 changes: 11 additions & 0 deletions code/source/UI/UITextEditable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@ void UITextEditable::init() {

// ---------------------------------------------------------------------------

void UITextEditable::onFocusLost() {
if(hasFocus()){
UIText::onFocusLost();

mOnTextChangedFunctor.execute();
mInputString.clear();

UIText::onFocusLost();
}
}

} /* namespace DE */
2 changes: 1 addition & 1 deletion config/engine.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scene.defaultSize=2000
scene.defaultSize=4000
scene.chunks.gridSize=6
scene.quadTreeMinSize=500
scene.maxNewObjectsToSpawn=20
Expand Down
9 changes: 9 additions & 0 deletions tools/map_editor/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,13 @@ void Grid::loadMapIntoGrid(const List<GameObject*>* gameObjects) {
}
}

void Grid::forEachSelectedTile(std::function<void(GameObject*)> callback) {
FOR_LIST(it, getSelectedTiles()){
GameObject* tile = it.get();
if(tile){
callback(tile);
}
}
}

} /* namespace DE */
3 changes: 3 additions & 0 deletions tools/map_editor/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Vector2.hpp"
#include "MapEditorUI.hpp"
#include <string>
#include "Functor.hpp"

#include "EditorEvents.hpp"

Expand Down Expand Up @@ -71,6 +72,8 @@ class Grid: public DE_Class {
f32 getGridTileSize() const { return mGridTileSize; }

void loadMapIntoGrid(const List<GameObject*>* gameObjects);

void forEachSelectedTile(std::function<void(GameObject*)> callback);
};

} /* namespace DE */
Expand Down
9 changes: 7 additions & 2 deletions tools/map_editor/MapEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,15 @@ void MapEditor::cameraZoom() {
f32 scroll = Input::getInstance()->getScroll();
mZoom += std::fabs(scroll) * 0.05f * Time::getInstance()->getDeltaTimeSeconds();

f32 finalZoom = mZoom;
if(Input::getInstance()->isKeyPressed(GLFW_KEY_LEFT_CONTROL)) {
finalZoom = mZoom * 2.0f;
}

if (scroll == 1) {
mCamera->setZoom(1.0f / mZoom);
mCamera->setZoom(1.0f / finalZoom);
} else if (scroll == -1) {
mCamera->setZoom(mZoom);
mCamera->setZoom(finalZoom);
}
}

Expand Down
Loading

0 comments on commit 1ad03c2

Please sign in to comment.