diff --git a/README.md b/README.md index 9582471..791309c 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,42 @@ Draw a line between two points: `alpha` - alpha channel 0..255. Optional parameter for rgba textures +#### drawpixels.pixel(buffer_info, x, y, red, green, blue, alpha) +Draw a pixel: + +`buffer_info` - buffer information + +`x` - x position of pixel + +`y` - y position of pixel + +`red` - red channel of the color 0..255 + +`green` - green channel of the color 0..255 + +`blue` - blue channel of the color 0..255 + +`alpha` - alpha channel 0..255. Optional parameter for rgba textures + +#### drawpixels.color(buffer_info, x, y) +Read color from a position in the buffer: + +`buffer_info` - buffer information + +`x` - x position to get color from + +`y` - y position to get color from + +RETURNS: + +`red` - red channel of the color 0..255 + +`green` - green channel of the color 0..255 + +`blue` - blue channel of the color 0..255 + +`alpha` - alpha channel 0..255. Optional parameter for rgba textures + -------- If you have any questions or suggestions contact me: me@agulev.com diff --git a/drawpixels/src/drawpixels.cpp b/drawpixels/src/drawpixels.cpp index d63720e..c74cde3 100755 --- a/drawpixels/src/drawpixels.cpp +++ b/drawpixels/src/drawpixels.cpp @@ -182,6 +182,50 @@ static int draw_line(lua_State* L) { return 0; } +static int draw_pixel(lua_State* L) { + int top = lua_gettop(L) + 4; + + read_and_validate_buffer_info(L, 1); + int32_t x = luaL_checknumber(L, 2); + int32_t y = luaL_checknumber(L, 3); + uint32_t r = luaL_checknumber(L, 4); + uint32_t g = luaL_checknumber(L, 5); + uint32_t b = luaL_checknumber(L, 6); + uint32_t a = 0; + if (lua_isnumber(L, 7) == 1) + { + a = luaL_checknumber(L, 7); + } + + putpixel(x, y, r, g, b, a); + + assert(top == lua_gettop(L)); + return 0; +} + +static int read_color(lua_State* L) { + int top = lua_gettop(L) + 4; + + read_and_validate_buffer_info(L, 1); + int32_t x = luaL_checknumber(L, 2); + int32_t y = luaL_checknumber(L, 3); + + if (!in_buffer(x, y)) { + assert(top == lua_gettop(L)); + return 0; + } + + int i = xytoi(x, y); + lua_pushnumber(L, buffer_info.bytes[i]); + lua_pushnumber(L, buffer_info.bytes[i + 1]); + lua_pushnumber(L, buffer_info.bytes[i + 2]); + if (buffer_info.channels == 4) { + lua_pushnumber(L, buffer_info.bytes[i + 3]); + } + assert(top + buffer_info.channels == lua_gettop(L)); + return buffer_info.channels; +} + //https://en.wikipedia.org/wiki/Midpoint_circle_algorithm static int draw_circle(lua_State* L) { int top = lua_gettop(L) + 4; @@ -426,6 +470,8 @@ static const luaL_reg Module_methods[] = { {"fill", fill_texture}, {"rect", draw_rect}, {"filled_rect", draw_filled_rect}, + {"pixel", draw_pixel}, + {"color", read_color}, {0, 0} }; diff --git a/example/canvas.script b/example/canvas.script index e8226c8..9e780dd 100755 --- a/example/canvas.script +++ b/example/canvas.script @@ -64,6 +64,10 @@ local function color_vector_to_bytes(color) return color.x * 255, color.y * 255, color.z * 255, color.w * 255 end +local function bytes_to_color_vector(r, g, b, a) + return vmath.vector4(r / 255, g / 255, b / 255, a / 255) +end + function on_input(self, action_id, action) if action_id == hash("touch") then local pos = vmath.vector3(action.x, action.y, 0) @@ -97,7 +101,10 @@ function on_input(self, action_id, action) while length > 0 do local r, g, b, a = color_vector_to_bytes(self.current_color) if self.current_tool == "pencil" then - drawpixels.filled_rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 4, 4, r, g, b, a) + drawpixels.pixel(self.buffer_info, self.touch_pos.x, self.touch_pos.y, r, g, b, a) + elseif self.current_tool == "colorpicker" then + local r,g,b,a = drawpixels.color(self.buffer_info, self.touch_pos.x, self.touch_pos.y) + self.current_color = bytes_to_color_vector(r, g, b, a) elseif self.current_tool == "circle" then drawpixels.circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, r, g, b, a) elseif self.current_tool == "line" and self.last_pos and next(self.last_pos) then diff --git a/example/draw_pixels.atlas b/example/draw_pixels.atlas index 45a3e7d..a68f2a1 100755 --- a/example/draw_pixels.atlas +++ b/example/draw_pixels.atlas @@ -31,6 +31,9 @@ images { images { image: "/example/images/line.png" } +images { + image: "/example/images/gimp-tool-color-picker.png" +} margin: 0 extrude_borders: 2 inner_padding: 0 diff --git a/example/images/gimp-tool-color-picker.png b/example/images/gimp-tool-color-picker.png new file mode 100644 index 0000000..2e664c9 Binary files /dev/null and b/example/images/gimp-tool-color-picker.png differ diff --git a/example/toolbox.gui b/example/toolbox.gui index bee4fc3..8d37ace 100755 --- a/example/toolbox.gui +++ b/example/toolbox.gui @@ -674,7 +674,7 @@ nodes { } nodes { position { - x: -46.804 + x: -64.0 y: 10.711 z: 0.0 w: 1.0 @@ -729,7 +729,7 @@ nodes { } nodes { position { - x: -46.804 + x: -64.0 y: -34.704 z: 0.0 w: 1.0 @@ -784,7 +784,7 @@ nodes { } nodes { position { - x: -105.0 + x: -133.0 y: 10.0 z: 0.0 w: 1.0 @@ -802,7 +802,7 @@ nodes { w: 1.0 } size { - x: 60.0 + x: 24.0 y: 30.0 z: 0.0 w: 1.0 @@ -851,8 +851,8 @@ nodes { w: 1.0 } scale { - x: 1.0 - y: 1.0 + x: 0.8 + y: 0.8 z: 1.0 w: 1.0 } @@ -863,14 +863,14 @@ nodes { w: 1.0 } color { - x: 1.0 - y: 1.0 - z: 1.0 + x: 0.3019608 + y: 0.3019608 + z: 0.3019608 w: 1.0 } type: TYPE_TEXT blend_mode: BLEND_MODE_ALPHA - text: "Clear" + text: "CLR" font: "system_font" id: "text" xanchor: XANCHOR_NONE @@ -957,7 +957,7 @@ nodes { } nodes { position { - x: -120.0 + x: -133.0 y: -35.0 z: 0.0 w: 1.0 @@ -1012,7 +1012,7 @@ nodes { } nodes { position { - x: -160.0 + x: -167.0 y: -35.0 z: 0.0 w: 1.0 @@ -1067,7 +1067,7 @@ nodes { } nodes { position { - x: -160.0 + x: -167.0 y: 10.0 z: 0.0 w: 1.0 @@ -1177,7 +1177,7 @@ nodes { } nodes { position { - x: -83.0 + x: -99.0 y: -35.0 z: 0.0 w: 1.0 @@ -1230,6 +1230,61 @@ nodes { template_node_child: false size_mode: SIZE_MODE_AUTO } +nodes { + position { + x: -99.0 + y: 10.711 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 24.0 + y: 24.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "draw_pixels/gimp-tool-color-picker" + id: "colorpicker" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "toolbox" + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_AUTO +} material: "/builtins/materials/gui.material" adjust_reference: ADJUST_REFERENCE_LEGACY max_nodes: 512 diff --git a/example/toolbox.gui_script b/example/toolbox.gui_script index 7387e00..860d2dd 100755 --- a/example/toolbox.gui_script +++ b/example/toolbox.gui_script @@ -34,6 +34,10 @@ local function use_line() msg.post("canvas", "change_tool", { tool = "line" }) end +local function use_colorpicker() + msg.post("canvas", "change_tool", { tool = "colorpicker" }) +end + local function clear() msg.post("canvas", "clear") end @@ -51,6 +55,7 @@ function init(self) self.nodes[gui.get_node("filled_rect")] = use_filled_rect self.nodes[gui.get_node("rotated_rect")] = use_rotated_rect self.nodes[gui.get_node("line")] = use_line + self.nodes[gui.get_node("colorpicker")] = use_colorpicker for i = 1, 10 do self.nodes[gui.get_node("color" .. i)] = pick_color end