Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. #pragma once
  2. #include "../comp.h"
  3. #if EDITOR
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include "../SDLW/Window.h"
  6. #define GLM_ENABLE_EXPERIMENTAL
  7. #include <gtx/string_cast.hpp>
  8. #include <iostream>
  9. #include "TileMap.h"
  10. #include "imgui/imgui.h"
  11. #include "imgui/imgui_impl_sdl.h"
  12. #include "Derent.h"
  13.  
  14. #define SELECT_COLOR 0.8, 0.2, 1, 0.4
  15.  
  16. namespace TileMapEditor {
  17. XYi size = { 800, 600 };
  18.  
  19. Cam* cam;
  20. Window* win;
  21. Font* font;
  22. Texture* spriteSheet;
  23. TileMap* tilemap;
  24.  
  25. int brushTile = 0;
  26.  
  27. bool load = false;
  28. std::string loadPath;
  29. char loadType = 0; //1=tmap, 2=png
  30.  
  31. void searchDir(char path[], char dispName[]) {
  32. if (ImGui::TreeNode(path))
  33. {
  34. DIR *dir = opendir(path);
  35. struct dirent *entry = readdir(dir);
  36. while (entry != NULL)
  37. {
  38. if (entry->d_type == DT_REG) {
  39. int len = std::strlen(entry->d_name);
  40. int comp1 = (len > 5) && (std::strcmp(entry->d_name + len - std::strlen(".tmap"), ".tmap") == 0);
  41. int comp2 = (len > 4) && (std::strcmp(entry->d_name + len - std::strlen(".png"), ".png") == 0);
  42. if (comp1 || comp2) {
  43. int type;
  44. std::string typeName;
  45. if (comp1) {
  46. typeName = " (TileMap)";
  47. type = 1;
  48. }
  49. else {
  50. typeName = " (SpriteSheet)";
  51. type = 2;
  52. }
  53. if (ImGui::Button((std::string(entry->d_name) + typeName).c_str())) {
  54. load = true;
  55. loadType = type;
  56. loadPath = std::string(path) + "\\" + entry->d_name;
  57. }
  58. }
  59. }
  60. else if (entry->d_type == DT_DIR) {
  61. if (std::strcmp(entry->d_name, "..") && std::strcmp(entry->d_name, ".")) {
  62. char param[256];
  63. strcpy(param, path);
  64. strcat(param, "\\");
  65. strcat(param, entry->d_name);
  66. searchDir(param, entry->d_name);
  67. }
  68. }
  69. entry = readdir(dir);
  70. }
  71. closedir(dir);
  72. ImGui::TreePop();
  73. }
  74. }
  75.  
  76. bool panStart = false;
  77. XYi mousePanStart;
  78. glm::vec2 camPosAtPanStart;
  79. char currentPath[256], saveAs[256];
  80. char inWidth[8], inHeight[8], inDepth[8];
  81.  
  82. int tileSize = 16, numTiles;
  83. #define NUM_TOOLS 2
  84. std::string tools[NUM_TOOLS] = { "Pencil","Fill" };
  85. int currentTool = 0;
  86.  
  87. XYi(*getTileTextureXY)(int);
  88.  
  89. bool selecting = false, selected = false;
  90. XYi select1rounded, select2rounded, select1use, select2use;
  91. void tick(float delta, Draw* draw) {
  92. size = win->getSize();
  93. cam->SetOffset(size.x / 2, size.y / 2);
  94.  
  95. cam->CalculateMatrix();
  96. draw->UpdateView(&cam->matrix);
  97.  
  98. bool trueBool = true;
  99. {
  100. ImGui::Begin("Open/Save", &trueBool);
  101. ImGui::Text("File Selector:");
  102. ImGui::InputText("Path: ", currentPath, 256);
  103. ImGui::BeginChild(1, ImVec2{ 0, ImGui::GetWindowHeight() - 80 });
  104. searchDir(currentPath, currentPath);
  105. ImGui::EndChild();
  106. if (load) {
  107. load = false;
  108. if (loadType) {
  109. if (spriteSheet) {
  110. delete spriteSheet;
  111. }
  112. spriteSheet = new Texture(loadPath);
  113. if (tilemap) {
  114. tilemap->spriteSheet = spriteSheet;
  115. }
  116. }
  117. else {
  118. delete tilemap;
  119. tilemap = new TileMap(spriteSheet, tileSize, getTileTextureXY);
  120. tilemap->ReadFromFile(loadPath);
  121. tilemap->SendAllVBO();
  122. }
  123. }
  124. if (tilemap != NULL) {
  125. ImGui::InputText("Save As", saveAs, 256);
  126. if (ImGui::Button("Save anim")) {
  127. tilemap->WriteToFile(saveAs);
  128. }
  129. }
  130. ImGui::InputText("Width: ", inWidth, 8);
  131. ImGui::InputText("Hieght: ", inHeight, 8);
  132. ImGui::InputText("Depth: ", inDepth, 8);
  133. if (ImGui::Button("Create New TileMap")) {
  134. delete tilemap;
  135. tilemap = new TileMap(spriteSheet, tileSize, getTileTextureXY);
  136. int w = std::stoi(inWidth, nullptr, 0);
  137. int h = std::stoi(inHeight, nullptr, 0);
  138. int depth = std::stoi(inDepth, nullptr, 0);
  139. tilemap->CreateEmpty(w, h, depth);
  140. for (int d = 0; d < depth; d++) {
  141. for (int x = 0; x < w; x++) {
  142. for (int y = 0; y < h; y++) {
  143. *tilemap->getTilePtr(x, y, d) = x + y;
  144. }
  145. }
  146. }
  147. tilemap->SendAllVBO();
  148. }
  149. ImGui::SliderInt("Size", &tileSize, 4, 128);
  150. if (tilemap) {
  151. if (tileSize != tilemap->size) {
  152. tilemap->size = tileSize;
  153. tilemap->SendAllVBO();
  154. }
  155. }
  156. ImGui::End();
  157.  
  158. ImGui::Begin("Tile Select", &trueBool);
  159. for (int i = 0; i < numTiles; i++) {
  160. glm::vec2 uv = getTileTextureXY(i);
  161. ImGui::PushID(i);
  162. 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 })) {
  163. brushTile = i;
  164. }
  165. ImGui::PopID();
  166. }
  167. ImGui::End();
  168. ImGui::Begin("Select Tool:", &trueBool);
  169. for (int i = 0; i < NUM_TOOLS; i++) {
  170. if (ImGui::Button(tools[i].c_str())) {
  171. currentTool = i;
  172. }
  173. }
  174. ImGui::End();
  175. if (tilemap) {
  176. ImGui::Begin("Layers:");
  177. for (int i = 0; i < tilemap->layers.size(); i++) {
  178. ImGui::PushID(i);
  179. bool clicked = false;
  180. if (tilemap->layers[i].enabled) {
  181. clicked = ImGui::ColorButton(ImVec4(0.5, 0.5, 1, 1));
  182. }
  183. else {
  184. clicked = ImGui::ColorButton(ImVec4(0.5, 0.5, 0.5, 1));
  185. }
  186. ImGui::SameLine();
  187. clicked |= ImGui::Button(("Layer: " + std::to_string(i)).c_str());
  188. if (win->KeyDown(SDL_SCANCODE_LCTRL)) {
  189. tilemap->layers[i].enabled ^= clicked;
  190. }
  191. else {
  192. if (clicked) {
  193. for (int n = 0; n < tilemap->layers.size(); n++) {
  194. tilemap->layers[n].enabled = false;
  195. }tilemap->layers[i].enabled = true;
  196. }
  197. }
  198. ImGui::PopID();
  199. }
  200. ImGui::End();
  201. }
  202.  
  203. }
  204. //cam->rot += (win->KeyDown(SDL_SCANCODE_E) - win->KeyDown(SDL_SCANCODE_Q))* delta * 0.01f;
  205. //return;
  206.  
  207. if (tilemap != NULL) {
  208. if (win->MouseDown(SDL_BUTTON_MIDDLE)) {
  209. if (panStart) {
  210. cam->pos = camPosAtPanStart + glm::vec2(mousePanStart - win->MouseXY()) / cam->scale;
  211. }
  212. else {
  213. mousePanStart = win->MouseXY();
  214. camPosAtPanStart = cam->pos;
  215. panStart = true;
  216. }
  217. }
  218. else {
  219. if (panStart) {
  220. panStart = false;
  221. }
  222. if (win->MouseInWindow) {
  223. if (win->MouseX() > size.x - 15) {
  224. cam->pos.x++;
  225. } if (win->MouseX() < 15) {
  226. cam->pos.x--;
  227. }
  228. if (win->MouseY() > size.y - 15) {
  229. cam->pos.y++;
  230. } if (win->MouseY() < 15) {
  231. cam->pos.y--;
  232. }
  233. }
  234. }
  235.  
  236. if (win->MouseDoubleClicked(SDL_BUTTON_MIDDLE)) {
  237. cam->pos = glm::vec2(tilemap->width / 2 * tileSize, tilemap->height / 2 * tileSize);
  238. }
  239.  
  240. if (win->KeyDown(SDL_SCANCODE_LCTRL)) {
  241. cam->scale += win->getMouseWheelDelta().y * delta * 0.1f;
  242.  
  243. if (cam->scale.x > 40) {
  244. cam->scale.x = cam->scale.y = 40;
  245. } if (cam->scale.y < 0.1f) {
  246. cam->scale.x = cam->scale.y = 0.1f;
  247. }
  248. }
  249. else {
  250. brushTile += int(win->getMouseWheelDelta().y);
  251. if (brushTile < 0) {
  252. brushTile = 0;
  253. }
  254. if (brushTile > numTiles) {
  255. brushTile = numTiles - 1;
  256. }
  257. }
  258.  
  259. draw->UseDrawShader();
  260. draw->SetDrawColor(1, 1, 1);
  261. tilemap->Render(draw);
  262.  
  263. if (win->MouseDown(SDL_BUTTON_RIGHT)) {
  264. if (selecting) {
  265. select2rounded = cam->unProj(win->MouseXY()) / float(tileSize);
  266. }
  267. else {
  268. select1rounded = cam->unProj(win->MouseXY()) / float(tileSize);
  269. selecting = true;
  270. }
  271. }
  272. else {
  273. if (selecting) {
  274. selecting = false;
  275. if (select1rounded == select2rounded) {
  276. selected = false;
  277. }
  278. else {
  279. selected = true;
  280. }
  281. }
  282. }
  283. if (selecting || selected) {
  284. draw->BindTex();
  285. draw->SetDrawColor(SELECT_COLOR);
  286. 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);
  287. }
  288. if (win->MouseDown(SDL_BUTTON_LEFT)) {
  289.  
  290. }
  291. draw->SetDrawColor(1, 1, 1);
  292. draw->BindTex(spriteSheet);
  293. draw->UpdateView();
  294. draw->UseDrawShader();
  295. glm::vec2 uv = getTileTextureXY(brushTile);
  296. std::string tool = tools[currentTool] + ": ";
  297. int textOffset = font->GetTextWidth(tool);
  298. 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);
  299.  
  300. draw->UseTextShader();
  301. draw->SetTextColor(1, 0.8, 0.8);
  302. draw->DrawText(font, tool, 5, 50);
  303. }
  304. }
  305.  
  306. int run(XYi(*getTileTextureXYin)(int tileID), int _numTiles) {
  307. numTiles = _numTiles;
  308. getTileTextureXY = getTileTextureXYin;
  309. int error = 0;
  310. win = new Window(size, 0, &error);
  311. if (error) {
  312. return -1;
  313. }
  314.  
  315. font = new Font(30, 30, "C:\\Windows\\Fonts\\arial.ttf");
  316. spriteSheet = new Texture("assets/defualt/textures.png");
  317.  
  318. cam = new Cam(size.x / 2, size.y / 2);
  319. cam->pos = glm::vec2(0, 0);
  320. cam->rot = 0;
  321. cam->scale = glm::vec2(1, 1);
  322.  
  323. win->Run(&tick);
  324. return 0;
  325. }
  326. }
  327. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement