The Fjord Tilemap Editor
at master 30 kB view raw
1using System.Linq; 2using System; 3using System.IO; 4using System.Text.Json.Serialization; 5using System.Collections.Generic; 6using Newtonsoft.Json; 7using Fjord; 8using Fjord.Modules.Camera; 9using Fjord.Modules.Game; 10using Fjord.Modules.Graphics; 11using Fjord.Modules.Input; 12using Fjord.Modules.Mathf; 13using Fjord.Modules.Debug; 14using Fjord.Modules.Ui; 15using Fjord.Modules.Tilemaps; 16 17namespace Glacier { 18 19 enum scenes { 20 Tiles, 21 Config, 22 Property, 23 Tools 24 } 25 26 public class main : scene 27 { 28 29 string font = "Roboto"; 30 31 List<List<Dictionary<string, dynamic>>> tile_map = new List<List<Dictionary<string, dynamic>>>(); 32 Dictionary<string, tile> tiles = new Dictionary<string, tile>(); 33 34 V2 size = new V2(30, 30); 35 V2 tile_size = new V2(16, 16); 36 float zoom = 1; 37 38 int grid_width, grid_height, tile_width, tile_height; 39 40 V2f position = new V2f(0, 0); 41 42 List<property> properties = new List<property>() { 43 new property("tile_id", "string", ""), 44 }; 45 46 V2 hovered = new V2(0, 0); 47 V2 selected = new V2(0, 0); 48 49 int selected_panel = 0; 50 51 // Add tile vars 52 53 string add_tile_path = ""; 54 string add_tile_name = ""; 55 bool add_tile_button = false; 56 57 // Add property Vars 58 59 string add_prop_name = ""; 60 string add_prop_type = "string"; 61 string add_prop_def_s = ""; 62 bool add_prop_def_b = false; 63 int add_prop_def_i = 0; 64 65 // Tools vars 66 67 string paint_prop_name = ""; 68 dynamic paint_prop_value = ""; 69 70 string highlight_prop_name = ""; 71 dynamic highlight_prop_value = ""; 72 73 // File vars 74 75 string save_path = ""; 76 string load_path = ""; 77 78 // Colors 79 80 V4 color_text = new V4(0, 0, 0, 255); 81 V4 color_background = new V4(255, 255, 255, 255); 82 V4 color_foreground = new V4(232, 232, 232, 255); 83 V4 color_darkerforeground = new V4(192, 175, 250, 255); 84 85 public override void on_load() 86 { 87 for(var i = 0; i < size.x; i++) { 88 tile_map.Add(new List<Dictionary<string, dynamic>>()); 89 for(var j = 0; j < size.y; j++) { 90 tile_map[i].Add(new Dictionary<string, dynamic>() { 91 {"tile_id", ""} 92 }); 93 } 94 } 95 96 set_background(new V4(255, 255, 255, 255)); 97 input.set_input_state("general"); 98 99 draw.load_font(font); 100 fui.set_font(font); 101 fui.font_size = 24; 102 103 grid_width = size.x; grid_height = size.y; tile_width = tile_size.x; tile_height = tile_size.y; 104 105 // This is where you load all your scenes 106 // The if statement is so that it doesn't trigger multiple times 107 108 if(!scene_handler.scene_exists("glacier")) { 109 110 // Add all scenes 111 scene_handler.register("glacier", new main()); 112 113 // Load the first scene this can later be called in any file as for example a win condition to switch scene. 114 scene_handler.load("glacier"); 115 } 116 } 117 118 // Update method 119 // This is where all your gamelogic is 120 121 public override void update() 122 { 123 float move_speed = 40f * (float)game.delta_time; 124 float zoom_speed = 0.2f * (float)game.delta_time; 125 126 if(input.key_pressed(input.key_up, "general")) { 127 zoom += zoom < 5 ? zoom_speed : 0; 128 } else if(input.key_pressed(input.key_down, "general")) { 129 zoom -= zoom > 1 ? zoom_speed : 0; 130 } 131 132 if(input.key_pressed(input.key_w, "general")) { 133 position.y -= move_speed; 134 } else if(input.key_pressed(input.key_s, "general")) { 135 position.y += move_speed; 136 } 137 138 if(input.key_pressed(input.key_a, "general")) { 139 position.x -= move_speed; 140 } else if(input.key_pressed(input.key_d, "general")) { 141 position.x += move_speed; 142 } 143 144 camera.set(position.x + size.x * tile_size.x / 2 * zoom, position.y + size.y * tile_size.y / 2 * zoom); 145 146 zoom = Math.Clamp(zoom, 1, 5); 147 148 hovered.x = Math.Clamp((mouse.game_position.x + (int)camera.get().x) / (int)(tile_size.x * zoom), 0, size.x); 149 hovered.y = Math.Clamp((mouse.game_position.y + (int)camera.get().y) / (int)(tile_size.y * zoom), 0, size.y); 150 151 if(mouse.button_just_pressed(mb.left, "general")) { 152 if(helpers.mouse_inside(new V4(0, 0, 95, 50))) { 153 selected_panel = 0; 154 } else if(helpers.mouse_inside(new V4(95, 0, 100, 50))) { 155 selected_panel = 1; 156 } else if(helpers.mouse_inside(new V4(90, 0, 200, 50))) { 157 selected_panel = 2; 158 } else if(helpers.mouse_inside(new V4(200, 0, 200, 50))) { 159 selected_panel = 3; 160 } 161 } 162 163 if(mouse.button_pressed(mb.left, "general")) { 164 if(!helpers.mouse_inside(new V4(0, 0, 400, game.resolution.y))) { 165 if(hovered.x < size.x && hovered.y < size.y && hovered.x > 0 && hovered.y > 0) { 166 tile_map[hovered.x][hovered.y][paint_prop_name] = paint_prop_value; 167 } 168 selected.x = hovered.x; 169 selected.y = hovered.y; 170 } 171 } 172 173 selected.x = Math.Clamp(selected.x, 0, size.x); 174 selected.y = Math.Clamp(selected.y, 0, size.y); 175 176 base.update(); 177 } 178 179 // Render method 180 // This is where all your rendering is 181 182 public override void render() 183 { 184 draw_tile_map(); 185 186 draw_header(); 187 188 switch(selected_panel) { 189 case (int)scenes.Tiles: 190 draw_tiles(); 191 break; 192 case (int)scenes.Config: 193 draw_config(); 194 break; 195 case (int)scenes.Property: 196 draw_property(); 197 break; 198 case (int)scenes.Tools: 199 draw_tools(); 200 break; 201 } 202 203 if(input.get_input_state() == "add_tile") { 204 draw_add_tile(); 205 } else if(input.get_input_state() == "add_property") { 206 draw_add_property(); 207 } else if(input.get_input_state() == "file") { 208 draw_file(); 209 } 210 211 // draw.text(new V2(0, 0), font, 24, "x: " + selected.x.ToString() + " y: " + selected.y.ToString(), color.black); 212 213 base.render(); 214 } 215 216 private void draw_tile_map() { 217 for(var i = 0; i < size.x; i++) { 218 for(var j = 0; j < size.y; j++) { 219 V2 pos = new V2((int)(i * (tile_size.x * zoom) - camera.get().x), (int)(j * (tile_size.y * zoom) - camera.get().y)); 220 V2 size = new V2((int)(tile_size.x * zoom), (int)(tile_size.y * zoom)); 221 draw.rect(new V4(pos.x, pos.y, size.x, size.y), color.black, false); 222 if(tiles.Keys.ToList().Contains(tile_map[i][j]["tile_id"])) { 223 texture tmp = (texture)tiles[tile_map[i][j]["tile_id"]].tex.Clone(); 224 float size_ =(zoom / 3.2f + 0.6875f); 225 tmp.set_scale(new V2f(size_, size_)); 226 draw.texture_direct(pos, tmp); 227 } 228 if(selected.x == i && selected.y == j) { 229 draw.rect(new V4(pos.x, pos.y, size.x, size.y), new V4(214, 229, 250, 170)); 230 } else if(hovered.x == i && hovered.y == j) { 231 draw.rect(new V4(pos.x, pos.y, size.x, size.y), new V4(163, 218, 141, 170)); 232 } 233 234 bool contains = false; 235 property prop = properties[0]; 236 237 foreach(property x in properties) { 238 if(x.name == highlight_prop_name) { 239 contains = true; 240 prop = x; 241 break; 242 } 243 } 244 245 if(!tile_map[i][j].Keys.ToArray().Contains(highlight_prop_name) && contains) { 246 tile_map[i][j][highlight_prop_name] = prop.default_value; 247 } 248 249 if(tile_map[i][j].Keys.ToArray().Contains(highlight_prop_name)) { 250 if(highlight_prop_value.GetType() != tile_map[i][j][highlight_prop_name].GetType()) { 251 switch(tile_map[i][j][highlight_prop_name].GetType()) { 252 case string: 253 highlight_prop_value = ""; 254 break; 255 case int: 256 highlight_prop_value = 0; 257 break; 258 case bool: 259 highlight_prop_value = false; 260 break; 261 } 262 } 263 if(tile_map[i][j][highlight_prop_name] == highlight_prop_value) { 264 if(contains) { 265 draw.rect(new V4(pos.x, pos.y, size.x, size.y), new V4(255, 89, 89, 170)); 266 } 267 } 268 } 269 } 270 } 271 } 272 273 private void draw_header() { 274 draw.rect(new V4(0, 0, 400, game.resolution.y), new V4(250, 250, 250, 255)); 275 draw.rect(new V4(400, 0, 4, game.resolution.y), new V4(245, 245, 245, 255)); 276 277 List<int> widths = new List<int>(); 278 int iter = 0; 279 foreach(var scene in Enum.GetValues(typeof(scenes))) { 280 string? stringed = scene.ToString(); 281 int loc_width = 30; 282 foreach(int width in widths) { 283 loc_width += width; 284 } 285 draw.text(new V2(loc_width, 20), "Roboto-Bold", 20, stringed, selected_panel == iter ? new V4(236, 72, 112, 255) : new V4(180, 180, 190, 255)); 286 widths.Add(draw.get_text_rect(new V2(0, 0), "Roboto-Bold", 20, stringed).z + 30); 287 288 if(iter == selected_panel) { 289 draw.rect(new V4(loc_width - 5, -4, draw.get_text_rect(new V2(0, 0), "Roboto-Bold", 20, stringed).z + 10, 10), new V4(236, 72, 112, 255), true, 6); 290 } 291 292 iter++; 293 } 294 } 295 296 private void draw_tiles() { 297 V4 add_button_rect = new V4(30, 70, 150, 50); 298 299 draw.rect(add_button_rect, !helpers.mouse_inside(add_button_rect) ? new V4(90, 45, 230, 255) : new V4(170, 150, 240, 255), true, 25); 300 draw.text(new V2(65, 84), "Roboto-Bold", 20, "Add Tile"); 301 if(helpers.mouse_inside(add_button_rect) && mouse.button_just_pressed(mb.left, "general")) { 302 input.set_input_state("add_tile"); 303 } 304 305 for(var i = 0; i < tiles.Count; i++) { 306 texture tmp = (texture)tiles[tiles.Keys.ToList()[i]].tex.Clone(); 307 int size = 64 / tile_size.y / 2; 308 tmp.set_scale(new V2f(size, size)); 309 draw.texture(new V2(30, 150 + i * tile_size.y * size), tmp); 310 draw.text(new V2(100, 150 + i * tile_size.y * size), font, 24, tiles[tiles.Keys.ToList()[i]].name, color.black); 311 draw.text(new V2(100, 190 + i * tile_size.y * size), font, 24, tiles[tiles.Keys.ToList()[i]].path, color.black); 312 } 313 } 314 315 private void draw_add_tile() { 316 draw.rect(new V4(0, 0, game.resolution.x, game.resolution.y), new V4(0, 0, 0, 170), true); 317 V4 panel_rect = new V4(game.resolution.x / 2 - 200, game.resolution.y / 2 - 300, 400, 600); 318 draw.rect(panel_rect, color.white, true, 25); 319 320 devgui.input_box(new V4(20 + panel_rect.x, 75 + panel_rect.y, 250, 35), font, ref add_tile_path, null, "add_tile_tex", "Texture Path"); 321 devgui.input_box(new V4(20 + panel_rect.x, 20 + panel_rect.y, 250, 35), font, ref add_tile_name, null, "add_tile_name", "Name"); 322 323 devgui.button(new V4(20 + panel_rect.x, 145 + panel_rect.y, 80, 35), ref add_tile_button, "Roboto-Bold", "Add"); 324 325 if(add_tile_button) { 326 tiles.Add(add_tile_name, new tile(add_tile_name, add_tile_path)); 327 add_tile_name = ""; 328 add_tile_path = ""; 329 add_tile_button = false; 330 input.set_input_state("general"); 331 } 332 333 if(input.key_just_pressed(input.key_escape)) { 334 add_tile_name = ""; 335 add_tile_path = ""; 336 add_tile_button = false; 337 input.set_input_state("general"); 338 } 339 340 if(input.key_just_pressed(input.key_return)) { 341 tiles.Add(add_tile_name, new tile(add_tile_name, add_tile_path)); 342 add_tile_name = ""; 343 add_tile_path = ""; 344 add_tile_button = false; 345 input.set_input_state("general"); 346 } 347 } 348 349 private void draw_config() { 350 for(var i = 0; i < properties.Count; i++) { 351 if(!tile_map[selected.x][selected.y].Keys.ToArray().Contains(properties[i].name)) { 352 tile_map[selected.x][selected.y].Add(properties[i].name, properties[i].default_value); 353 } 354 355 int offset = 130 * i; 356 357 draw.text(new V2(30, 70 + offset), font, 24, properties[i].name, color.black); 358 draw.rect(new V4(40 + draw.get_text_rect(new V2(30, 70), font, 24, properties[i].name).z, 70 + offset, 30, 30), new V4(103, 111, 163, 255), true, 5); 359 draw.text(new V2(47 + draw.get_text_rect(new V2(30, 70), font, 24, properties[i].name).z, 71 + offset), "Roboto-Bold", 24, properties[i].datatype.ToString()[0].ToString().ToUpper()); 360 361 if(properties[i].datatype == "string") { 362 string tmp = tile_map[selected.x][selected.y][properties[i].name]; 363 devgui.input_box(new V4(30, 110 + offset, 200, 35), font, ref tmp, "general", properties[i].name, ""); 364 tile_map[selected.x][selected.y][properties[i].name] = tmp; 365 } else if(properties[i].datatype == "int") { 366 int tmp = tile_map[selected.x][selected.y][properties[i].name]; 367 devgui.num_input_box(new V4(30, 110 + offset, 200, 35), font, ref tmp, "general", properties[i].name); 368 tile_map[selected.x][selected.y][properties[i].name] = tmp; 369 } else if(properties[i].datatype == "bool") { 370 bool tmp = tile_map[selected.x][selected.y][properties[i].name]; 371 devgui.button(new V4(30, 110 + offset, draw.get_text_rect(new V2(0, 0), font, 24, properties[i].name).z + 10, 35), ref tmp, font, "x"); 372 tile_map[selected.x][selected.y][properties[i].name] = tmp; 373 } 374 } 375 } 376 377 private void draw_property() { 378 V4 add_button_rect = new V4(30, 70, 200, 50); 379 380 draw.rect(add_button_rect, !helpers.mouse_inside(add_button_rect) ? new V4(90, 45, 230, 255) : new V4(170, 150, 240, 255), true, 25); 381 draw.text(new V2(65, 84), "Roboto-Bold", 20, "Add Property"); 382 if(helpers.mouse_inside(add_button_rect) && mouse.button_just_pressed(mb.left, "general")) { 383 input.set_input_state("add_property"); 384 } 385 386 for(var i = 0; i < properties.Count; i++) { 387 int offset = 80 + 80 * i; 388 389 draw.text(new V2(30, 70 + offset), font, 24, properties[i].name, color.black); 390 draw.rect(new V4(40 + draw.get_text_rect(new V2(30, 70), font, 24, properties[i].name).z, 70 + offset, 30, 30), new V4(103, 111, 163, 255), true, 5); 391 draw.text(new V2(47 + draw.get_text_rect(new V2(30, 70), font, 24, properties[i].name).z, 71 + offset), "Roboto-Bold", 24, properties[i].datatype.ToString()[0].ToString().ToUpper()); 392 393 if(properties[i].datatype == "string") { 394 property tmp = properties[i]; 395 string tmp_ = tmp.default_value; 396 devgui.input_box(new V4(30, 110 + offset, 200, 35), font, ref tmp_, "general", properties[i].name, ""); 397 tmp.default_value = tmp_; 398 properties[i] = tmp; 399 } else if(properties[i].datatype == "int") { 400 property tmp = properties[i]; 401 int tmp_ = tmp.default_value; 402 devgui.num_input_box(new V4(30, 110 + offset, 200, 35), font, ref tmp_, "general", properties[i].name); 403 tmp.default_value = tmp_; 404 properties[i] = tmp; 405 } else if(properties[i].datatype == "bool") { 406 property tmp = properties[i]; 407 bool tmp_ = tmp.default_value; 408 devgui.button(new V4(30, 110 + offset, draw.get_text_rect(new V2(0, 0), font, 24, properties[i].name).z + 10, 35), ref tmp_, font, "x"); 409 tmp.default_value = tmp_; 410 properties[i] = tmp; 411 } 412 } 413 } 414 415 private void draw_add_property() { 416 draw.rect(new V4(0, 0, game.resolution.x, game.resolution.y), new V4(0, 0, 0, 170), true); 417 V4 panel_rect = new V4(game.resolution.x / 2 - 200, game.resolution.y / 2 - 300, 400, 600); 418 draw.rect(panel_rect, color.white, true, 25); 419 420 draw.text(new V2(20 + panel_rect.x, 20 + panel_rect.y), "Roboto", 24, "Name", color.black); 421 devgui.input_box(new V4(20 + panel_rect.x, 70 + panel_rect.y, 200, 35), "Roboto", ref add_prop_name, "add_property", "add_prop_name", ""); 422 423 bool string_ = false; 424 bool int_ = false; 425 bool bool_ = false; 426 427 draw.text(new V2(20 + panel_rect.x, 130 + panel_rect.y), "Roboto", 24, "Type", color.black); 428 429 draw.rect(new V4(80 + panel_rect.x, 130 + panel_rect.y, 30, 30), new V4(103, 111, 163, 255), true, 5); 430 draw.text(new V2(87 + panel_rect.x, 131 + panel_rect.y), "Roboto-Bold", 24, add_prop_type.ToString()[0].ToString().ToUpper()); 431 432 devgui.button(new V4(20 + panel_rect.x, 175 + panel_rect.y, 80, 35), ref string_, "Roboto", "String"); 433 devgui.button(new V4(120 + panel_rect.x, 175 + panel_rect.y, 80, 35), ref bool_, "Roboto", "Bool"); 434 devgui.button(new V4(220 + panel_rect.x, 175 + panel_rect.y, 80, 35), ref int_, "Roboto", "Int"); 435 436 if(string_) { 437 add_prop_type = "string"; 438 } else if(bool_) { 439 add_prop_type = "bool"; 440 } else if(int_) { 441 add_prop_type = "int"; 442 } 443 444 draw.text(new V2(20 + panel_rect.x, 285 + panel_rect.y), "Roboto", 24, "Default", color.black); 445 446 if(add_prop_type == "string") { 447 devgui.input_box(new V4(20 + panel_rect.x, 330 + panel_rect.y, 300, 35), "Roboto", ref add_prop_def_s, "add_property", "add_prop_def", ""); 448 } else if(add_prop_type == "int") { 449 devgui.num_input_box(new V4(20 + panel_rect.x, 330 + panel_rect.y, 300, 35), "Roboto", ref add_prop_def_i, "add_property", "add_prop_def"); 450 } else if(add_prop_type == "bool") { 451 devgui.button(new V4(20 + panel_rect.x, 330 + panel_rect.y, 110, 35), ref add_prop_def_b, "Roboto", "Default"); 452 } 453 454 bool add = false; 455 devgui.button(new V4(20 + panel_rect.x, 385 + panel_rect.y, 80, 35), ref add, "Roboto", "Add"); 456 457 if(add || input.key_just_pressed(input.key_return)) { 458 if(add_prop_name != "") { 459 properties.Add(new property(add_prop_name, add_prop_type, add_prop_type == "string" ? add_prop_def_s : add_prop_type == "bool" ? add_prop_def_b : add_prop_type == "int" ? add_prop_def_i : "")); 460 461 add_prop_name = ""; 462 add_prop_def_b = false; 463 add_prop_def_i = 0; 464 add_prop_def_s = ""; 465 add_prop_type = "string"; 466 467 input.set_input_state("general"); 468 } 469 } 470 } 471 472 private void draw_tools() { 473 draw.text(new V2(30, 70), font, 24, "Paint", color.black); 474 475 string tmp_ = paint_prop_name; 476 devgui.input_box(new V4(30, 120, 200, 35), font, ref paint_prop_name, "general", "paint_prop_name", ""); 477 478 foreach(property i in properties) { 479 if(i.name == paint_prop_name) { 480 if(i.datatype == "string") { 481 if(paint_prop_name != tmp_) { 482 paint_prop_value = ""; 483 } 484 485 string tmp = paint_prop_value; 486 devgui.input_box(new V4(30, 175, 300, 35), font, ref tmp, "general", "paint_prop_value", ""); 487 paint_prop_value = tmp; 488 } else if(i.datatype == "int") { 489 if(paint_prop_name != tmp_) { 490 paint_prop_value = 0; 491 } 492 493 int tmp = paint_prop_value; 494 devgui.num_input_box(new V4(30, 175, 300, 35), font, ref tmp, "general", "paint_prop_value"); 495 paint_prop_value = tmp; 496 } else if(i.datatype == "bool") { 497 if(paint_prop_name != tmp_) { 498 paint_prop_value = false; 499 } 500 501 bool tmp = paint_prop_value; 502 devgui.button(new V4(30, 175, 110, 35), ref tmp, font, "x"); 503 paint_prop_value = tmp; 504 } 505 break; 506 } 507 } 508 509 draw.text(new V2(30, 250), font, 24, "Highlight", color.black); 510 511 tmp_ = highlight_prop_name; 512 devgui.input_box(new V4(30, 300, 200, 35), font, ref highlight_prop_name, "general", "highlight_prop_name", ""); 513 514 foreach(property i in properties) { 515 if(i.name == highlight_prop_name) { 516 if(i.datatype == "string") { 517 if(highlight_prop_name != tmp_) { 518 highlight_prop_value = ""; 519 } 520 521 string tmp = highlight_prop_value; 522 devgui.input_box(new V4(30, 355, 300, 35), font, ref tmp, "general", "highlight_prop_value", ""); 523 highlight_prop_value = tmp; 524 } else if(i.datatype == "int") { 525 if(highlight_prop_name != tmp_) { 526 highlight_prop_value = 0; 527 } 528 529 int tmp = highlight_prop_value; 530 devgui.num_input_box(new V4(30, 355, 300, 35), font, ref tmp, "general", "highlight_prop_value"); 531 highlight_prop_value = tmp; 532 } else if(i.datatype == "bool") { 533 if(highlight_prop_name != tmp_) { 534 highlight_prop_value = false; 535 } 536 537 bool tmp = highlight_prop_value; 538 devgui.button(new V4(30, 355, 110, 35), ref tmp, font, "x"); 539 highlight_prop_value = tmp; 540 } 541 break; 542 } 543 } 544 545 V4 file_button_rect = new V4(30, game.resolution.y - 80, 100, 50); 546 547 draw.rect(file_button_rect, !helpers.mouse_inside(file_button_rect) ? new V4(90, 45, 230, 255) : new V4(170, 150, 240, 255), true, 25); 548 draw.text(new V2(65, game.resolution.y - 66), "Roboto-Bold", 20, "File"); 549 if(helpers.mouse_inside(file_button_rect) && mouse.button_just_pressed(mb.left, "general")) { 550 input.set_input_state("file"); 551 } 552 } 553 554 private void draw_file() { 555 draw.rect(new V4(0, 0, game.resolution.x, game.resolution.y), new V4(0, 0, 0, 170), true); 556 V4 panel_rect = new V4(game.resolution.x / 2 - 200, game.resolution.y / 2 - 300, 400, 600); 557 // draw.rect(panel_rect, color.white, true, 25); 558 559 // bool save_ = false; 560 // draw.text(new V2(30 + panel_rect.x, 30 + panel_rect.y), font, 24, "Save", color.black); 561 // devgui.input_box(new V4(30 + panel_rect.x, 60 + panel_rect.y, 300, 35), font, ref save_path, "file", "save_file", ""); 562 // devgui.button(new V4(30 + panel_rect.x, 125 + panel_rect.y, 80, 35), ref save_, font, "Save"); 563 // if(save_) { 564 // save(save_path); 565 // input.set_input_state("general"); 566 // } 567 568 // bool load_ = false; 569 // draw.text(new V2(30 + panel_rect.x, 180 + panel_rect.y), font, 24, "Load", color.black); 570 // devgui.input_box(new V4(30 + panel_rect.x, 210 + panel_rect.y, 300, 35), font, ref load_path, "file", "load_file", ""); 571 // devgui.button(new V4(30 + panel_rect.x, 275 + panel_rect.y, 80, 35), ref load_, font, "Load"); 572 // if(load_) { 573 // load(load_path); 574 // input.set_input_state("general"); 575 // } 576 577 fui.begin("File", false); 578 fui.options() 579 .position(new V2(panel_rect.x, panel_rect.y)) 580 .size(new V2(panel_rect.z, panel_rect.w)) 581 .input_state("file") 582 583 .background(color_background) 584 .text(color_text) 585 .foreground(color_foreground) 586 .darkerforeground(color_darkerforeground) 587 588 .assign(); 589 590 fui.header("Save"); 591 fui.input_box("path", ref save_path, "save_path"); 592 if(fui.button("Save", "save_button")) { 593 save(save_path); 594 input.set_input_state("general"); 595 } 596 597 fui.header("Load"); 598 fui.input_box("path", ref load_path, "load_path"); 599 if (fui.button("Load", "load_button")) { 600 load(load_path); 601 input.set_input_state("general"); 602 } 603 604 fui.header("Resize"); 605 fui.slider_int("Grid Width", ref grid_width, new V2(0, 128)); 606 fui.slider_int("Grid Height", ref grid_height, new V2(0, 128)); 607 fui.slider_int("Tile Width", ref tile_width, new V2(0, 128)); 608 fui.slider_int("Tile Height", ref tile_height, new V2(0, 128)); 609 if(fui.button("Resize", "resize_button")) { 610 zoom = 1; 611 size = new V2(grid_width, grid_height); 612 tile_size = new V2(tile_width, tile_height); 613 input.input_state = "general"; 614 615 tile_map = new List<List<Dictionary<string, dynamic>>>(); 616 tiles = new Dictionary<string, tile>(); 617 618 for(var i = 0; i < size.x; i++) { 619 tile_map.Add(new List<Dictionary<string, dynamic>>()); 620 for(var j = 0; j < size.y; j++) { 621 tile_map[i].Add(new Dictionary<string, dynamic>() { 622 {"tile_id", ""} 623 }); 624 } 625 } 626 } 627 628 fui.end(); 629 } 630 631 private void save(string path) { 632 tilemap format = new tilemap(); 633 format.grid_size = size; 634 format.tile_size = tile_size; 635 format.properties = properties; 636 format.tile_map = tile_map; 637 format.tiles = tiles; 638 639 string jsonString = JsonConvert.SerializeObject(format); 640 // Debug.send(jsonString); 641 if(!Directory.Exists(game.get_resource_folder() + "/" + game.asset_pack + "/data")) { 642 Directory.CreateDirectory(game.get_resource_folder() + "/" + game.asset_pack + "/data"); 643 } 644 645 if(!Directory.Exists(game.get_resource_folder() + "/" + game.asset_pack + "/data/tilemaps")) { 646 Directory.CreateDirectory(game.get_resource_folder() + "/" + game.asset_pack + "/data/tilemaps"); 647 } 648 649 File.WriteAllText(game.get_resource_folder() + "/" + game.asset_pack + "/data/tilemaps/" + path + ".ftm", jsonString); 650 } 651 652 private void load(string path) { 653 string JsonString = ""; 654 if(File.Exists(game.get_resource_folder() + "/" + game.asset_pack + "/data/tilemaps/" + path + ".ftm")) { 655 JsonString = File.ReadAllText(game.get_resource_folder() + "/" + game.asset_pack + "/data/tilemaps/" + path + ".ftm"); 656 } else { 657 return; 658 } 659 660 tilemap format = JsonConvert.DeserializeObject<tilemap>(JsonString); 661 662 foreach(string key in format.tiles.Keys) { 663 format.tiles[key].tex.set_texture(format.tiles[key].path); 664 } 665 666 size = format.grid_size; 667 tile_size = format.tile_size; 668 properties = format.properties; 669 tile_map = format.tile_map; 670 tiles = format.tiles; 671 } 672 } 673 674 // Main Class 675 676 class Program 677 { 678 public static void Main(string[] args) 679 { 680 // Function that starts game 681 // The parameter should be your start scene 682 game.set_resource_folder("resources"); 683 game.run(new main()); 684 } 685 } 686}