Colourb.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Colourb.h"
  29. namespace Rml {
  30. namespace Lua {
  31. template<> void ExtraInit<Colourb>(lua_State* L, int metatable_index)
  32. {
  33. lua_pushcfunction(L,Colourbnew);
  34. lua_setfield(L,metatable_index-1,"new");
  35. lua_pushcfunction(L,Colourb__eq);
  36. lua_setfield(L,metatable_index,"__eq");
  37. lua_pushcfunction(L,Colourb__add);
  38. lua_setfield(L,metatable_index,"__add");
  39. lua_pushcfunction(L,Colourb__mul);
  40. lua_setfield(L,metatable_index,"__mul");
  41. return;
  42. }
  43. int Colourbnew(lua_State* L)
  44. {
  45. byte red = (byte)luaL_checkinteger(L,1);
  46. byte green = (byte)luaL_checkinteger(L,2);
  47. byte blue = (byte)luaL_checkinteger(L,3);
  48. byte alpha = (byte)luaL_checkinteger(L,4);
  49. Colourb* col = new Colourb(red,green,blue,alpha);
  50. LuaType<Colourb>::push(L,col,true);
  51. return 1;
  52. }
  53. int Colourb__eq(lua_State* L)
  54. {
  55. Colourb* lhs = LuaType<Colourb>::check(L,1);
  56. RMLUI_CHECK_OBJ(lhs);
  57. Colourb* rhs = LuaType<Colourb>::check(L,2);
  58. RMLUI_CHECK_OBJ(rhs);
  59. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  60. return 1;
  61. }
  62. int Colourb__add(lua_State* L)
  63. {
  64. Colourb* lhs = LuaType<Colourb>::check(L,1);
  65. RMLUI_CHECK_OBJ(lhs);
  66. Colourb* rhs = LuaType<Colourb>::check(L,2);
  67. RMLUI_CHECK_OBJ(rhs);
  68. Colourb* res = new Colourb((*lhs) + (*rhs));
  69. LuaType<Colourb>::push(L,res,true);
  70. return 1;
  71. }
  72. int Colourb__mul(lua_State* L)
  73. {
  74. Colourb* lhs = LuaType<Colourb>::check(L,1);
  75. RMLUI_CHECK_OBJ(lhs);
  76. float rhs = (float)luaL_checknumber(L,2);
  77. Colourb* res = new Colourb((*lhs) * rhs);
  78. LuaType<Colourb>::push(L,res,true);
  79. return 1;
  80. }
  81. //getters
  82. int ColourbGetAttrred(lua_State* L)
  83. {
  84. Colourb* obj = LuaType<Colourb>::check(L,1);
  85. RMLUI_CHECK_OBJ(obj);
  86. lua_pushinteger(L,obj->red);
  87. return 1;
  88. }
  89. int ColourbGetAttrgreen(lua_State* L)
  90. {
  91. Colourb* obj = LuaType<Colourb>::check(L,1);
  92. RMLUI_CHECK_OBJ(obj);
  93. lua_pushinteger(L,obj->green);
  94. return 1;
  95. }
  96. int ColourbGetAttrblue(lua_State* L)
  97. {
  98. Colourb* obj = LuaType<Colourb>::check(L,1);
  99. RMLUI_CHECK_OBJ(obj);
  100. lua_pushinteger(L,obj->blue);
  101. return 1;
  102. }
  103. int ColourbGetAttralpha(lua_State* L)
  104. {
  105. Colourb* obj = LuaType<Colourb>::check(L,1);
  106. RMLUI_CHECK_OBJ(obj);
  107. lua_pushinteger(L,obj->alpha);
  108. return 1;
  109. }
  110. int ColourbGetAttrrgba(lua_State* L)
  111. {
  112. Colourb* obj = LuaType<Colourb>::check(L,1);
  113. RMLUI_CHECK_OBJ(obj);
  114. lua_pushinteger(L,obj->red);
  115. lua_pushinteger(L,obj->green);
  116. lua_pushinteger(L,obj->blue);
  117. lua_pushinteger(L,obj->alpha);
  118. return 4;
  119. }
  120. //setters
  121. int ColourbSetAttrred(lua_State* L)
  122. {
  123. Colourb* obj = LuaType<Colourb>::check(L,1);
  124. RMLUI_CHECK_OBJ(obj);
  125. byte red = (byte)luaL_checkinteger(L,2);
  126. obj->red = red;
  127. return 0;
  128. }
  129. int ColourbSetAttrgreen(lua_State* L)
  130. {
  131. Colourb* obj = LuaType<Colourb>::check(L,1);
  132. RMLUI_CHECK_OBJ(obj);
  133. byte green = (byte)luaL_checkinteger(L,2);
  134. obj->green = green;
  135. return 0;
  136. }
  137. int ColourbSetAttrblue(lua_State* L)
  138. {
  139. Colourb* obj = LuaType<Colourb>::check(L,1);
  140. RMLUI_CHECK_OBJ(obj);
  141. byte blue = (byte)luaL_checkinteger(L,2);
  142. obj->blue = blue;
  143. return 0;
  144. }
  145. int ColourbSetAttralpha(lua_State* L)
  146. {
  147. Colourb* obj = LuaType<Colourb>::check(L,1);
  148. RMLUI_CHECK_OBJ(obj);
  149. byte alpha = (byte)luaL_checkinteger(L,2);
  150. obj->alpha = alpha;
  151. return 0;
  152. }
  153. int ColourbSetAttrrgba(lua_State* L)
  154. {
  155. Colourb* obj = nullptr;
  156. int top = lua_gettop(L);
  157. //each of the items are optional.
  158. if(top > 0)
  159. {
  160. obj = LuaType<Colourb>::check(L,1);
  161. RMLUI_CHECK_OBJ(obj);
  162. if(top > 1)
  163. {
  164. if(top > 2)
  165. {
  166. if(top > 3)
  167. obj->alpha = (byte)luaL_checkinteger(L,4);
  168. obj->blue = (byte)luaL_checkinteger(L,3);
  169. }
  170. obj->green = (byte)luaL_checkinteger(L,2);
  171. }
  172. obj->red = (byte)luaL_checkinteger(L,1);
  173. }
  174. return 0;
  175. }
  176. RegType<Colourb> ColourbMethods[] =
  177. {
  178. { nullptr, nullptr },
  179. };
  180. luaL_Reg ColourbGetters[] =
  181. {
  182. RMLUI_LUAGETTER(Colourb,red)
  183. RMLUI_LUAGETTER(Colourb,green)
  184. RMLUI_LUAGETTER(Colourb,blue)
  185. RMLUI_LUAGETTER(Colourb,alpha)
  186. RMLUI_LUAGETTER(Colourb,rgba)
  187. { nullptr, nullptr },
  188. };
  189. luaL_Reg ColourbSetters[] =
  190. {
  191. RMLUI_LUASETTER(Colourb,red)
  192. RMLUI_LUASETTER(Colourb,green)
  193. RMLUI_LUASETTER(Colourb,blue)
  194. RMLUI_LUASETTER(Colourb,alpha)
  195. RMLUI_LUASETTER(Colourb,rgba)
  196. { nullptr, nullptr },
  197. };
  198. RMLUI_LUATYPE_DEFINE(Colourb)
  199. } // namespace Lua
  200. } // namespace Rml