Didn't know what else to call it. It's a skeleton demo for a larger project (building automotive wire diagrams) and thought some might find it useful. This will draw lines along the X and Y axis only, no diagonals. Use the left mouse button to drag a line on screen, snapping to grid. Right-click while drawing will alternate the orientation of the line. Ctrl+Z will undo previous lines.
// Project: wire_diagram
// Created: 2024-09-06
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "wire_diagram" )
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
SetSyncRate(0, 0)
UseNewDefaultFonts(1)
Type WirePointObject
x as integer
y as integer
EndType
Type WireObject
points as WirePointObject[]
orientation as integer
EndType
segX = getVirtualWidth()/32
segY = getVirtualHeight()/32
c_grid = makeColor(24, 24, 24)
c_snap = makeColor(255, 128, 0)
c_line = makeColor(255, 0, 0)
wires as WireObject[]
lastId = -1
wireOrientation = 0
do
snapX = round(getRawMouseX() / 32) * 32
snapY = round(getRawMouseY() / 32) * 32
if getRawMouseRightPressed()
wireOrientation = 1 - wireOrientation
endif
if getRawMouseLeftState() = 1
mx = getRawMouseX()
my = getRawMouseY()
if deltaCheck = 0
oldmx = mx
oldmy = my
deltaCheck = 1
endif
if deltaCheck = 1
if abs(oldmx - mx) > 8 OR abs(oldmy - my) > 8
deltaCheck = 2
wireOrientation = 0
endif
endif
if deltaCheck = 2
if snapStart = 0
snapStart = 1
startX = snapX
startY = snapY
else
endX = snapX
endY = snapY
endif
endif
else
deltaCheck = 0
if snapStart = 1
snapStart = 0
if startX <> endX OR startY <> endY
if (startX <> endX) and (startY <> endY)
lastId = addWire(wires, wireOrientation)
if wireOrientation = 0
insertPoint(wires[lastId], startX, startY)
insertPoint(wires[lastId], startX, endY)
insertPoint(wires[lastId], endX, endY)
elseif wireOrientation = 1
insertPoint(wires[lastId], startX, startY)
insertPoint(wires[lastId], endX, startY)
insertPoint(wires[lastId], endX, endY)
endif
else
lastId = addWire(wires, wireOrientation)
insertPoint(wires[lastId], startX, endY)
insertPoint(wires[lastId], endX, endY)
endif
endif
endif
endif
for x = 1 to segX
drawLine(x*32, 0, x*32, 768, c_grid, c_grid)
next x
for y = 1 to segY
drawLine(0, y*32, 1024, y*32, c_grid, c_grid)
next y
for i = 0 to wires.length
if wires[i].points.length = 2
drawLine(wires[i].points[0].x, wires[i].points[0].y, wires[i].points[1].x, wires[i].points[1].y, c_line, c_line)
drawLine(wires[i].points[1].x, wires[i].points[1].y, wires[i].points[2].x, wires[i].points[2].y, c_line, c_line)
else
drawLine(wires[i].points[0].x, wires[i].points[0].y, wires[i].points[1].x, wires[i].points[1].y, c_line, c_line)
endif
for j = 0 to wires[i].points.length
drawEllipse(wires[i].points[j].x, wires[i].points[j].y, 4, 4, c_snap, c_snap, 0)
next j
next i
if deltaCheck = 2
if (startX <> endX) and (startY <> endY)
if wireOrientation = 0
drawLine(startX, startY, startX, endY, c_line, c_line)
drawLine(startX, endY, endX, endY, c_line, c_line)
elseif wireOrientation = 1
drawLine(startX, startY, endX, startY, c_line, c_line)
drawLine(endX, startY, endX, endY, c_line, c_line)
endif
else
drawLine(startX, startY, endX, endY, c_line, c_line)
endif
endif
if getRawKeyState(17) = 1 and getRawKeyState(90)
if undo = 0
undo = 1
if lastId > -1
wires.remove(lastId)
dec lastId
endif
endif
else
undo = 0
endif
drawEllipse(snapX, snapY, 8, 8, c_snap, c_snap, 0)
Sync()
loop
function addWire(w ref as WireObject[], orientation as integer)
local o as WireObject
o.orientation = orientation
w.insert(o)
endfunction w.length
function insertPoint(w ref as WireObject, x as integer, y as integer)
local p as WirePointObject
p.x = x
p.y = y
w.points.insert(p)
endfunction