Skip to content

Commit

Permalink
feat(ui): make color picker movable (#577)
Browse files Browse the repository at this point in the history
Signed-off-by: Mathis Mensing <[email protected]>
  • Loading branch information
matmen authored Mar 16, 2022
1 parent 4cf6f43 commit a8c16af
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
78 changes: 76 additions & 2 deletions src/components/ui/AppColorPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@
$circle
</v-icon>
</template>
<v-card>
<v-card ref="card">
<v-card-title
v-if="title"
class="card-heading mb-2"
style="cursor: move"
@mousedown="startMouseDrag"
@touchstart="startTouchDrag"
@touchmove="pointerMove"
>
{{ title }}
</v-card-title>
<v-card-text>
<v-icon
:color="primaryColor.hexString"
Expand Down Expand Up @@ -120,7 +130,7 @@
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { Component, Vue, Prop, Watch, Ref } from 'vue-property-decorator'
import { Debounce } from 'vue-debounce-decorator'
import iro from '@jaames/iro'
import { IroColor } from '@irojs/iro-core'
Expand All @@ -136,6 +146,11 @@ interface AppColor {
rgb: RgbwColor;
}
interface PointerPosition {
x: number;
y: number;
}
@Component({
components: {}
})
Expand All @@ -155,6 +170,12 @@ export default class AppColorPicker extends Vue {
menu = false
@Ref('card')
card!: Vue
dragging = false
lastPointerPosition: PointerPosition = { x: 0, y: 0 }
primaryColor: AppColor = {
hexString: '#ffffff',
rgb: { r: 255, g: 255, b: 255 }
Expand Down Expand Up @@ -262,6 +283,59 @@ export default class AppColorPicker extends Vue {
debouncedChange (channel: string, color: IroColor) {
this.$emit('change', { channel, color })
}
startMouseDrag (event: MouseEvent) {
this.dragging = true
this.lastPointerPosition = { x: event.clientX, y: event.clientY }
}
stopMouseDrag () {
this.dragging = false
}
startTouchDrag (event: TouchEvent) {
this.lastPointerPosition = { x: event.touches[0].clientX, y: event.touches[0].clientY }
}
relativeMove (newPosition: PointerPosition) {
const parent = this.card.$el.parentElement as HTMLElement
parent.style.left = (parseFloat(parent.style.left) + (newPosition.x - this.lastPointerPosition.x)) + 'px'
parent.style.top = (parseFloat(parent.style.top) + (newPosition.y - this.lastPointerPosition.y)) + 'px'
}
pointerMove (event: TouchEvent | MouseEvent) {
let newPosition
if (event instanceof TouchEvent) {
event.preventDefault()
newPosition = { x: event.touches[0].clientX, y: event.touches[0].clientY }
} else if (this.dragging) {
newPosition = { x: event.clientX, y: event.clientY }
} else {
return
}
this.relativeMove(newPosition)
this.lastPointerPosition = newPosition
}
preventSelection (event: Event) {
if (this.dragging) {
event.preventDefault()
}
}
mounted () {
window.addEventListener('mousemove', this.pointerMove)
window.addEventListener('mouseup', this.stopMouseDrag)
window.addEventListener('selectstart', this.preventSelection)
}
beforeUnmount () {
window.removeEventListener('mousemove', this.pointerMove)
window.removeEventListener('mouseup', this.stopMouseDrag)
window.removeEventListener('selectstart', this.preventSelection)
}
}
</script>

Expand Down
1 change: 1 addition & 0 deletions src/components/widgets/outputs/OutputLed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<app-color-picker
:primary="primaryColor"
:white="whiteColor"
:title="led.prettyName"
dot
@change="handleColorChange"
/>
Expand Down

0 comments on commit a8c16af

Please sign in to comment.