Add touchpad controls to editor; move dup to entity after spawned

This commit is contained in:
John Alanbrook 2023-09-16 03:40:19 +00:00
parent d15c4ec6d1
commit 97483d4ce5
6 changed files with 71 additions and 56 deletions

View file

@ -1095,6 +1095,10 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
str = JS_ToCString(js,argv[1]); str = JS_ToCString(js,argv[1]);
app_name(str); app_name(str);
break; break;
case 135:
ret = float2js(cam_zoom());
break;
} }
if (str) if (str)

View file

@ -12,8 +12,6 @@
#include "stb_ds.h" #include "stb_ds.h"
int32_t mouseWheelX = 0;
int32_t mouseWheelY = 0;
float deltaT = 0; float deltaT = 0;
static int mouse_states[3] = {INPUT_UP}; static int mouse_states[3] = {INPUT_UP};
@ -29,7 +27,9 @@ JSValue jsany;
JSValue jsmouse; JSValue jsmouse;
JSValue jspos; JSValue jspos;
JSValue jsmove; JSValue jsmove;
JSValue jsscroll;
cpVect mousewheel = {0,0};
cpVect mouse_pos = {0, 0}; cpVect mouse_pos = {0, 0};
cpVect mouse_delta = {0, 0}; cpVect mouse_delta = {0, 0};
@ -115,7 +115,7 @@ void input_mouse(int btn, int state, uint32_t mod)
JS_FreeValue(js, argv[1]); JS_FreeValue(js, argv[1]);
} }
void input_mouse_move(float x, float y, float dx, float dy) void input_mouse_move(float x, float y, float dx, float dy, uint32_t mod)
{ {
mouse_pos.x = x; mouse_pos.x = x;
mouse_pos.y = y; mouse_pos.y = y;
@ -132,10 +132,24 @@ void input_mouse_move(float x, float y, float dx, float dy)
JS_FreeValue(js, argv[3]); JS_FreeValue(js, argv[3]);
} }
void input_mouse_scroll(float x, float y) void input_mouse_scroll(float x, float y, uint32_t mod)
{ {
mouseWheelY = y; mousewheel.x = x;
mouseWheelX = x; mousewheel.y = y;
JSValue argv[4];
argv[0] = jsmouse;
char out[16] = {0};
snprintf(out, 16, "%s%s%sscroll",
mod & SAPP_MODIFIER_CTRL ? "C-" : "",
mod & SAPP_MODIFIER_ALT ? "M-" : "",
mod & SAPP_MODIFIER_SUPER ? "S-" : ""
);
argv[1] = JS_NewString(js,out);
argv[2] = vec2js(mousewheel);
script_callee(pawn_callee, 3, argv);
JS_FreeValue(js, argv[1]);
JS_FreeValue(js, argv[2]);
} }
void input_btn(int btn, int state, uint32_t mod) void input_btn(int btn, int state, uint32_t mod)
@ -232,6 +246,7 @@ void input_init() {
jsmouse = str2js("mouse"); jsmouse = str2js("mouse");
jspos = str2js("pos"); jspos = str2js("pos");
jsmove = str2js("move"); jsmove = str2js("move");
jsscroll = str2js("scroll");
for (int i = 0; i < 512; i++) for (int i = 0; i < 512; i++)
key_states[i] = INPUT_UP; key_states[i] = INPUT_UP;
@ -368,8 +383,7 @@ void call_input_down(int *key) {
/* This is called once every frame - or more if we want it more! */ /* This is called once every frame - or more if we want it more! */
void input_poll(double wait) { void input_poll(double wait) {
mouse_delta = cpvzero; mouse_delta = cpvzero;
mouseWheelX = 0; mousewheel = cpvzero;
mouseWheelY = 0;
for (int i = 0; i < arrlen(downkeys); i++) for (int i = 0; i < arrlen(downkeys); i++)
call_input_down(&downkeys[i]); call_input_down(&downkeys[i]);

View file

@ -8,9 +8,7 @@
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
#include <stdint.h> #include <stdint.h>
extern int32_t mouseWheelX; extern cpVect mousewheel;
extern int32_t mouseWheelY;
extern cpVect mouse_pos; extern cpVect mouse_pos;
extern cpVect mouse_delta; extern cpVect mouse_delta;
@ -28,8 +26,8 @@ void cursor_show();
void set_mouse_mode(int mousemode); void set_mouse_mode(int mousemode);
void input_mouse(int btn, int state, uint32_t mod); void input_mouse(int btn, int state, uint32_t mod);
void input_mouse_move(float x, float y, float dx, float dy); void input_mouse_move(float x, float y, float dx, float dy, uint32_t mod);
void input_mouse_scroll(float x, float y); void input_mouse_scroll(float x, float y, uint32_t mod);
void input_btn(int btn, int state, uint32_t mod); void input_btn(int btn, int state, uint32_t mod);
void input_key(int key, uint32_t mod); void input_key(int key, uint32_t mod);

View file

@ -213,11 +213,11 @@ void c_event(const sapp_event *e)
switch (e->type) { switch (e->type) {
case SAPP_EVENTTYPE_MOUSE_MOVE: case SAPP_EVENTTYPE_MOUSE_MOVE:
input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy); input_mouse_move(e->mouse_x, e->mouse_y, e->mouse_dx, e->mouse_dy, e->modifiers);
break; break;
case SAPP_EVENTTYPE_MOUSE_SCROLL: case SAPP_EVENTTYPE_MOUSE_SCROLL:
input_mouse_scroll(e->scroll_x, e->scroll_y); input_mouse_scroll(e->scroll_x, e->scroll_y, e->modifiers);
break; break;
case SAPP_EVENTTYPE_KEY_DOWN: case SAPP_EVENTTYPE_KEY_DOWN:

View file

@ -6,6 +6,7 @@ prototypes.generate_ur('.');
var editor_config = { var editor_config = {
grid_size: 100, grid_size: 100,
ruler_mark_px: 100,
grid_color: [99, 255, 128, 100], grid_color: [99, 255, 128, 100],
}; };
@ -164,16 +165,8 @@ var editor = {
var objs = x.slice(); var objs = x.slice();
var duped = []; var duped = [];
objs.forEach(function(x) { objs.forEach(function(x) { duped.push(x.dup()); } );
var newobj = this.edit_level.spawn(x.ur); return duped;
//dainty_assign(newobj, x);
Object.assign(newobj,x);
newobj.pos = x.pos;
newobj.angle = x.angle;
duped.push(newobj);
} ,this);
return duped.flat();
}, },
sel_start: [], sel_start: [],
@ -576,17 +569,23 @@ var editor = {
}); });
Debug.draw_grid(1, editor_config.grid_size/editor.camera.zoom, editor_config.grid_color); Debug.draw_grid(1, editor_config.grid_size/editor.camera.zoom, editor_config.grid_color);
var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); }, this); var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); });
var endgrid = screen2world([Window.width, 0]); var endgrid = screen2world([Window.width, 0]);
var w_step = Math.round(editor_config.ruler_mark_px/Window.width * (endgrid.x-startgrid.x)/editor_config.grid_size)*editor_config.grid_size;
if (w_step === 0) w_step = editor_config.grid_size;
var h_step = Math.round(editor_config.ruler_mark_px/Window.height * (endgrid.y-startgrid.y)/editor_config.grid_size)*editor_config.grid_size;
if (h_step === 0) h_step = editor_config.grid_size;
while(startgrid[0] <= endgrid[0]) { while(startgrid[0] <= endgrid[0]) {
GUI.text(startgrid[0], [world2screen([startgrid[0], 0])[0],0]); GUI.text(startgrid[0], [world2screen([startgrid[0], 0])[0],0]);
startgrid[0] += editor_config.grid_size; startgrid[0] += w_step;
} }
while(startgrid[1] <= endgrid[1]) { while(startgrid[1] <= endgrid[1]) {
GUI.text(startgrid[1], [0, world2screen([0, startgrid[1]])[1]]); GUI.text(startgrid[1], [0, world2screen([0, startgrid[1]])[1]]);
startgrid[1] += editor_config.grid_size; startgrid[1] += h_step;
} }
/* Draw selection box */ /* Draw selection box */
@ -948,12 +947,7 @@ editor.inputs['C-n'] = function() {
editor.inputs['C-n'].doc = "Open a new level."; editor.inputs['C-n'].doc = "Open a new level.";
editor.inputs['C-o'] = function() { editor.inputs['C-o'] = function() {
if (editor.check_level_nested()) { if (editor.edit_level.dirty()) {
Log.warn("Nested level ...");
return;
}
if (editor.edit_level.dirty) {
editor.openpanel(gen_notify("Level is changed. Are you sure you want to close it?", function() { editor.openpanel(gen_notify("Level is changed. Are you sure you want to close it?", function() {
editor.clear_level(); editor.clear_level();
editor.openpanel(openlevelpanel); editor.openpanel(openlevelpanel);
@ -1219,6 +1213,16 @@ editor.inputs.mouse.move = function(pos, dpos)
}); });
} }
editor.inputs.mouse['C-scroll'] = function(scroll)
{
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.camera.zoom += scroll.y/100;
}
editor.inputs['C-M-S-lm'] = function() { editor.selectlist[0].set_center(Mouse.worldpos); }; editor.inputs['C-M-S-lm'] = function() { editor.selectlist[0].set_center(Mouse.worldpos); };
editor.inputs['C-M-S-lm'].doc = "Set world center to mouse position."; editor.inputs['C-M-S-lm'].doc = "Set world center to mouse position.";
@ -1424,7 +1428,6 @@ var inputpanel = {
}, },
start() {}, start() {},
close() { close() {
Player.players[0].uncontrol(this); Player.players[0].uncontrol(this);
@ -1918,5 +1921,4 @@ if (IO.exists("editor.config"))
/* This is the editor level & camera - NOT the currently edited level, but a level to hold editor things */ /* This is the editor level & camera - NOT the currently edited level, but a level to hold editor things */
editor.clear_level(); editor.clear_level();
editor.camera = Game.camera; editor.camera = Game.camera;
Game.stop(); Game.stop();

View file

@ -44,14 +44,6 @@ var gameobject = {
this.draw_layer = Nuke.radio(i, this.draw_layer, i); this.draw_layer = Nuke.radio(i, this.draw_layer, i);
}, },
dup(diff) {
var dup = World.spawn(gameobjects[this.from]);
Object.assign(dup, diff);
return dup;
},
ed_locked: false, ed_locked: false,
_visible: true, _visible: true,
@ -66,11 +58,6 @@ var gameobject = {
}, },
mass: 1, mass: 1,
bodytype: {
dynamic: 0,
kinematic: 1,
static: 2
},
phys: 2, phys: 2,
phys_nuke() { phys_nuke() {
@ -283,6 +270,13 @@ var gameobject = {
disable() { this.components.forEach(function(x) { x.disable(); });}, disable() { this.components.forEach(function(x) { x.disable(); });},
enable() { this.components.forEach(function(x) { x.enable(); });}, enable() { this.components.forEach(function(x) { x.enable(); });},
sync() { }, sync() { },
dirty() { return false; },
dup(diff) {
var dup = Primum.spawn(this.ur);
Object.assign(dup, this);
return dup;
},
kill() { kill() {
if (this.body === -1) { if (this.body === -1) {
@ -385,6 +379,7 @@ gameobject.make_parentable = function(obj) {
obj.add_child = function(child) { obj.add_child = function(child) {
child.unparent(); child.unparent();
objects.push(child); objects.push(child);
child.level = obj;
} }
/* Reparent this object to a new one */ /* Reparent this object to a new one */
@ -393,11 +388,13 @@ gameobject.make_parentable = function(obj) {
return; return;
parent.add_child(obj); parent.add_child(obj);
obj.level = parent;
} }
obj.unparent = function() { obj.unparent = function() {
if (!obj.level) return; if (!obj.level) return;
obj.level.remove_child(obj); obj.level.remove_child(obj);
obj.parent = undefined;
} }
obj.objects = objects; obj.objects = objects;
} }
@ -553,22 +550,22 @@ prototypes.generate_ur = function(path)
var ur = prototypes.ur; var ur = prototypes.ur;
prototypes.from_obj("camera2d", { prototypes.from_obj("camera2d", {
phys: gameobject.bodytype.kinematic, phys: Physics.kinematic,
speed: 300, speed: 300,
get zoom() { return this._zoom; }, get zoom() { return cmd(135); },
set zoom(x) { set zoom(x) {
if (x <= 0) return; x = Math.clamp(x,0.1,10);
this._zoom = x; cmd(62, x);
cmd(62, this._zoom);
}, },
_zoom: 1.0,
speedmult: 1.0, speedmult: 1.0,
selectable: false, selectable: false,
view2world(pos) { view2world(pos) {
return pos.mapc(mult, [1,-1]).add([-Window.width,Window.height].scale(0.5)).scale(this.zoom).add(this.pos); pos.y *= -1;
return pos.add([-Window.width,Window.height].scale(0.5)).scale(this.zoom).add(this.pos);
}, },
world2view(pos) { world2view(pos) {