Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local text_objects = { --All of our text objects
- [1] = { --Layer by layer: it's necessary to do this in seqential order if we want to draw things correctly
- text = "Layer 1",
- color=colors.orange,
- x1=1,
- y =1
- },
- [2] = {
- text=" Layer 2",
- color=colors.blue,
- x1=1,
- y=2
- },
- [3] = {
- text= "Layer 3",
- color=colors.green,
- x1=1,
- y=3
- },
- [4] = {
- text="Layer 4",
- color=colors.cyan,
- x1=1,
- y=3
- }
- }
- local function draw() --The main draw function
- term.setBackgroundColor(colors.black)
- term.clear()
- for index=#text_objects, 1, -1 do --Start at 4, goto 1
- local ref=text_objects[index] --Our "pointer" to the text object
- ref.x2 = ref.x1+#ref.text --The rightermost boundary of the text object
- term.setBackgroundColor(ref.color)
- term.setCursorPos(ref.x1, ref.y)
- term.write(ref.text)
- end
- end
- local function checkClick(x,y) --Check if we have actually clicked on something
- for index=1, #text_objects do --We can start at 1 and goto 4 now because layer 1 is on top
- local ref=text_objects[index] --Our reference again
- if x>=ref.x1 and x<=ref.x2 and y==ref.y then --Check to see if the click is inside of the boundaries
- return index --If it is, then return the index
- end
- end
- return false --Looks like nothing matched. Return false
- end
- local function moveTextObject(index, clickX, dragX, dragY) --Move the text object to a new position
- local ref = text_objects[index]
- ref.x1 = ref.x1 + (dragX-clickX) --Make sure that we don't have jumping text objects!
- ref.y = dragY
- end
- local selector = {false} --All of the data about the "selected" text object
- while true do
- draw() --Draw first so we can see what we're clicking
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" then
- selector = {checkClick(e[3], e[4]), e[3]} --Get the data about the click and store it inside of selector
- elseif e[1] == "mouse_drag" then
- if selector[1] then
- moveTextObject(selector[1], selector[2], e[3], e[4]) --Move the text object if there selector[1] ~= false or nil
- selector[2] = e[3] --Since we've moved the text object, our old clickX will no longer apply so we have to set it to the dragX
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement