Skip to content
Milind Gupta edited this page Feb 5, 2020 · 10 revisions

Documentation

NEEDS TO BE UPDATED WITH THE UPDATED API

Overview

lua-gl is a graphical library using IUP extensible by Lua to allow easy creation of software like a schematic editor/flowchart creator/mind maps/block diagrams in Lua. The library is able to create basic mechanisms and graphic checks to create custom blocks and interconnections and provide an API to use these to create complex interactions like hierarchical schematic editors, etc.

Table of Contents

Getting started

First of all, you should have an IUP and CD library on your local system.

Download IUP from link

Download CD from link

Install lua-gl using luarocks

luarocks install lua-gl

Importing the library and creating a canvas object

require("iuplua")
require("iupluaimglib")
require("cdlua")
require("iupluacd")


LGL = require("lua-gl")

cnvobj = LGL.new{ grid_x = 40, grid_y = 40, width = 600, height = 300, gridVisibility = true}  

here new function takes a table as arguments. The table which contains information about the canvas. The table must contain this five-element grid_x, grid_y, width, height, gridVisibility.

Canvas object functions

drawObj

cnvobj:drawObj("LINE")

drawObj function takes a string as an input. A string can be "LINE", "RECT", "FILLEDRECT", "ELLIPSE", "FILLEDELLIPSE". This function draws Shape according to the input string.

save

 local str = cnvobj:save()

save function return string which contains all information about the shapes which is drawn on the canvas. we can use this string in load function.

erase

cnvobj:erase()

using erase function we can clear the canvas or we can erase all shapes which are drawn on the canvas.

load

cnvobj:load(str)

the load function is used to load shapes on the canvas. this function takes one string as an input. the string must contain all the information about the shapes. This string is the same as a string return by save() function

whichShape

cnvobj:whichShape(x,y)

function whichShape takes two arguments x, y. If the point(x,y) lies on any shape which is drawn on the canvas then this function return ShapeID. ShapeID is unique for every shape. ShapeID is a positive integer.

groupShapes

cnvobj:grouopShapes({1,2,3}) or cnvobj:groupShapes{1,2,3}

groupShapes is used to group Shapes. This function takes a table as an input. groupShapes function group all the shape which shapeId is given in the table.

addHook

cnvobj:addHook("MOUSECLICKPOST", function(x,y,button,status)
		                    shapeID = cnvobj.whichShape(x,y)
				    shapeList[#shapeList + 1] = shapeID
				end
              )

addHook takes two arguments first one is string and second is function. first argument string is work as a key for a second argument function. The first argument string can be "MOUSECLICKPOST" or "MOUSECLICKPRE". "MOUSECLICKPREE" value pair function run just before mouse button press or release. "MOUSECLICKPRE" value pair function run at the beginning of the button_cb. value pair function run at the end of button_cb. button_cb is a function which just calls when the button is pressed or released.

addPort

cnvobj:addPort(x,y,shapeID)

addPort function takes three arguments as input first and second is x,y and third are shapeID. If the point (x,y) lies on the shape which shapeID is given then this function adds the port else it will not add port.

canvas object member variables

width and height

width and height are the positive integers. this is for canvas width and height. width and height will set when we create a canvas object. we can change canvas width and height

cnvobj.width = 400
cnvobj.height = 400

grid_x and grid_y

grid_x and grid_y are positive integers. grid_x is grid size in x-direction and grid_y is grid size in the y-direction. grid_x and grid_y are set when we create a canvas object. we can change grid_x and grid_y

cnvobj.grid_x = 10
cnvobj.grid_y = 10

snapGrid

snapGrid is a boolean variable. snapGrid is used to snap the shape to the grid. by default snapGrid is false but we can change it

cnvobj.snapGrid = true or cnvobj.snapGrid = false

gridVisibility

gridVisibility is a boolean variable. this is set as true/false when we create a canvas object. we can change gridVisibility

cnvobj.gridVisibility = true or cnvobj.gridVisibility

drawing

drawing is a variable which used to store a string. The string can be "START", "STOP", "LOAD", "CLICK", "CONNECTOR". when user draw any shape using then cnvobj:drawObj("LINE") then cnvobj.drawing will become "START". After drawing a shape cnvobj.drawing will become "STOP". when the user uses cnvobj:load(str) then cnvobj.drawing will become "LOAD". When the user clicks on any shape this will become "CLICK". User can draw the connector using this variable. He has to set this variable equal to the "CONNECTOR".

cnvobj.drawing = "CONNECTOR"

Demo project example

I have built a demo project using Lua-GL to create a very crude schematic editor. Code

Watch the video for more link