Interfaces.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../../Include/Rocket/Core/URL.h"
  29. #include "../../../Include/Rocket/Core/Log.h"
  30. #include "../../../Include/Rocket/Core/Dictionary.h"
  31. //#include "DataSourceWrapper.h"
  32. #include "../../../Include/Rocket/Core/Python/ConverterScriptObject.h"
  33. #include "../../../Include/Rocket/Core/Python/PickleTypeConverter.h"
  34. #include "../../../Include/Rocket/Core/Python/Utilities.h"
  35. #include "../../../Include/Rocket/Core/Python/VectorInterface.h"
  36. #include <vector>
  37. namespace Rocket {
  38. namespace Core {
  39. namespace Python {
  40. // Dictionary Interface
  41. class DictionaryInterface
  42. {
  43. public:
  44. DictionaryInterface()
  45. {
  46. python::class_< Dictionary >("Dictionary")
  47. .def("__len__", &DictionaryInterface::Size)
  48. .def("__setitem__", &DictionaryInterface::SetItem)
  49. .def("__delitem__", &DictionaryInterface::DelItem)
  50. .def("__getitem__", &DictionaryInterface::GetItem, python::return_value_policy< python::return_by_value >())
  51. .def("__contains__", &DictionaryInterface::Contains)
  52. ;
  53. }
  54. static size_t Size(const Dictionary& dict)
  55. {
  56. return dict.Size();
  57. }
  58. static void SetItem(Dictionary& dict, const char* key, const Variant& value)
  59. {
  60. dict.Set(key, value);
  61. }
  62. static void DelItem(Dictionary& dict, const char* key)
  63. {
  64. dict.Remove(key);
  65. }
  66. static Variant& GetItem(const Dictionary& dict, const char* key)
  67. {
  68. Variant* variant = dict.Get(key);
  69. if (!variant)
  70. {
  71. PyErr_SetString(PyExc_KeyError, Rocket::Core::String(64, "Invalid key %s", key).CString());
  72. python::throw_error_already_set();
  73. }
  74. return *variant;
  75. }
  76. static bool Contains(const Dictionary& dict, const char* key)
  77. {
  78. return dict.Get(key) != NULL;
  79. }
  80. };
  81. static void Log(Log::Type level, const char* message)
  82. {
  83. Core::Log::Message(level, "%s", message);
  84. }
  85. void RegisterPythonInterfaces()
  86. {
  87. python::class_< ScriptInterface, boost::noncopyable >("ScriptInterface", python::no_init);
  88. VectorInterface< StringList >("StringList");
  89. python::class_< Vector2f >("Vector2f")
  90. .def(python::init< float, float >())
  91. .def_readwrite("x", &Vector2f::x)
  92. .def_readwrite("y", &Vector2f::y)
  93. .def(python::self * float()) // * float
  94. .def(python::self / float()) // / float
  95. .def(python::self + Vector2f()) // + Vector2f
  96. .def(python::self - Vector2f()) // - Vector2f
  97. .def(python::self == Vector2f()) // ==
  98. .def(python::self != Vector2f()) // !=
  99. .def("DotProduct", &Vector2f::DotProduct)
  100. .def("Normalise", &Vector2f::Normalise)
  101. .def("Rotate", &Vector2f::Rotate)
  102. .add_property("magnitude", &Vector2f::Magnitude)
  103. .def_pickle(PickleTypeConverter< Vector2f >())
  104. ;
  105. python::class_< Vector2i >("Vector2i")
  106. .def(python::init< int, int >())
  107. .def_readwrite("x", &Vector2i::x)
  108. .def_readwrite("y", &Vector2i::y)
  109. .def(python::self * int()) // * int
  110. .def(python::self / int()) // / int
  111. .def(python::self + Vector2i()) // + Vector2i
  112. .def(python::self - Vector2i()) // - Vector2i
  113. .def(python::self == Vector2i()) // ==
  114. .def(python::self != Vector2i()) // !=
  115. .def_pickle(PickleTypeConverter< Vector2i >())
  116. ;
  117. python::class_< Colourf >("Colourf")
  118. .def(python::init< float, float, float, float >())
  119. .def_readwrite("red", &Colourf::red)
  120. .def_readwrite("green", &Colourf::green)
  121. .def_readwrite("blue", &Colourf::blue)
  122. .def_readwrite("alpha", &Colourf::alpha)
  123. .def(python::self == Colourf()) // ==
  124. .def_pickle(PickleTypeConverter< Colourf >())
  125. ;
  126. python::class_< Colourb >("Colourb")
  127. .def(python::init< byte, byte, byte, byte>())
  128. .def_readwrite("red", &Colourb::red)
  129. .def_readwrite("green", &Colourb::green)
  130. .def_readwrite("blue", &Colourb::blue)
  131. .def_readwrite("alpha", &Colourb::alpha)
  132. .def(python::self == Colourb()) // ==
  133. .def(python::self + Colourb()) // +
  134. .def(python::self *= float()) // *=
  135. .def(python::self * float()) // *
  136. .def_pickle(PickleTypeConverter< Colourb >())
  137. ;
  138. python::class_< URL >("URL")
  139. .def(python::init< const char* >())
  140. .def_pickle(PickleTypeConverter< URL >())
  141. ;
  142. python::def("Log", &Log);
  143. python::enum_< Core::Log::Type >("logtype")
  144. .value("always", Core::Log::LT_ALWAYS)
  145. .value("error", Core::Log::LT_ERROR)
  146. .value("warning", Core::Log::LT_WARNING)
  147. .value("info", Core::Log::LT_INFO)
  148. .value("debug", Core::Log::LT_DEBUG)
  149. ;
  150. DictionaryInterface();
  151. //DataSourceWrapper::InitialisePythonInterface();
  152. }
  153. }
  154. }
  155. }