draw2d uses object prototype for command creation
This commit is contained in:
@@ -17,10 +17,19 @@ var commands = []
|
||||
var renderer_actor = null
|
||||
var renderer_id = null
|
||||
|
||||
// Prototype object for commands
|
||||
var command_proto = null
|
||||
|
||||
// Set the renderer
|
||||
draw.set_renderer = function(actor, id) {
|
||||
renderer_actor = actor
|
||||
renderer_id = id
|
||||
|
||||
// Create prototype object with common fields
|
||||
command_proto = {
|
||||
kind: "renderer",
|
||||
id: id
|
||||
}
|
||||
}
|
||||
|
||||
// Clear accumulated commands
|
||||
@@ -60,27 +69,21 @@ draw.flush = function() {
|
||||
}
|
||||
|
||||
// Helper to add a command
|
||||
function add_command(cmd) {
|
||||
function add_command(op, data, prop, value) {
|
||||
var cmd = Object.create(command_proto)
|
||||
cmd.op = op
|
||||
if (data) cmd.data = data
|
||||
if (prop) cmd.prop = prop
|
||||
if (value !== undefined) cmd.value = value
|
||||
commands.push(cmd)
|
||||
}
|
||||
|
||||
// Drawing functions
|
||||
draw.point = function(pos, size, opt = {color: Color.white}, pipeline) {
|
||||
if (opt.color) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "set",
|
||||
prop: "drawColor",
|
||||
value: opt.color
|
||||
})
|
||||
add_command("set", null, "drawColor", opt.color)
|
||||
}
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "point",
|
||||
data: {points: [pos]}
|
||||
})
|
||||
add_command("point", {points: [pos]})
|
||||
}
|
||||
draw.point[prosperon.DOC] = `
|
||||
:param pos: A 2D position ([x, y]) where the point should be drawn.
|
||||
@@ -139,12 +142,7 @@ function software_ellipse(pos, radii, opt)
|
||||
[cx + x, cy - y], [cx - x, cy - y]
|
||||
].filter(pt => within_wedge(pt[0]-cx, pt[1]-cy, start, end, full_circle))
|
||||
if (pts.length) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "point",
|
||||
data: {points: pts}
|
||||
})
|
||||
add_command("point", {points: pts})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,12 +202,7 @@ function software_ellipse(pos, radii, opt)
|
||||
}
|
||||
}
|
||||
if (strips.length) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {rects: strips}
|
||||
})
|
||||
add_command("rects", {rects: strips})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,13 +217,7 @@ var ellipse_def = {
|
||||
draw.ellipse = function(pos, radii, def, pipeline) {
|
||||
var opt = def ? {...ellipse_def, ...def} : ellipse_def
|
||||
if (opt.color) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "set",
|
||||
prop: "drawColor",
|
||||
value: opt.color
|
||||
})
|
||||
add_command("set", null, "drawColor", opt.color)
|
||||
}
|
||||
if (opt.thickness <= 0) opt.thickness = Math.max(radii[0], radii[1])
|
||||
software_ellipse(pos, radii, opt)
|
||||
@@ -251,20 +238,9 @@ draw.line = function(points, def, pipeline)
|
||||
opt = line_def
|
||||
|
||||
if (opt.color) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "set",
|
||||
prop: "drawColor",
|
||||
value: opt.color
|
||||
})
|
||||
add_command("set", null, "drawColor", opt.color)
|
||||
}
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "line",
|
||||
data: {points: points}
|
||||
})
|
||||
add_command("line", {points: points})
|
||||
}
|
||||
|
||||
draw.cross = function render_cross(pos, size, def, pipe) {
|
||||
@@ -289,24 +265,14 @@ draw.arrow = function render_arrow(start, end, wingspan = 4, wingangle = 10, def
|
||||
function software_outline_rect(rect, thickness)
|
||||
{
|
||||
if (thickness <= 0) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "fillRect",
|
||||
data: {rect: rect}
|
||||
})
|
||||
add_command("fillRect", {rect: rect})
|
||||
return;
|
||||
}
|
||||
|
||||
/* stroke swallows the whole thing → fill instead */
|
||||
if ((thickness << 1) >= rect.width ||
|
||||
(thickness << 1) >= rect.height) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "fillRect",
|
||||
data: {rect: rect}
|
||||
})
|
||||
add_command("fillRect", {rect: rect})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -315,11 +281,7 @@ function software_outline_rect(rect, thickness)
|
||||
x1 = rect.x + rect.width,
|
||||
y1 = rect.y + rect.height
|
||||
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {
|
||||
add_command("rects", {
|
||||
rects: [
|
||||
{ x:x0, y:y0, width:rect.width, height:thickness }, // top
|
||||
{ x:x0, y:y1-thickness, width:rect.width, height:thickness }, // bottom
|
||||
@@ -328,7 +290,6 @@ function software_outline_rect(rect, thickness)
|
||||
{ x:x1-thickness, y:y0+thickness, width:thickness,
|
||||
height:rect.height - (thickness<<1) } // right
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -363,11 +324,7 @@ function software_round_rect(rect, radius, thickness = 1)
|
||||
const r_in = radius - thickness
|
||||
|
||||
/* straight bands (top/bottom/left/right) ------------------------- */
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {
|
||||
add_command("rects", {
|
||||
rects: [
|
||||
{ x:x0 + radius, y:y0, width:rect.width - (radius << 1),
|
||||
height:thickness }, // top
|
||||
@@ -378,7 +335,6 @@ function software_round_rect(rect, radius, thickness = 1)
|
||||
{ x:x1 - thickness + 1, y:y0 + radius, width:thickness,
|
||||
height:rect.height - (radius << 1) } // right
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
/* corner arcs ---------------------------------------------------- */
|
||||
@@ -406,12 +362,7 @@ function software_round_rect(rect, radius, thickness = 1)
|
||||
)
|
||||
}
|
||||
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {rects: strips}
|
||||
})
|
||||
add_command("rects", {rects: strips})
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------
|
||||
@@ -427,31 +378,21 @@ function software_fill_round_rect(rect, radius)
|
||||
y1 = rect.y + rect.height - 1
|
||||
|
||||
/* main column */
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {
|
||||
add_command("rects", {
|
||||
rects: [
|
||||
{ x:x0 + radius, y:y0, width:rect.width - (radius << 1),
|
||||
height:rect.height }
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
/* side columns */
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {
|
||||
add_command("rects", {
|
||||
rects: [
|
||||
{ x:x0, y:y0 + radius, width:radius,
|
||||
height:rect.height - (radius << 1) },
|
||||
{ x:x1 - radius + 1, y:y0 + radius, width:radius,
|
||||
height:rect.height - (radius << 1) }
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
/* corner caps */
|
||||
@@ -471,12 +412,7 @@ function software_fill_round_rect(rect, radius)
|
||||
)
|
||||
}
|
||||
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "rects",
|
||||
data: {rects: caps}
|
||||
})
|
||||
add_command("rects", {rects: caps})
|
||||
}
|
||||
|
||||
var rect_def = {
|
||||
@@ -487,13 +423,7 @@ var rect_def = {
|
||||
draw.rectangle = function render_rectangle(rect, def, pipeline) {
|
||||
var opt = def ? {...rect_def, ...def} : rect_def
|
||||
if (opt.color) {
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "set",
|
||||
prop: "drawColor",
|
||||
value: opt.color
|
||||
})
|
||||
add_command("set", null, "drawColor", opt.color)
|
||||
}
|
||||
|
||||
var t = opt.thickness|0
|
||||
@@ -502,12 +432,7 @@ draw.rectangle = function render_rectangle(rect, def, pipeline) {
|
||||
if (opt.radius)
|
||||
software_fill_round_rect(rect, opt.radius)
|
||||
else
|
||||
add_command({
|
||||
kind: "renderer",
|
||||
id: renderer_id,
|
||||
op: "fillRect",
|
||||
data: {rect: rect}
|
||||
})
|
||||
add_command("fillRect", {rect: rect})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user