|
|
@@ -0,0 +1,258 @@
|
|
|
+#include <containers/isometricGame/isometricGameEditor.h>
|
|
|
+#include <glui/glui.h>
|
|
|
+
|
|
|
+static int blocksCount = 8;
|
|
|
+
|
|
|
+static bool pointInBox(glm::vec2 p, glm::vec4 box)
|
|
|
+{
|
|
|
+ if
|
|
|
+ (
|
|
|
+ (p.x >= box.x && p.x <= (box.x + box.z)) &&
|
|
|
+ (p.y >= box.y && p.y <= (box.y + box.w))
|
|
|
+ )
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool IsometricGameEditor::create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument)
|
|
|
+{
|
|
|
+ renderer.create(requestedInfo.requestedFBO.fbo);
|
|
|
+
|
|
|
+ tiles = pika::gl2d::loadTextureWithPixelPadding(PIKA_RESOURCES_PATH "isoTiles/Isometric-Tiles.png", requestedInfo, 32, true);
|
|
|
+
|
|
|
+ tilesAtlas = gl2d::TextureAtlasPadding(8, 2, tiles.GetSize().x, tiles.GetSize().y);
|
|
|
+
|
|
|
+ //fileChanged.setFile(mapFile.c_str());
|
|
|
+
|
|
|
+ loadedLevel.setInfo("Level", PIKA_RESOURCES_PATH, {".mcDungeons"});
|
|
|
+
|
|
|
+ bool created = 0;
|
|
|
+ if (commandLineArgument.size() > 0)
|
|
|
+ {
|
|
|
+ //editor.loadFromFile(renderer, commandLineArgument.to_string(), requestedInfo);
|
|
|
+ size_t s = 0;
|
|
|
+ pika::strlcpy(loadedLevel.file, commandLineArgument.to_string(), sizeof(loadedLevel.file));
|
|
|
+
|
|
|
+ glm::ivec3 mapSize = {};
|
|
|
+ if (requestedInfo.readEntireFileBinary(commandLineArgument.to_string(), &mapSize, sizeof(mapSize), 0))
|
|
|
+ {
|
|
|
+ if (requestedInfo.getFileSizeBinary(commandLineArgument.to_string().c_str(), s))
|
|
|
+ {
|
|
|
+
|
|
|
+ if (s == mapSize.x * mapSize.y * mapSize.z * sizeof(Block) + sizeof(mapSize))
|
|
|
+ {
|
|
|
+ created = 1;
|
|
|
+
|
|
|
+ map.init(mapSize);
|
|
|
+
|
|
|
+ requestedInfo.readEntireFileBinary(commandLineArgument.to_string().c_str(),
|
|
|
+ map.mapData.data(), mapSize.x * mapSize.y * mapSize.z * sizeof(Block), sizeof(mapSize));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!created)
|
|
|
+ {
|
|
|
+ map.init({10,10,10});
|
|
|
+
|
|
|
+ for (int i = 0; i < 10; i++)
|
|
|
+ for (int j = 0; j < 10; j++)
|
|
|
+ {
|
|
|
+ map.setSafe({i,0,j}, 1, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ map.setSafe({5,1,5}, 6, 0);
|
|
|
+ map.setSafe({5,2,5}, 6, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+bool IsometricGameEditor::update(pika::Input input, pika::WindowState windowState, RequestedContainerInfo &requestedInfo)
|
|
|
+{
|
|
|
+
|
|
|
+#pragma region clear stuff
|
|
|
+ glClear(GL_COLOR_BUFFER_BIT);
|
|
|
+ renderer.updateWindowMetrics(windowState.windowW, windowState.windowH);
|
|
|
+#pragma endregion
|
|
|
+
|
|
|
+ float size = 100;
|
|
|
+
|
|
|
+ auto calculateBlockPos = [size](glm::ivec3 in)
|
|
|
+ {
|
|
|
+ glm::vec2 position = {};
|
|
|
+
|
|
|
+ position += glm::vec2(-size / 2.f, size / 4.f) * float(in.x);
|
|
|
+ position += glm::vec2(size / 2.f, size / 4.f) * float(in.z);
|
|
|
+
|
|
|
+ position += glm::vec2(0, -size / 2.f) * float(in.y);
|
|
|
+
|
|
|
+ return position;
|
|
|
+ };
|
|
|
+
|
|
|
+ for (int y = 0; y < map.size.y; y++)
|
|
|
+ for (int z = 0; z < map.size.z; z++)
|
|
|
+ for (int x = 0; x < map.size.x; x++)
|
|
|
+ {
|
|
|
+ auto b = map.getSafe({x,y,z});
|
|
|
+
|
|
|
+ if (b->get().x != 0)
|
|
|
+ {
|
|
|
+ glm::vec2 position = calculateBlockPos({x,y,z});
|
|
|
+
|
|
|
+ requestedInfo.consoleWrite("yes");
|
|
|
+ renderer.renderRectangle({position,size,size}, tiles, Colors_White, {}, 0,
|
|
|
+ tilesAtlas.get(b->get().x, b->get().y));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ auto viewRect = renderer.getViewRect();
|
|
|
+
|
|
|
+ auto lerp = [](auto a, auto b, auto c)
|
|
|
+ {
|
|
|
+ return a * (1.f - c) + b * c;
|
|
|
+ };
|
|
|
+
|
|
|
+ glm::vec2 blockPositionScreen = lerp(glm::vec2(viewRect.x, viewRect.y),
|
|
|
+ glm::vec2(viewRect.x + viewRect.z, viewRect.y + viewRect.w),
|
|
|
+ glm::vec2(input.mouseX, input.mouseY) / glm::vec2(windowState.windowW, windowState.windowH));
|
|
|
+
|
|
|
+
|
|
|
+ glm::ivec3 currentSelectedBlock{-1};
|
|
|
+
|
|
|
+ if(input.rMouse.pressed())
|
|
|
+ for (int y = 0; y < map.size.y; y++)
|
|
|
+ for (int z = 0; z < map.size.z; z++)
|
|
|
+ for (int x = 0; x < map.size.x; x++)
|
|
|
+ {
|
|
|
+
|
|
|
+ auto b = map.getSafe({x,y,z});
|
|
|
+
|
|
|
+ if (b->get().x != 0)
|
|
|
+ {
|
|
|
+ glm::vec2 position = calculateBlockPos({x,y,z});
|
|
|
+ glm::vec4 box = renderer.toScreen({position, size, size});
|
|
|
+ //todo new functon
|
|
|
+
|
|
|
+ box.x += 1;
|
|
|
+ box.x /= 2.f;
|
|
|
+ box.x *= renderer.windowW;
|
|
|
+
|
|
|
+ box.y *= -1;
|
|
|
+ box.y += 1;
|
|
|
+ box.y /= 2.f;
|
|
|
+ box.y *= renderer.windowH;
|
|
|
+
|
|
|
+ box.z += 1;
|
|
|
+ box.z /= 2.f;
|
|
|
+ box.z *= renderer.windowW;
|
|
|
+
|
|
|
+ box.w *= -1;
|
|
|
+ box.w += 1;
|
|
|
+ box.w /= 2.f;
|
|
|
+ box.w *= renderer.windowH;
|
|
|
+
|
|
|
+ box.z = box.z - box.x;
|
|
|
+ box.w = box.w - box.y;
|
|
|
+
|
|
|
+ if(pointInBox(glm::vec2(input.mouseX, input.mouseY), box))
|
|
|
+ {
|
|
|
+ currentSelectedBlock = {x,y,z};
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (currentSelectedBlock.x > -1)
|
|
|
+ {
|
|
|
+ auto b = map.getSafe(currentSelectedBlock);
|
|
|
+ if (b)
|
|
|
+ {
|
|
|
+ b->set(0, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ renderer.flush();
|
|
|
+
|
|
|
+ ImGui::Begin("camera editor");
|
|
|
+
|
|
|
+ ImGui::DragFloat2("camera", &renderer.currentCamera.position[0], 2, -3200, 3200);
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ auto uv1 = tilesAtlas.get(currentBlock, 0);
|
|
|
+ ImGui::Image((void *)(intptr_t)tiles.id,
|
|
|
+ {35,35}, {uv1.x, uv1.y}, {uv1.z, uv1.w});
|
|
|
+ }
|
|
|
+
|
|
|
+ ImGui::NewLine();
|
|
|
+ {
|
|
|
+ for (int mCount = 0; mCount < blocksCount; mCount++)
|
|
|
+ {
|
|
|
+ auto uv1 = tilesAtlas.get(mCount, 0);
|
|
|
+
|
|
|
+ ImGui::PushID(mCount);
|
|
|
+ if (ImGui::ImageButton((void *)(intptr_t)tiles.id,
|
|
|
+ {35,35}, {uv1.x, uv1.y}, {uv1.z, uv1.w}))
|
|
|
+ {
|
|
|
+ currentBlock = mCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ ImGui::PopID();
|
|
|
+
|
|
|
+ if ((mCount + 1) % 5 != 0)
|
|
|
+ {
|
|
|
+ ImGui::SameLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ currentBlock = glm::clamp(currentBlock, 0, blocksCount-1);
|
|
|
+
|
|
|
+ ImGui::NewLine();
|
|
|
+
|
|
|
+ loadedLevel.run(requestedInfo.requestedImguiIds);
|
|
|
+
|
|
|
+ if (ImGui::Button("save map"))
|
|
|
+ {
|
|
|
+ if (requestedInfo.writeEntireFileBinary(loadedLevel.file, &map.size, sizeof(map.size)))
|
|
|
+ {
|
|
|
+ requestedInfo.appendFileBinary(loadedLevel.file, map.mapData.data(),
|
|
|
+ sizeof(Block) * map.mapData.size());
|
|
|
+
|
|
|
+ //toto log errors
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ImGui::End();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+void IsometricGameEditor::destruct(RequestedContainerInfo &requestedInfo)
|
|
|
+{
|
|
|
+ renderer.cleanup();
|
|
|
+
|
|
|
+}
|