InputObject.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "InputObject.h"
  2. #include "EngineObject.h"
  3. #include "ScriptEngine.h"
  4. #include "WallpaperEngine/Render/Wallpapers/CScene.h"
  5. using namespace WallpaperEngine::Scripting;
  6. JSValue get_cursor_world_position (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  7. JSClassID classId;
  8. auto* input = static_cast<InputObject*> (JS_GetAnyOpaque (this_val, &classId));
  9. auto& scene = input->getScene ();
  10. const glm::vec3 position = scene.getCursorWorldPosition ();
  11. JSValue result = scene.getScriptEngine ().getAdapters ().vec3->instantiate ();
  12. JS_SetPropertyStr (ctx, result, "x", JS_NewFloat64 (ctx, position.x));
  13. JS_SetPropertyStr (ctx, result, "y", JS_NewFloat64 (ctx, position.y));
  14. JS_SetPropertyStr (ctx, result, "z", JS_NewFloat64 (ctx, position.z));
  15. return result;
  16. }
  17. JSValue get_cursor_screen_position (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  18. JSClassID classId;
  19. auto* input = static_cast<InputObject*> (JS_GetAnyOpaque (this_val, &classId));
  20. const glm::vec2 position = input->getScene ().getCursorPixelPosition ();
  21. JSValue result = input->getScene ().getScriptEngine ().getAdapters ().vec2->instantiate ();
  22. JS_SetPropertyStr (ctx, result, "x", JS_NewFloat64 (ctx, position.x));
  23. JS_SetPropertyStr (ctx, result, "y", JS_NewFloat64 (ctx, position.y));
  24. return result;
  25. }
  26. JSValue get_cursor_left_down (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  27. JSClassID classId;
  28. auto* input = static_cast<InputObject*> (JS_GetAnyOpaque (this_val, &classId));
  29. return JS_NewBool (ctx, input->getScene ().isCursorLeftDown ());
  30. }
  31. // read-only properties, writes are ignored instead of aborting the calling script
  32. JSValue input_set_value (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { return JS_UNDEFINED; }
  33. InputObject::InputObject (ScriptEngine& engine, Render::Wallpapers::CScene& scene) :
  34. m_scene (scene), m_engine (engine), m_classId (0) {
  35. this->m_definition = { .class_name = "IInput" };
  36. JS_NewClassID (this->m_engine.getRuntime (), &this->m_classId);
  37. JS_NewClass (this->m_engine.getRuntime (), this->m_classId, &this->m_definition);
  38. this->m_instance = JS_NewObjectClass (this->m_engine.getContext (), this->m_classId);
  39. JS_DupValue (this->m_engine.getContext (), this->m_instance);
  40. JS_SetOpaque (this->m_instance, this);
  41. JS_DefinePropertyGetSet (
  42. this->m_engine.getContext (), this->m_instance,
  43. JS_NewAtom (this->m_engine.getContext (), "cursorWorldPosition"),
  44. JS_NewCFunction (this->m_engine.getContext (), get_cursor_world_position, "get", 0),
  45. JS_NewCFunction (this->m_engine.getContext (), input_set_value, "set", 1), JS_PROP_ENUMERABLE
  46. );
  47. JS_DefinePropertyGetSet (
  48. this->m_engine.getContext (), this->m_instance,
  49. JS_NewAtom (this->m_engine.getContext (), "cursorScreenPosition"),
  50. JS_NewCFunction (this->m_engine.getContext (), get_cursor_screen_position, "get", 0),
  51. JS_NewCFunction (this->m_engine.getContext (), input_set_value, "set", 1), JS_PROP_ENUMERABLE
  52. );
  53. JS_DefinePropertyGetSet (
  54. this->m_engine.getContext (), this->m_instance, JS_NewAtom (this->m_engine.getContext (), "cursorLeftDown"),
  55. JS_NewCFunction (this->m_engine.getContext (), get_cursor_left_down, "get", 0),
  56. JS_NewCFunction (this->m_engine.getContext (), input_set_value, "set", 1), JS_PROP_ENUMERABLE
  57. );
  58. }
  59. InputObject::~InputObject () { JS_FreeValue (this->m_engine.getContext (), this->m_instance); }