add object merging

This commit is contained in:
John Alanbrook 2023-09-22 00:51:38 +00:00
parent 14d743e704
commit a8eb444991
6 changed files with 101 additions and 43 deletions

View file

@ -72,8 +72,63 @@ Object.dainty_assign = function(target, source)
}
}
/* This name is more consistent with Ruby, etc */
Object.merge = Object.dainty_assign;
Object.isAccessor = function(obj, prop)
{
var prop = Object.getOwnPropertyDescriptor(obj,prop);
if (prop) return false;
return true;
}
/* Same as merge from Ruby */
Object.merge = function(target, ...objs)
{
var objmerge = function(tar, obj)
{
for (var key of Object.keys(obj)) {
if (typeof obj[key] === 'object') {
if (tar[key]) {
objmerge(tar[key], obj[key]);
continue;
}
else {
tar[key] = obj[key];
continue;
}
}
tar[key] = obj[key];
}
}
for (var obj of objs)
objmerge(target,obj);
}
Object.totalmerge = function(target, ...objs)
{
for (var obj of objs) {
for (var key in obj) {
if (typeof obj[key] === 'object') {
if (typeof target[key] === 'object') {
if (Object.isAccessor(target,key))
target[key] = obj[key];
else
Object.merge(target[key], obj[key]);
}
else
target[key] = obj[key];
} else
target[key] = obj[key];
}
}
}
/* Returns a new object with undefined, null, and empty values removed. */
Object.compact = function(obj)
{
}
Object.totalassign = function(to, from)
{

View file

@ -522,7 +522,16 @@ var editor = {
this.selectlist.forEach(function(x) {
var color = x.color ? x.color : Color.white;
GUI.text(x.ur.toString(), world2screen(x.pos).add([0, 16]), 1, Color.purple);
var ojson = JSON.parse(JSON.stringify(x));
delete ojson.pos;
delete ojson.angle;
var sname = x.ur.toString();
if (!ojson.empty)
x.dirty = true;
else
x.dirty = false;
if (x.dirty) sname += "*";
GUI.text(sname, world2screen(x.pos).add([0, 16]), 1, Color.purple);
for (var key in x.$) {
var o = x.$[key];
GUI.text(o.ur.toString(), world2screen(o.pos).add([0,16]),1,Color.purple);
@ -704,8 +713,8 @@ editor.inputs['C-a'] = function() {
};
editor.inputs['C-a'].doc = "Select all objects.";
editor.inputs['`'] = function() { editor.openpanel(replpanel); }
editor.inputs['`'].doc = "Open or close the repl.";
editor.inputs['C-`'] = function() { editor.openpanel(replpanel); }
editor.inputs['C-`'].doc = "Open or close the repl.";
/* Return if selected component. */
editor.inputs['h'] = function() {
@ -1494,8 +1503,8 @@ var replpanel = Object.copy(inputpanel, {
ecode += this.value;
this.value = "";
Log.say(eval(ecode));
var ret = eval(ecode);
if (ret) Log.say(ret);
},
});
@ -1727,7 +1736,7 @@ var savetypeas = Object.copy(inputpanel, {
var quitpanel = Object.copy(inputpanel, {
title: "really quit?",
action() {
quit();
Game.quit();
},
guibody () {

View file

@ -319,42 +319,32 @@ var gameobject = {
cmd(113, obj.body, obj); // set the internal obj reference to this obj
Object.totalassign(obj, ur);
obj.$ = {};
obj.ur = ur;
/* for (var prop in obj) {
if (typeof obj[prop] === 'object' && 'comp' in obj[prop]) {
var newcomp = component[obj[prop].comp].make(obj.body);
Object.assign(newcomp, obj[prop]);
newcomp.sync?.();
obj[prop] = newcomp;
obj[prop].defn('gameobject', obj);
obj.components[prop] = obj[prop];
}
};
*/
level.add_child(obj);
for (var prop in obj) {
var p = obj[prop];
for (var prop in ur) {
var p = ur[prop];
if (typeof p !== 'object') continue;
if ('ur' in p) {
Log.warn(`spawning a ${prop} on this obj`);
var newobj = obj.spawn(prototypes.get_ur(p.ur));
Object.assign(newobj, p);
obj.$[prop] = newobj;
obj[prop] = obj.spawn(prototypes.get_ur(p.ur));
obj.$[prop] = obj[prop];
// Object.assign(obj[prop], p);
} else if ('make' in p) {
obj[prop] = obj[prop].make(obj.body);
obj[prop] = p.make(obj.body);
obj.components[prop] = obj[prop];
} else if ('comp' in p) {
obj[prop] = component[p.comp].make(obj.body);
obj.components[prop] = obj[prop];
}
};
Object.totalmerge(obj,ur);
obj.check_registers(obj);
if (typeof obj.start === 'function') obj.start();
return obj;
},
@ -469,8 +459,8 @@ prototypes.from_file = function(file)
delete json.$;
}
compile_env(script, newur, file);
Object.dainty_assign(newur, json);
compile_env(script, newur, file);
Object.merge(newur,json);
file = file.replaceAll('/', '.');
var path = file.name().split('.');
@ -486,10 +476,11 @@ prototypes.from_file = function(file)
prototypes.list.push(tag);
newur.toString = function() { return tag; };
Object.assign(nested_access(ur,path), newur);
ur[path] = nested_access(ur,path);
Object.assign(ur[path], newur);
nested_access(ur,path).__proto__ = newur.__proto__;
return nested_access(ur,path);
return ur[path];
}
prototypes.from_file.doc = "Create a new ur-type from a given script file.";
prototypes.list = [];

View file

@ -1314,11 +1314,9 @@ JSValue duk_set_body(JSContext *js, JSValueConst this, int argc, JSValueConst *a
break;
case 7:
go->mass = js2number(argv[2]);
if (go->bodytype == CP_BODY_TYPE_DYNAMIC)
cpBodySetMass(go->body, js2number(argv[2]));
else
YughWarn("Cannot set mass of a non dynamic body.");
cpBodySetMass(go->body, go->mass);
break;
case 8:

View file

@ -8,6 +8,7 @@
#include "time.h"
#include <stdio.h>
#include <ctype.h>
#include <wchar.h>
#include "resources.h"
#include "stb_ds.h"
@ -190,13 +191,17 @@ void input_btn(int btn, int state, uint32_t mod)
}
}
void input_key(int key, uint32_t mod)
static const uint32_t UNCHAR_FLAGS = SAPP_MODIFIER_CTRL | SAPP_MODIFIER_ALT | SAPP_MODIFIER_SUPER;
void input_key(uint32_t key, uint32_t mod)
{
if (mod & UNCHAR_FLAGS) return;
if (key <= 31 || key >= 127) return;
JSValue argv[2];
char out[2] = {0};
out[0] = (char)key;
char s[2] = {key, '\0'};
argv[0] = JS_NewString(js, "char");
argv[1] = JS_NewString(js, out);
argv[1] = JS_NewString(js, s);
script_callee(pawn_callee, 2, argv);
JS_FreeValue(js, argv[0]);

View file

@ -29,7 +29,7 @@ void input_mouse(int btn, int state, uint32_t mod);
void input_mouse_move(float x, float y, float dx, float dy, uint32_t mod);
void input_mouse_scroll(float x, float y, uint32_t mod);
void input_btn(int btn, int state, uint32_t mod);
void input_key(int key, uint32_t mod);
void input_key(uint32_t key, uint32_t mod);
const char *keyname_extd(int key);
int action_down(int key);