Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local addComponent = {}
- local interface_s = {
- Components = {}
- }
- interface_s.Components.Window = {}
- interface_s.Components.Button = {}
- interface_s.Components.Selection = {}
- interface_s.Components.SelectionMover = {}
- local windows = {}
- local step = nil
- local click = nil
- local key = nil
- --=================================================================--
- -- THEME START --
- --=================================================================--
- local DefaultTheme = {
- Window = {},
- Button = {},
- Selection = {}
- }
- DefaultTheme.Window = {}
- DefaultTheme.Window.BackColor = gfx.getHexColor(0,0,0,255)
- DefaultTheme.Window.UnfocusedBackColor = gfx.getHexColor(0,0,0,255)
- DefaultTheme.Window.BorderColor = gfx.getHexColor(255,255,255,255)
- DefaultTheme.Window.UnfocusedBorderColor = gfx.getHexColor(150,150,150,255)
- DefaultTheme.Window.HeaderColor = gfx.getHexColor(150,150,255,255)
- DefaultTheme.Window.UnfocusedHeaderColor = gfx.getHexColor(32,32,55,255)
- DefaultTheme.Button.BackColor = gfx.getHexColor(0,0,0,0)
- DefaultTheme.Button.UnfocusedBackColor = gfx.getHexColor(0,0,0,0)
- DefaultTheme.Button.PressedBackColor = gfx.getHexColor(240,240,240,255)
- DefaultTheme.Button.BorderColor = gfx.getHexColor(255,255,255,255)
- DefaultTheme.Button.UnfocusedBorderColor = gfx.getHexColor(150,150,150,255)
- DefaultTheme.Button.MouseOverColor = gfx.getHexColor(255,255,255,128)
- DefaultTheme.Button.UnfocusedMouseOverColor = gfx.getHexColor(150,150,150,128)
- DefaultTheme.Button.TextColor = gfx.getHexColor(255,255,255,255)
- DefaultTheme.Button.UnfocusedTextColor = gfx.getHexColor(150,150,150,255)
- DefaultTheme.Button.PressedTextColor = gfx.getHexColor(0,0,0,255)
- DefaultTheme.Selection.BackColor = gfx.getHexColor(140,140,200,100)
- DefaultTheme.Selection.BorderColor = gfx.getHexColor(140,140,200,255)
- --=================================================================--
- -- THEME END --
- --=================================================================--
- function interface_s.Components.Window:new(x, y, Width, Height, Movable, theme)
- local obj= {}
- obj.X = x
- obj.Y = y
- obj.Width = Width
- obj.Height = Height
- obj.Focused=false
- obj.Items = {}
- obj.MoveStartX = -1
- obj.MoveStartY = -1
- obj.IsMoving = false
- obj.Theme = theme
- obj.IsShowing = true
- obj.IsMovable = Movable
- function obj:Draw(IsFocused,mx,my)
- self.Focused=IsFocused
- if IsFocused == true then
- gfx.fillRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Window.BackColor))
- gfx.drawRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Window.BorderColor))
- if self.IsMovable then
- gfx.fillRect(self.X, self.Y, self.Width,8, gfx.getColors(self.Theme.Window.HeaderColor))
- end
- else
- gfx.fillRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Window.UnfocusedBackColor))
- gfx.drawRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Window.UnfocusedBorderColor))
- if self.IsMovable then
- gfx.fillRect(self.X, self.Y, self.Width,8, gfx.getColors(self.Theme.Window.UnfocusedHeaderColor))
- end
- end
- for i=0,#self.Items do
- if (self.Items[i]~=nil) and (self.Items[i].IsShowing==true) then
- self.Items[i]:Draw(IsFocused,self.X,self.Y,mx-self.X,my-self.Y)
- end
- end
- end
- function obj:Show()
- self.IsShowing = true
- end
- function obj:Hide()
- self.IsShowing = false
- end
- function obj:AddComponent(component)
- self.Items[#self.Items+1] = component
- end
- function obj:Click(x,y,e,b)
- if (self.IsMovable) and (e==3) and (self.IsMoving) then
- self.X = self.X - self.MoveStartX + x
- self.Y = self.Y - self.MoveStartY + y
- self.MoveStartX = x
- self.MoveStartY = y
- else
- if (self.IsMovable) and (e==2) and (self.IsMoving) then
- self.IsMoving = false
- end
- end
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) then
- if self.Focused then
- if (self.IsMovable) and (y>self.Y) and (y<self.Y+8) and (e==1) then
- self.MoveStartX = x
- self.MoveStartY = y
- self.IsMoving = true
- end
- for i=0,#self.Items do
- if (self.Items[i]~=nil) and (self.Items[i].IsShowing==true) then
- if self.Items[i]:Click(x-self.X,y-self.Y,e,b) then
- break
- end
- end
- end
- return true
- else
- if e==1 then
- return true
- end
- end
- else
- if (self.IsMovable) and (e==3) and (self.IsMoving) and (self.IsMoving) then
- self.X = self.X - self.MoveStartX + x
- self.Y = self.Y - self.MoveStartY + y
- self.MoveStartX = x
- self.MoveStartY = y
- return true
- end
- return false
- end
- end
- setmetatable(obj, self)
- self.__index = self;
- return obj
- end
- function interface_s.Components.Button:new(x, y, Width, Height, text, theme)
- local obj= {}
- obj.X = x
- obj.Y = y
- obj.Width = Width
- obj.Height = Height
- obj.Focused=false
- obj.Theme = theme
- obj.IsShowing = true
- obj.Text = text
- obj.TextW,obj.TextH = gfx.textSize(obj.Text)
- obj.IsPressed = false
- obj.Pressed = false
- obj.OnPressed = nil
- function obj:Draw(IsFocused, baseX, baseY,x,y)
- self.Focused=IsFocused
- if IsFocused == true then
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) then
- if self.IsPressed then
- gfx.fillRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.PressedBackColor))
- gfx.drawText(self.X+baseX+(self.Width-self.TextW)/2, self.Y+baseY + (self.Height-5)/2, self.Text, gfx.getColors(self.Theme.Button.PressedTextColor))
- else
- gfx.fillRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.MouseOverColor))
- gfx.drawText(self.X+baseX+(self.Width-self.TextW)/2, self.Y+baseY + (self.Height-5)/2, self.Text, gfx.getColors(self.Theme.Button.TextColor))
- end
- else
- gfx.fillRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.BackColor))
- gfx.drawText(self.X+baseX+(self.Width-self.TextW)/2, self.Y+baseY + (self.Height-5)/2, self.Text, gfx.getColors(self.Theme.Button.TextColor))
- end
- gfx.drawRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.BorderColor))
- else
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) then
- if self.IsPressed then
- gfx.fillRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.PressedBackColor))
- gfx.drawText(self.X+baseX+(self.Width-self.TextW)/2, self.Y+baseY + (self.Height-5)/2, self.Text, gfx.getColors(self.Theme.Button.PressedTextColor))
- else
- gfx.fillRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.UnfocusedMouseOverColor))
- gfx.drawText(self.X+baseX+(self.Width-self.TextW)/2, self.Y+baseY + (self.Height-5)/2, self.Text, gfx.getColors(self.Theme.Button.UnfocusedTextColor))
- end
- else
- gfx.fillRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.UnfocusedBackColor))
- gfx.drawText(self.X+baseX+(self.Width-self.TextW)/2, self.Y+baseY + (self.Height-5)/2, self.Text, gfx.getColors(self.Theme.Button.UnfocusedTextColor))
- end
- gfx.drawRect(self.X+baseX, self.Y+baseY, self.Width,self.Height, gfx.getColors(self.Theme.Button.UnfocusedBorderColor))
- end
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) then
- else
- self.Pressed = false
- self.IsPressed = false
- end
- end
- function obj:Show()
- self.IsShowing = true
- end
- function obj:Hide()
- self.IsShowing = false
- end
- function obj:Click(x,y,e,b)
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) and (self.IsPressed) and (self.Pressed) and (e==2) then
- if self.OnPressed~=nil then
- self.OnPressed()
- end
- self.IsPressed = false
- self.Pressed = false
- end
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) and (self.Pressed) and (e==3) then
- self.IsPressed = true
- end
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) and (e==1) then
- self.Pressed = true
- return true
- else
- return false
- end
- end
- setmetatable(obj, self)
- self.__index = self;
- return obj
- end
- function interface_s.Components.Selection:new(x, y, theme)
- local obj= {}
- obj.X = x
- obj.Y = y
- obj.Focused=false
- obj.Theme = theme
- obj.IsShowing = true
- obj.IsPointSet = false
- obj.FirstPointX = -1
- obj.FirstPointY = -1
- obj.IsFinished = false
- obj.EndPointX = -1
- obj.EndPointY = -1
- obj.OnSelectionStart = nil
- obj.OnSelected = nil
- obj.OnSelectionAborted = nil
- obj.V2EndWidth = -1
- obj.V2EndHeight = -1
- obj.V2StartX = -1
- obj.V2StartY = -1
- function obj:Draw(IsFocused,x,y)
- self.Focused = IsFocused
- if IsFocused then
- if (self.IsPointSet) and (self.IsFinished==false) then
- local arg11 = -1
- local arg12 = -1
- local arg21 = -1
- local arg22 = -1
- if self.FirstPointX-x>=0 then
- arg11 = x
- arg12 = self.FirstPointX-x
- else
- arg11 = self.FirstPointX
- arg12 = x-self.FirstPointX
- end
- if self.FirstPointY - y>=0 then
- arg21 = y
- arg22 = self.FirstPointY - y
- else
- arg21 = self.FirstPointY
- arg22 = y-self.FirstPointY
- end
- if (self.IsFinished==false) and (self.IsPointSet==true) then
- self.V2EndWidth = arg12
- self.V2EndHeight = arg22
- self.V2StartX = arg11
- self.V2StartY = arg21
- end
- gfx.fillRect(arg11, arg21, arg12,arg22, gfx.getColors(self.Theme.Selection.BackColor))
- gfx.drawRect(arg11, arg21, arg12,arg22, gfx.getColors(self.Theme.Selection.BorderColor))
- end
- else
- self.IsPointSet = false
- end
- end
- function obj:Show()
- self.IsShowing = true
- end
- function obj:Hide()
- self.IsShowing = false
- end
- function obj:Click(x,y,e,b)
- if (e==1) and (self.IsPointSet==false) and (b==1) then
- self.IsPointSet = true
- self.FirstPointX = x
- self.FirstPointY = y
- if self.OnSelectionStart ~= nil then
- self.OnSelectionStart(self.FirstPointX,self.FirstPointY)
- end
- return true
- else if (e==2) and (self.IsPointSet==true) and (b==1) and (self.IsFinished == false) then
- self.IsFinished = true
- self.EndPointX = x
- self.EndPointY = y
- if self.OnSelected ~= nil then
- self.OnSelected(self.FirstPointX,self.FirstPointY,self.EndPointX,self.EndPointY, self.V2StartX, self.V2StartY, self.V2EndWidth,self.V2EndHeight)
- end
- return true
- end
- if (self.IsPointSet==true) and (b~=1) and (self.IsFinished==false) then
- self.IsPointSet=false
- if self.OnSelectionAborted ~= nil then
- self.OnSelectionAborted()
- end
- end
- end
- end
- setmetatable(obj, self)
- self.__index = self;
- return obj
- end
- function interface_s.Components.SelectionMover:new(x, y,w,h, theme)
- local obj= {}
- obj.X = x
- obj.Y = y
- obj.Focused=false
- obj.Theme = theme
- obj.IsShowing = true
- obj.Width = w
- obj.Height = h
- obj.OnMovement = nil
- obj.OnAbort = nil
- obj.OnDone = nil
- obj.MoveStartX = -1
- obj.MoveStartY = -1
- obj.NewPosX = -1
- obj.NewPosY = -1
- obj.IsMoving = false
- obj.SumDX = 0
- obj.SumDY = 0
- obj.IsMoved = false
- function obj:Draw(IsFocused,x,y)
- self.Focused = IsFocused
- if IsFocused then
- gfx.fillRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Selection.BackColor))
- gfx.drawRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Selection.BorderColor))
- else
- gfx.fillRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Selection.BackColor))
- gfx.drawRect(self.X, self.Y, self.Width,self.Height, gfx.getColors(self.Theme.Selection.BorderColor))
- end
- end
- function obj:Show()
- self.IsShowing = true
- end
- function obj:Hide()
- self.IsShowing = false
- end
- function obj:Click(x,y,e,b)
- if (e==1) and (b==4) then
- if self.OnAbort ~= nil then
- self.OnAbort(self.SumDX,self.SumDY)
- end
- end
- if (e==3) and (self.IsMoving) then
- if self.OnMovement ~= nil then
- self.OnMovement(-self.MoveStartX + x,- self.MoveStartY + y)
- end
- if (-self.MoveStartX + x ~= 0) and (- self.MoveStartY + y ~= 0) then
- self.IsMoved = true
- end
- self.SumDX = self.SumDX - self.MoveStartX + x
- self.SumDY = self.SumDY - self.MoveStartY + y
- self.X = self.X - self.MoveStartX + x
- self.Y = self.Y - self.MoveStartY + y
- self.MoveStartX = x
- self.MoveStartY = y
- else
- if (e==2) and (self.IsMoving) then
- if self.IsMoved == false then
- if self.OnDone ~= nil then
- self.OnDone(self.SumDX,self.SumDY)
- end
- end
- self.IsMoving = false
- end
- end
- if (x>self.X) and (x<self.X+self.Width) and (y>self.Y) and (y<self.Y+self.Height) then
- if self.Focused then
- if (y>self.Y) and (y<self.Y+self.Height) and (e==1) then
- self.MoveStartX = x
- self.MoveStartY = y
- self.IsMoving = true
- end
- if (e==1) and (b==1) then
- self.IsMoved = false
- end
- return true
- end
- else
- return true
- end
- end
- setmetatable(obj, self)
- self.__index = self;
- return obj
- end
- local function addLeft(obj)
- for i=#windows+1,1,-1 do
- windows[i]=windows[i-1]
- end
- windows[0]=obj
- end
- local function addComponent(obj)
- addLeft(obj)
- end
- local function GetComponentIndex(obj)
- for i=0, #windows do
- if windows[i] == obj then
- return i
- end
- end
- return -1
- end
- local function RemoveComponent(obj)
- local index = GetComponentIndex(obj)
- if index~=-1 then
- for i=index, #windows-1 do
- windows[i]=windows[i+1]
- end
- windows[#windows] = nil
- end
- end
- local function focusComponent(index)
- local temp= windows[index]
- for i=index, 1,-1 do
- windows[i] = windows[i-1]
- end
- windows[0] = temp
- end
- --=================================================================--
- -- CODE IS BELOW THIS LINE --
- --=================================================================--
- function layer()
- Layer()
- end
- function Layer()
- tpt.log("Press \"Select\" to start.")
- tpt.register_step(step)
- tpt.register_mouseclick(click)
- tpt.register_keypress(key)
- local pause = false
- if tpt.set_pause() == 1 then
- pause = true
- else
- pause = false
- end
- tpt.set_pause(1)
- local test = interface_s.Components.Window:new(10, 10, 60, 60,true, DefaultTheme)
- addComponent(test)
- local testButton = interface_s.Components.Button:new(5, 10, 50, 15,"Select", DefaultTheme)
- local Exit = interface_s.Components.Button:new(5, 40, 50, 15,"Exit", DefaultTheme)
- local selectionMover = nil
- local selection = nil
- Exit.OnPressed = (function()
- RemoveComponent(test)
- RemoveComponent(selectionMover)
- RemoveComponent(selection)
- tpt.unregister_step(step)
- tpt.unregister_mouseclick(click)
- tpt.unregister_keypress(key)
- if pause == true then
- tpt.set_pause(1)
- else
- tpt.set_pause(0)
- end
- end)
- testButton.OnPressed = (function()
- tpt.log("Select area that you need to layer.")
- if selectionMover ~= nil then
- RemoveComponent(selectionMover)
- end
- selection = interface_s.Components.Selection:new(50, 50, DefaultTheme)
- selection.OnSelected = function(x,y,x2,y2,v2x,v2y,v2w,v2h)
- tpt.log("Now move the selection on the other particles. Press RMB to cancel. Press LMB to finish")
- RemoveComponent(selection)
- local particlesIndexes = {}
- for i in sim.parts() do
- local cx,cy = sim.partPosition(i)
- if (cx>v2x) and (cx<v2x+v2w) and (cy>v2y) and (cy<v2y+v2h) then
- particlesIndexes[#particlesIndexes+1]=i
- end
- end
- local selectionMover = interface_s.Components.SelectionMover:new(v2x,v2y,v2w,v2h, DefaultTheme)
- selectionMover.OnDone = (function()
- RemoveComponent(selectionMover)
- end)
- selectionMover.OnAbort = (function(tdx,tdy)
- for i=0, #particlesIndexes do
- if particlesIndexes[i]~=nil then
- local cx,cy = sim.partPosition(particlesIndexes[i])
- sim.partPosition(particlesIndexes[i],cx-tdx,cy-tdy)
- end
- end
- RemoveComponent(selectionMover)
- end)
- selectionMover.OnMovement = (function(xd,yd)
- for i=0, #particlesIndexes do
- if particlesIndexes[i]~=nil then
- local cx,cy = sim.partPosition(particlesIndexes[i])
- sim.partPosition(particlesIndexes[i],cx+xd,cy+yd)
- end
- end
- end)
- addComponent(selectionMover)
- end
- selection.OnSelectionAborted = function()
- RemoveComponent(selection)
- end
- addComponent(selection)
- end)
- test:AddComponent(testButton)
- test:AddComponent(Exit)
- end
- --=================================================================--
- -- CODE IS ABOVE THIS LINE --
- --=================================================================--
- step = function()
- for i=#windows, 0,-1 do
- if (windows[i] ~= nil) and (windows[i].IsShowing==true) then
- if i==0 then
- windows[i]:Draw(true,tpt.mousex,tpt.mousey)
- else
- windows[i]:Draw(false,tpt.mousex,tpt.mousey)
- end
- end
- end
- end
- click = function(mousex, mousey, button, event)
- for i=0,#windows do
- if windows[i] == nil then
- RemoveComponent(windows[i])
- break
- end
- if (windows ~= nil) then
- if windows[i]:Click(mousex , mousey , event, button)then
- if (windows[i]~=nil) and (windows[i].IsShowing~=nil) and (windows[i].IsShowing==true) then
- focusComponent(i)
- break
- end
- end
- end
- end
- return false
- end
- key = function()
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement