그리드 임의 설정 흡착점 작업, 그리드 삭제 추가

This commit is contained in:
yjnoh 2024-08-26 12:36:15 +09:00
parent 39e8dc335e
commit 3871c7d0d1
3 changed files with 90 additions and 141 deletions

View File

@ -80,7 +80,6 @@ export default function Roof2(props) {
setMode,
changeMode,
handleClear,
fillCellInPolygon,
zoomIn,
zoomOut,
zoom,
@ -580,7 +579,7 @@ export default function Roof2(props) {
>
그리드 설정
</Button>
<Button className="m-1 p-2" color={`${mode === Mode.DEFAULT ? 'primary' : 'default'}`} onClick={fillCellInPolygon}>
<Button className="m-1 p-2" color={`${mode === Mode.DEFAULT ? 'primary' : 'default'}`} onClick={() => setMode(Mode.DEFAULT)}>
모드 DEFAULT
</Button>
<Button className="m-1 p-2" color={`${mode === Mode.DRAW_LINE ? 'primary' : 'default'}`} onClick={() => setMode(Mode.DRAW_LINE)}>

View File

@ -9,69 +9,50 @@ export default function SettingsModal(props) {
const { canvasProps } = props
const [isCustomGridSetting, setIsCustomGridSetting] = useState(true)
const [gridCheckedValue, setGridCheckValue] = useState([])
const [ratioValue, setRatioValue] = useState('원치수')
const moduleLength = useRef(90) // mm
const [ratioValue, setRatioValue] = useState('1')
const moduleLength = useRef(null) // mm
const customModuleHoriLength = useRef(null)
const customModuleVertLength = useRef(null)
const [open, setOpen] = useRecoilState(modalState)
const [guideLine, setGuideLine] = useRecoilState(guideLineState)
const gridSettingArray = []
useEffect(() => {
moduleLength.current.value = 90
customModuleHoriLength.current.value = 90
customModuleVertLength.current.value = 90
}, [])
useEffect(() => {
setIsCustomGridSetting(ratioValue !== 'custom')
}, [ratioValue])
const drawGridSettings = () => {
const gridSettingArray = []
//
if (!(Object.keys(guideLine).length === 0 && guideLine.constructor === Object)) {
gridSettingArray.push(...guideLine)
}
let moduleHoriLength = moduleLength.current.value //
let moduleVertLength = moduleLength.current.value //
if (ratioValue === 'custom') {
moduleHoriLength = customModuleHoriLength.current.value
moduleVertLength = customModuleVertLength.current.value
} else {
moduleHoriLength = moduleHoriLength / ratioValue
moduleVertLength = moduleVertLength / ratioValue
}
if (gridCheckedValue.includes('line')) {
// fabric.GuideRect = fabric.util.createClass(fabric.Rect, {
// _render: function (ctx) {
// this.callSuper('_render', ctx)
// ctx.strokeStyle = this.stroke || 'blue'
// ctx.lineWidth = this.strokeWidth || 1
// ctx.beginPath()
// ctx.moveTo(-this.width / 2, -this.height / 2)
// ctx.lineTo(this.width / 2, -this.height / 2)
// ctx.lineTo(this.width / 2, this.height / 2)
// ctx.stroke()
// },
// })
// const guideRect = new fabric.GuideRect({
// width: canvasProps.width,
// height: canvasProps.height,
// stroke: 'blue',
// strokeWidth: 1,
// selectable: false,
// fill: 'transparent',
// name: 'guideRect',
// })
// const patternSourceCanvas = new fabric.StaticCanvas(null, {
// width: moduleLength.current.value,
// height: moduleLength.current.value,
// backgroundColor: 'white',
// })
// patternSourceCanvas.add(guideRect)
// patternSourceCanvas.renderAll()
// const pattern = new fabric.Pattern({
// source: patternSourceCanvas.getElement(),
// repeat: 'repeat',
// })
const horizontalLineArray = []
const verticalLineArray = []
for (let i = 0; i < canvasProps.height / moduleLength.current.value + 1; i++) {
for (let i = 0; i < canvasProps.height / moduleVertLength + 1; i++) {
const horizontalLine = new fabric.Line(
[
0,
i * moduleLength.current.value - moduleLength.current.value / 2,
canvasProps.width,
i * moduleLength.current.value - moduleLength.current.value / 2,
],
[0, i * moduleVertLength - moduleVertLength / 2, canvasProps.width, i * moduleVertLength - moduleVertLength / 2],
{
stroke: 'gray',
strokeWidth: 1,
@ -83,14 +64,9 @@ export default function SettingsModal(props) {
horizontalLineArray.push(horizontalLine)
}
for (let i = 0; i < canvasProps.width / moduleLength.current.value + 1; i++) {
for (let i = 0; i < canvasProps.width / moduleHoriLength + 1; i++) {
const verticalLine = new fabric.Line(
[
i * moduleLength.current.value - moduleLength.current.value / 2,
0,
i * moduleLength.current.value - moduleLength.current.value / 2,
canvasProps.height,
],
[i * moduleHoriLength - moduleHoriLength / 2, 0, i * moduleHoriLength - moduleHoriLength / 2, canvasProps.height],
{
stroke: 'gray',
strokeWidth: 1,
@ -105,60 +81,12 @@ export default function SettingsModal(props) {
const snapDistance = 10
canvasProps.on('mouse:move', function (e) {
let mouseObj = e
const mouseCursorX = e.pointer.x
const mouseCursorY = e.pointer.y
horizontalLineArray.forEach((line) => {
if (Math.abs(mouseCursorY - line.y1) < snapDistance) {
mouseObj.x = mouseCursorX
mouseObj.y = line.y1
}
// if (Math.abs(mouseCursorX - line.x2) < snapDistance) {
// line.set({ x2: mouseCursorX })
// }
})
})
// canvasProps.on('mouse:move', (e) => {
// console.log('event', e.pointer)
// const obj = e
// if (!obj) return
// canvasProps.forEachObject(function (target) {
// if (target.name === 'guideLine' || target.name === 'guideDot') {
// console.log('line : ', target)
// const lineStartX = target.x1,
// lineStartY = target.y1
// const lineEndX = target.x2,
// lineEndY = target.y2
// console.log('target.x1', target.x1)
// console.log('target.y1', target.y1)
// console.log(`obj.pointer.y : ${obj.pointer.y}, obj.pointer.x : ${obj.pointer.x}`)
// //
// if (Math.abs(obj.pointer.y - lineStartY) < snapDistance) {
// obj.pointer.y = lineStartY
// }
// //
// if (Math.abs(obj.pointer.x - lineStartX) < snapDistance) {
// obj.pointer.x = lineStartX
// }
// }
// })
// })
const recoilObj = {
guideMode: 'guideLine',
horizontalLineArray,
verticalLineArray,
moduleLength: moduleLength.current.value,
moduleVertLength: moduleVertLength,
moduleHoriLength: moduleHoriLength,
}
gridSettingArray.push(recoilObj)
}
@ -175,12 +103,11 @@ export default function SettingsModal(props) {
lockRotation: true,
lockScalingX: true,
lockScalingY: true,
name: 'guideDot',
})
const patternSourceCanvas = new fabric.StaticCanvas(null, {
width: moduleLength.current.value,
height: moduleLength.current.value,
width: moduleHoriLength,
height: moduleVertLength,
})
patternSourceCanvas.add(circle)
@ -207,6 +134,7 @@ export default function SettingsModal(props) {
{
fill: pattern,
selectable: false,
name: 'guideDot',
},
)
canvasProps.add(backgroundPolygon)
@ -214,7 +142,8 @@ export default function SettingsModal(props) {
const recoilObj = {
guideMode: 'guideDot',
moduleLength: moduleLength.current.value,
moduleVertLength: moduleVertLength,
moduleHoriLength: moduleHoriLength,
}
gridSettingArray.push(recoilObj)
@ -223,27 +152,39 @@ export default function SettingsModal(props) {
setGuideLine(gridSettingArray)
}
const removeGuideLines = () => {
if (!(Object.keys(guideLine).length === 0 && guideLine.constructor === Object)) {
const guideLines = canvasProps._objects.filter((obj) => obj.name === 'guideLine' || obj.name === 'guideDot')
guideLines?.forEach((item) => canvasProps.remove(item))
canvasProps.renderAll()
setGuideLine([])
} else {
alert('그리드가 없습니다.')
return
}
}
return (
<>
<div>
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<label style={{ display: 'block', marginBottom: '5px' }}>
<CheckboxGroup label="그리드 설정" value={gridCheckedValue} onValueChange={setGridCheckValue}>
<CheckboxGroup label="그리드 설정" value={gridCheckedValue} defaultChecked={gridCheckedValue} onValueChange={setGridCheckValue}>
<Checkbox value="dot"> 그리드</Checkbox>
<Checkbox value="line">점선 그리드</Checkbox>
</CheckboxGroup>
</label>
</div>
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<Input type="number" label="모듈" ref={moduleLength} defaultValue="90" />
<Input type="number" label="모듈" ref={moduleLength} />
mm
</div>
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<RadioGroup label="비율 설정" onValueChange={setRatioValue}>
<Radio value="원치수">원치수</Radio>
<Radio value="1/2">1/2</Radio>
<Radio value="1/4">1/4</Radio>
<Radio value="1/10">1/10</Radio>
<RadioGroup label="비율 설정" value={ratioValue} defaultValue={ratioValue} onValueChange={setRatioValue}>
<Radio value="1">원치수</Radio>
<Radio value="2">1/2</Radio>
<Radio value="4">1/4</Radio>
<Radio value="10">1/10</Radio>
<Radio value="custom">임의간격</Radio>
</RadioGroup>
</div>
@ -253,11 +194,11 @@ export default function SettingsModal(props) {
</Checkbox>
</div>
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<Input type="number" label="가로간격" min={0} isDisabled={isCustomGridSetting} />
<Input type="number" label="가로간격" ref={customModuleHoriLength} min={0} isDisabled={isCustomGridSetting} />
mm
</div>
<div className="flex w-full flex-wrap md:flex-nowrap gap-4">
<Input type="number" label="세로간격" min={0} isDisabled={isCustomGridSetting} />
<Input type="number" label="세로간격" ref={customModuleVertLength} min={0} isDisabled={isCustomGridSetting} />
mm
</div>
<div className="flex gap-4 items-center">
@ -268,6 +209,9 @@ export default function SettingsModal(props) {
<Button size="sm" onClick={() => setOpen(!open)}>
취소
</Button>
<Button size="sm" onClick={() => removeGuideLines()}>
그리드 삭제
</Button>
</div>
</>
)

View File

@ -123,9 +123,13 @@ export function useMode() {
}, [guideLineInfo])
const drawMouseLines = (e) => {
console.log('guideLineInfo', guideLineInfo)
let isGuideLineMode = false,
isGuideDotMode = false
let guideDotLength, guideLineLength, horizontalLineArray, verticalLineArray
let guideDotLength, guideLineLengthHori, guideLineLengthVert, horizontalLineArray, verticalLineArray
console.log()
if (isObjectNotEmpty(guideLineInfo)) {
const guideLineState = guideLineInfo.filter((item) => item.guideMode === 'guideLine')
@ -136,13 +140,15 @@ export function useMode() {
isGuideDotMode = guideDotState.length > 0
if (isGuideDotMode) {
guideDotLength = Number(guideDotState[0].moduleLength)
guideLineLengthHori = Number(guideDotState[0].moduleHoriLength)
guideLineLengthVert = Number(guideDotState[0].moduleVertLength)
}
if (isGuideLineMode) {
horizontalLineArray = [...guideLineState[0].horizontalLineArray]
verticalLineArray = [...guideLineState[0].verticalLineArray]
guideLineLength = Number(guideLineState[0].moduleLength)
guideLineLengthHori = Number(guideLineState[0].moduleHoriLength)
guideLineLengthVert = Number(guideLineState[0].moduleVertLength)
}
}
@ -161,17 +167,17 @@ export function useMode() {
const xDiff = Math.abs(pointer.x - closetVerticalLine.x1)
const yDiff = Math.abs(pointer.y - closestHorizontalLine.y1)
const x = pointer.x - guideLineLength * Math.floor(pointer.x / guideLineLength)
const y = pointer.y - guideLineLength * Math.floor(pointer.y / guideLineLength)
const x = pointer.x - guideLineLengthHori * Math.floor(pointer.x / guideLineLengthHori)
const y = pointer.y - guideLineLengthVert * Math.floor(pointer.y / guideLineLengthVert)
const xRate = x / guideLineLength
const yRate = y / guideLineLength
const xRate = x / guideLineLengthHori
const yRate = y / guideLineLengthVert
const isAttachX = xRate >= 0.4 && xRate <= 0.7
const isAttachY = yRate >= 0.4 && yRate <= 0.7
if (isAttachX && isAttachY) {
newX = Math.floor(pointer.x / guideLineLength) * guideLineLength + guideLineLength / 2
newY = Math.floor(pointer.y / guideLineLength) * guideLineLength + guideLineLength / 2
newX = Math.floor(pointer.x / guideLineLengthHori) * guideLineLengthHori + guideLineLengthHori / 2
newY = Math.floor(pointer.y / guideLineLengthVert) * guideLineLengthVert + guideLineLengthVert / 2
} else {
if (Math.min(xDiff, yDiff) <= 20) {
if (xDiff < yDiff) {
@ -184,17 +190,17 @@ export function useMode() {
}
}
} else if (isGuideDotMode && mode === Mode.EDIT) {
const x = pointer.x - guideDotLength * Math.floor(pointer.x / guideDotLength)
const y = pointer.y - guideDotLength * Math.floor(pointer.y / guideDotLength)
const x = pointer.x - guideLineLengthHori * Math.floor(pointer.x / guideLineLengthHori)
const y = pointer.y - guideLineLengthVert * Math.floor(pointer.y / guideLineLengthVert)
const xRate = x / guideDotLength
const yRate = y / guideDotLength
const xRate = x / guideLineLengthHori
const yRate = y / guideLineLengthVert
const isAttachX = xRate >= 0.4 && xRate <= 0.7
const isAttachY = yRate >= 0.4 && yRate <= 0.7
if (isAttachX && isAttachY) {
newX = Math.floor(pointer.x / guideDotLength) * guideDotLength + guideDotLength / 2
newY = Math.floor(pointer.y / guideDotLength) * guideDotLength + guideDotLength / 2
newX = Math.floor(pointer.x / guideLineLengthHori) * guideLineLengthHori + guideLineLengthHori / 2
newY = Math.floor(pointer.y / guideLineLengthVert) * guideLineLengthVert + guideLineLengthVert / 2
}
} else if (isGuideLineMode && mode === Mode.EDIT) {
const closestHorizontalLine = getClosestHorizontalLine(pointer, horizontalLineArray)
@ -202,17 +208,17 @@ export function useMode() {
const xDiff = Math.abs(pointer.x - closetVerticalLine.x1)
const yDiff = Math.abs(pointer.y - closestHorizontalLine.y1)
const x = pointer.x - guideLineLength * Math.floor(pointer.x / guideLineLength)
const y = pointer.y - guideLineLength * Math.floor(pointer.y / guideLineLength)
const x = pointer.x - guideLineLengthHori * Math.floor(pointer.x / guideLineLengthHori)
const y = pointer.y - guideLineLengthVert * Math.floor(pointer.y / guideLineLengthVert)
const xRate = x / guideLineLength
const yRate = y / guideLineLength
const xRate = x / guideLineLengthHori
const yRate = y / guideLineLengthVert
const isAttachX = xRate >= 0.4 && xRate <= 0.7
const isAttachY = yRate >= 0.4 && yRate <= 0.7
if (isAttachX && isAttachY) {
newX = Math.floor(pointer.x / guideLineLength) * guideLineLength + guideLineLength / 2
newY = Math.floor(pointer.y / guideLineLength) * guideLineLength + guideLineLength / 2
newX = Math.floor(pointer.x / guideLineLengthHori) * guideLineLengthHori + guideLineLengthHori / 2
newY = Math.floor(pointer.y / guideLineLengthVert) * guideLineLengthVert + guideLineLengthVert / 2
} else {
if (Math.min(xDiff, yDiff) <= 20) {
if (xDiff < yDiff) {