diff options
Diffstat (limited to 'Data/Libraries/GameLab/Editor/Window/SplitWindow.lua')
-rw-r--r-- | Data/Libraries/GameLab/Editor/Window/SplitWindow.lua | 130 |
1 files changed, 89 insertions, 41 deletions
diff --git a/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua b/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua index 9dd6446..8f126a9 100644 --- a/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua +++ b/Data/Libraries/GameLab/Editor/Window/SplitWindow.lua @@ -1,31 +1,38 @@ local Debug = GameLab.Debug local EEventType = GameLab.Events.EEventType -local Rect = require("GameLab.Engine.Math.Rect") -local GUI = require("GameLab.Engine.GUI") +local Math = require "GameLab.Engine.Math" +local GUI = require "GameLab.Engine.GUI" +local inspect = require "inspect" + +local Rect = Math.Rect local Splitter = GameLab.Class("GameLab.Editor.Window.Internal.Splitter") Splitter.Ctor = function(self, value) - self.size = 10 + self.size = 20 self.value = value -- [0-1] 位置 end --- 抽象的窗口,用来处理布局 -local SplitWindow = GameLab.GlobalClass("GameLab.Editor.Window.SplitWindow") - local ESplitMode = GameLab.GlobalEnum("GameLab.Editor.Window.ESplitMode", { "Horizontal", -- 水平划分 "Vertical", -- 垂直划分 }) +-- 布局状态 +local SplitState = { + currentSplitter = nil +} + +-- 抽象的窗口,用来处理布局 +local SplitWindow = GameLab.GlobalClass("GameLab.Editor.Window.SplitWindow") + SplitWindow.Ctor = function(self, mode, splitter) + self.m_ContainerWindow = nil self.m_SplitMode = mode self.m_Parent = nil -- 父节点是一个split window或者空 - self.m_SubSplit = {} -- 子节点也是split windows - self.m_GUIWindow = {} -- 不包含subSplit的有一个GUIWindow + self.m_SubWindows = {} -- 子窗口,可以是SplitWindow或GUIWindow self.m_Splitter = {} - self.m_CurSplitter = nil - self.m_ContainerWindow = nil + self.m_Position = Rect.New() if splitter ~= nil and type(splitter) == "table" then for _, v in ipairs(splitter) do local sp = Splitter.New(v) @@ -43,50 +50,86 @@ SplitWindow.DoSplit = function(self, event) local id = GUI.GetControlID() if event.type == EEventType.MouseDown then - + local bHandled = false + for i, sp in ipairs(self.m_Splitter) do + local rect = Rect.New() + if self.m_SplitMode == ESplitMode.Horizontal then + local x = sp.value * self.m_Position.width + self.m_Position.x - sp.size / 2 + local w = sp.size + rect.x = x + rect.y = self.m_Position.y + rect.width = w + rect.height = self.m_Position.height + else + local y = sp.value * self.m_Position.height + self.m_Position.y - sp.size / 2 + local h = sp.size + rect.x = self.m_Position.x + rect.y = y + rect.width = self.m_Position.width + rect.height = h + end + if rect:Contains(event.mousePosition) then + SplitState.currentSplitter = sp + GUI.SetHotControl(id) + bHandled = true + break + end + end elseif event.type == EEventType.MouseDrag then - + local hot = GUI.GetHotControl() + if hot == id then + local splitter = SplitState.currentSplitter + if splitter ~= nil then + local mousePos = event.mousePosition + mousePos.x = mousePos.x - self.m_Position.x + mousePos.y = mousePos.y - self.m_Position.y + if self.m_SplitMode == ESplitMode.Horizontal then + splitter.value = mousePos.x / self.m_Position.width + else + splitter.value = mousePos.y / self.m_Position.height + end + splitter.value = Math.Clamp(splitter.value, 0, 1) + self:SetPosition(self.m_Position) + end + end elseif event.type == EEventType.MouseUp then - + if GUI.GetHotControl() == id then + SplitState.currentSplitter = nil + end end end --- 返回在containerWindow下的像素大小和位置 -SplitWindow.GetPosition = function(self) - local position = Rect.New() - local parent = self.GetParent - if parent == nil then --最顶层的split window - position.x = 0 - position.y = 0 - position.width = self.m_ContainerWindow:GetWidth() - position.height = self.m_ContainerWindow:GetHeight() - return position - end - local parentPos = parent:GetPosition() - local index = self:GetIndexOfParent() - -- 前后的splitter - local prev = index > 1 and parent.m_Splitter[index - 1] or nil - local next = index <= #parent.m_Splitter and parent.m_Splitter[index] or nil - local offset = prev ~= nil and prev.value or 0 - local pv = offset - local nv = next ~= nil and next.value or 1 - local size = nv - pv - position:CopyFrom(parentPos) - if parent.m_SplitMode == ESplitMode.Horizontal then - position.x = position.x + offset - position.width = position.width * size - else - position.y = position.y + offset - position.height = position.height * size +-- 设置在containerwindow中的位置和大小 +SplitWindow.SetPosition = function(self, position) + self.m_Position:CopyFrom(position) + for i, subWindow in ipairs(self.m_SubWindows) do + local prev = i > 1 and self.m_Splitter[i-1].value or 0 + local next = i <= #self.m_Splitter and self.m_Splitter[i].value or 1 + local pos = Rect.New() + pos:CopyFrom(position) + if self.m_SplitMode == ESplitMode.Horizontal then + pos.x = pos.x + prev * position.width + pos.width = position.width * (next - prev) + else + pos.y = pos.y + prev * position.height + pos.height = position.height * (next - prev) + end + subWindow:SetPosition(pos) end end +SplitWindow.GetPosition = function(self) + local pos = Rect.New() + pos:CopyFrom(self.m_Position) + return pos +end + -- 返回guiWnd SplitWindow.GetIndexOfParent = function(self) if self.m_Parent == nil then return 1 end - for i, sp in ipairs(self.m_Parent.m_SubSplit) do + for i, sp in ipairs(self.m_Parent.m_SubWindows) do if sp == self then return i end @@ -97,4 +140,9 @@ SplitWindow.GetParent = function(self) return self.m_Parent end +SplitWindow.AddSubWindow = function(self, subWindow) + table.insert(self.m_SubWindows, subWindow) + subWindow.m_Parent = self +end + return SplitWindow
\ No newline at end of file |