Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "../comp.h"
- #if EDITOR
- #define _CRT_SECURE_NO_WARNINGS
- #include "../SDLW/Window.h"
- #define GLM_ENABLE_EXPERIMENTAL
- #include <gtx/string_cast.hpp>
- #include <iostream>
- #include "TileMap.h"
- #include "imgui/imgui.h"
- #include "imgui/imgui_impl_sdl.h"
- #include "Derent.h"
- #define SELECT_COLOR 0.8, 0.2, 1, 0.4
- namespace TileMapEditor {
- XYi size = { 800, 600 };
- Cam* cam;
- Window* win;
- Font* font;
- Texture* spriteSheet;
- TileMap* tilemap;
- int brushTile = 0;
- bool load = false;
- std::string loadPath;
- char loadType = 0; //1=tmap, 2=png
- void searchDir(char path[], char dispName[]) {
- if (ImGui::TreeNode(path))
- {
- DIR *dir = opendir(path);
- struct dirent *entry = readdir(dir);
- while (entry != NULL)
- {
- if (entry->d_type == DT_REG) {
- int len = std::strlen(entry->d_name);
- int comp1 = (len > 5) && (std::strcmp(entry->d_name + len - std::strlen(".tmap"), ".tmap") == 0);
- int comp2 = (len > 4) && (std::strcmp(entry->d_name + len - std::strlen(".png"), ".png") == 0);
- if (comp1 || comp2) {
- int type;
- std::string typeName;
- if (comp1) {
- typeName = " (TileMap)";
- type = 1;
- }
- else {
- typeName = " (SpriteSheet)";
- type = 2;
- }
- if (ImGui::Button((std::string(entry->d_name) + typeName).c_str())) {
- load = true;
- loadType = type;
- loadPath = std::string(path) + "\\" + entry->d_name;
- }
- }
- }
- else if (entry->d_type == DT_DIR) {
- if (std::strcmp(entry->d_name, "..") && std::strcmp(entry->d_name, ".")) {
- char param[256];
- strcpy(param, path);
- strcat(param, "\\");
- strcat(param, entry->d_name);
- searchDir(param, entry->d_name);
- }
- }
- entry = readdir(dir);
- }
- closedir(dir);
- ImGui::TreePop();
- }
- }
- bool panStart = false;
- XYi mousePanStart;
- glm::vec2 camPosAtPanStart;
- char currentPath[256], saveAs[256];
- char inWidth[8], inHeight[8], inDepth[8];
- int tileSize = 16, numTiles;
- #define NUM_TOOLS 2
- std::string tools[NUM_TOOLS] = { "Pencil","Fill" };
- int currentTool = 0;
- XYi(*getTileTextureXY)(int);
- bool selecting = false, selected = false;
- XYi select1rounded, select2rounded, select1use, select2use;
- void tick(float delta, Draw* draw) {
- size = win->getSize();
- cam->SetOffset(size.x / 2, size.y / 2);
- cam->CalculateMatrix();
- draw->UpdateView(&cam->matrix);
- bool trueBool = true;
- {
- ImGui::Begin("Open/Save", &trueBool);
- ImGui::Text("File Selector:");
- ImGui::InputText("Path: ", currentPath, 256);
- ImGui::BeginChild(1, ImVec2{ 0, ImGui::GetWindowHeight() - 80 });
- searchDir(currentPath, currentPath);
- ImGui::EndChild();
- if (load) {
- load = false;
- if (loadType) {
- if (spriteSheet) {
- delete spriteSheet;
- }
- spriteSheet = new Texture(loadPath);
- if (tilemap) {
- tilemap->spriteSheet = spriteSheet;
- }
- }
- else {
- delete tilemap;
- tilemap = new TileMap(spriteSheet, tileSize, getTileTextureXY);
- tilemap->ReadFromFile(loadPath);
- tilemap->SendAllVBO();
- }
- }
- if (tilemap != NULL) {
- ImGui::InputText("Save As", saveAs, 256);
- if (ImGui::Button("Save anim")) {
- tilemap->WriteToFile(saveAs);
- }
- }
- ImGui::InputText("Width: ", inWidth, 8);
- ImGui::InputText("Hieght: ", inHeight, 8);
- ImGui::InputText("Depth: ", inDepth, 8);
- if (ImGui::Button("Create New TileMap")) {
- delete tilemap;
- tilemap = new TileMap(spriteSheet, tileSize, getTileTextureXY);
- int w = std::stoi(inWidth, nullptr, 0);
- int h = std::stoi(inHeight, nullptr, 0);
- int depth = std::stoi(inDepth, nullptr, 0);
- tilemap->CreateEmpty(w, h, depth);
- for (int d = 0; d < depth; d++) {
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- *tilemap->getTilePtr(x, y, d) = x + y;
- }
- }
- }
- tilemap->SendAllVBO();
- }
- ImGui::SliderInt("Size", &tileSize, 4, 128);
- if (tilemap) {
- if (tileSize != tilemap->size) {
- tilemap->size = tileSize;
- tilemap->SendAllVBO();
- }
- }
- ImGui::End();
- ImGui::Begin("Tile Select", &trueBool);
- for (int i = 0; i < numTiles; i++) {
- glm::vec2 uv = getTileTextureXY(i);
- ImGui::PushID(i);
- if (ImGui::ImageButton((ImTextureID*)spriteSheet->textureID, ImVec2 { 50.f, 50.f }, ImVec2{ uv.x * tileSize / spriteSheet->width, uv.y * tileSize / spriteSheet->height }, ImVec2{ (uv.x + 1) * tileSize / spriteSheet->width, (uv.y + 1) * tileSize / spriteSheet->height })) {
- brushTile = i;
- }
- ImGui::PopID();
- }
- ImGui::End();
- ImGui::Begin("Select Tool:", &trueBool);
- for (int i = 0; i < NUM_TOOLS; i++) {
- if (ImGui::Button(tools[i].c_str())) {
- currentTool = i;
- }
- }
- ImGui::End();
- if (tilemap) {
- ImGui::Begin("Layers:");
- for (int i = 0; i < tilemap->layers.size(); i++) {
- ImGui::PushID(i);
- bool clicked = false;
- if (tilemap->layers[i].enabled) {
- clicked = ImGui::ColorButton(ImVec4(0.5, 0.5, 1, 1));
- }
- else {
- clicked = ImGui::ColorButton(ImVec4(0.5, 0.5, 0.5, 1));
- }
- ImGui::SameLine();
- clicked |= ImGui::Button(("Layer: " + std::to_string(i)).c_str());
- if (win->KeyDown(SDL_SCANCODE_LCTRL)) {
- tilemap->layers[i].enabled ^= clicked;
- }
- else {
- if (clicked) {
- for (int n = 0; n < tilemap->layers.size(); n++) {
- tilemap->layers[n].enabled = false;
- }tilemap->layers[i].enabled = true;
- }
- }
- ImGui::PopID();
- }
- ImGui::End();
- }
- }
- //cam->rot += (win->KeyDown(SDL_SCANCODE_E) - win->KeyDown(SDL_SCANCODE_Q))* delta * 0.01f;
- //return;
- if (tilemap != NULL) {
- if (win->MouseDown(SDL_BUTTON_MIDDLE)) {
- if (panStart) {
- cam->pos = camPosAtPanStart + glm::vec2(mousePanStart - win->MouseXY()) / cam->scale;
- }
- else {
- mousePanStart = win->MouseXY();
- camPosAtPanStart = cam->pos;
- panStart = true;
- }
- }
- else {
- if (panStart) {
- panStart = false;
- }
- if (win->MouseInWindow) {
- if (win->MouseX() > size.x - 15) {
- cam->pos.x++;
- } if (win->MouseX() < 15) {
- cam->pos.x--;
- }
- if (win->MouseY() > size.y - 15) {
- cam->pos.y++;
- } if (win->MouseY() < 15) {
- cam->pos.y--;
- }
- }
- }
- if (win->MouseDoubleClicked(SDL_BUTTON_MIDDLE)) {
- cam->pos = glm::vec2(tilemap->width / 2 * tileSize, tilemap->height / 2 * tileSize);
- }
- if (win->KeyDown(SDL_SCANCODE_LCTRL)) {
- cam->scale += win->getMouseWheelDelta().y * delta * 0.1f;
- if (cam->scale.x > 40) {
- cam->scale.x = cam->scale.y = 40;
- } if (cam->scale.y < 0.1f) {
- cam->scale.x = cam->scale.y = 0.1f;
- }
- }
- else {
- brushTile += int(win->getMouseWheelDelta().y);
- if (brushTile < 0) {
- brushTile = 0;
- }
- if (brushTile > numTiles) {
- brushTile = numTiles - 1;
- }
- }
- draw->UseDrawShader();
- draw->SetDrawColor(1, 1, 1);
- tilemap->Render(draw);
- if (win->MouseDown(SDL_BUTTON_RIGHT)) {
- if (selecting) {
- select2rounded = cam->unProj(win->MouseXY()) / float(tileSize);
- }
- else {
- select1rounded = cam->unProj(win->MouseXY()) / float(tileSize);
- selecting = true;
- }
- }
- else {
- if (selecting) {
- selecting = false;
- if (select1rounded == select2rounded) {
- selected = false;
- }
- else {
- selected = true;
- }
- }
- }
- if (selecting || selected) {
- draw->BindTex();
- draw->SetDrawColor(SELECT_COLOR);
- draw->DrawRect(min(select1rounded.x, select2rounded.x) * tileSize, min(select1rounded.y, select2rounded.y) * tileSize, (max(select1rounded.x, select2rounded.x) + 1)* tileSize, (max(select1rounded.y, select2rounded.y) + 1)* tileSize);
- }
- if (win->MouseDown(SDL_BUTTON_LEFT)) {
- }
- draw->SetDrawColor(1, 1, 1);
- draw->BindTex(spriteSheet);
- draw->UpdateView();
- draw->UseDrawShader();
- glm::vec2 uv = getTileTextureXY(brushTile);
- std::string tool = tools[currentTool] + ": ";
- int textOffset = font->GetTextWidth(tool);
- draw->DrawRectUV(textOffset, 5, textOffset + 50, 55, uv.x * tileSize / spriteSheet->width, uv.y * tileSize / spriteSheet->height, (uv.x + 1) * tileSize / spriteSheet->width, (uv.y + 1) * tileSize / spriteSheet->height);
- draw->UseTextShader();
- draw->SetTextColor(1, 0.8, 0.8);
- draw->DrawText(font, tool, 5, 50);
- }
- }
- int run(XYi(*getTileTextureXYin)(int tileID), int _numTiles) {
- numTiles = _numTiles;
- getTileTextureXY = getTileTextureXYin;
- int error = 0;
- win = new Window(size, 0, &error);
- if (error) {
- return -1;
- }
- font = new Font(30, 30, "C:\\Windows\\Fonts\\arial.ttf");
- spriteSheet = new Texture("assets/defualt/textures.png");
- cam = new Cam(size.x / 2, size.y / 2);
- cam->pos = glm::vec2(0, 0);
- cam->rot = 0;
- cam->scale = glm::vec2(1, 1);
- win->Run(&tick);
- return 0;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement