Colourf.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "Colourf.h"
  30. namespace Rml {
  31. namespace Core {
  32. namespace Lua {
  33. template<> void ExtraInit<Colourf>(lua_State* L, int metatable_index)
  34. {
  35. lua_pushcfunction(L,Colourfnew);
  36. lua_setfield(L,metatable_index-1,"new");
  37. lua_pushcfunction(L,Colourf__eq);
  38. lua_setfield(L,metatable_index,"__eq");
  39. return;
  40. }
  41. //metamethods
  42. int Colourfnew(lua_State* L)
  43. {
  44. float red = (float)luaL_checknumber(L,1);
  45. float green = (float)luaL_checknumber(L,2);
  46. float blue = (float)luaL_checknumber(L,3);
  47. float alpha = (float)luaL_checknumber(L,4);
  48. Colourf* col = new Colourf(red,green,blue,alpha);
  49. LuaType<Colourf>::push(L,col,true);
  50. return 1;
  51. }
  52. int Colourf__eq(lua_State* L)
  53. {
  54. Colourf* lhs = LuaType<Colourf>::check(L,1);
  55. LUACHECKOBJ(lhs);
  56. Colourf* rhs = LuaType<Colourf>::check(L,2);
  57. LUACHECKOBJ(rhs);
  58. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  59. return 1;
  60. }
  61. //getters
  62. int ColourfGetAttrred(lua_State* L)
  63. {
  64. Colourf* obj = LuaType<Colourf>::check(L,1);
  65. LUACHECKOBJ(obj);
  66. lua_pushnumber(L,obj->red);
  67. return 1;
  68. }
  69. int ColourfGetAttrgreen(lua_State* L)
  70. {
  71. Colourf* obj = LuaType<Colourf>::check(L,1);
  72. LUACHECKOBJ(obj);
  73. lua_pushnumber(L,obj->green);
  74. return 1;
  75. }
  76. int ColourfGetAttrblue(lua_State* L)
  77. {
  78. Colourf* obj = LuaType<Colourf>::check(L,1);
  79. LUACHECKOBJ(obj);
  80. lua_pushnumber(L,obj->blue);
  81. return 1;
  82. }
  83. int ColourfGetAttralpha(lua_State* L)
  84. {
  85. Colourf* obj = LuaType<Colourf>::check(L,1);
  86. LUACHECKOBJ(obj);
  87. lua_pushnumber(L,obj->alpha);
  88. return 1;
  89. }
  90. int ColourfGetAttrrgba(lua_State* L)
  91. {
  92. Colourf* obj = LuaType<Colourf>::check(L,1);
  93. LUACHECKOBJ(obj);
  94. lua_pushnumber(L,obj->red);
  95. lua_pushnumber(L,obj->green);
  96. lua_pushnumber(L,obj->blue);
  97. lua_pushnumber(L,obj->alpha);
  98. return 4;
  99. }
  100. //setters
  101. int ColourfSetAttrred(lua_State* L)
  102. {
  103. Colourf* obj = LuaType<Colourf>::check(L,1);
  104. LUACHECKOBJ(obj);
  105. float red = (float)luaL_checknumber(L,2);
  106. obj->red = red;
  107. return 0;
  108. }
  109. int ColourfSetAttrgreen(lua_State* L)
  110. {
  111. Colourf* obj = LuaType<Colourf>::check(L,1);
  112. LUACHECKOBJ(obj);
  113. float green = (float)luaL_checknumber(L,2);
  114. obj->green = green;
  115. return 0;
  116. }
  117. int ColourfSetAttrblue(lua_State* L)
  118. {
  119. Colourf* obj = LuaType<Colourf>::check(L,1);
  120. LUACHECKOBJ(obj);
  121. float blue = (float)luaL_checknumber(L,2);
  122. obj->blue = blue;
  123. return 0;
  124. }
  125. int ColourfSetAttralpha(lua_State* L)
  126. {
  127. Colourf* obj = LuaType<Colourf>::check(L,1);
  128. LUACHECKOBJ(obj);
  129. float alpha = (float)luaL_checknumber(L,2);
  130. obj->alpha = alpha;
  131. return 0;
  132. }
  133. int ColourfSetAttrrgba(lua_State* L)
  134. {
  135. Colourf* obj = nullptr;
  136. int top = lua_gettop(L);
  137. //each of the items are optional.
  138. if(top > 0)
  139. {
  140. obj = LuaType<Colourf>::check(L,1);
  141. LUACHECKOBJ(obj);
  142. if(top > 1)
  143. {
  144. if(top > 2)
  145. {
  146. if(top > 3)
  147. obj->alpha = (float)luaL_checknumber(L,4);
  148. obj->blue = (float)luaL_checknumber(L,3);
  149. }
  150. obj->green = (float)luaL_checknumber(L,2);
  151. }
  152. obj->red = (float)luaL_checknumber(L,1);
  153. }
  154. return 0;
  155. }
  156. RegType<Colourf> ColourfMethods[] =
  157. {
  158. { nullptr, nullptr },
  159. };
  160. luaL_Reg ColourfGetters[] =
  161. {
  162. LUAGETTER(Colourf,red)
  163. LUAGETTER(Colourf,green)
  164. LUAGETTER(Colourf,blue)
  165. LUAGETTER(Colourf,alpha)
  166. LUAGETTER(Colourf,rgba)
  167. { nullptr, nullptr },
  168. };
  169. luaL_Reg ColourfSetters[] =
  170. {
  171. LUASETTER(Colourf,red)
  172. LUASETTER(Colourf,green)
  173. LUASETTER(Colourf,blue)
  174. LUASETTER(Colourf,alpha)
  175. LUASETTER(Colourf,rgba)
  176. { nullptr, nullptr },
  177. };
  178. LUACORETYPEDEFINE(Colourf)
  179. }
  180. }
  181. }