Configurator.vue 9.6 KB
Newer Older
L
LeoKu 已提交
1 2 3 4 5 6 7 8 9 10 11 12
<template>
  <PerfectScrollbar class="configurator-scroll">
    <div class="configurator">
      <SectionWrapper :title="t('label.wrapperShape')">
        <ul class="wrapper-shape">
          <li
            v-for="wrapperShape in SETTINGS.wrapperShape"
            :key="wrapperShape"
            class="wrapper-shape__item"
            :title="t(`wrapperShape.${wrapperShape}`)"
            @click="switchWrapperShape(wrapperShape)"
          >
L
LeoKu 已提交
13
            <div
L
LeoKu 已提交
14 15 16 17 18 19 20 21 22 23 24
              class="shape"
              :class="[
                wrapperShape,
                { active: wrapperShape === avatarOption.wrapperShape },
              ]"
            />
          </li>
        </ul>
      </SectionWrapper>

      <SectionWrapper :title="t('label.backgroundColor')">
L
LeoKu 已提交
25
        <ul class="color-list">
L
LeoKu 已提交
26 27 28
          <li
            v-for="bgColor in SETTINGS.backgroundColor"
            :key="bgColor"
L
LeoKu 已提交
29
            class="color-list__item"
L
LeoKu 已提交
30 31 32 33 34
            @click="switchBgColor(bgColor)"
          >
            <div
              :style="{ background: bgColor }"
              class="bg-color"
L
LeoKu 已提交
35 36 37 38
              :class="{
                active: bgColor === avatarOption.background.color,
                transparent: bgColor === 'transparent',
              }"
L
LeoKu 已提交
39
            ></div>
L
LeoKu 已提交
40 41 42 43 44 45 46 47 48
          </li>
        </ul>
      </SectionWrapper>

      <SectionWrapper
        v-for="s in sections"
        :key="s.widgetType"
        :title="t(`widgetType.${s.widgetType}`)"
      >
L
LeoKu 已提交
49 50 51
        <details
          v-if="
            s.widgetType === WidgetType.Tops ||
L
LeoKu 已提交
52
            s.widgetType === WidgetType.Face ||
L
LeoKu 已提交
53 54 55
            s.widgetType === WidgetType.Clothes
          "
          class="color-picker"
L
LeoKu 已提交
56
          :open="s.widgetType === WidgetType.Face"
L
LeoKu 已提交
57
        >
L
LeoKu 已提交
58
          <summary class="color">{{ t('label.colors') }}</summary>
L
LeoKu 已提交
59 60
          <ul class="color-list">
            <li
L
LeoKu 已提交
61 62 63
              v-for="fillColor in SETTINGS[
                s.widgetType === WidgetType.Face ? 'skinColors' : 'commonColors'
              ]"
L
LeoKu 已提交
64 65 66 67
              :key="fillColor"
              class="color-list__item"
              @click="setWidgetColor(s.widgetType, fillColor)"
            >
68 69 70 71 72 73 74
              <div
                :style="{ background: fillColor }"
                class="bg-color"
                :class="{
                  active: fillColor === getWidgetColor(s.widgetType),
                }"
              />
L
LeoKu 已提交
75 76 77
            </li>
          </ul>
        </details>
L
LeoKu 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91

        <ul class="widget-list">
          <li
            v-for="it in s.widgetList"
            :key="it.widgetShape"
            class="list-item"
            :class="{
              selected:
                it.widgetShape === avatarOption.widgets?.[s.widgetType]?.shape,
            }"
            @click="switchWidget(s.widgetType, it.widgetShape)"
            v-html="it.svgRaw"
          />
        </ul>
L
LeoKu 已提交
92 93 94 95 96 97 98 99 100 101 102
      </SectionWrapper>
    </div>
  </PerfectScrollbar>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'

import PerfectScrollbar from '@/components/PerfectScrollbar.vue'
import SectionWrapper from '@/components/SectionWrapper.vue'
L
LeoKu 已提交
103 104 105 106 107 108
import {
  type WidgetShape,
  type WrapperShape,
  BeardShape,
  WidgetType,
} from '@/enums'
L
LeoKu 已提交
109
import { useAvatarOption } from '@/hooks'
L
LeoKu 已提交
110
import { AVATAR_LAYER, SETTINGS } from '@/utils/constant'
L
LeoKu 已提交
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
import { previewData } from '@/utils/dynamic-data'

const { t } = useI18n()

const [avatarOption, setAvatarOption] = useAvatarOption()

const sectionList = reactive(Object.values(WidgetType))
const sections = ref<
  {
    widgetType: WidgetType
    widgetList: {
      widgetType: WidgetType
      widgetShape: WidgetShape
      svgRaw: string
    }[]
  }[]
>([])

onMounted(() => {
  void (async () => {
    const a = await Promise.all(
      sectionList.map((section) => {
        return getWidgets(section)
      })
    )

    sections.value = sectionList.map((li, i) => {
      return {
        widgetType: li,
        widgetList: a[i],
      }
    })
  })()
})

async function getWidgets(widgetType: WidgetType) {
  const list = SETTINGS[`${widgetType}Shape`]
  // const promises: Promise<string>[] = list.map(async (widget: string) => {
  //   return (await import(`../assets/preview/${widgetType}/${widget}.svg?raw`))
  //     .default
  // })
  const promises: Promise<string>[] = list.map(async (widget: string) => {
    if (widget !== 'none' && previewData?.[widgetType]?.[widget]) {
      return (await previewData[widgetType][widget]()).default
    }
    return 'X'
  })
  const svgRawList = await Promise.all(promises).then((raw) => {
    return raw.map((svgRaw, i) => {
      return {
        widgetType,
        widgetShape: list[i],
        svgRaw,
      }
    })
  })
  return svgRawList
}

function switchWrapperShape(wrapperShape: WrapperShape) {
  if (wrapperShape !== avatarOption.value.wrapperShape) {
    setAvatarOption({ ...avatarOption.value, wrapperShape })
  }
}

function switchBgColor(bgColor: string) {
  if (bgColor !== avatarOption.value.background.color) {
    setAvatarOption({
      ...avatarOption.value,
      background: { ...avatarOption.value.background, color: bgColor },
    })
  }
}

function switchWidget(widgetType: WidgetType, widgetShape: WidgetShape) {
  if (widgetShape && avatarOption.value.widgets?.[widgetType]) {
    setAvatarOption({
      ...avatarOption.value,
      widgets: {
        ...avatarOption.value.widgets,
        [widgetType]: {
          ...avatarOption.value.widgets?.[widgetType],
          shape: widgetShape,
L
LeoKu 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
          ...(widgetShape === BeardShape.Scruff
            ? { zIndex: AVATAR_LAYER['mouth'].zIndex - 1 }
            : undefined),
        },
      },
    })
  }
}

function setWidgetColor(widgetType: WidgetType, fillColor: string) {
  if (avatarOption.value.widgets?.[widgetType]) {
    setAvatarOption({
      ...avatarOption.value,
      widgets: {
        ...avatarOption.value.widgets,
        [widgetType]: {
          ...avatarOption.value.widgets?.[widgetType],
          fillColor,
L
LeoKu 已提交
212 213 214 215 216
        },
      },
    })
  }
}
217 218

function getWidgetColor(type: string) {
L
LeoKu 已提交
219 220 221 222 223
  if (
    type === WidgetType.Face ||
    type === WidgetType.Tops ||
    type === WidgetType.Clothes
  ) {
224 225 226
    return avatarOption.value.widgets[type]?.fillColor
  } else return ''
}
L
LeoKu 已提交
227 228 229
</script>

<style lang="scss" scoped>
230 231
@use 'src/styles/var';

L
LeoKu 已提交
232
.configurator-scroll {
233
  width: var.$layout-sider-width;
L
LeoKu 已提交
234
  height: 100%;
L
LeoKu 已提交
235 236 237 238

  @media screen and (max-width: var.$screen-lg) {
    background-color: var.$color-configurator;
  }
L
LeoKu 已提交
239 240 241 242
}

.configurator {
  width: 100%;
243
  color: var.$color-text;
L
LeoKu 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256

  .wrapper-shape {
    display: flex;
    align-items: center;

    .wrapper-shape__item {
      padding: 0.4rem 0.5rem;
      cursor: pointer;

      .shape {
        display: inline-block;
        width: 1.5rem;
        height: 1.5rem;
257
        background-color: var.$color-text;
L
LeoKu 已提交
258 259 260 261 262 263 264 265 266 267 268
        transition: background-color 0.2s;

        &.circle {
          border-radius: 50%;
        }

        &.squircle {
          border-radius: 20%;
        }

        &.active {
269
          background-color: var.$color-accent;
L
LeoKu 已提交
270 271 272 273 274
        }
      }
    }
  }

L
LeoKu 已提交
275
  .color-picker {
L
LeoKu 已提交
276
    margin: 1rem 0 0.5rem 0;
L
LeoKu 已提交
277 278 279 280 281 282 283 284 285 286

    summary {
      color: darken(var.$color-text, 20);
      font-size: small;
      cursor: pointer;
      user-select: none;
    }
  }

  .color-list {
L
LeoKu 已提交
287 288 289 290
    display: flex;
    flex-wrap: wrap;
    align-items: center;

L
LeoKu 已提交
291
    .color-list__item {
L
LeoKu 已提交
292 293
      position: relative;
      z-index: 1;
L
LeoKu 已提交
294 295
      width: calc(100% / 7);
      padding: 0.6rem 0;
L
LeoKu 已提交
296 297 298 299 300 301 302 303
      cursor: pointer;
      transition: transform 0.2s;

      .bg-color {
        position: relative;
        box-sizing: content-box;
        width: 1.3em;
        height: 1.3em;
L
LeoKu 已提交
304
        margin: 0 auto;
L
LeoKu 已提交
305 306
        font-size: 16px;
        border-radius: 50%;
307
        box-shadow: 0 0 0.05em 0.2em var.$color-configurator;
L
LeoKu 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

        &.transparent {
          background: #fff !important;

          &::after {
            position: absolute;
            top: 50%;
            left: 50%;
            z-index: 1;
            color: #ff4757;
            font-weight: bold;
            font-size: 1.8rem;
            transform: translate(-50%, -50%) scale(0.5);
            opacity: 1;
            content: '\\';
          }
        }
L
LeoKu 已提交
325 326 327 328 329 330 331 332

        &::before {
          position: absolute;
          top: 50%;
          left: 50%;
          z-index: -1;
          width: 100%;
          height: 100%;
L
LeoKu 已提交
333
          background: inherit;
L
LeoKu 已提交
334 335 336 337 338 339 340 341 342 343 344
          border-radius: 50%;
          transform: translate(-50%, -50%);
          opacity: 0.5;
          transition: width 0.15s, height 0.15s;
          content: '';
        }

        &::after {
          position: absolute;
          top: 50%;
          left: 50%;
L
LeoKu 已提交
345
          z-index: 1;
346
          color: var.$color-configurator;
L
LeoKu 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
          font-size: 1.5rem;
          transform: translate(-50%, -50%) scale(0.5);
          opacity: 0;
          transition: opacity 0.15s;
          content: '\2714';
        }

        &.active::before {
          width: 160%;
          height: 160%;
        }

        &.active::after {
          opacity: 1;
        }
      }
    }
  }

  .widget-list {
    display: flex;
    flex-wrap: wrap;

    .list-item {
      display: flex;
      align-items: center;
      justify-content: center;
      width: calc(100% / 4);
      height: 5rem;
      padding: 1rem;
      border-radius: 0.8rem;
      cursor: pointer;
      transition: background-color 0.2s;

      &.selected.selected {
382
        background-color: lighten(var.$color-configurator, 6);
L
LeoKu 已提交
383 384 385
      }

      &:hover {
L
LeoKu 已提交
386
        background-color: lighten(var.$color-configurator, 0);
L
LeoKu 已提交
387 388 389 390 391 392 393 394
      }

      & > :deep(svg) {
        width: 100% !important;
        height: 100% !important;
      }

      & :deep(path) {
395
        stroke: var.$color-stroke !important;
L
LeoKu 已提交
396 397 398 399 400
      }
    }
  }
}
</style>