File size: 7,664 Bytes
b6a38d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
----- XEditorBrushTool
--
-- Implements the following functionality:
-- 1. Calls StartDraw, Draw, EndDraw, passing brush position in world coordinates
-- 2. Supports horizontal/vertical snapping when holding Shift
-- 3. Supports drawing a brush cursor
DefineClass.XEditorBrushTool = {
__parents = { "XEditorTool" },
properties = {
{ id = "Size", editor = "number", default = 30 * guim, scale = "m", min = const.HeightTileSize, max = 300 * guim, step = guim / 10,
slider = true, persisted_setting = true, auto_select_all = true, sort_order = -1, exponent = 3, },
},
UsesCodeRenderables = true,
first_pos = false,
last_pos = false,
snap_axis = 0,
invalid_box = false,
cursor_mesh = false,
cursor_circles = 2,
cursor_max_tiles = const.SlabSizeZ and (const.MaxTerrainHeight / const.SlabSizeZ) or 100,
cursor_tile_size = const.SlabSizeX,
cursor_verts = 100,
cursor_default_flags = const.mfOffsetByTerrainCursor + const.mfTerrainDistorted + const.mfWorldSpace,
}
function XEditorBrushTool:Init()
DelayedCall(0, self.CreateCursor, self)
end
function XEditorBrushTool:Done()
if self.last_pos then -- destroyed while drawing
self:OnMouseButtonUp()
end
self:DestroyCursor()
end
----- Drawing
function XEditorBrushTool:GetWorldMousePos()
return GetTerrainCursor():SetInvalidZ()
end
function XEditorBrushTool:OnMouseButtonDown(pt, button)
if button == "L" then
self.snap_axis = 0
self.first_pos = self:GetWorldMousePos()
self.last_pos = self.first_pos
self.invalid_box = editor.GetSegmentBoundingBox(self.last_pos, self.last_pos, self:GetAffectedRadius(), self:IsCursorSquare())
self:StartDraw(self.last_pos)
self:Draw(self.last_pos, self.last_pos)
self.desktop:SetMouseCapture(self)
ForceHideMouseCursor("XEditorBrushTool")
return "break"
end
return XEditorTool.OnMouseButtonDown(self, pt, button)
end
function XEditorBrushTool:GetWorldPos()
local pos = self:GetWorldMousePos()
if terminal.IsKeyPressed(const.vkShift) then
pos = self:SnapPosToAxis(pos)
end
return pos
end
function XEditorBrushTool:OnMousePos(pt, button)
if self.last_pos then
local pos = self:GetWorldPos()
self.invalid_box:InplaceExtend(editor.GetSegmentBoundingBox(self.last_pos, pos, self:GetAffectedRadius(), self:IsCursorSquare()))
self:Draw(self.last_pos, pos)
self.last_pos = pos
return "break"
end
return XEditorTool.OnMousePos(self, pt, button)
end
function XEditorBrushTool:OnMouseButtonUp(pt, button)
if self.last_pos then
self.desktop:SetMouseCapture() -- calls OnCaptureLost below
return "break"
end
return XEditorTool.OnMouseButtonUp(self, pt, button)
end
function XEditorBrushTool:OnCaptureLost()
local pos = self:GetWorldPos()
self.invalid_box:InplaceExtend(editor.GetSegmentBoundingBox(self.last_pos, pos, self:GetAffectedRadius(), self:IsCursorSquare()))
self:EndDraw(self.last_pos, pos, self.invalid_box)
UnforceHideMouseCursor("XEditorBrushTool")
self.last_pos = false
end
function XEditorBrushTool:SnapPosToAxis(pos)
local x0, y0 = self.first_pos:xy()
local x1, y1 = pos:xy()
if self.snap_axis == 0 then
local dx, dy = abs(x1 - x0), abs(y1 - y0)
if dx > guim and dy < guim then
self.snap_axis = 1
elseif dy > guim and dx < guim then
self.snap_axis = 2
elseif dx > guim and dy > guim then
self.snap_axis = dx > dy and 1 or 2
end
end
if self.snap_axis == 1 then
return pos:SetY(y0)
elseif self.snap_axis == 2 then
return pos:SetX(x0)
end
return pos
end
----- Shortcuts
function XEditorBrushTool:OnShortcut(shortcut, source, ...)
local key = string.gsub(shortcut, "^Shift%-", "") -- ignore Shift, use it to decrease step size
local divisor = terminal.IsKeyPressed(const.vkShift) and 10 or 1
if shortcut == "Shift-MouseWheelFwd" then
self:SetSize(self:GetSize() + (self:IsCursorSquare() and const.SlabSizeX or guim * (self:GetSize() < 10 * guim and 1 or 5)))
return "break"
elseif shortcut == "Shift-MouseWheelBack" then
self:SetSize(self:GetSize() - (self:IsCursorSquare() and const.SlabSizeX or guim * (self:GetSize() <= 10 * guim and 1 or 5)))
return "break"
elseif key == "]" then
self:SetSize(self:GetSize() + (self:IsCursorSquare() and const.SlabSizeX or guim / divisor))
return "break"
elseif key == "[" then
self:SetSize(self:GetSize() - (self:IsCursorSquare() and const.SlabSizeX or guim / divisor))
return "break"
end
-- don't change tool modes, allow undo, etc. while in the process of dragging
if terminal.desktop:GetMouseCapture() and shortcut ~= "Ctrl-F1" and shortcut ~= "Escape" then
return "break"
end
return XEditorTool.OnShortcut(self, shortcut, source, ...)
end
----- Cursor
function XEditorBrushTool:CreateCursor()
if self.cursor_mesh then
self:DestroyCursor()
end
local cursor = Mesh:new()
cursor:SetShader(ProceduralMeshShaders.mesh_linelist)
self.cursor_mesh = cursor
self:UpdateCursor()
self:CreateThread("UpdateCursorThread", function()
while true do
self:UpdateCursor()
Sleep(100)
end
end)
self:OnCursorCreate(self.cursor_mesh)
end
function XEditorBrushTool:OnCursorCreate(cursor_mesh)
end
function XEditorBrushTool:CreateCircleCursor()
local vpstr = pstr("")
local cursor_verts = self.cursor_verts
local inner_rad, outer_rad = self:GetCursorRadius()
vpstr = AppendCircleVertices(nil, nil, inner_rad, self:GetCursorColor())
vpstr = AppendCircleVertices(vpstr, nil, outer_rad, self:GetCursorColor())
return vpstr
end
function XEditorBrushTool:CreateSquareCursor()
local vpstr = pstr("")
local inner_rad, outer_rad = self:GetCursorRadius()
local tilesize = self.cursor_tile_size
local tiles = outer_rad * 2 / tilesize
local offset_x, offset_y, offset_xy = point(tilesize, 0, 0), point(0, tilesize, 0), point(tilesize, tilesize, 0)
for x = 0, tiles - 1 do
for y = 0, tiles - 1 do
local start_pt = point(x * tilesize - outer_rad, y * tilesize - outer_rad, 0)
vpstr:AppendVertex(start_pt, self:GetCursorColor(), 0)
vpstr:AppendVertex(start_pt + offset_x)
vpstr:AppendVertex(start_pt)
vpstr:AppendVertex(start_pt + offset_y)
if x == tiles - 1 then
vpstr:AppendVertex(start_pt + offset_x)
vpstr:AppendVertex(start_pt + offset_xy)
end
if y == tiles - 1 then
vpstr:AppendVertex(start_pt + offset_y)
vpstr:AppendVertex(start_pt + offset_xy)
end
end
end
return vpstr
end
function XEditorBrushTool:UpdateCursor()
local v_pstr
if self:IsCursorSquare() then
v_pstr = self:CreateSquareCursor()
else
v_pstr = self:CreateCircleCursor()
end
local strength = self:GetCursorHeight()
if strength then
v_pstr:AppendVertex(point(0, 0, 0))
v_pstr:AppendVertex(point(0, 0, strength))
end
self.cursor_mesh:SetMeshFlags(self.cursor_default_flags + self:GetCursorExtraFlags())
self.cursor_mesh:SetMesh(v_pstr)
self.cursor_mesh:SetPos(GetTerrainCursor())
self.cursor_mesh:SetGameFlags(const.gofAlwaysRenderable)
end
function XEditorBrushTool:DestroyCursor()
if not self.cursor_mesh then return end
self:DeleteThread("UpdateCursorThread")
DoneObject(self.cursor_mesh)
self.cursor_mesh = nil
end
----- Overrides
function XEditorBrushTool:StartDraw(pt)
end
function XEditorBrushTool:Draw(pt1, pt2)
end
function XEditorBrushTool:EndDraw(pt1, pt2)
end
function XEditorBrushTool:GetCursorRadius()
return 5 * guim, 5 * guim
end
function XEditorBrushTool:GetAffectedRadius()
local _, outer_radius = self:GetCursorRadius()
return outer_radius
end
function XEditorBrushTool:GetCursorColor()
return RGB(255, 255, 255)
end
function XEditorBrushTool:GetCursorHeight()
end
function XEditorBrushTool:IsCursorSquare()
return false
end
function XEditorBrushTool:GetCursorExtraFlags()
return 0
end
|