Configurator.vue 9.4 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 52 53 54 55
        <details
          v-if="
            s.widgetType === WidgetType.Tops ||
            s.widgetType === WidgetType.Clothes
          "
          class="color-picker"
        >
L
LeoKu 已提交
56
          <summary class="color">{{ t('label.colors') }}</summary>
L
LeoKu 已提交
57 58 59 60 61 62 63
          <ul class="color-list">
            <li
              v-for="fillColor in SETTINGS.commonColors"
              :key="fillColor"
              class="color-list__item"
              @click="setWidgetColor(s.widgetType, fillColor)"
            >
64 65 66 67 68 69 70
              <div
                :style="{ background: fillColor }"
                class="bg-color"
                :class="{
                  active: fillColor === getWidgetColor(s.widgetType),
                }"
              />
L
LeoKu 已提交
71 72 73
            </li>
          </ul>
        </details>
L
LeoKu 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87

        <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 已提交
88 89 90 91 92 93 94 95 96 97 98
      </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 已提交
99 100 101 102 103 104
import {
  type WidgetShape,
  type WrapperShape,
  BeardShape,
  WidgetType,
} from '@/enums'
L
LeoKu 已提交
105
import { useAvatarOption } from '@/hooks'
L
LeoKu 已提交
106
import { AVATAR_LAYER, SETTINGS } from '@/utils/constant'
L
LeoKu 已提交
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
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 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
          ...(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 已提交
208 209 210 211 212
        },
      },
    })
  }
}
213 214 215 216 217 218

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

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

L
LeoKu 已提交
224
.configurator-scroll {
225
  width: var.$layout-sider-width;
L
LeoKu 已提交
226
  height: 100%;
L
LeoKu 已提交
227 228 229 230

  @media screen and (max-width: var.$screen-lg) {
    background-color: var.$color-configurator;
  }
L
LeoKu 已提交
231 232 233 234
}

.configurator {
  width: 100%;
235
  color: var.$color-text;
L
LeoKu 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248

  .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;
249
        background-color: var.$color-text;
L
LeoKu 已提交
250 251 252 253 254 255 256 257 258 259 260
        transition: background-color 0.2s;

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

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

        &.active {
261
          background-color: var.$color-accent;
L
LeoKu 已提交
262 263 264 265 266
        }
      }
    }
  }

L
LeoKu 已提交
267
  .color-picker {
L
LeoKu 已提交
268
    margin: 1rem 0 0.5rem 0;
L
LeoKu 已提交
269 270 271 272 273 274 275 276 277 278

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

  .color-list {
L
LeoKu 已提交
279 280 281 282
    display: flex;
    flex-wrap: wrap;
    align-items: center;

L
LeoKu 已提交
283
    .color-list__item {
L
LeoKu 已提交
284 285
      position: relative;
      z-index: 1;
L
LeoKu 已提交
286 287
      width: calc(100% / 7);
      padding: 0.6rem 0;
L
LeoKu 已提交
288 289 290 291 292 293 294 295
      cursor: pointer;
      transition: transform 0.2s;

      .bg-color {
        position: relative;
        box-sizing: content-box;
        width: 1.3em;
        height: 1.3em;
L
LeoKu 已提交
296
        margin: 0 auto;
L
LeoKu 已提交
297 298
        font-size: 16px;
        border-radius: 50%;
299
        box-shadow: 0 0 0.05em 0.2em var.$color-configurator;
L
LeoKu 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

        &.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 已提交
317 318 319 320 321 322 323 324

        &::before {
          position: absolute;
          top: 50%;
          left: 50%;
          z-index: -1;
          width: 100%;
          height: 100%;
L
LeoKu 已提交
325
          background: inherit;
L
LeoKu 已提交
326 327 328 329 330 331 332 333 334 335 336
          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 已提交
337
          z-index: 1;
338
          color: var.$color-configurator;
L
LeoKu 已提交
339 340 341 342 343 344 345 346 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
          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 {
374
        background-color: lighten(var.$color-configurator, 6);
L
LeoKu 已提交
375 376 377
      }

      &:hover {
L
LeoKu 已提交
378
        background-color: lighten(var.$color-configurator, 0);
L
LeoKu 已提交
379 380 381 382 383 384 385 386
      }

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

      & :deep(path) {
387
        stroke: var.$color-stroke !important;
L
LeoKu 已提交
388 389 390 391 392
      }
    }
  }
}
</style>