Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game.editor;
- import cuttlefish.*;
- import cuttlefish.utils.Console;
- import cuttlefish.input.Mouse;
- import cuttlefish.input.Keyboard;
- import cuttlefish.components.CollisionSystem;
- import cuttlefish.components.CollisionBox;
- import cuttlefish.graphics.Atlas;
- import kha.input.KeyCode;
- import cuttlefish.io.Files;
- import kha.Blob;
- import game.*;
- import game.battle.*;
- import game.dialogs.*;
- import game.editor.*;
- import game.entities.*;
- import cuttlefish.math.CMath;
- import tween.Delta;
- import tween.easing.*;
- import tween.tweens.FloatTween;
- import cuttlefish.ui.UI_Window;
- import cuttlefish.ui.UI_SelectItem;
- typedef LevelsStruct = {
- var levels: Array<String>;
- };
- private class InputTextField {
- public var x: Float;
- public var y: Float;
- var offset: Float = 0;
- public var content: String = "";
- public var desc: String = "";
- public var activated: Bool = false;
- var onEnter: String -> Void;
- public function new(x: Float, y: Float, d: String, fun: String->Void) {
- this.x=x;
- this.y=y;
- onEnter = fun;
- desc = d;
- }
- public function update(): Void {
- // if(Mouse.checkPressed(Mouse.LEFT)){
- // var mx = Mouse.me.x;
- // var my = Mouse.me.y;
- // if(mx >= x-offset && mx <= x+offset*2+Graphics.stringWidth(content)*Graphics.fontScale*2
- // && my >= y-offset && my <= y+offset*2+(Graphics.stringHeight(content)+Graphics.fontScale)*Graphics.fontScale*2){
- // activated = !activated;
- // }
- // }
- if(activated){
- var char = Keyboard.getKeyPressed();
- if(char != ""){
- if(Atlas.get(0).exists(char)){
- content += char;
- }
- }
- if(Keyboard.checkPressed(KeyCode.Backspace)){
- content = content.substring(0,content.length-1);
- }
- if(Keyboard.checkPressed(KeyCode.Return)){
- if(onEnter != null){
- onEnter(content);
- activated = false;
- }
- }
- }
- }
- public function draw(): Void {
- if(activated){
- var lfs = Graphics.fontScale;
- Graphics.fontScale = lfs*2;
- Graphics.setColor(Graphics.Black);
- Graphics.drawRect(true
- ,x-offset -Graphics.stringWidth(content)*Graphics.fontScale/2
- ,y-offset -(Graphics.stringHeight(content)+Graphics.fontScale)*Graphics.fontScale/2
- , Graphics.stringWidth(content)*Graphics.fontScale + offset*2
- , (Graphics.stringHeight(content)+Graphics.fontScale)*Graphics.fontScale + offset*2);
- Graphics.setColor(Graphics.White);
- Graphics.drawString(content, x-Graphics.stringWidth(content)*Graphics.fontScale/2, y-(Graphics.stringHeight(content)+Graphics.fontScale)*Graphics.fontScale/2);
- Graphics.setColor(Graphics.Black);
- Graphics.drawRect(true,
- x - Graphics.stringWidth(desc)*Graphics.fontScale/2,
- y-(Graphics.stringHeight(desc)+Graphics.fontScale)*Graphics.fontScale*2,
- Graphics.stringWidth(desc)*Graphics.fontScale,
- (Graphics.stringHeight(desc))*Graphics.fontScale);
- Graphics.setColor(Graphics.White);
- Graphics.drawString(desc, x - Graphics.stringWidth(desc)*Graphics.fontScale/2, y-(Graphics.stringHeight(desc)+Graphics.fontScale)*Graphics.fontScale*2);
- Graphics.fontScale = lfs;
- }
- }
- }
- private class IconButton {
- var index: Int = 0;
- var hovered: Bool = false;
- var yoff: Float = 0;
- var editMode: EditMode;
- public function new(ind: Int, em: EditMode) {
- index = ind;
- editMode = em;
- }
- public function activate(): Void {
- switch(index){
- case 0://save
- // input.activated = true;
- var cw: EditInputWindow;
- cw = new EditInputWindow((1280)/2, (800)/2, function() {
- editMode.save_state(cw.value);
- editMode.current_file = cw.value;
- Console.print('Level saved to ${cw.value}');
- cw.delayDestroy();
- });
- cw.manager = MainScene.testUI;
- cw.center();
- cw.setTitle("Save level:");
- MainScene.testUI.windows.push(cw);
- case 1://load
- // input.activated = true;
- var cw: EditListWindow;
- cw = new EditListWindow((1280)/2, (800)/2, function() {
- LocationZone.id_destroying = true;
- editMode.load_state(cw.value);
- LocationZone.id_destroying = false;
- Console.print('Loaded level from ${cw.value}');
- cw.delayDestroy();
- });
- cw.manager = MainScene.testUI;
- cw.center();
- cw.setTitle("Load level:");
- MainScene.testUI.windows.push(cw);
- case 2:
- // editMode.show_objects = !editMode.show_objects;
- }
- }
- public function update(input: InputTextField): Void {
- var mx = Mouse.me.realx;
- var my = Mouse.me.realy;
- var h = kha.System.windowHeight(0);
- if(my > h-64 && mx > index*64 && mx < (index+1)*64){
- hovered = true;
- }else{
- hovered = false;
- }
- if(hovered){
- if(Mouse.checkPressed(Mouse.LEFT)){
- activate();
- }
- }
- }
- public function draw(spr: String): Void {
- var atl = AtlasManager.getAtlas("ui");
- var w = 72;//atl.getSpriteWidth(spr);
- var h= atl.getSpriteHeight(spr);
- if(!hovered){
- Graphics.drawAtlasImage(atl,spr, Camera.getLeft() + index*w, Camera.getBottom()-h);
- }else{
- Graphics.drawAtlasImage(atl,spr, Camera.getLeft() + index*w, Camera.getBottom()-h-32);
- }
- }
- }
- class EditMode extends Entity {
- var camera: Camera;
- var console: Console;
- var camera_dragged: Bool = false;
- var start_x: Float;
- var start_y: Float;
- var camera_start_x: Float;
- var camera_start_y: Float;
- var collision_system: CollisionSystem;
- var entity_dragging: Bool = false;
- var entity_dragging_xo: Float = 0;
- var entity_dragging_yo: Float = 0;
- var snap_size: Float = 64;
- public var snap_x: Float = 64;
- public var snap_y: Float = 64;
- var icons: Array<String>;
- var buttons: Array<IconButton>;
- var inputs: Array<InputTextField>;
- var test_string: String = "";
- public var current_file: String = "";
- public static var levels: Array<String> = [];
- public var skipClick: Bool = false;
- public var selected_entity: Entity = null; //actually a reference to some entity
- public var enabled: Bool = false;
- public var edit_entities: Array<Entity>;
- public var show_objects: Bool = false;
- public var objects_to_create: Array<String>;
- public var objects_sprite: Array<String>;
- public var holding_object: Int = -1;
- var deleted_entities: Array<Entity>;
- var debug_info: Bool = false;
- public var actions: Array<String>;
- var pickedFromX: Float = 0;
- var pickedFromY: Float = 0;
- var draw_grid: Bool = false;
- public var current_zoom: Float = 100;
- public var groups: Array<String>;
- public function getEntitiesByGroup(name: String): Array<Entity> {
- var return_array: Array<Entity> = new Array<Entity>();
- for(i in edit_entities){
- if(cast(i.getComponent(Editable), Editable).group == name){
- return_array.push(i);
- }
- }
- return return_array;
- }
- public var levels_json: LevelsStruct;
- public function reload_levels(): Void {
- levels = [];
- kha.Assets.loadBlobFromPath("data/levels.json", function(a: kha.Blob){
- levels_json = haxe.Json.parse(a.toString());
- for(i in levels_json.levels) {
- levels.push(i + ".chl");
- }
- });
- }
- public function new() {
- super("editor");
- edit_entities = new Array<Entity>();
- deleted_entities = new Array<Entity>();
- actions = new Array<String>();
- icons = ["save", "icon_load", "object"];
- buttons = new Array<IconButton>();
- buttons.push(new IconButton(0, this));
- buttons.push(new IconButton(1, this));
- // buttons.push(new IconButton(2, this));
- groups = new Array<String>();
- groups.push('default');
- groups.push('invisible');
- groups.push('npcs');
- groups.push('dialogs');
- groups.push('lighting');
- reload_levels();
- inputs = new Array<InputTextField>();
- inputs.push(new InputTextField(0,256, "Save File:", function(filename: String){
- //check for proper filename
- // current_file = filename;
- // save_state(filename);
- // Console.print('Level saved to ${filename}');
- // for(i in inputs){
- // i.content = filename;
- // }
- // current_file = filename;
- }));
- inputs.push(new InputTextField(0,256, "Load File:", function(filename: String){
- //check for proper filename
- // load_state(filename);
- // Console.print('Loaded level from ${filename}');
- // for(i in inputs){
- // i.content = filename;
- // }
- }));
- inputs.push(new InputTextField(0,256, "", function(filename: String){
- //kinda weird, fix it
- }));
- objects_to_create = new Array<String>();
- objects_to_create.push("NPC");
- objects_to_create.push("image");
- objects_to_create.push("block");
- objects_to_create.push("light");
- objects_to_create.push("scast");
- objects_to_create.push("location");
- objects_to_create.push("dzone");
- objects_to_create.push("sleep");
- objects_to_create.push("egroup");
- objects_to_create.push("ezone");
- objects_to_create.push("computer");
- objects_to_create.push("czone");
- objects_to_create.push("inv_chest");
- objects_to_create.push("scrzone");
- objects_to_create.push("note");
- objects_to_create.push("bugs");
- objects_to_create.push("part_sys");
- objects_to_create.push("eset_zone");
- objects_to_create.push("darkjune");
- // objects_sprite = new Sprite(AtlasManager.getAtlas("ui"), ["NPC", "image", "icon_block", "location_zone", "icon_dialog", "sleep_zone", "NPC", "image", "image", "icon_cutscene"]);
- objects_sprite = ["NPC", "image", "icon_block", "image", "icon_scast", "location_zone", "icon_dialog", "sleep_zone", "NPC", "icon_ezone", "image", "icon_cutscene", "icon_chest", "icon_scrzone", "icon_note", "NPC", "image", "image", "NPC"];
- }
- private function insertLine(a: String, b: Dynamic, ?c: String = " "): String{
- return a + Std.string(b) + c;
- }
- //FIXME: please
- //kinda a hack
- public inline function resetIDs(): Void {
- // return;
- // Entity.IDS = 100000;
- // VisualObject.NID = 0;
- // Block.NID = 0;
- // Light.NID = 0;
- // Enemy.NID = 0;
- // SleepZone.NID = 0;
- // NPC_Entity.NID = 1000;//?????????????
- // ShadowCast.NID = 0;
- // ScriptZone.NID = 0;
- // EnemyGroup.NID = 0;
- // ComputerZone.NID = 0;
- // CutsceneZone.NID = 0;
- // LocationZone.NID = 0;
- // DialogTriggerZone.NID = 0;
- // EnemyAttractionZone.NID = 0;
- }
- public function save_state(?filename: String = "level0.chl"): Void {
- var ss: String = "";
- for(i in edit_entities){
- if(i == null) continue;
- var current_editable: Editable = cast(i.getComponent(Editable));
- if(current_editable == null) continue;
- ss = insertLine(ss, current_editable.edit_id);
- ss = insertLine(ss, i.x);
- ss = insertLine(ss, i.y);
- ss = insertLine(ss, current_editable.locked ? "1" : "0");
- ss = insertLine(ss, 'group:s\"${current_editable.group}\"');
- //fields
- for(j in 0...current_editable.fields.length){
- var isLast: Bool = (j == current_editable.fields.length-1);
- if(current_editable.fields_type[j] == "s"){
- var string_to_save: String = Reflect.field(current_editable.entity, current_editable.fields[j]);
- string_to_save = StringTools.replace(string_to_save, " ", "&");
- ss = insertLine(ss, '${current_editable.fields[j]}:${current_editable.fields_type[j]}\"${string_to_save}\"', isLast ? "" : " ");
- }else{
- ss = insertLine(ss, '${current_editable.fields[j]}:${current_editable.fields_type[j]}${Reflect.field(current_editable.entity, current_editable.fields[j])}', isLast ? "" : " ");
- }
- }
- if(i != edit_entities[edit_entities.length-1]){
- ss = insertLine(ss, '
- ', "");
- }
- }
- Files.saveStringToFile(ss, "data/levels/" + filename);
- var filename_wp = filename.substring(0, filename.length-4);
- for(i in levels_json.levels){
- if(i == filename_wp){
- return;
- }
- }
- levels_json.levels.push(filename_wp);
- cuttlefish.io.Files.saveStringToFile(haxe.Json.stringify(levels_json), "data/levels.json");
- }
- public function load_state(?filename: String = "level0.chl", ?done: Void -> Void = null): Void {
- actions = new Array<String>();
- // if(selected_entity != null){
- // var box: CollisionBox = cast(selected_entity.getComponent(CollisionBox));
- // box.isDebug = false;
- // }
- // selected_entity = null;
- resetIDs();
- kha.Assets.loadBlobFromPath("data/levels/" + filename, function(a: Blob){
- current_file = filename;
- for(j in 0...edit_entities.length){
- if(edit_entities[j] != null){
- if(!Std.is(edit_entities[j], Player)
- && !Std.is(edit_entities[j], Rammon)){
- // edit_entities[j].id = "null";
- if(edit_entities[j] != null){
- if(!edit_entities[j].toDestroy){
- edit_entities[j]._destroy();
- }
- }
- }
- }
- }
- edit_entities = new Array<Entity>();
- edit_entities.push(scene.getEntityById("player"));
- edit_entities.push(scene.getEntityById("rammon"));
- var loadedText: String = a.toString();
- var tokens: Array<String> = loadedText.split("
- ");
- for(i in 0...tokens.length){
- var data_of_entity: Array<String> = tokens[i].split(" ");
- var name: String = data_of_entity[0];
- var entity_x: Float = Std.parseFloat(data_of_entity[1]);
- var entity_y: Float = Std.parseFloat(data_of_entity[2]);
- var entity_locked: Bool = data_of_entity[3] == "1" ? true : false;
- var data: Array<String> = new Array<String>();
- if(data_of_entity.length >= 5){
- for(j in 4...data_of_entity.length){
- var args: Array<String> = data_of_entity[j].split(":");
- var arg_name: String = args[0];
- var arg_type: String = args[1].charAt(0);
- var arg_value: String = args[1].substring(1,args[1].length);
- if(arg_type == "s"){
- arg_value = arg_value.substring(1, arg_value.length-1);
- //change & to space
- arg_value = StringTools.replace(arg_value, "&", " ");
- }
- data.push(arg_name);
- data.push(arg_type);
- data.push(arg_value);
- // Console.print('${arg_name} = ${arg_value}, typeof ${arg_type}');
- }
- }
- createEntityFromData(name, entity_x, entity_y, entity_locked, data);
- }
- switch(filename){
- case "mansion_outside.chl": {
- Camera.lock(-1000, -2850, 3600, 700);
- }
- default: {
- Camera.unlock();
- }
- }
- if(done != null) {
- done();
- }
- // var tokens: Array<String> = loadedText.split(" ");
- // var entity_to_create: String = "";
- // var entity_x: Float = 0;
- // var entity_y: Float = 0;
- // var created: Bool = false;
- // for(i in 0...Math.floor(tokens.length/3)){
- // entity_to_create = tokens[i*3];
- // entity_x = Std.parseFloat(tokens[i*3+1]);
- // entity_y = Std.parseFloat(tokens[i*3+2]);
- // createEntityFromData(entity_to_create, entity_x, entity_y);
- // }
- }, function(error: kha.AssetError){
- Console.print('Error: ${error.url}: ${error.error}');
- });
- }
- inline function arrows_movement(): Void {
- if(Keyboard.check(KeyCode.Shift)){
- if(Keyboard.checkPressed(KeyCode.Right)) selected_entity.x += 3;
- if(Keyboard.checkPressed(KeyCode.Left)) selected_entity.x -= 3;
- if(Keyboard.checkPressed(KeyCode.Down)) selected_entity.y += 3;
- if(Keyboard.checkPressed(KeyCode.Up)) selected_entity.y -= 3;
- }else{
- if(Keyboard.checkPressed(KeyCode.Right)) selected_entity.x += 1;
- if(Keyboard.checkPressed(KeyCode.Left)) selected_entity.x -= 1;
- if(Keyboard.checkPressed(KeyCode.Down)) selected_entity.y += 1;
- if(Keyboard.checkPressed(KeyCode.Up)) selected_entity.y -= 1;
- }
- }
- function createEntityFromData(name: String, x: Float, y: Float, locked: Bool, data: Array<String>): Void {
- var createdEntity: Entity = null;
- switch(name) {
- case "player":
- // scene.add(new Player(x, y));
- var player = scene.getEntityById("player");
- player.x = x;
- player.y = y;
- createdEntity = player;
- Camera.me.setPosition(player.x-Camera.getWidth()/2,player.y-Camera.getHeight()/2 - 32);
- // Camera.me.setPosition(x-Camera.getWidth()/2,y-Camera.getHeight()/2);
- // cast(createdEntity, Player).setPos(x, y);
- case "NPC":
- var npc = new NPC_Entity(x, y);
- scene.add(npc);
- npc.setRect(32,32);
- // npc.setDialog("boop");
- npc.setDepth(1);
- createdEntity = npc;
- case "rammon":
- var rammon = scene.getEntityById("rammon");
- rammon.x = x;
- rammon.y = y;
- createdEntity = rammon;
- case "visualobject":
- var vo = new VisualObject(x, y);
- scene.add(vo);
- createdEntity = vo;
- case "darkjune":
- var j = new DarkJune(x, y);
- scene.add(j);
- createdEntity = j;
- case "block":
- setAllCollisionVisible(true);
- var block = new Block(x, y);
- scene.add(block);
- createdEntity = block;
- case "inv_chest":
- var chest = new InventoryChest(x,y);
- scene.add(chest);
- createdEntity = chest;
- case "light":
- var light = new Light(x, y);
- scene.add(light);
- createdEntity = light;
- case "scast":
- var scast = new ShadowCast(x, y);
- scene.add(scast);
- createdEntity = scast;
- case "location":
- var location = new LocationZone(x, y);
- scene.add(location);
- createdEntity = location;
- case "sleep_zone":
- var loc = new SleepZone(x, y);
- scene.add(loc);
- createdEntity = loc;
- case "egroup":
- var enemygroup = new EnemyGroup(x, y);
- scene.add(enemygroup);
- createdEntity = enemygroup;
- case "dzone":
- var dzone = new DialogTriggerZone(x, y);
- scene.add(dzone);
- createdEntity = dzone;
- case "czone":
- var czone = new CutsceneZone(x,y);
- scene.add(czone);
- createdEntity = czone;
- case "scrzone":
- var szone = new ScriptZone(x, y);
- scene.add(szone);
- createdEntity = szone;
- case "ezone":
- var enem = new EnemyAttractionZone(x, y);
- scene.add(enem);
- createdEntity = enem;
- case "eset_zone":
- var zone = new EnemySetZone(x,y);
- scene.add(zone);
- createdEntity = zone;
- case "computer":
- var comp = new ComputerZone(x, y);
- scene.add(comp);
- createdEntity = comp;
- case "note":
- var note = new Note(x, y);
- scene.add(note);
- createdEntity = note;
- case "bugs":
- var bugs = new Bugs(x, y);
- scene.add(bugs);
- createdEntity = bugs;
- case "part_sys":
- var p = new ParticleSystem(x, y);
- scene.add(p);
- createdEntity = p;
- }
- if(createdEntity != null){
- var createdEditable: Editable = cast(createdEntity.getComponent(Editable));
- createdEditable.locked = locked;
- if(data.length == 0) return;
- for(i in 0...Math.floor(data.length/3)){
- if(data[i*3] == "group"){
- // trace(data[i*3 +2]);
- createdEditable.group = data[i*3 + 2];
- createdEditable.changeVisualField("group", data[i*3 + 2]);
- continue;
- }
- // var no_save: Bool = Reflect.hasField(Reflect.field(createdEntity, data[i*3]), "nosave");
- // if(no_save){
- // trace('${data[i*3]}');
- // }
- // if(Reflect.hasField(createdEntity, data[i*3])){
- switch(data[i*3 + 1]){
- case "s":
- Reflect.setField(createdEntity, data[i*3], data[i*3+2]);
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "s", data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- case "l":
- Reflect.setField(createdEntity, data[i*3], data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "l", data[i*3+2]);
- case "f":
- Reflect.setField(createdEntity, data[i*3], Std.parseFloat(data[i*3+2]));
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "f", data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- case "i":
- Reflect.setField(createdEntity, data[i*3], Std.parseInt(data[i*3+2]));
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "i", data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- default:
- }
- // }else{
- // Console.print('${createdEntity.id} dont have a ${data[i*3]}');
- // }
- }
- }
- }
- public override function init(): Void {
- super.init();
- camera = Camera.me;
- console = Console.me;
- start_x = 0;
- start_y = 0;
- camera_start_x = 0;
- camera_start_y = 0;
- collision_system = new CollisionSystem();
- scene.addSystem(collision_system);
- setDepth(-10000);
- // camera.setSize(Std.int(camera.width - 64*9), Std.int(camera.height - 40*9));
- // camera.setPosition(camera.x + 64*4.5, camera.y + 40*4.5);
- }
- public function update_objects(): Void {
- if(Mouse.checkReleased(Mouse.LEFT)){
- if(holding_object != -1){
- if(Mouse.me.realy <= Engine.core.window_height - 64){
- var mx: Float = Math.floor(Mouse.me.x);
- var my: Float = Math.floor(Mouse.me.y);
- try{
- var entity: Entity = cast _createEntity(id_to_class[objects_to_create[holding_object]], mx, my);
- scene.add(entity);
- actions.push('create ${entity.id}');
- }catch(e: Dynamic){
- trace(objects_to_create[holding_object]);
- trace(e);
- }
- /*
- switch(objects_to_create[holding_object]){
- case "NPC":
- var npc = new NPC_Entity(mx,my);
- scene.add(npc);
- npc.setRect(32,32);
- // npc.setDialog("boop");
- npc.setDepth(1);
- actions.push('create ${npc.id}');
- case "image":
- var image = new VisualObject(mx, my);
- scene.add(image);
- actions.push('create ${image.id}');
- case "block":
- setAllCollisionVisible(true);
- var block = new Block(mx, my);
- scene.add(block);
- cast(block.getComponent(Editable), Editable).group = "invisible";
- cast(block.getComponent(Editable), Editable).changeVisualField("group", "invisible");
- actions.push('create ${block.id}');
- case "inv_chest":
- var chest = new InventoryChest(mx, my);
- scene.add(chest);
- cast(chest.getComponent(Editable), Editable).group = "invisible";
- cast(chest.getComponent(Editable), Editable).changeVisualField("group", "invisible");
- actions.push('create ${chest.id}');
- case "light":
- var light = new Light(mx ,my);
- scene.add(light);
- actions.push('create ${light.id}');
- case "scast":
- var scast = new ShadowCast(mx, my);
- scene.add(scast);
- cast(scast.getComponent(Editable), Editable).group = "invisible";
- cast(scast.getComponent(Editable), Editable).changeVisualField("group", "invisible");
- actions.push('create ${scast.id}');
- case "location":
- var location = new LocationZone(mx, my);
- scene.add(location);
- actions.push('create ${location.id}');
- case "sleep":
- var loc = new SleepZone(mx, my);
- scene.add(loc);
- actions.push('create ${loc.id}');
- case "egroup":
- var enemygroup = new EnemyGroup(mx, my);
- scene.add(enemygroup);
- actions.push('create ${enemygroup.id}');
- case "dzone":
- var dzone = new DialogTriggerZone(mx, my);
- scene.add(dzone);
- cast(dzone.getComponent(Editable), Editable).group = "dialogs";
- actions.push('create ${dzone.id}');
- case "czone":
- var czone = new CutsceneZone(mx,my);
- scene.add(czone);
- cast(czone.getComponent(Editable), Editable).group = "dialogs";
- actions.push('create ${czone.id}');
- case "scrzone":
- var szone = new ScriptZone(mx, my);
- scene.add(szone);
- cast(szone.getComponent(Editable), Editable).group = "dialogs";
- actions.push('create ${szone.id}');
- case "ezone":
- var enemy_zone = new EnemyAttractionZone(mx, my);
- scene.add(enemy_zone);
- actions.push('create ${enemy_zone.id}');
- case "computer":
- var computer_zone = new ComputerZone(mx, my);
- scene.add(computer_zone);
- actions.push('create ${computer_zone.id}');
- default:
- Console.print("nah");
- }
- */
- holding_object = -1;
- show_objects = false;
- }
- }
- }
- }
- public function updateZoom(): Void {
- var w = Engine.core.view_width;
- var h = Engine.core.view_height;
- var aspect = w/h;
- var nh = 1000;
- var nw = nh * aspect;
- var mw = (camera.width - Math.floor(nw*(current_zoom/100)))/2;
- var mh = (camera.height - Math.floor(nh*(current_zoom/100)))/2;
- camera.setSize(Math.floor(nw*(current_zoom/100)), Math.floor(nh*(current_zoom/100)));
- camera.setPosition(camera.x + mw, camera.y + mh);
- }
- public function _tween(obj: Dynamic, p: String, time: Float, val: Float){
- var t = Delta.tween(obj);
- return t.createTween(p, time,
- new FloatTween(t, p, val, time));
- }
- public function zoomTime(v: Float, t: Float): Void {
- var w = Engine.core.view_width;
- var h = Engine.core.view_height;
- var aspect = w/h;
- var nh = 1000;
- var nw = nh * aspect;
- var mw = (camera.width - Std.int(nw*(current_zoom/100)))/2;
- var mh = (camera.height - Std.int(nh*(current_zoom/100)))/2;
- camera.setSize(Std.int(nw*(current_zoom/100)), Std.int(nh*(current_zoom/100)));
- _tween(this, "current_zoom", t, v)
- .ease(tween.easing.Sine.easeInOut)
- .onUpdate(function(v2: Float){
- updateZoom();
- });
- }
- @:generic
- function _createEntity<T: haxe.Constraints.Constructible<(Float, Float) -> Void>>(a: Class<T>, x: Float, y: Float): T {
- // return new T(x, y);
- return Type.createInstance(a, [x, y]);
- }
- var id_to_class: Map<String, Class<haxe.Constraints.Constructible<(Float, Float) -> Void>>> = [
- "NPC" => NPC_Entity,
- "visualobject" => VisualObject,
- "darkjune" => DarkJune,
- "image" => VisualObject,
- "block" => Block,
- "inv_chest" => InventoryChest,
- "light" => Light,
- "scast" => ShadowCast,
- "location" => LocationZone,
- "sleep_zone" => SleepZone,
- "egroup" => EnemyGroup, //fix
- "dzone" => DialogTriggerZone,
- "czone" => CutsceneZone,
- "srczone" => ScriptZone,
- "scrzone" => ScriptZone,
- "ezone" => EnemyAttractionZone,
- "eset_zone" => EnemySetZone,
- "note" => Note,
- "computer" => ComputerZone,
- "bugs" => Bugs,
- "part_sys" => ParticleSystem,
- ];
- function create_cached_entity(cached_entity_name: String, data_fields: Array<Editable.EditableField>): Void {
- var entity: Entity = null;
- var mx = Mouse.me.x;
- var my = Mouse.me.y;
- var createdEntity: Entity = null;
- var x = mx;
- var y = my;
- if(cached_entity_name == "player" || cached_entity_name == "rammon"){
- trace('copy player or rammon');
- return;
- }
- createdEntity = cast _createEntity(id_to_class[cached_entity_name], x, y);
- scene.add(createdEntity);
- // switch(cached_entity_name) {
- // case "player":
- // // scene.add(new Player(x, y));
- // scene.getEntityById("player").x = x;
- // scene.getEntityById("player").y = y;
- // createdEntity = scene.getEntityById("player");
- // case "NPC":
- // var npc = new NPC_Entity(x, y);
- // scene.add(npc);
- // npc.setRect(32,32);
- // // npc.setDialog("boop");
- // npc.setDepth(1);
- // createdEntity = npc;
- // case "rammon":
- // scene.getEntityById("rammon").x = x;
- // scene.getEntityById("rammon").y = y;
- // createdEntity = scene.getEntityById("rammon");
- // case "visualobject":
- // var vo = new VisualObject(x, y);
- // scene.add(vo);
- // createdEntity = vo;
- // case "block":
- // setAllCollisionVisible(true);
- // var block = new Block(x, y);
- // scene.add(block);
- // createdEntity = block;
- // case "inv_chest":
- // var chest = new InventoryChest(x, y);
- // scene.add(chest);
- // createdEntity = chest;
- // case "light":
- // var light = new Light(x, y);
- // scene.add(light);
- // createdEntity = light;
- // case "scast":
- // var scast = new ShadowCast(x, y);
- // scene.add(scast);
- // createdEntity = scast;
- // case "location":
- // var location = new LocationZone(x, y);
- // scene.add(location);
- // createdEntity = location;
- // case "sleep_zone":
- // var loc = new SleepZone(x, y);
- // scene.add(loc);
- // createdEntity = loc;
- // case "egroup":
- // var enemygroup = new EnemyGroup(x, y);
- // scene.add(enemygroup);
- // createdEntity = enemygroup;
- // case "dzone":
- // var dzone = new DialogTriggerZone(x, y);
- // scene.add(dzone);
- // createdEntity = dzone;
- // case "czone":
- // var czone = new CutsceneZone(x,y);
- // scene.add(czone);
- // createdEntity = czone;
- // case "scrzone":
- // var szone = new ScriptZone(x,y);
- // scene.add(szone);
- // createdEntity = szone;
- // case "ezone":
- // var enem = new EnemyAttractionZone(x, y);
- // scene.add(enem);
- // createdEntity = enem;
- // case "computer":
- // var comp = new ComputerZone(x, y);
- // scene.add(comp);
- // createdEntity = comp;
- // }
- if(createdEntity != null) {
- var createdEditable: Editable = cast(createdEntity.getComponent(Editable));
- createdEditable.group = cached_entity_group;
- // createdEditable.locked = locked;
- var data: Array<String> = [];
- for(i in data_fields){
- data.push(i.var_name);
- data.push(i.type);
- data.push(i.value);
- }
- for(i in 0...Math.floor(data.length/3)){
- if(data[i*3] == "group"){
- // trace(data[i*3 + 2]);
- // createdEditable.group = data[i*3 + 2];
- // createdEditable.changeVisualField("group", data[i*3 + 2]);
- continue;
- }
- // if(Reflect.hasField(createdEntity, data[i*3])){
- switch(data[i*3 + 1]){
- case "s":
- Reflect.setField(createdEntity, data[i*3], data[i*3+2]);
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "s", data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- case "l":
- Reflect.setField(createdEntity, data[i*3], data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "l", data[i*3+2]);
- case "f":
- Reflect.setField(createdEntity, data[i*3], Std.parseFloat(data[i*3+2]));
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "f", data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- case "i":
- Reflect.setField(createdEntity, data[i*3], Std.parseInt(data[i*3+2]));
- if(createdEditable.onChangeValue != null) createdEditable.onChangeValue(data[i*3], "i", data[i*3+2]);
- createdEditable.changeVisualField(data[i*3], data[i*3+2]);
- default:
- }
- // }else{
- // Console.print('${createdEntity.id} dont have a ${data[i*3]}');
- // }
- }
- }
- }
- function setAllCollisionVisible(a: Bool): Void {
- for(i in edit_entities){
- var ie: Editable = cast(i.getComponent(Editable));
- if(ie.hasTag("hidable")){
- ie.visible = a;
- }
- }
- }
- var cached_entity_name: String = "";
- var cached_entity_fields: Array<Editable.EditableField>;
- var cached_entity_group: String = "";
- var scaling: Bool = false;
- var scaling_entity: Entity = null;
- var light_autooff: Bool = false;
- public override function update(dt: Float): Void {
- super.update(dt);
- if(Keyboard.checkPressed(KeyCode.Tab)){
- enabled = !enabled;
- if(light_autooff){
- if(enabled) {
- Engine.core.is_bloom = false;
- cast(scene.getEntityById("light_system"), LightSystem).enabled = false;
- }else{
- Engine.core.is_bloom = true;
- cast(scene.getEntityById("light_system"), LightSystem).enabled = true;
- }
- }
- if(!enabled){
- if(selected_entity != null){
- var box: CollisionBox = cast(selected_entity.getComponent(CollisionBox));
- box.isDebug = false;
- selected_entity = null;
- }
- }
- }
- //NOPE
- // if(Keyboard.checkPressed(KeyCode.M)){
- // SaveManager.saveSceneYaml(edit_entities);
- // }
- Graphics.debugBackgroundDraw = enabled;
- if(!enabled) return;
- update_objects();
- if(selected_entity != null){
- var _editable: Editable = cast(selected_entity.getComponent(Editable));
- if(_editable != null){
- _editable.update_meta();
- }
- //hotkey for locking and unlocking an entity
- if(Keyboard.checkPressed(KeyCode.Q)){
- _editable.locked = !_editable.locked;
- actions.push('locked ${selected_entity.id} ${_editable.locked ? "1" : "0"}');
- }
- if(Keyboard.checkPressed(KeyCode.C) && Keyboard.check(KeyCode.Control)){
- // trace('${_editable.edit_id} copied.');
- cached_entity_name = _editable.edit_id;
- cached_entity_group = _editable.group;
- //TODO: hard copy to new Array
- // cached_entity_fields = _editable.fields_editable;
- cached_entity_fields = [];
- for(i in _editable.fields_editable){
- cached_entity_fields.push({
- x: i.x,
- y: i.y,
- width: i.width,
- height: i.height,
- name: i.name,
- var_name: i.var_name,
- value: i.value,
- type: i.type,
- options: i.options,
- show_options: i.show_options,
- options_offset: i.options_offset,
- options_atlas: i.options_atlas,
- editing: i.editing,
- slider: i.slider,
- slider_min: i.slider_min,
- slider_max: i.slider_max,
- slider_changing: i.slider_changing,
- inEditable: i.inEditable,
- button: i.button,
- });
- }
- }
- }
- if(Keyboard.checkPressed(KeyCode.V) && Keyboard.check(KeyCode.Control)){
- if(cached_entity_name != ""){
- create_cached_entity(cached_entity_name, cached_entity_fields);
- }
- }
- for(i in inputs){
- i.x = Camera.getX() + Camera.getWidth()/2;
- i.y = Camera.getY() + Camera.getHeight()/2;
- i.update();
- }
- for(i in 0...buttons.length){
- buttons[i].update(inputs[i]);
- }
- var shifted = Keyboard.check(KeyCode.Shift);
- //TODO:
- //scale depended
- var mx: Float = Mouse.me.realx*2;
- var my: Float = Mouse.me.realy*2;
- var controlled = Keyboard.check(KeyCode.Control);
- if(shifted){
- if(Keyboard.checkPressed(KeyCode.G)){
- draw_grid = !draw_grid;
- }
- }else{
- if(Keyboard.checkPressed(KeyCode.G)){
- var window = new UI_Window(Mouse.me.realx, Mouse.me.realy);
- window.manager = MainScene.testUI;
- MainScene.testUI.windows.push(window);
- window.setTitle("Grid Settings");
- window.createInput(Std.string(snap_x), "w", function(a){
- snap_x = Std.parseFloat(a);
- });
- window.createInput(Std.string(snap_x), "h", function(a){
- snap_y = Std.parseFloat(a);
- });
- }
- if(Keyboard.checkPressed(KeyCode.L)){
- var window = new UI_Window(Mouse.me.realx, Mouse.me.realy);
- window.manager = MainScene.testUI;
- MainScene.testUI.windows.push(window);
- window.setTitle("Light System");
- cast(scene.getEntityById("light_system"), LightSystem).debugWindow(window);
- }
- }
- if(controlled){
- if(!shifted){
- if(Keyboard.checkPressed(KeyCode.S)){
- if(current_file != ""){
- save_state(current_file);
- Console.print('${current_file} saved.');
- }
- }
- }else{
- if(Keyboard.checkPressed(KeyCode.S)){
- buttons[0].activate();
- }
- }
- }
- if(Keyboard.checkPressed(KeyCode.Delete)){
- if(selected_entity != null){
- if(!Std.is(selected_entity, Player)
- && !Std.is(selected_entity, Rammon)){
- var temp_entity = selected_entity;
- var box: CollisionBox = cast(selected_entity.getComponent(Editable), Editable).edit_box;
- box.isDebug = false;
- edit_entities.remove(selected_entity);
- deleted_entities.push(temp_entity);
- actions.push('delete ${deleted_entities.length-1}');
- scene.remove(temp_entity.id);
- cast(scene.getSystem("collision_system"), CollisionSystem).removeComponent(box);
- //temp_entity.destroy();
- selected_entity = null;
- }
- }
- }
- if(selected_entity != null)
- inline arrows_movement();
- if(controlled){
- if(Keyboard.checkPressed(KeyCode.Z)){
- var last_action: String = actions.pop();
- if(last_action != null){
- var tokens = last_action.split(" ");
- switch(tokens[0]){
- case "delete":
- setAllCollisionVisible(true);
- //what about entity specific components that included in some systems?
- //TODO: Block and collision system boxes
- //FIXME: error btw
- scene.add(deleted_entities[Std.parseInt(tokens[1])]);
- cast(scene.getSystem("collision_system"), CollisionSystem).addComponent((cast(deleted_entities[Std.parseInt(tokens[1])].getComponent(Editable), Editable)).edit_box);
- deleted_entities.pop();
- case "create":
- setAllCollisionVisible(true);
- var entity: Entity = scene.getEntityById(tokens[1]);
- edit_entities.remove(entity);
- entity.destroy();
- entity = null;
- case "locked":
- var entity: Entity = scene.getEntityById(tokens[1]);
- cast(entity.getComponent(Editable), Editable).locked = tokens[2] == "1" ? false : true;
- case "change":
- var entity: Entity = scene.getEntityById(tokens[1]);
- var var_name: String = tokens[2];
- var var_type: String = tokens[3];
- var var_from: String = tokens[4];
- var var_to: String = tokens[5];
- switch(var_type){
- case "f":
- Reflect.setField(entity, var_name, Std.parseFloat(var_from));
- if(cast(entity.getComponent(Editable),Editable).onChangeValue != null) cast(entity.getComponent(Editable),Editable).onChangeValue(var_name, "f", var_from);
- cast(entity.getComponent(Editable), Editable).changeVisualField(var_name, var_from);
- case "i":
- Reflect.setField(entity, var_name, Std.parseInt(var_from));
- if(cast(entity.getComponent(Editable),Editable).onChangeValue != null) cast(entity.getComponent(Editable),Editable).onChangeValue(var_name, "i", var_from);
- cast(entity.getComponent(Editable), Editable).changeVisualField(var_name, var_from);
- case "s":
- Reflect.setField(entity, var_name, var_from);
- if(cast(entity.getComponent(Editable),Editable).onChangeValue != null) cast(entity.getComponent(Editable),Editable).onChangeValue(var_name, "s", var_from);
- cast(entity.getComponent(Editable), Editable).changeVisualField(var_name, var_from);
- case "l":
- Reflect.setField(entity, var_name, var_from);
- if(cast(entity.getComponent(Editable),Editable).onChangeValue != null) cast(entity.getComponent(Editable),Editable).onChangeValue(var_name, "l", var_from);
- cast(entity.getComponent(Editable), Editable).changeVisualField(var_name, var_from);
- default:
- Console.print("Wrong type when Ctrl+Z");
- }
- case "move":
- var entity: Entity = scene.getEntityById(tokens[1]);
- var from_x: Float = Std.parseFloat(tokens[2]);
- var from_y: Float = Std.parseFloat(tokens[3]);
- var to_x: Float = Std.parseFloat(tokens[4]);
- var to_y: Float = Std.parseFloat(tokens[5]);
- entity.x = from_x;
- entity.y = from_y;
- }
- }
- }
- }
- if(Keyboard.checkPressed(KeyCode.C) && !controlled){
- var window = new UI_Window(Mouse.me.realx, Mouse.me.realy);
- window.manager = MainScene.testUI;
- MainScene.testUI.windows.push(window);
- window.setTitle("Group Manager");
- for(i in groups){
- window.createButton(i, () -> {
- for(j in edit_entities){
- var ie: Editable = cast(j.getComponent(Editable));
- // trace('${i} vs ${ie.group}: ${j.id}');
- if(ie.group == i){
- ie.visible = !ie.visible;
- }
- }
- });
- }
- }
- //
- // Window settings.
- //
- if(Keyboard.checkPressed(KeyCode.W)){
- var window = new UI_Window(1280/2, 800/2);
- window.manager = MainScene.testUI;
- MainScene.testUI.windows.push(window);
- window.setTitle("Window Settings");
- window.createInput(Std.string(cuttlefish.Engine.core.window_width), "width", function(a: String){
- });
- window.createInput(Std.string(cuttlefish.Engine.core.window_height), "height", function(a: String){
- });
- window.createInput(Std.string(Engine.core.bloom_tresh), "tresh1", function(a: String){
- Engine.core.bloom_tresh = Std.parseFloat(a);
- });
- window.createInput(Std.string(Engine.core.bloom_tresh2), "tresh2", function(a: String){
- Engine.core.bloom_tresh2 = Std.parseFloat(a);
- });
- window.createInput(Std.string(Engine.core.bloom_vign_out), "vign_outter", function(a: String){
- Engine.core.bloom_vign_out = Std.parseFloat(a);
- });
- window.createInput(Std.string(Engine.core.bloom_vign_inn), "vign_inner", function(a: String){
- Engine.core.bloom_vign_inn = Std.parseFloat(a);
- });
- window.createInput(Std.string(Engine.core.bloom_r), "r", function(a: String){
- Engine.core.bloom_r = Std.parseFloat(a);
- });
- window.createInput(Std.string(Engine.core.bloom_g), "g", function(a: String){
- Engine.core.bloom_g = Std.parseFloat(a);
- });
- window.createInput(Std.string(Engine.core.bloom_b), "b", function(a: String){
- Engine.core.bloom_b = Std.parseFloat(a);
- });
- window.createButton("Resize", function(){
- var iw: cuttlefish.ui.UI_Input = cast window.components[0];
- var ih: cuttlefish.ui.UI_Input = cast window.components[1];
- cuttlefish.Engine.core.resize(
- Std.parseInt(iw.str),
- Std.parseInt(ih.str)
- );
- });
- window.createButton("Debug Info", function(){
- this.debug_info = !this.debug_info;
- });
- window.createButton("Draw Mouse Pos", function(){
- this.draw_mouse_pos = !this.draw_mouse_pos;
- });
- window.createButton("Bloom", function(){
- Engine.core.is_bloom = !Engine.core.is_bloom;
- });
- }
- if(Keyboard.checkPressed(KeyCode.A)){
- var window = new UI_Window(1280/2, 800/2);
- window.manager = MainScene.testUI;
- MainScene.testUI.windows.push(window);
- window.setTitle("Add Object");
- window.focus = true;
- MainScene.testUI.windowFocus(window);
- window.createSelectField(objects_to_create, objects_sprite, "ui", function(item: UI_SelectItem) {
- for(i in 0...objects_to_create.length){
- if(item.name == objects_to_create[i]){
- holding_object = i;
- skipClick = true;
- }
- }
- // item.createItem();
- window.delayDestroy();
- });
- window.center();
- }
- final zoomSpeedX: Int = 64;
- final zoomSpeedY: Int = 40;
- if(selected_entity == null){
- var w = Engine.core.window_width;
- var h = Engine.core.window_height;
- if(Mouse.checkWheelUp()){
- // camera.setSize(Std.int(camera.width + zoomSpeedX), Std.int(camera.height + zoomSpeedY));
- // camera.setPosition(camera.x - zoomSpeedX/2, camera.y - zoomSpeedY/2);
- current_zoom -= 5;
- camera.setSize(Std.int(w*(current_zoom/100)), Std.int(h*(current_zoom/100)));
- }
- if(Mouse.checkWheelDown()){
- // camera.setSize(Std.int(camera.width - zoomSpeedX), Std.int(camera.height - zoomSpeedY));
- // camera.setPosition(camera.x + zoomSpeedX/2, camera.y + zoomSpeedY/2);
- current_zoom += 5;
- camera.setSize(Std.int(w*(current_zoom/100)), Std.int(h*(current_zoom/100)));
- }
- }
- final cz = 200 / current_zoom;
- if(Mouse.checkPressed(Mouse.RIGHT)) {
- start_x = Mouse.me.realx * (cz);
- start_y = Mouse.me.realy * (cz);
- camera_start_x = camera.x + camera.offset_x;
- camera_start_y = camera.y + camera.offset_y;
- camera_dragged = true;
- }
- if(camera_dragged) {
- camera.setPosition(camera_start_x + (start_x - Mouse.me.realx* (cz)), camera_start_y + (start_y - Mouse.me.realy* (cz)));
- }
- if(Mouse.checkReleased(Mouse.RIGHT)) {
- camera_dragged = false;
- }
- if(Keyboard.checkPressed(E) && selected_entity != null){
- cast(selected_entity.getComponent(Editable), Editable).createWindow(Mouse.me.x, Mouse.me.y);
- }
- if(Mouse.checkPressed(Mouse.LEFT) && !skipClick) {
- if(selected_entity == null){
- var syst: CollisionSystem = cast(scene.getSystem("collision_system"));
- var entities = syst.instancesPosition(Mouse.me.x, Mouse.me.y);
- if(entities.length > 0){
- var entity: Entity = null;
- if(entities.length == 1){
- if(cast(entities[0].getComponent(Editable), Editable).visible) entity = entities[0];
- if(entity != null){
- var _editable: Editable = cast entity.getComponent(Editable);
- var box: CollisionBox = _editable.edit_box;
- if(box != null){
- if(syst.hasBox(box)){
- if(!_editable.locked || Keyboard.check(Control)){
- selected_entity = entity;
- box.isDebug = true;
- // cast(entity.getComponent(Editable), Editable).createWindow(Mouse.me.x, Mouse.me.y);
- if(!(_editable.locked)){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }
- }
- }
- }
- }else{
- var second_pass_entities: Array<Entity> = [];
- if(Keyboard.check(Control)){
- for(i in entities){
- if(cast(i.getComponent(Editable), Editable).visible){
- var entity = i;
- var _editable: Editable = cast entity.getComponent(Editable);
- var box: CollisionBox = _editable.edit_box;
- //When you are not holding right click we just ignore everything that is locked
- second_pass_entities.push(i);
- // if(entity == null){
- // entity = i;
- // }else{
- // if(i.depth < entity.depth){
- // entity = i;
- // }
- // }
- }
- }
- haxe.ds.ArraySort.sort(second_pass_entities,function(a,b){
- if(a.getDepth() < b.getDepth()) return 1;
- else if(a.getDepth() > b.getDepth()) return -1;
- else return 0;
- });
- var entity = second_pass_entities[second_pass_entities.length-1];
- if(entity != null){
- var _editable: Editable = cast entity.getComponent(Editable);
- var box: CollisionBox = _editable.edit_box;
- if(box != null){
- if(syst.hasBox(box)){
- if(!_editable.locked || Keyboard.check(Control)){
- selected_entity = entity;
- box.isDebug = true;
- // cast(entity.getComponent(Editable), Editable).createWindow(Mouse.me.x, Mouse.me.y);
- if(!(_editable.locked)){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }
- }
- }
- }
- }else{
- for(i in entities){
- if(cast(i.getComponent(Editable), Editable).visible){
- var entity = i;
- var _editable: Editable = cast entity.getComponent(Editable);
- var box: CollisionBox = _editable.edit_box;
- //When you are not holding right click we just ignore everything that is locked
- if(!_editable.locked){
- second_pass_entities.push(i);
- }
- // if(entity == null){
- // entity = i;
- // }else{
- // if(i.depth < entity.depth){
- // entity = i;
- // }
- // }
- }
- }
- haxe.ds.ArraySort.sort(second_pass_entities,function(a,b){
- if(a.getDepth() < b.getDepth()) return 1;
- else if(a.getDepth() > b.getDepth()) return -1;
- else return 0;
- });
- var entity = second_pass_entities[second_pass_entities.length-1];
- if(entity != null){
- var _editable: Editable = cast entity.getComponent(Editable);
- var box: CollisionBox = _editable.edit_box;
- if(box != null){
- if(syst.hasBox(box)){
- if(!_editable.locked || Keyboard.check(Control)){
- selected_entity = entity;
- box.isDebug = true;
- // cast(entity.getComponent(Editable), Editable).createWindow(Mouse.me.x, Mouse.me.y);
- if(!(_editable.locked)){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }
- }
- }
- }
- }
- }
- }
- }else{
- if(cast(selected_entity.getComponent(Editable), Editable).inEditing) return;
- var syst: CollisionSystem = cast(scene.getSystem("collision_system"));
- var entities = syst.instancesPosition(Mouse.me.x, Mouse.me.y);
- var entity: Entity = null;
- if(entities.length > 0){
- if(entities.length == 1){
- if(cast(entities[0].getComponent(Editable), Editable).visible) entity = entities[0];
- if(entity == selected_entity) {
- if(!(cast(entity.getComponent(Editable), Editable).locked)){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }else {
- var _editable: Editable = cast entity.getComponent(Editable);
- var box: CollisionBox = _editable.edit_box;
- box = cast(selected_entity.getComponent(Editable), Editable).edit_box;
- box.isDebug = false;
- selected_entity = null;
- if(!_editable.locked || Keyboard.check(Control)){
- box = _editable.edit_box;
- selected_entity = entity;
- box.isDebug = true;
- if(!cast(selected_entity.getComponent(Editable), Editable).locked){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }
- }
- }else{
- var second_pass_entities: Array<Entity> = [];
- if(Keyboard.check(Control)){
- if(entities.indexOf(selected_entity) != -1){
- if(!(cast(selected_entity.getComponent(Editable), Editable).locked)){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }else{
- var box: CollisionBox = cast(selected_entity.getComponent(Editable), Editable).edit_box;
- box.isDebug = false;
- selected_entity = null;
- for(i in entities){
- if(cast(i.getComponent(Editable), Editable).visible){
- //When you are not holding right click we just ignore everything that is locked
- second_pass_entities.push(i);
- // if(entity == null){
- // entity = i;
- // }else{
- // if(i.depth < entity.depth){
- // entity = i;
- // }
- // }
- }
- }
- if(second_pass_entities.length > 0){
- haxe.ds.ArraySort.sort(second_pass_entities,function(a,b){
- if(a.getDepth() < b.getDepth()) return 1;
- else if(a.getDepth() > b.getDepth()) return -1;
- else return 0;
- });
- var entity = second_pass_entities[second_pass_entities.length-1];
- var _editable: Editable = cast entity.getComponent(Editable);
- box = _editable.edit_box;
- selected_entity = entity;
- box.isDebug = true;
- }
- }
- }else{
- if(entities.indexOf(selected_entity) != -1){
- if(!(cast(selected_entity.getComponent(Editable), Editable).locked)){
- entity_dragging = true;
- entity_dragging_xo = Mouse.me.x - selected_entity.x;
- entity_dragging_yo = Mouse.me.y - selected_entity.y;
- pickedFromX = selected_entity.x;
- pickedFromY = selected_entity.y;
- }
- }else{
- var box: CollisionBox = cast(selected_entity.getComponent(Editable), Editable).edit_box;
- box.isDebug = false;
- selected_entity = null;
- for(i in entities){
- if(cast(i.getComponent(Editable), Editable).visible){
- var entity = i;
- var _editable: Editable = cast entity.getComponent(Editable);
- box = _editable.edit_box;
- if(!_editable.locked){
- second_pass_entities.push(i);
- }
- }
- }
- if(second_pass_entities.length > 0){
- haxe.ds.ArraySort.sort(second_pass_entities,function(a,b){
- if(a.getDepth() < b.getDepth()) return 1;
- else if(a.getDepth() > b.getDepth()) return -1;
- else return 0;
- });
- var entity = second_pass_entities[second_pass_entities.length-1];
- var _editable: Editable = cast entity.getComponent(Editable);
- box = _editable.edit_box;
- selected_entity = entity;
- box.isDebug = true;
- }
- }
- }
- // for(i in entities){
- // if(cast(i.getComponent(Editable), Editable).visible){
- // if(entity == null){
- // entity = i;
- // }else{
- // if(i.depth < entity.depth){
- // entity = i;
- // }
- // }
- // }
- // }
- }
- }else{
- var box: CollisionBox = cast(selected_entity.getComponent(Editable), Editable).edit_box;
- box.isDebug = false;
- selected_entity = null;
- }
- }
- }else{
- if(skipClick){
- skipClick = false;
- }
- }
- if(entity_dragging){
- if(!shifted){
- selected_entity.x = Math.floor(Mouse.me.x - entity_dragging_xo);
- selected_entity.y = Math.floor(Mouse.me.y - entity_dragging_yo);
- }else{
- selected_entity.x = Math.floor((Mouse.me.x - entity_dragging_xo)/snap_x)*snap_x;
- selected_entity.y = Math.floor((Mouse.me.y - entity_dragging_yo)/snap_y)*snap_y;
- }
- }
- if(Mouse.checkReleased(Mouse.LEFT)){
- if(entity_dragging){
- entity_dragging = false;
- if(pickedFromX != selected_entity.x && pickedFromY != selected_entity.y){
- actions.push('move ${selected_entity.id} ${pickedFromX} ${pickedFromY} ${selected_entity.x} ${selected_entity.y}');
- }
- }
- }
- }
- public var draw_mouse_pos: Bool = false;
- public override function gui(): Void {
- //things
- if(debug_info){
- Graphics.drawString('fps: ${Engine.getFPS()}', 0, 0);
- Graphics.drawString('upd: ${CMath.precFloat(Engine.getUpdateTime()*60)}', 0, 32);
- Graphics.drawString('drw: ${CMath.precFloat(Engine.getRenderTime()*60)}', 0, 64);
- }
- if(draw_mouse_pos) {
- Graphics.drawString('${Mouse.me.x}, ${Mouse.me.y}', Mouse.me.realx, Mouse.me.realy);
- }
- if(!enabled) return;
- super.gui();
- // Graphics.drawString('All entities to edit: ${edit_entities.length}', Camera.getX() + Camera.getWidth() - Graphics.stringWidth('All entities to edit: ${edit_entities.length}')*Graphics.fontScale, Camera.getY());
- Graphics.drawString('${current_zoom}%', Camera.getX() + Camera.getWidth() - Graphics.stringWidth('${current_zoom}%')*Graphics.fontScale, Camera.getY());
- if(selected_entity != null) {
- // Graphics.drawRect(false, selected_entity.x, selected_entity.y, 32, 32);
- // Graphics.drawString("hey", 0, 0);
- var _editable: Editable = cast(selected_entity.getComponent(Editable));
- _editable.draw_meta(Camera.getX(), Camera.getBottom() - Camera.getHeight()/2);
- }
- for(i in 0...buttons.length){
- buttons[i].draw(icons[i]);
- }
- for(i in inputs){
- i.draw();
- }
- if(show_objects){
- Graphics.setColor(Graphics.Black);
- Graphics.drawRect(true,Camera.getX() + 64*3, Camera.getBottom() - 64, Camera.getWidth() - 64*3 -2, 64 -2);
- Graphics.setColor(Graphics.White);
- var mx: Float = Mouse.me.realx;
- var my: Float = Mouse.me.realy;
- var object_id: Int = Math.floor((mx - (64*3)) / (64));
- if(my < Camera.getBottom() - 64){
- object_id = -1;
- }
- for(i in 0...objects_to_create.length){
- if(object_id == i){
- Graphics.drawAtlasImage(AtlasManager.getAtlas("ui"),objects_sprite[i], Camera.getX() + 64*3 + 64*i + 16, Camera.getBottom() - 64 - 16 + 16, 0.5, 0.5);
- Graphics.drawString(objects_to_create[i],
- Camera.getX() + 64*3 + 64*i + 32 - Graphics.stringWidth(objects_to_create[i])/2,
- Camera.getBottom() - 64 - Graphics.stringHeight(objects_to_create[i]) - 16);
- }else{
- Graphics.drawAtlasImage(AtlasManager.getAtlas("ui"),objects_sprite[i], Camera.getX() + 64*3 + 64*i + 16, Camera.getBottom() - 64 + 16, 0.5, 0.5);
- }
- // final lastScale = Graphics.fontScale;
- }
- }
- if(holding_object != -1){
- Graphics.setColor(Graphics.Gray);
- Graphics.drawAtlasImage(AtlasManager.getAtlas("ui"),objects_sprite[holding_object], Mouse.me.realx, Mouse.me.realy);
- Graphics.setColor(Graphics.White);
- }
- Graphics.drawString(current_file, Camera.getCenterX() - Graphics.stringWidth(current_file)*Graphics.fontScale/2, Camera.getY());
- if(draw_grid){
- var sx = Math.round(Camera.getLeft() / snap_size) - 1;
- var ex = Math.round((Camera.getLeft() + Camera.getWidth()) / snap_size) + 1;
- var sy = Math.round(Camera.getTop()/snap_size) -1 ;
- var ey = Math.round((Camera.getTop()+Camera.getHeight()) / snap_size) + 1;
- for(i in sx...ex){
- for(j in sy...ey){
- Graphics.drawLine(i*snap_size, sy*snap_size, i*snap_size, ey*snap_size, 2);
- Graphics.drawLine(sx*snap_size, j*snap_size, ex*snap_size, j*snap_size, 2);
- }
- }
- }
- // for(i in 0...actions.length){
- // Graphics.drawString(actions[i], 0, i*32);
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement