Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial works for secondary language display. #11

Merged
merged 3 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bins/ayaka-check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ async fn main() -> Result<()> {
bail!("Check failed.");
}
ctx.init_new();
ctx.set_locale(opts.locale.unwrap_or_else(Locale::current));
ctx.set_settings(Settings {
lang: opts.locale.unwrap_or_else(Locale::current),
});
while let Some(action) = ctx.next_run() {
if let Some(name) = &action.character {
print!("_{}_", name);
Expand Down
36 changes: 34 additions & 2 deletions bins/ayaka-gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use ayaka_runtime::{
};
use flexi_logger::{FileSpec, LogSpecification, Logger};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Display};
use std::{
collections::{HashMap, HashSet},
fmt::Display,
};
use tauri::{async_runtime::Mutex, command, AppHandle, Manager, State};

type CommandResult<T> = std::result::Result<T, CommandError>;
Expand Down Expand Up @@ -174,7 +177,24 @@ async fn save_all(storage: State<'_, Storage>) -> CommandResult<()> {
}

#[command]
fn choose_locale(locales: Vec<Locale>) -> CommandResult<Option<Locale>> {
async fn avaliable_locale(
storage: State<'_, Storage>,
locales: HashSet<Locale>,
) -> CommandResult<HashSet<Locale>> {
if let Some(context) = storage.context.lock().await.as_ref() {
let avaliable = context.game.paras.keys().cloned().collect();
Ok(locales.intersection(&avaliable).cloned().collect())
} else {
Ok(locales)
}
}

#[command]
async fn choose_locale(
storage: State<'_, Storage>,
locales: HashSet<Locale>,
) -> CommandResult<Option<Locale>> {
let locales = avaliable_locale(storage, locales).await?;
let current = Locale::current();
debug!("Choose {} from {:?}", current, locales);
Ok(current.choose_from(&locales).cloned())
Expand Down Expand Up @@ -305,6 +325,16 @@ async fn current_run(storage: State<'_, Storage>) -> CommandResult<Option<Action
Ok(storage.action.lock().await.as_ref().cloned())
}

#[command]
async fn current_title(storage: State<'_, Storage>) -> CommandResult<Option<String>> {
Ok(storage
.context
.lock()
.await
.as_ref()
.and_then(|context| context.current_paragraph_title().cloned()))
}

#[command]
async fn switch(i: usize, storage: State<'_, Storage>) -> CommandResult<RawValue> {
debug!("Switch {}", i);
Expand Down Expand Up @@ -389,13 +419,15 @@ fn main() -> Result<()> {
get_records,
save_record_to,
save_all,
avaliable_locale,
choose_locale,
info,
start_new,
start_record,
next_run,
next_back_run,
current_run,
current_title,
current_visited,
switch,
history,
Expand Down
15 changes: 9 additions & 6 deletions bins/ayaka-gui/src/interop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ export interface Action {
line: ActionLine[],
ch_key?: string,
character?: string,
para_title?: string,
switches: Switch[],
props: {
bg?: string,
bgm?: string,
efm?: string,
voice?: string,
video?: string,
ch_models_count?: string,
ch_models?: string,
},
}

Expand Down Expand Up @@ -111,12 +110,12 @@ export function save_all(): Promise<void> {
return invoke("save_all")
}

export function choose_locale(locales: Locale[]): Promise<Locale | undefined> {
return invoke("choose_locale", { locales: locales })
export function avaliable_locale(locales: Locale[]): Promise<Locale[]> {
return invoke("avaliable_locale", { locales: locales })
}

export function locale_native_name(loc: Locale): string {
return new Intl.DisplayNames(loc, { type: "language" }).of(loc) ?? ""
export function choose_locale(locales: Locale[]): Promise<Locale | undefined> {
return invoke("choose_locale", { locales: locales })
}

export async function info(): Promise<GameInfo> {
Expand Down Expand Up @@ -144,6 +143,10 @@ export function current_run(): Promise<Action | undefined> {
return invoke("current_run")
}

export function current_title(): Promise<string | undefined> {
return invoke("current_title")
}

export async function current_visited(): Promise<boolean> {
return invoke("current_visited")
}
Expand Down
10 changes: 6 additions & 4 deletions bins/ayaka-gui/src/views/GameView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setTimeout } from 'timers-promises'
import { Mutex, tryAcquire } from 'async-mutex'
import ActionCard from '../components/ActionCard.vue'
import IconButton from '../components/IconButton.vue'
import { conv_src, current_run, next_run, next_back_run, switch_, merge_lines, Action, ActionLineType, ActionLine, current_visited } from '../interop'
import { conv_src, current_run, current_title, next_run, next_back_run, switch_, merge_lines, Action, ActionLineType, ActionLine, current_visited } from '../interop'
import { cloneDeep } from 'lodash'
import Live2D from '../components/Live2D.vue'
import { Modal } from 'bootstrap'
Expand All @@ -30,8 +30,8 @@ function wait_play(e: HTMLAudioElement): Promise<void> {
})
}

function live2d_names(props: any): string[] {
return ((props.ch_models ?? "") as string).split(",").filter(s => s.length != 0)
function live2d_names(props: { ch_models?: string }): string[] {
return (props.ch_models ?? "").split(",").filter(s => s.length != 0)
}

export default {
Expand All @@ -43,6 +43,7 @@ export default {
switches: [],
props: {},
} as Action,
title: "",
type_text: "",
type_text_buffer: [] as ActionLine[],
state: ActionState.End,
Expand All @@ -69,6 +70,7 @@ export default {
// Should be called in mutex
async fetch_current_run() {
const res = await current_run()
this.title = await current_title() ?? ""
console.info(res)
if (res) {
const load_new_bgm = (res.props.bgm != this.action.props.bgm);
Expand Down Expand Up @@ -263,7 +265,7 @@ export default {
<ActionCard :ch="action.character" :line="type_text"></ActionCard>
</div>
<div>
<h4><span class="badge bg-primary">{{ action.para_title }}</span></h4>
<h4><span class="badge bg-primary">{{ title }}</span></h4>
</div>
<div class="logo d-flex align-items-center">
<span>Powered by Ayaka.</span>
Expand Down
16 changes: 9 additions & 7 deletions bins/ayaka-gui/src/views/SettingsView.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<script setup lang="ts">
import { Locale } from 'vue-i18n'
import { locale_native_name, set_locale } from '../interop'
import { set_locale, avaliable_locale } from '../interop'
import IconButton from '../components/IconButton.vue';
</script>

<script lang="ts">
function locale_native_name(loc: Locale): string {
return new Intl.DisplayNames(loc, { type: "language" }).of(loc) ?? ""
}

export default {
emits: ["quit"],
data() {
return {
locale_names: new Map<Locale, string>(),
locales: [] as Locale[],
}
},
async created() {
this.$i18n.availableLocales.forEach(locale => {
this.locale_names.set(locale, locale_native_name(locale))
})
this.locales = await avaliable_locale(this.$i18n.availableLocales)
},
methods: {
async on_locale_select(e: Event) {
Expand All @@ -30,8 +32,8 @@ export default {
<div class="d-grid gap-4 col-4 mx-auto">
<h1>{{ $t("settings") }}</h1>
<select class="form-select" v-model="$i18n.locale" @change="on_locale_select">
<option v-for="locale in $i18n.availableLocales" :key="`locale-${locale}`" :value="locale">
{{ locale_names.get(locale) ?? locale }}
<option v-for="locale in locales" :value="locale">
{{ locale_native_name(locale) }}
</option>
</select>
</div>
Expand Down
16 changes: 11 additions & 5 deletions bins/ayaka-latex/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod writer;

use ayaka_runtime::{anyhow::Result, log::LevelFilter, Context, FrontendType, Locale};
use ayaka_runtime::{anyhow::Result, log::LevelFilter, *};
use clap::Parser;
use std::ffi::OsString;
use writer::LaTeXWriter;
Expand Down Expand Up @@ -39,17 +39,23 @@ async fn main() -> Result<()> {
output.command0("tableofcontents").await?;

ctx.init_new();
ctx.set_locale(opts.locale.unwrap_or_else(Locale::current));
ctx.set_settings(Settings {
lang: opts.locale.unwrap_or_else(Locale::current),
});

let mut current_para = None;
let mut current_bg = None;

while let Some(action) = ctx.next_run() {
if action.para_title != current_para {
current_para = action.para_title.clone();
let para_title = ctx.current_paragraph_title();
if para_title != current_para.as_ref() {
output
.command("section", [action.para_title.unwrap_or_default()])
.command(
"section",
[para_title.map(|s| s.as_str()).unwrap_or_default()],
)
.await?;
current_para = para_title.cloned();
}
let bg = action.props.get("bg");
if current_bg.as_ref() != bg {
Expand Down
4 changes: 1 addition & 3 deletions utils/ayaka-bindings-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,12 @@ pub struct Action {
pub ctx: RawContext,
/// The full texts.
pub line: ActionLines,
#[doc(hidden)]
/// The format params of texts.
pub line_params: Vec<RawValue>,
/// The key of current character.
pub ch_key: Option<String>,
/// The current character.
pub character: Option<String>,
/// The title of current paragraph.
pub para_title: Option<String>,
/// The switches.
pub switches: Vec<Switch>,
/// The other custom properties.
Expand Down
Loading