From 0a2965e26b7edd8578ce557e09a2c93e6b4b23cb Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sat, 21 Oct 2023 00:33:49 +0200 Subject: [PATCH] editor: fix multi-select, scaling, sideways-scaling, texcoords --- Aquaria/SceneEditor.cpp | 144 +++++++++++++++++++++++++--------------- Aquaria/TileMgr.cpp | 2 +- BBGE/Quad.cpp | 18 +++++ BBGE/Quad.h | 8 ++- 4 files changed, 114 insertions(+), 58 deletions(-) diff --git a/Aquaria/SceneEditor.cpp b/Aquaria/SceneEditor.cpp index d919874..0b69282 100644 --- a/Aquaria/SceneEditor.cpp +++ b/Aquaria/SceneEditor.cpp @@ -111,11 +111,12 @@ static void tileToQuad(Quad *q, const TileData& t) { q->setRepeatScale(Vector(t.rep->texscaleX, t.rep->texscaleY)); q->repeatTextureToFill(true); + q->setOverrideTexCoords(t.getTexcoords()); } - q->texcoords = t.getTexcoords(); q->renderBorder = true; q->renderBorderColor = TileRender::GetTagColor(t.tag); q->borderAlpha = 1.0f; + q->update(0); // to prevent 1 frame of lag when setting } static void quadToTile(TileData& t, const Quad *q) @@ -134,6 +135,7 @@ class MultiTileHelper : public Quad TileStorage& _ts; std::vector _indices; std::vector _quads; + Vector lastScale; MultiTileHelper(TileStorage& ts, const size_t *indices, size_t n) : Quad() @@ -191,10 +193,50 @@ public: { } - void onUpdate(float dt) + void changeRepeatScale(const Vector& rs) + { + size_t n = _indices.size(); + assert(n == 1); + TileData& t = _ts.tiles[_indices[0]]; + if((t.flags & TILEFLAG_REPEAT) && t.rep) + { + this->setRepeatScale(rs); + t.rep->texscaleX = rs.x; + t.rep->texscaleY = rs.y; + t.refreshRepeat(); + this->setOverrideTexCoords(t.getTexcoords()); + } + } + + virtual void onUpdate(float dt) OVERRIDE { - //for(size_t i = 0; i < _quads.size(); ++i) - // _quads[i]->refreshRepeatTextureToFill(); + if(scale != lastScale) + { + lastScale = scale; + if(_indices.size() == 1) + { + TileData& t = _ts.tiles[_indices[0]]; + t.scalex = scale.x; + t.scaley = scale.y; + t.refreshRepeat(); + this->setOverrideTexCoords(t.getTexcoords()); + } + else + { + for(size_t i = 0; i < _quads.size(); ++i) + { + TileData& t = _ts.tiles[_indices[i]]; + Quad *q = _quads[i]; + Vector s = q->getRealScale(); + t.scalex = s.x; + t.scaley = s.y; + t.refreshRepeat(); + q->setOverrideTexCoords(t.getTexcoords()); + } + } + } + + Quad::onUpdate(dt); } void finish() @@ -218,6 +260,7 @@ public: } alphaMod = 0; this->safeKill(); // also deletes all children + _ts.refreshAll(); } }; @@ -1078,17 +1121,14 @@ void SceneEditor::enterAnyStateHelper(EditorStates newstate) state = newstate; if (editType == ET_ELEMENTS) { + oldRepeatScale = Vector(1, 1); // not handled for multi-selection + if(selectedTiles.size() == 1) { const TileData& t = getCurrentLayerTiles().tiles[selectedTiles[0]]; - oldRepeatScale = Vector(1,1); - if(t.flags & TILEFLAG_REPEAT) + if(t.flags & TILEFLAG_REPEAT && t.rep) oldRepeatScale = Vector(t.rep->texscaleX, t.rep->texscaleY); } - else - { - oldRepeatScale = Vector(1, 1); // not handled for multi-selection - } MultiTileHelper *m = createMultiTileHelperFromSelection(); oldScale = m->scale; @@ -1676,13 +1716,18 @@ void SceneEditor::updateMultiSelect() clearSelection(); - const TileStorage& ts = getCurrentLayerTiles(); + TileStorage& ts = getCurrentLayerTiles(); for(size_t i = 0; i < ts.tiles.size(); ++i) { const TileData& t = ts.tiles[i]; if(t.isVisible() && t.x >= p1.x && t.y >= p1.y && t.x <= p2.x && t.y <= p2.y) + { selectedTiles.push_back(i); + } } + + if(size_t N = selectedTiles.size()) + ts.select(&selectedTiles[0], N); } } @@ -1762,9 +1807,12 @@ void SceneEditor::action(int id, int state, int source, InputDevice device) { if (state) { + if (!multiSelecting) + { + multiSelecting = true; clearSelection(); - multiSelecting = true; + } multiSelectPoint = dsq->getGameCursorPosition(); } else @@ -2431,16 +2479,16 @@ void SceneEditor::update(float dt) if (state == ES_SELECTING && !ismulti) { sel = this->getTileAtCursor(); - //if(sel < 0 || (selectedTiles.size() == 1 && selectedTiles[0] != (size_t)sel)) + if(selectedTiles.empty() || (selectedTiles.size() == 1 && selectedTiles[0] != sel)) { - selectedTiles.clear(); - getCurrentLayerTiles().clearSelection(); - } - if(sel >= 0) - { - selectedTiles.push_back(sel); - const size_t idx = sel; - getCurrentLayerTiles().select(&idx, 1); + if(!selectedTiles.empty()) + clearSelection(); + if(sel >= 0) + { + selectedTiles.push_back(sel); + const size_t idx = sel; + getCurrentLayerTiles().select(&idx, 1); + } } } @@ -2679,60 +2727,46 @@ void SceneEditor::update(float dt) repeatScale = core->getKeyState(KEY_X); if(repeatScale) - add *= 0.1f; + add *= 0.3f; } if (!selectedTiles.empty()) { if (!core->getCtrlState()) { - multi->scale=oldScale + add; - if (multi->scale.x < MIN_SIZE) - multi->scale.x = MIN_SIZE; - if (multi->scale.y < MIN_SIZE) - multi->scale.y = MIN_SIZE; - } - } - // FIXME: scaling - /*else if (editingElement) - { - Vector& editVec = repeatScale ? editingElement->repeatToFillScale : editingElement->scale; - if (core->getCtrlState()) - { - editVec = Vector(1,1); - } - else - { - - editVec = (repeatScale ? oldRepeatScale : oldScale) + add; - if (!uni && !repeatScale) + if(repeatScale) + { + if(selectedTiles.size() == 1) + multi->changeRepeatScale(oldRepeatScale + add); + } + else { - if (!middle) + multi->scale=oldScale + add; + if (multi->scale.x < MIN_SIZE) + multi->scale.x = MIN_SIZE; + if (multi->scale.y < MIN_SIZE) + multi->scale.y = MIN_SIZE; + + if(!middle && !uni && selectedTiles.size() == 1) { - Vector offsetChange = (add*Vector(editingElement->getWidth(), editingElement->getHeight()))*0.5f; + Vector offsetChange = (add*Vector(multi->getWidth(), multi->getHeight()))*0.5f; if (add.y == 0) { if (right) - editingElement->beforeScaleOffset = offsetChange; + multi->offset = offsetChange; else - editingElement->beforeScaleOffset = -offsetChange; + multi->offset = -offsetChange; } else { if (down) - editingElement->beforeScaleOffset = offsetChange; + multi->offset = offsetChange; else - editingElement->beforeScaleOffset = -offsetChange; + multi->offset = -offsetChange; } } } - if (editVec.x < MIN_SIZE) - editVec.x = MIN_SIZE; - if (editVec.y < MIN_SIZE) - editVec.y = MIN_SIZE; } - - editingElement->refreshRepeatTextureToFill(); - }*/ + } } break; case ES_MAX: diff --git a/Aquaria/TileMgr.cpp b/Aquaria/TileMgr.cpp index 1f88954..59fa00f 100644 --- a/Aquaria/TileMgr.cpp +++ b/Aquaria/TileMgr.cpp @@ -274,7 +274,7 @@ TileDef::TileDef(unsigned lr, const TileData& t) , sx(t.scalex), sy(t.scaley) , rsx(1), rsy(1) { - if(t.flags & TILEFLAG_REPEAT) + if(t.flags & TILEFLAG_REPEAT && t.rep) { rsx = t.rep->texscaleX; rsy = t.rep->texscaleY; diff --git a/BBGE/Quad.cpp b/BBGE/Quad.cpp index 93895c7..228e609 100644 --- a/BBGE/Quad.cpp +++ b/BBGE/Quad.cpp @@ -51,6 +51,7 @@ void Quad::initQuad() renderBorder = false; renderCenter = true; + texcoordOverride = false; width = 2; height = 2; texcoords.setStandard(); @@ -259,6 +260,9 @@ void Quad::onRender(const RenderState& rs) const void Quad::updateTexCoords() { + if(texcoordOverride) + goto setgrid; + if (repeatTexture && texture) { texcoords.u1 = texOff.x; @@ -266,6 +270,7 @@ void Quad::updateTexCoords() texcoords.u2 = (width*scale.x*repeatToFillScale.x)/texture->width + texOff.x; texcoords.v2 = (height*scale.y*repeatToFillScale.y)/texture->height + texOff.y; +setgrid: if(!grid) { createGrid(2, 2)->gridType = GRID_UNDEFINED; @@ -281,6 +286,19 @@ void Quad::updateTexCoords() } } +void Quad::setOverrideTexCoords(const TexCoordBox & tc) +{ + texcoordOverride = true; + texcoords = tc; + updateTexCoords(); +} + +void Quad::clearOverrideTexCoords() +{ + texcoordOverride = false; + updateTexCoords(); +} + void Quad::reloadDevice() { RenderObject::reloadDevice(); diff --git a/BBGE/Quad.h b/BBGE/Quad.h index 9f076e5..0258716 100644 --- a/BBGE/Quad.h +++ b/BBGE/Quad.h @@ -77,18 +77,21 @@ public: void setRepeatOffset(const Vector& repoffs); inline const Vector& getRepeatOffset() const { return texOff; } void setStripPoints(bool vert, const Vector *points, size_t n); + DynamicRenderGrid *getGrid() { return grid; } const DynamicRenderGrid *getGrid() const { return grid; } + void setOverrideTexCoords(const TexCoordBox& tc); + void clearOverrideTexCoords(); + void reloadDevice() OVERRIDE; void deleteGrid(); - TexCoordBox texcoords; - // TODO: this should be a bitmask bool renderQuad, renderCenter, renderBorder; + bool texcoordOverride; // urgh float borderAlpha; Vector renderBorderColor; @@ -96,6 +99,7 @@ public: protected: void updateTexCoords(); + TexCoordBox texcoords; Vector repeatToFillScale; Vector texOff; DynamicRenderGrid *grid;