Colourb.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "precompiled.h"
  29. #include "Colourb.h"
  30. namespace Rml {
  31. namespace Core {
  32. namespace Lua {
  33. template<> void ExtraInit<Colourb>(lua_State* L, int metatable_index)
  34. {
  35. lua_pushcfunction(L,Colourbnew);
  36. lua_setfield(L,metatable_index-1,"new");
  37. lua_pushcfunction(L,Colourb__eq);
  38. lua_setfield(L,metatable_index,"__eq");
  39. lua_pushcfunction(L,Colourb__add);
  40. lua_setfield(L,metatable_index,"__add");
  41. lua_pushcfunction(L,Colourb__mul);
  42. lua_setfield(L,metatable_index,"__mul");
  43. return;
  44. }
  45. int Colourbnew(lua_State* L)
  46. {
  47. byte red = (byte)luaL_checkinteger(L,1);
  48. byte green = (byte)luaL_checkinteger(L,2);
  49. byte blue = (byte)luaL_checkinteger(L,3);
  50. byte alpha = (byte)luaL_checkinteger(L,4);
  51. Colourb* col = new Colourb(red,green,blue,alpha);
  52. LuaType<Colourb>::push(L,col,true);
  53. return 1;
  54. }
  55. int Colourb__eq(lua_State* L)
  56. {
  57. Colourb* lhs = LuaType<Colourb>::check(L,1);
  58. LUACHECKOBJ(lhs);
  59. Colourb* rhs = LuaType<Colourb>::check(L,2);
  60. LUACHECKOBJ(rhs);
  61. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  62. return 1;
  63. }
  64. int Colourb__add(lua_State* L)
  65. {
  66. Colourb* lhs = LuaType<Colourb>::check(L,1);
  67. LUACHECKOBJ(lhs);
  68. Colourb* rhs = LuaType<Colourb>::check(L,2);
  69. LUACHECKOBJ(rhs);
  70. Colourb* res = new Colourb((*lhs) + (*rhs));
  71. LuaType<Colourb>::push(L,res,true);
  72. return 1;
  73. }
  74. int Colourb__mul(lua_State* L)
  75. {
  76. Colourb* lhs = LuaType<Colourb>::check(L,1);
  77. LUACHECKOBJ(lhs);
  78. float rhs = (float)luaL_checknumber(L,2);
  79. Colourb* res = new Colourb((*lhs) * rhs);
  80. LuaType<Colourb>::push(L,res,true);
  81. return 1;
  82. }
  83. //getters
  84. int ColourbGetAttrred(lua_State* L)
  85. {
  86. Colourb* obj = LuaType<Colourb>::check(L,1);
  87. LUACHECKOBJ(obj);
  88. lua_pushinteger(L,obj->red);
  89. return 1;
  90. }
  91. int ColourbGetAttrgreen(lua_State* L)
  92. {
  93. Colourb* obj = LuaType<Colourb>::check(L,1);
  94. LUACHECKOBJ(obj);
  95. lua_pushinteger(L,obj->green);
  96. return 1;
  97. }
  98. int ColourbGetAttrblue(lua_State* L)
  99. {
  100. Colourb* obj = LuaType<Colourb>::check(L,1);
  101. LUACHECKOBJ(obj);
  102. lua_pushinteger(L,obj->blue);
  103. return 1;
  104. }
  105. int ColourbGetAttralpha(lua_State* L)
  106. {
  107. Colourb* obj = LuaType<Colourb>::check(L,1);
  108. LUACHECKOBJ(obj);
  109. lua_pushinteger(L,obj->alpha);
  110. return 1;
  111. }
  112. int ColourbGetAttrrgba(lua_State* L)
  113. {
  114. Colourb* obj = LuaType<Colourb>::check(L,1);
  115. LUACHECKOBJ(obj);
  116. lua_pushinteger(L,obj->red);
  117. lua_pushinteger(L,obj->green);
  118. lua_pushinteger(L,obj->blue);
  119. lua_pushinteger(L,obj->alpha);
  120. return 4;
  121. }
  122. //setters
  123. int ColourbSetAttrred(lua_State* L)
  124. {
  125. Colourb* obj = LuaType<Colourb>::check(L,1);
  126. LUACHECKOBJ(obj);
  127. byte red = (byte)luaL_checkinteger(L,2);
  128. obj->red = red;
  129. return 0;
  130. }
  131. int ColourbSetAttrgreen(lua_State* L)
  132. {
  133. Colourb* obj = LuaType<Colourb>::check(L,1);
  134. LUACHECKOBJ(obj);
  135. byte green = (byte)luaL_checkinteger(L,2);
  136. obj->green = green;
  137. return 0;
  138. }
  139. int ColourbSetAttrblue(lua_State* L)
  140. {
  141. Colourb* obj = LuaType<Colourb>::check(L,1);
  142. LUACHECKOBJ(obj);
  143. byte blue = (byte)luaL_checkinteger(L,2);
  144. obj->blue = blue;
  145. return 0;
  146. }
  147. int ColourbSetAttralpha(lua_State* L)
  148. {
  149. Colourb* obj = LuaType<Colourb>::check(L,1);
  150. LUACHECKOBJ(obj);
  151. byte alpha = (byte)luaL_checkinteger(L,2);
  152. obj->alpha = alpha;
  153. return 0;
  154. }
  155. int ColourbSetAttrrgba(lua_State* L)
  156. {
  157. Colourb* obj = nullptr;
  158. int top = lua_gettop(L);
  159. //each of the items are optional.
  160. if(top > 0)
  161. {
  162. obj = LuaType<Colourb>::check(L,1);
  163. LUACHECKOBJ(obj);
  164. if(top > 1)
  165. {
  166. if(top > 2)
  167. {
  168. if(top > 3)
  169. obj->alpha = luaL_checkinteger(L,4);
  170. obj->blue = luaL_checkinteger(L,3);
  171. }
  172. obj->green = luaL_checkinteger(L,2);
  173. }
  174. obj->red = luaL_checkinteger(L,1);
  175. }
  176. return 0;
  177. }
  178. RegType<Colourb> ColourbMethods[] =
  179. {
  180. { nullptr, nullptr },
  181. };
  182. luaL_Reg ColourbGetters[] =
  183. {
  184. LUAGETTER(Colourb,red)
  185. LUAGETTER(Colourb,green)
  186. LUAGETTER(Colourb,blue)
  187. LUAGETTER(Colourb,alpha)
  188. LUAGETTER(Colourb,rgba)
  189. { nullptr, nullptr },
  190. };
  191. luaL_Reg ColourbSetters[] =
  192. {
  193. LUASETTER(Colourb,red)
  194. LUASETTER(Colourb,green)
  195. LUASETTER(Colourb,blue)
  196. LUASETTER(Colourb,alpha)
  197. LUASETTER(Colourb,rgba)
  198. { nullptr, nullptr },
  199. };
  200. LUACORETYPEDEFINE(Colourb)
  201. }
  202. }
  203. }