Configurator.vue 9.0 KB
Newer Older
L
LeoKu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<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)"
          >
            <i
              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 49 50 51 52 53 54 55 56 57 58 59
          </li>
        </ul>
      </SectionWrapper>

      <SectionWrapper
        v-for="s in sections"
        :key="s.widgetType"
        :title="t(`widgetType.${s.widgetType}`)"
      >
        <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"
L
LeoKu 已提交
60
          />
L
LeoKu 已提交
61
        </ul>
L
LeoKu 已提交
62 63 64 65 66 67 68 69

        <details
          v-if="
            s.widgetType === WidgetType.Tops ||
            s.widgetType === WidgetType.Clothes
          "
          class="color-picker"
        >
L
LeoKu 已提交
70
          <summary class="color">{{ t('label.colors') }}</summary>
L
LeoKu 已提交
71 72 73 74 75 76 77 78 79 80 81
          <ul class="color-list">
            <li
              v-for="fillColor in SETTINGS.commonColors"
              :key="fillColor"
              class="color-list__item"
              @click="setWidgetColor(s.widgetType, fillColor)"
            >
              <div :style="{ background: fillColor }" class="bg-color" />
            </li>
          </ul>
        </details>
L
LeoKu 已提交
82 83 84 85 86 87 88 89 90 91 92
      </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 已提交
93 94 95 96 97 98
import {
  type WidgetShape,
  type WrapperShape,
  BeardShape,
  WidgetType,
} from '@/enums'
L
LeoKu 已提交
99
import { useAvatarOption } from '@/hooks'
L
LeoKu 已提交
100
import { AVATAR_LAYER, SETTINGS } from '@/utils/constant'
L
LeoKu 已提交
101 102 103 104 105 106 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
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 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
          ...(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 已提交
202 203 204 205 206 207 208 209
        },
      },
    })
  }
}
</script>

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

L
LeoKu 已提交
212
.configurator-scroll {
213
  width: var.$layout-sider-width;
L
LeoKu 已提交
214
  height: 100%;
215
  background-color: var.$color-configurator;
L
LeoKu 已提交
216 217 218 219
}

.configurator {
  width: 100%;
220
  color: var.$color-text;
L
LeoKu 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233

  .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;
234
        background-color: var.$color-text;
L
LeoKu 已提交
235 236 237 238 239 240 241 242 243 244 245
        transition: background-color 0.2s;

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

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

        &.active {
246
          background-color: var.$color-accent;
L
LeoKu 已提交
247 248 249 250 251
        }
      }
    }
  }

L
LeoKu 已提交
252 253 254 255 256 257 258 259 260 261 262 263
  .color-picker {
    margin-top: 1rem;

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

  .color-list {
L
LeoKu 已提交
264 265 266 267
    display: flex;
    flex-wrap: wrap;
    align-items: center;

L
LeoKu 已提交
268
    .color-list__item {
L
LeoKu 已提交
269 270
      position: relative;
      z-index: 1;
L
LeoKu 已提交
271 272
      width: calc(100% / 7);
      padding: 0.6rem 0;
L
LeoKu 已提交
273 274 275 276 277 278 279 280
      cursor: pointer;
      transition: transform 0.2s;

      .bg-color {
        position: relative;
        box-sizing: content-box;
        width: 1.3em;
        height: 1.3em;
L
LeoKu 已提交
281
        margin: 0 auto;
L
LeoKu 已提交
282 283
        font-size: 16px;
        border-radius: 50%;
284
        box-shadow: 0 0 0.05em 0.2em var.$color-configurator;
L
LeoKu 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

        &.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 已提交
302 303 304 305 306 307 308 309

        &::before {
          position: absolute;
          top: 50%;
          left: 50%;
          z-index: -1;
          width: 100%;
          height: 100%;
L
LeoKu 已提交
310
          background: inherit;
L
LeoKu 已提交
311 312 313 314 315 316 317 318 319 320 321
          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 已提交
322
          z-index: 1;
323
          color: var.$color-configurator;
L
LeoKu 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
          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 {
359
        background-color: lighten(var.$color-configurator, 6);
L
LeoKu 已提交
360 361 362
      }

      &:hover {
363
        background-color: darken(var.$color-configurator, 3);
L
LeoKu 已提交
364 365 366 367 368 369 370 371
      }

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

      & :deep(path) {
372
        stroke: var.$color-stroke !important;
L
LeoKu 已提交
373 374 375 376 377
      }
    }
  }
}
</style>