StyleBackground.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div>
  3. <el-form-item label="背景颜色" label-position="left" label-width="70px">
  4. <!-- 支持变量 -->
  5. <VariableBindWrapper v-model="color" :variable-config="{ type: 'int32_t' }">
  6. <div class="flex items-center">
  7. <ColorModal
  8. v-model:pureColor="pureColor"
  9. v-model:gradientColor="gradientColor"
  10. :useType="useType"
  11. :width="width"
  12. :height="height"
  13. />
  14. <span class="text-text-active">{{
  15. typeof modelValue?.color === 'object' ? '渐变颜色' : modelValue?.color
  16. }}</span>
  17. </div>
  18. </VariableBindWrapper>
  19. </el-form-item>
  20. <el-form-item v-if="!onlyColor" label="背景图片" label-position="left" label-width="70px">
  21. <ImageSelect v-model="image" />
  22. </el-form-item>
  23. <el-form-item v-if="!onlyColor" label="图片透明度" label-position="left" label-width="70px">
  24. <!-- 支持变量 -->
  25. <VariableBindWrapper v-model="imageAlpha" :variable-config="{ type: 'uint8_t' }">
  26. <div class="w-full flex gap-20px items-center">
  27. <el-slider
  28. v-model="imageAlpha"
  29. :disabled="!modelValue?.image"
  30. :max="255"
  31. :min="0"
  32. style="flex: 1"
  33. ></el-slider>
  34. <span class="text-text-active inline w-30px cursor-pointer">
  35. {{ imageAlpha }}
  36. </span>
  37. </div>
  38. </VariableBindWrapper>
  39. </el-form-item>
  40. <el-form-item v-if="!onlyColor" label="图片遮罩" label-position="left" label-width="70px">
  41. <!-- 支持变量 -->
  42. <VariableBindWrapper v-model="imageColor" :variable-config="{ type: 'int32_t' }">
  43. <div class="flex items-center">
  44. <ColorPicker
  45. use-type="pure"
  46. picker-type="chrome"
  47. format="hex8"
  48. v-model:pureColor="imageColor"
  49. :disabled="!modelValue?.image"
  50. />
  51. <span class="text-text-active">{{ imageColor }}</span>
  52. </div>
  53. </VariableBindWrapper>
  54. </el-form-item>
  55. </div>
  56. </template>
  57. <script setup lang="ts">
  58. import { computed } from 'vue'
  59. import { ColorPicker, ColorModal } from '@/components'
  60. import ImageSelect from './ImageSelect.vue'
  61. import { useProjectStore } from '@/store/modules/project'
  62. import VariableBindWrapper from './VariableBindWrapper.vue'
  63. import type { GradientColor } from '@/lvgl-widgets/type'
  64. withDefaults(
  65. defineProps<{
  66. useType?: 'pure' | 'gradient' | 'both'
  67. onlyColor?: boolean
  68. }>(),
  69. {
  70. useType: 'both'
  71. }
  72. )
  73. const modelValue = defineModel<{
  74. color: string | GradientColor
  75. image?: {
  76. imgId: string
  77. recolor: string
  78. alpha: number
  79. }
  80. }>('modelValue')
  81. const projectStore = useProjectStore()
  82. const width = computed(() => {
  83. if (projectStore.activeWidget?.type === 'page') return projectStore.activeScreen?.width
  84. return projectStore.activeWidget?.props?.width
  85. })
  86. const height = computed(() => {
  87. if (projectStore.activeWidget?.type === 'page') return projectStore.activeScreen?.height
  88. return projectStore.activeWidget?.props?.height
  89. })
  90. // 颜色
  91. const color = computed({
  92. get() {
  93. return modelValue.value?.color
  94. },
  95. set(val: string) {
  96. if (modelValue.value) {
  97. modelValue.value.color = val
  98. }
  99. }
  100. })
  101. // 纯色
  102. const pureColor = computed({
  103. get() {
  104. return typeof modelValue.value?.color === 'string' ? modelValue.value?.color : undefined
  105. },
  106. set(val: string) {
  107. if (modelValue.value) {
  108. modelValue.value.color = val
  109. }
  110. }
  111. })
  112. // 渐变
  113. const gradientColor = computed({
  114. get() {
  115. return typeof modelValue.value?.color === 'object' ? modelValue.value?.color : undefined
  116. },
  117. set(val: GradientColor) {
  118. if (modelValue.value?.color) {
  119. modelValue.value.color = val
  120. }
  121. }
  122. })
  123. // 图像颜色
  124. const imageColor = computed({
  125. get() {
  126. return modelValue.value?.image?.recolor || '#ffffff00'
  127. },
  128. set(val: string) {
  129. if (modelValue.value?.image) {
  130. modelValue.value.image.recolor = val
  131. }
  132. }
  133. })
  134. // 图像透明度
  135. const imageAlpha = computed({
  136. get() {
  137. return modelValue.value?.image?.alpha
  138. },
  139. set(val: number) {
  140. if (modelValue.value?.image) {
  141. modelValue.value.image.alpha = val
  142. }
  143. }
  144. })
  145. const image = computed({
  146. get() {
  147. return modelValue.value?.image?.imgId
  148. },
  149. set(val: string) {
  150. if (modelValue.value) {
  151. if (!modelValue.value?.image) {
  152. modelValue.value.image = {
  153. imgId: '',
  154. recolor: '#ffffff00',
  155. alpha: 255
  156. }
  157. }
  158. modelValue.value.image.imgId = val
  159. if (!imageColor.value) {
  160. imageColor.value = '#ffffff00'
  161. }
  162. }
  163. }
  164. })
  165. </script>