From 0168e81ffe285b3f1dc8da052728ac8b3f87eb17 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 29 Sep 2023 13:27:34 +0000 Subject: [PATCH] unify grabbing controls --- scripts/editor.js | 70 ++++++++++++++++++++------------------------ scripts/entity.js | 37 +++++++++++------------ scripts/gui.js | 3 +- scripts/textedit.js | 41 +++++++++++++------------- source/engine/font.c | 48 +++++++++--------------------- 5 files changed, 87 insertions(+), 112 deletions(-) diff --git a/scripts/editor.js b/scripts/editor.js index 834a248..923304b 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -197,9 +197,9 @@ var editor = { stash: "", start_play_ed() { - this.stash = this.edit_level.save(); - this.edit_level.kill(); - load_configs("game.config"); +// this.stash = this.edit_level.save(); +// this.edit_level.kill(); +// load_configs("game.config"); Game.play(); Player.players[0].uncontrol(this); Player.players[0].control(limited_editor); @@ -719,7 +719,7 @@ editor.inputs['C-p'] = function() { if (!Game.playing()) { editor.start_play_ed(); // if (!Level.loadlevel("debug")) - World.loadlevel("game"); + Primum.spawn(ur.start); } else { Game.pause(); } @@ -856,9 +856,7 @@ editor.inputs['C-y'] = function() { texteditor.on_close = function() { editor.edit_level.script = texteditor.value;}; editor.openpanel(texteditor); - if (!editor.edit_level.script) - editor.edit_level.script = ""; - texteditor.value = editor.edit_level.script; + texteditor.value = ""; texteditor.start(); }; editor.inputs['C-y'].doc = "Open script editor for the level."; @@ -983,6 +981,29 @@ editor.inputs.rm = function() { editor.unselect(); }; +editor.try_pick = function() +{ + editor.grabselect = []; + + if (editor.sel_comp && 'pick' in editor.sel_comp) { + var o = editor.sel_comp.pick(Mouse.worldpos); + if (o) + editor.grabselect = [o]; + return; + } + + if (editor.selectlist.length > 0) { + editor.grabselect = editor.selectlist.slice(); + return; + } + + var grabobj = editor.try_select(); + + if (!grabobj) return; + editor.grabselect = [grabobj]; + editor.selectlist = [grabobj]; +} + editor.inputs.mm = function() { if (editor.brush_obj) { editor.selectlist = editor.dup_objects([editor.brush_obj]); @@ -990,24 +1011,8 @@ editor.inputs.mm = function() { editor.grabselect = editor.selectlist[0]; return; } - - if (editor.sel_comp && 'pick' in editor.sel_comp) { - var o = editor.sel_comp.pick(Mouse.worldpos); - if (o) - editor.grabselect = [o]; - return; - } - var grabobj = editor.try_select(); - editor.grabselect = []; - if (!grabobj) return; - - if (Keys.ctrl()) { - grabobj = editor.dup_objects([grabobj])[0]; - } - - editor.grabselect = [grabobj]; - editor.selectlist = [grabobj]; + editor.try_pick(); }; editor.inputs['C-mm'] = editor.inputs.mm; @@ -1075,14 +1080,12 @@ editor.inputs.mouse.scroll = function(scroll) editor.grabselect?.forEach(function(x) { x.pos = x.pos.add(scroll.scale(editor.camera.zoom)); }); -} -editor.inputs.mouse['C-scroll'] = function(scroll) -{ + scroll.y *= -1; editor.camera.pos = editor.camera.pos.sub(scroll.scale(editor.camera.zoom * 3).scale([1,-1])); } -editor.inputs.mouse['C-M-scroll'] = function(scroll) +editor.inputs.mouse['C-scroll'] = function(scroll) { editor.camera.zoom += scroll.y/100; } @@ -1116,16 +1119,7 @@ editor.inputs['C-S-g'] = function() { editor.openpanel(groupsaveaspanel); }; editor.inputs['C-S-g'].doc = "Save selected objects as a new level."; editor.inputs.g = function() { - editor.grabselect = []; - - if (this.sel_comp) { - if ('pos' in this.sel_comp) - editor.grabselect = [this.sel_comp]; - } else - editor.grabselect = editor.selectlist.slice(); - - if (!editor.grabselect.empty) - Mouse.disabled(); + editor.try_pick(); }; editor.inputs.g.doc = "Move selected objects."; editor.inputs.g.released = function() { editor.grabselect = []; Mouse.normal(); }; diff --git a/scripts/entity.js b/scripts/entity.js index 6ad942f..be0748d 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -466,28 +466,14 @@ prototypes.from_file = function(file) var newur = Object.create(upperur); file = file.replaceAll('.','/'); - var jsfile = file + ".js"; - var jsonfile = file + ".json"; + var jsfile = prototypes.get_ur_file(urpath, ".js"); + var jsonfile = prototypes.get_ur_file(urpath, ".json"); var script = undefined; var json = undefined; - if (IO.exists(jsfile)) - script = IO.slurp(jsfile); - else { - jsfile = urpath + "/" + path.at(-1) + ".js"; - if (IO.exists(jsfile)) script = IO.slurp(jsfile); - } - - try { - try {json = JSON.parse(IO.slurp(jsonfile)); } - catch(e) { - jsonfile = file + "/" + path.at(-1) + ".json"; - if (IO.exists(jsonfile)) json = JSON.parse(IO.slurp(jsonfile)); - } - } catch(e) { - Log.warn(`JSON in file ${jsonfile} is malformed.${e}`); - } + if (jsfile) script = IO.slurp(jsfile); + if (jsonfile) json = JSON.parse(IO.slurp(jsonfile)); if (!json && !script) { Log.warn(`Could not make ur from ${file}`); @@ -535,6 +521,11 @@ prototypes.list_ur = function() return list_obj(ur); } +prototypes.ur2file = function(urpath) +{ + return urpath.replaceAll('.', '/'); +} + prototypes.file2ur = function(file) { file = file.strip_ext(); @@ -564,6 +555,16 @@ prototypes.get_ur = function(name) return prototypes.ur[urpath]; } +prototypes.get_ur_file = function(path, ext) +{ + var urpath = prototypes.ur2file(path); + var file = urpath + ext; + if (IO.exists(file)) return file; + file = urpath + "/" + path.split('.').at(-1) + ext; + if (IO.exists(file)) return file; + return undefined; +} + prototypes.generate_ur = function(path) { var ob = IO.glob("**.js"); diff --git a/scripts/gui.js b/scripts/gui.js index 39184cd..c9c2453 100644 --- a/scripts/gui.js +++ b/scripts/gui.js @@ -135,8 +135,7 @@ var GUI = { }; def.items.forEach(function(item,idx) { - Object.setPrototypeOf(def.items[idx], def); - + def.items[idx].__proto__ = def; if (def.items[idx-1]) def.up = def.items[idx-1]; diff --git a/scripts/textedit.js b/scripts/textedit.js index 5b785d6..1369c2d 100644 --- a/scripts/textedit.js +++ b/scripts/textedit.js @@ -78,26 +78,6 @@ var texteditor = Object.copy(inputpanel, { this.cursor++; }, - input_enter_pressrep() { - var white = this.line_starting_whitespace(this.cursor); - this.insert_char('\n'); - - for (var i = 0; i < white; i++) - this.insert_char(" "); - - }, - - input_text(char) { - if (Keys.ctrl() || Keys.alt()) return; - this.insert_char(char); - this.keycb(); - }, - - input_backspace_pressrep() { - this.value = this.value.slice(0,this.cursor-1) + this.value.slice(this.cursor); - this.cursor--; - }, - line_starting_whitespace(p) { var white = 0; var l = this.line_start(p); @@ -189,6 +169,27 @@ var texteditor = Object.copy(inputpanel, { }); texteditor.inputs = {}; +texteditor.inputs.char = function(char) { + this.insert_char(char); + this.keycb(); +}, + +texteditor.inputs.enter = function(){ + var white = this.line_starting_whitespace(this.cursor); + this.insert_char('\n'); + + for (var i = 0; i < white; i++) + this.insert_char(" "); +}; +texteditor.inputs.enter.rep = true; + +texteditor.inputs.backspace = function(){ + this.value = this.value.slice(0,this.cursor-1) + this.value.slice(this.cursor); + this.cursor--; +}; +texteditor.inputs.backspace.rep = true; + + texteditor.inputs['C-s'] = function() { editor.edit_level.script = texteditor.value; editor.save_current(); diff --git a/source/engine/font.c b/source/engine/font.c index 6d4297c..e0c6eee 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -12,7 +12,7 @@ #include #include "2dphysics.h" #include "resources.h" - +#include "debugdraw.h" #include "text.sglsl.h" #include "stb_image_write.h" @@ -178,16 +178,20 @@ struct sFont *MakeFont(const char *fontfile, int height) { static int curchar = 0; -void draw_char_box(struct Character c, cpVect cursor, float scale, struct rgba color) +void draw_char_box(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color) { cpVect wh; wh.x = 8 * scale; wh.y = 14; - cursor.x += wh.x / 2.f; - cursor.y += wh.y / 2.f; + cursor.X += wh.x / 2.f; + cursor.Y += wh.y / 2.f; -// draw_box(cursor, wh, color); + cpVect b; + b.x = cursor.X; + b.y = cursor.Y; + + draw_box(b, wh, color); } void text_flush(HMM_Mat4 *proj) { @@ -229,31 +233,6 @@ void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgb memcpy(text_buffer + curchar, &vert, sizeof(struct text_vert)); curchar++; - return; - - /* - if (drawcaret == curchar) { - draw_char_box(c, cursor, scale, color); - shader_use(shader); - } - */ - /* - sg_append_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts)); - - offset[0] = 1; - offset[1] = -1; - fill_charverts(verts, cursor, scale, c, offset); - sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts)); - - offset[1] = 1; - fill_charverts(verts, cursor, scale, c, offset); - sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts)); - - offset[0] = -1; - offset[1] = -1; - fill_charverts(verts, cursor, scale, c, offset); - sg_update_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts)); - */ } void text_settype(struct sFont *mfont) { @@ -321,6 +300,9 @@ int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, f struct rgba usecolor = color; while (*line != '\0') { + if (caret == curchar) + draw_char_box(font->Characters[69], cursor, scale, color); + if (isblank(*line)) { sdrawCharacter(font->Characters[*line], cursor, scale, usecolor); cursor.X += font->Characters[*line].Advance * tracking * scale; @@ -351,10 +333,8 @@ int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, f } } } - /* if (caret > curchar) { - draw_char_box(font->Characters[69], cursor, scale, color); - } - */ +// if (caret > curchar) +// draw_char_box(font->Characters[69], cursor, scale, color); return cursor.Y - pos.Y; }