InputObject.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // TODO: PROPERLY IMPLEMENT THIS
  10. return input->getScene ().getScriptEngine ().getAdapters ().vec3->instantiate ();
  11. }
  12. JSValue get_cursor_screen_position (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  13. JSClassID classId;
  14. auto* input = static_cast<InputObject*> (JS_GetAnyOpaque (this_val, &classId));
  15. auto position = input->getScene ().getMousePositionNormalized ();
  16. JSValue result = input->getScene ().getScriptEngine ().getAdapters ().vec2->instantiate ();
  17. JS_SetPropertyStr (ctx, result, "x", JS_NewFloat64 (ctx, position->x));
  18. JS_SetPropertyStr (ctx, result, "y", JS_NewFloat64 (ctx, position->y));
  19. return result;
  20. }
  21. JSValue get_cursor_left_down (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  22. // TODO: IMPLEMENT THIS
  23. return JS_NewBool (ctx, false);
  24. }
  25. JSValue input_set_value (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { return JS_EXCEPTION; }
  26. InputObject::InputObject (ScriptEngine& engine, Render::Wallpapers::CScene& scene) :
  27. m_scene (scene), m_engine (engine), m_classId (0) {
  28. this->m_definition = { .class_name = "IInput" };
  29. JS_NewClassID (this->m_engine.getRuntime (), &this->m_classId);
  30. JS_NewClass (this->m_engine.getRuntime (), this->m_classId, &this->m_definition);
  31. this->m_instance = JS_NewObjectClass (this->m_engine.getContext (), this->m_classId);
  32. JS_DupValue (this->m_engine.getContext (), this->m_instance);
  33. // set properties
  34. JS_SetOpaque (this->m_instance, this);
  35. JS_DefinePropertyGetSet (
  36. this->m_engine.getContext (), this->m_instance,
  37. JS_NewAtom (this->m_engine.getContext (), "cursorWorldPosition"),
  38. JS_NewCFunction (this->m_engine.getContext (), get_cursor_world_position, "get", 0),
  39. JS_NewCFunction (this->m_engine.getContext (), input_set_value, "set", 1), JS_PROP_ENUMERABLE
  40. );
  41. JS_DefinePropertyGetSet (
  42. this->m_engine.getContext (), this->m_instance,
  43. JS_NewAtom (this->m_engine.getContext (), "cursorScreenPosition"),
  44. JS_NewCFunction (this->m_engine.getContext (), get_cursor_screen_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, JS_NewAtom (this->m_engine.getContext (), "cursorLeftDown"),
  49. JS_NewCFunction (this->m_engine.getContext (), get_cursor_left_down, "get", 0),
  50. JS_NewCFunction (this->m_engine.getContext (), input_set_value, "set", 1), JS_PROP_ENUMERABLE
  51. );
  52. }
  53. InputObject::~InputObject () { JS_FreeValue (this->m_engine.getContext (), this->m_instance); }